Skip to content

Commit

Permalink
feat(Wechat): 支持自动加载微信 JSSDK
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed May 22, 2019
1 parent 64610a0 commit e7e22e0
Showing 1 changed file with 40 additions and 17 deletions.
57 changes: 40 additions & 17 deletions src/Wechat.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { EventBus } from './EventBus'
import { isBoolean } from './isBoolean'
import { loadResource, LoadResourceUrlType } from './loadResource'
import { promiseSeries } from './promiseSeries'

declare const wx: any
Expand Down Expand Up @@ -88,6 +89,12 @@ export interface WechatConfigParams {
* @default false
*/
sharable?: boolean,
/**
* 是否自动引入微信 JSSDK。
*
* @default true
*/
autoLoadJSSDK: boolean,
}

export type WechatErrorCallback = (err: any) => void
Expand Down Expand Up @@ -229,24 +236,40 @@ export class Wechat {
* @param params 配置参数
*/
config(params: WechatConfigParams) {
if (typeof wx === 'undefined') {
throw new Error('请先引入微信 JSSDK')
const config = () => {
const sharable = isBoolean(params.sharable) ? params.sharable : false
wx.config({
...params,
jsApiList: [
...(params.jsApiList || []),
...(sharable ? shareJsApiList : []),
],
})
wx.ready(() => {
this.ready = true
this.bus.emit('ready')
})
wx.error((err: any) => {
this.bus.emit('error', err)
})
}
if (params.autoLoadJSSDK) {
loadResource({
type: LoadResourceUrlType.js,
path: 'https://res.wx.qq.com/open/js/jweixin-1.4.0.js',
alternatePath: 'https://res2.wx.qq.com/open/js/jweixin-1.4.0.js',
}).then(() => {
if (typeof wx === 'undefined') {
throw new Error('微信 JSSDK 加载失败')
}
config()
})
} else {
if (typeof wx === 'undefined') {
throw new Error('请先引入微信 JSSDK')
}
config()
}
const sharable = isBoolean(params.sharable) ? params.sharable : false
wx.config({
...params,
jsApiList: [
...(params.jsApiList || []),
...(sharable ? shareJsApiList : []),
],
})
wx.ready(() => {
this.ready = true
this.bus.emit('ready')
})
wx.error((err: any) => {
this.bus.emit('error', err)
})
}

/**
Expand Down

0 comments on commit e7e22e0

Please sign in to comment.