-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (54 loc) · 1.89 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* @param payment 类型:Object
*/
function chooseWxPay(payment) {
return new Promise((resolve, reject) => {
let wx = window['wx'];
/*先初始化微信JS-SDK,才能使用微信支付*/
if(!wx){
reject('wx is undefined! please initWxJSSDK')
return;
}
/*支付参数是个对象*/
if (typeof payment !== 'object') {
reject('payment must be a object!');
return;
}
if(!payment.timestamp) payment.timestamp = payment.timeStamp;
payment.success = resolve;
payment.fail = reject;
// console.log('wx chooseWxPay', payment);
wx.chooseWXPay(payment)
})
}
function createWxJSSDK() {
return new Promise((resolve, reject) => {
if(window['wx'])
return resolve();
let script = document.createElement('script')
script.src = 'https://res.wx.qq.com/open/js/jweixin-1.4.0.js';
script.onload = () => resolve();
script.onerror = () => reject();
document.body.appendChild(script)
})
}
function initWxJSSDK(config) {
return new Promise((resolve, reject) => {
createWxJSSDK().then(() => {
if(typeof config !== 'object')
return reject('config 不正确!');
let list = config.jsApiList || [];
list.includes('chooseWXPay') || list.push('chooseWXPay');
let wx = window.wx;
wx.config(config);
wx.ready(function () {
resolve();
})
// config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。
wx.error(function (res) {
reject(res);
});
})
})
}
export {createWxJSSDK,initWxJSSDK,chooseWxPay}