Commit 3e9ddc52 by 李伟康

fix(sdk): 修复地图SDK配置获取和脚本加载问题

- 移除环境对象变量,直接在请求URL中使用环境参数
- 为动态加载的脚本添加ignore属性以避免冲突
- 优化可选链操作符的使用方式以提高兼容性
- 更新版本号至0.1.6并重新构建dist文件
1 parent 383e1322
{ {
"name": "glsk-mapapi-sdk", "name": "glsk-mapapi-sdk",
"version": "0.1.4", "version": "0.1.6",
"type": "module", "type": "module",
"description": "glsk-mapapi-sdk", "description": "glsk-mapapi-sdk",
"main": "src/index.js", "main": "src/index.js",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1", "test": "echo \"Error: no test specified\" && exit 1",
"build": "rollup -c", "build": "rollup -c",
"doc": "jsdoc src/index.js" "doc": "jsdoc src/index.js"
}, },
"author": "limin", "author": "limin",
"license": "ISC", "license": "ISC",
"devDependencies": { "devDependencies": {
"@babel/preset-env": "^7.26.0", "@babel/preset-env": "^7.26.0",
"@rollup/plugin-babel": "^6.0.4", "@rollup/plugin-babel": "^6.0.4",
"@rollup/plugin-commonjs": "^28.0.1", "@rollup/plugin-commonjs": "^28.0.1",
"@rollup/plugin-node-resolve": "^15.3.0", "@rollup/plugin-node-resolve": "^15.3.0",
"@rollup/plugin-terser": "^0.4.4", "@rollup/plugin-terser": "^0.4.4",
"jsdoc": "^4.0.4", "jsdoc": "^4.0.4",
"rollup": "^4.28.0" "rollup": "^4.28.0"
} }
} }
const envObject = { class GlMapSdk {
1: 'develop', constructor() {
2: 'test', let mapConfig = {
3: 'production', env: 3,
4: 'beta' version: 3
} },
class GlMapSdk { _callBack = arguments[0]
constructor() { if (arguments.length > 1) {
let mapConfig = { mapConfig = typeof arguments[0] == 'object' ? arguments[0] : mapConfig
env: 3, _callBack = arguments[1]
version: 3 if (!mapConfig.env) mapConfig.env = 3
}, if (!mapConfig.version) mapConfig.version = 3
_callBack = arguments[0] }
if (arguments.length > 1) { if (typeof window !== 'undefined') {
mapConfig = typeof arguments[0] == 'object' ? arguments[0] : mapConfig const fetchConfig = async () => {
_callBack = arguments[1] try {
if (!mapConfig?.env) mapConfig.env = 3 const response = await fetch(`https://config-center-backend.glsx.com.cn/glsxAdmin/config?moduleUrl=map-api-js`);
if (!mapConfig?.version) mapConfig.version = 3 const res = await response.json(),
} scriptExists = Array.from(document.head.getElementsByTagName('script')).some(script => script.src === res.apiUrl);
if (typeof window !== 'undefined') { if (!scriptExists) {
const fetchConfig = async () => { const script = document.createElement('script');
try { script.setAttribute('ignore', '')
const response = await fetch(`https://config-center-backend.glsx.com.cn/glsxAdmin/config?moduleUrl=map-api-js&env=${envObject[mapConfig.env]}`); script.src = res.apiUrl;
const res = await response.json(), document.head.appendChild(script)
scriptExists = Array.from(document.head.getElementsByTagName('script')).some(script => script.src === res.apiUrl); script.onload = () => {
if (!scriptExists) { this.GLMap = new window.$GlMapApi(mapConfig, () => {
const script = document.createElement('script'); if (_callBack && typeof _callBack == 'function') _callBack(this)
script.src = res.apiUrl; })
document.head.appendChild(script) }
script.onload = () => { } else if (_callBack && typeof _callBack == 'function') {
this.GLMap = new window.$GlMapApi(mapConfig, () => { this.GLMap = new window.$GlMapApi(mapConfig, () => {
if (_callBack && typeof _callBack == 'function') _callBack(this) if (_callBack && typeof _callBack == 'function') _callBack(this)
}) })
} }
} else if (_callBack && typeof _callBack == 'function') { } catch (error) {
this.GLMap = new window.$GlMapApi(mapConfig, () => { console.error('Failed to fetch configuration:', error)
if (_callBack && typeof _callBack == 'function') _callBack(this) }
}) }
} fetchConfig()
} catch (error) { }
console.error('Failed to fetch configuration:', error) }
} invoke (target) {
} let params = Array.prototype.slice.call(arguments, 1)
fetchConfig() return this.GLMap[target](...params)
} }
} get map () {
invoke (target) { return this.GLMap._BMAP
let params = Array.prototype.slice.call(arguments, 1) }
return this.GLMap[target](...params) get mapGL () {
} return this.GLMap._BMapGL
get map () { }
return this.GLMap._BMAP }
}
get mapGL () {
return this.GLMap._BMapGL
}
}
export default GlMapSdk export default GlMapSdk
\ No newline at end of file \ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!