Skip to content

Commit

Permalink
feat(Wechat): config 返回 Promise
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Aug 28, 2023
1 parent b386d7e commit 3217457
Showing 1 changed file with 60 additions and 52 deletions.
112 changes: 60 additions & 52 deletions src/utils/Wechat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,64 +423,72 @@ export class Wechat {
*
* @param params 配置参数
*/
config(params: WechatConfigParams = this.configParams) {
this.configParams = params

const config = () => {
const sharable =
typeof params.sharable === 'boolean' ? params.sharable : true
wx.config({
...params,
jsApiList: [
...(params.jsApiList || []),
...(sharable ? shareJsApiList : []),
],
})
if (!this.ready) {
wx.ready(() => {
this.ready = true
this.bus.emit('ready')
})
wx.error((err: any) => {
this.bus.emit('error', err)
config(params: WechatConfigParams = this.configParams): Promise<void> {
return new Promise<void>((resolve, reject) => {
this.configParams = params

const config = () => {
const sharable =
typeof params.sharable === 'boolean' ? params.sharable : true
wx.config({
...params,
jsApiList: [
...(params.jsApiList || []),
...(sharable ? shareJsApiList : []),
],
})
if (!this.ready) {
wx.ready(() => {
this.ready = true
this.bus.emit('ready')
resolve()
})
wx.error((err: any) => {
this.bus.emit('error', err)
reject(err)
})
} else {
resolve()
}
}
}

if (typeof wx !== 'undefined') {
config()
} else {
const autoLoadJSSDK =
params.autoLoadJSSDK == null ? '1.4.0' : params.autoLoadJSSDK
if (autoLoadJSSDK !== false) {
const jssdkUrl = /^[0-9.]+$/.test(autoLoadJSSDK)
? `https://res.wx.qq.com/open/js/jweixin-${autoLoadJSSDK}.js`
: autoLoadJSSDK
const alternateJssdkUrl = jssdkUrl.replace(
/^https:\/\/res\.wx\.qq\.com\//,
'https://res2.wx.qq.com/',
)
loadResource({
type: LoadResourceUrlType.js,
path: jssdkUrl,
alternatePath: alternateJssdkUrl,
})
.then(([scriptEl]) => {
scriptEl.parentNode?.removeChild(scriptEl)
}, noop)
.then(() => {
if (typeof wx === 'undefined') {
throw new Error('微信 JSSDK 加载失败')
}
config()
})
if (typeof wx !== 'undefined') {
config()
} else {
if (typeof wx === 'undefined') {
throw new Error('请先引入微信 JSSDK')
const autoLoadJSSDK =
params.autoLoadJSSDK == null ? '1.6.0' : params.autoLoadJSSDK
if (autoLoadJSSDK !== false) {
const jssdkUrl = /^[0-9.]+$/.test(autoLoadJSSDK)
? `https://res.wx.qq.com/open/js/jweixin-${autoLoadJSSDK}.js`
: autoLoadJSSDK
const alternateJssdkUrl = jssdkUrl.replace(
/^https:\/\/res\.wx\.qq\.com\//,
'https://res2.wx.qq.com/',
)
loadResource({
type: LoadResourceUrlType.js,
path: jssdkUrl,
alternatePath: alternateJssdkUrl,
})
.then(([scriptEl]) => {
scriptEl.parentNode?.removeChild(scriptEl)
}, noop)
.then(() => {
if (typeof wx === 'undefined') {
reject('微信 JSSDK 加载失败')
} else {
config()
}
})
} else {
if (typeof wx === 'undefined') {
reject('请先引入微信 JSSDK')
} else {
config()
}
}
config()
}
}
})
}

/**
Expand Down

0 comments on commit 3217457

Please sign in to comment.