Commit 3e9ddc52 by 李伟康

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

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