Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
saas_plus
/
linkcore-admin
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 028e35e7
authored
Aug 05, 2026
by
张立顺
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
refactor: ROP 工具改用本地环境配置
1 parent
a13e7e33
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
61 additions
and
92 deletions
src/libs/rop/loadAxiosConfig.js
src/libs/rop/loadAxiosConfig.spec.js
src/libs/rop/ropClient.js
src/libs/rop/loadAxiosConfig.js
View file @
028e35e
const
fs
=
require
(
"fs"
);
const
{
config
:
baseConfig
}
=
require
(
"../../common/runtimeConfig"
);
const
http
=
require
(
"http"
);
const
https
=
require
(
"https"
);
const
path
=
require
(
"path"
);
const
{
getAxiosConfigKey
}
=
require
(
"./resolveApiName"
);
const
{
getAxiosConfigKey
}
=
require
(
"./resolveApiName"
);
function
tryParseJson
(
raw
)
{
function
pickAxiosBlock
(
targetConfig
,
apiName
=
"dj"
)
{
if
(
typeof
raw
!==
"string"
||
!
raw
.
trim
())
{
return
null
;
}
try
{
return
JSON
.
parse
(
raw
);
}
catch
(
error
)
{
return
null
;
}
}
function
parseConfigCenterResponse
(
response
)
{
if
(
!
response
||
typeof
response
!==
"object"
)
{
return
null
;
}
let
payload
=
response
;
if
(
response
.
code
==
"200"
&&
response
.
data
)
{
payload
=
typeof
response
.
data
===
"string"
?
tryParseJson
(
response
.
data
)
:
response
.
data
;
}
if
(
payload
&&
typeof
payload
===
"object"
&&
payload
.
axios
?.
baseURL
&&
payload
.
axios
?.
appkey
)
{
return
payload
;
}
return
null
;
}
const
DEFAULT_CONFIG_URL
=
process
.
env
.
VUE_APP_CONFIG_CENTER_URL
||
"https://config-center-backend.glsx.com.cn/glsxAdmin/config?moduleUrl=dj-car-life-system-config"
;
const
DEVELOP_CONFIG_PATH
=
path
.
join
(
__dirname
,
"../../common/baseConfig.develop.json"
);
function
fetchJson
(
url
)
{
return
new
Promise
((
resolve
,
reject
)
=>
{
const
lib
=
url
.
startsWith
(
"https"
)
?
https
:
http
;
lib
.
get
(
url
,
(
res
)
=>
{
let
raw
=
""
;
res
.
on
(
"data"
,
(
chunk
)
=>
{
raw
+=
chunk
;
});
res
.
on
(
"end"
,
()
=>
{
try
{
resolve
(
JSON
.
parse
(
raw
));
}
catch
(
error
)
{
reject
(
new
Error
(
`JSON parse failed:
${
raw
.
slice
(
0
,
200
)}
`
));
}
});
}).
on
(
"error"
,
reject
);
});
}
function
loadDevelopFallback
()
{
return
JSON
.
parse
(
fs
.
readFileSync
(
DEVELOP_CONFIG_PATH
,
"utf8"
));
}
function
pickAxiosBlock
(
baseConfig
,
apiName
=
"dj"
)
{
const
configKey
=
getAxiosConfigKey
(
apiName
);
const
configKey
=
getAxiosConfigKey
(
apiName
);
if
(
!
configKey
)
{
if
(
!
configKey
)
{
throw
new
Error
(
`Unknown apiName for axios config:
${
apiName
}
`
);
throw
new
Error
(
`Unknown apiName for axios config:
${
apiName
}
`
);
}
}
const
block
=
base
Config
?.[
configKey
];
const
block
=
target
Config
?.[
configKey
];
if
(
!
block
?.
baseURL
||
!
block
?.
appkey
||
!
block
?.
secret
)
{
if
(
!
block
?.
baseURL
||
!
block
?.
appkey
||
!
block
?.
secret
)
{
throw
new
Error
(
`Invalid ROP config: missing
${
configKey
}
.baseURL/appkey/secret`
);
throw
new
Error
(
`Invalid ROP config: missing
${
configKey
}
.baseURL/appkey/secret`
);
}
}
...
@@ -80,30 +19,14 @@ function pickAxiosBlock(baseConfig, apiName = "dj") {
...
@@ -80,30 +19,14 @@ function pickAxiosBlock(baseConfig, apiName = "dj") {
}
}
/**
/**
* 加载 axios 配置:优先配置中心,失败回退 baseConfig.develop.json
* 从代码内的当前环境读取 axios 配置。
* @param {string|{ configUrl?: string, apiName?: string }} options
* 保留异步返回值,避免影响现有 Node/脚本调用方。
* @param {{ apiName?: string }} options
*/
*/
async
function
loadAxiosConfig
(
options
)
{
async
function
loadAxiosConfig
(
options
=
{})
{
let
configUrl
=
DEFAULT_CONFIG_URL
;
const
apiName
=
typeof
options
===
"object"
&&
options
.
apiName
let
apiName
=
"dj"
;
?
options
.
apiName
if
(
typeof
options
===
"string"
)
{
:
"dj"
;
configUrl
=
options
;
}
else
if
(
options
&&
typeof
options
===
"object"
)
{
configUrl
=
options
.
configUrl
||
DEFAULT_CONFIG_URL
;
apiName
=
options
.
apiName
||
"dj"
;
}
let
baseConfig
=
null
;
try
{
const
response
=
await
fetchJson
(
configUrl
);
baseConfig
=
parseConfigCenterResponse
(
response
);
}
catch
(
error
)
{
// 回退 develop 本地配置
}
if
(
!
baseConfig
?.
axios
?.
appkey
||
!
baseConfig
?.
axios
?.
secret
)
{
baseConfig
=
loadDevelopFallback
();
}
const
{
baseURL
,
appkey
,
secret
,
configKey
}
=
pickAxiosBlock
(
baseConfig
,
apiName
);
const
{
baseURL
,
appkey
,
secret
,
configKey
}
=
pickAxiosBlock
(
baseConfig
,
apiName
);
return
{
return
{
baseURL
,
baseURL
,
...
@@ -117,7 +40,5 @@ async function loadAxiosConfig(options) {
...
@@ -117,7 +40,5 @@ async function loadAxiosConfig(options) {
module
.
exports
=
{
module
.
exports
=
{
loadAxiosConfig
,
loadAxiosConfig
,
loadDevelopFallback
,
pickAxiosBlock
,
pickAxiosBlock
,
DEFAULT_CONFIG_URL
,
};
};
src/libs/rop/loadAxiosConfig.spec.js
0 → 100644
View file @
028e35e
const
mockHttpGet
=
jest
.
fn
(()
=>
{
throw
new
Error
(
"不应发起 HTTP 配置请求"
);
});
const
mockHttpsGet
=
jest
.
fn
(()
=>
{
throw
new
Error
(
"不应发起 HTTPS 配置请求"
);
});
jest
.
mock
(
"http"
,
()
=>
({
get
:
mockHttpGet
,
}));
jest
.
mock
(
"https"
,
()
=>
({
get
:
mockHttpsGet
,
}));
const
{
loadAxiosConfig
,
pickAxiosBlock
,
}
=
require
(
"./loadAxiosConfig"
);
describe
(
"loadAxiosConfig"
,
()
=>
{
beforeEach
(()
=>
{
jest
.
clearAllMocks
();
});
it
(
"从默认开发配置读取 DJ ROP 且不访问网络"
,
async
()
=>
{
const
result
=
await
loadAxiosConfig
({
apiName
:
"dj"
,
configUrl
:
"https://config.invalid/never-request"
,
});
expect
(
result
.
env
).
toBe
(
"develop"
);
expect
(
result
.
configKey
).
toBe
(
"axios"
);
expect
(
result
.
baseURL
).
toContain
(
"dj.dev.glsx.com.cn"
);
expect
(
mockHttpGet
).
not
.
toHaveBeenCalled
();
expect
(
mockHttpsGet
).
not
.
toHaveBeenCalled
();
});
it
(
"从同一开发配置读取 SCRM ROP"
,
async
()
=>
{
const
result
=
await
loadAxiosConfig
({
apiName
:
"scrm"
});
expect
(
result
.
configKey
).
toBe
(
"axios2"
);
expect
(
result
.
baseURL
).
toContain
(
"scrm.dev.glsx.com.cn"
);
});
it
(
"未知 API 名称返回明确错误"
,
()
=>
{
expect
(()
=>
pickAxiosBlock
({},
"unknown"
)).
toThrow
(
"Unknown apiName for axios config: unknown"
);
});
});
src/libs/rop/ropClient.js
View file @
028e35e
const
process
=
require
(
"process"
);
const
buildRopPayload
=
require
(
"./buildRopPayload"
);
const
buildRopPayload
=
require
(
"./buildRopPayload"
);
const
{
loadAxiosConfig
}
=
require
(
"./loadAxiosConfig"
);
const
{
loadAxiosConfig
}
=
require
(
"./loadAxiosConfig"
);
const
{
login
}
=
require
(
"./ropSession"
);
const
postRop
=
require
(
"./ropHttpClient"
);
const
postRop
=
require
(
"./ropHttpClient"
);
const
{
login
}
=
require
(
"./ropSession"
);
const
SMARTCARLIFE_PREFIX
=
"dj.api.smartcarlife."
;
const
SMARTCARLIFE_PREFIX
=
"dj.api.smartcarlife."
;
...
@@ -47,10 +48,9 @@ function createRopClient(config, sessionId) {
...
@@ -47,10 +48,9 @@ function createRopClient(config, sessionId) {
async
function
createLoggedInClient
({
async
function
createLoggedInClient
({
userName
=
process
.
env
.
ROP_TEST_USER
||
"glsadmin"
,
userName
=
process
.
env
.
ROP_TEST_USER
||
"glsadmin"
,
password
=
process
.
env
.
ROP_TEST_PASSWORD
||
"123456"
,
password
=
process
.
env
.
ROP_TEST_PASSWORD
||
"123456"
,
configUrl
,
apiName
=
"dj"
,
apiName
=
"dj"
,
}
=
{})
{
}
=
{})
{
const
config
=
await
loadAxiosConfig
({
configUrl
,
apiName
});
const
config
=
await
loadAxiosConfig
({
apiName
});
const
sessionId
=
await
login
(
config
,
{
userName
,
password
});
const
sessionId
=
await
login
(
config
,
{
userName
,
password
});
return
createRopClient
(
config
,
sessionId
);
return
createRopClient
(
config
,
sessionId
);
}
}
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment