WxConfig.js
3.29 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { common } from '@r/api'
class WxConfig {
constructor(jsApiList) {
jsApiList.map(e => {
if (e == 'chooseImage') jsApiList.push('getLocalImgData')
})
let [redirectUrl] = window.location.href.split('?')
const appId = sessionStorage.getItem('GL_WX_APPID')
common.getWxConfig({
url: redirectUrl.split('#')[0],
appId
}).then(res => {
if (res) {
let { data } = res
this.setConfig({
...data,
jsApiList
})
} else {
window.vm.showToast('系统异常')
}
})
}
setConfig(params) {
let obj = {}
Object.keys(params).map(key => {
if (params[key]) obj[key] = params[key]
})
wx.config(obj)
}
ready(callBack) {
return new Promise(resolve => {
wx.ready(() => {
callBack(resolve)
})
})
}
scanQRCode(needResult = 0) {
return this.ready(resolve => {
wx.scanQRCode({
needResult,
scanType: ['qrCode', 'barCode'],
success(res) {
resolve(res)
}
})
})
}
chooseImage(count = 1) {
return this.ready(resolve => {
wx.chooseImage({
count, // 默认9
sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
success(res) {
let { localIds } = res
let imgs = []
localIds.map((e, index) => {
wx.getLocalImgData({
localId: e,
success(img) {
imgs.push(img)
if (index == localIds.length - 1) resolve(imgs)
}
})
})
}
})
})
}
getLocation(type = 'wgs84') {
return this.ready(resolve => {
wx.getLocation({
type, // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
success(res) {
resolve(res)
}
})
})
}
openLocation(latitude,longitude,name,address,scale = '18',infoUrl) {
return this.ready(resolve => {
wx.openLocation({
latitude,
longitude,
name,
address,
scale,
infoUrl,
success(res) {
console.log(res)
resolve(res)
}
})
})
}
chooseWXPay(timestamp,nonceStr,packages,signType,paySign) {
return this.ready(resolve => {
wx.chooseWXPay({
timestamp, // 支付签名时间戳,注意微信js sdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
nonceStr, // 支付签名随机串,不长于 32 位
package:packages, // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=\*\*\*)
signType, // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
paySign, // 支付签名
success(res) {
window.vm.showToast('支付成功')
resolve(res)
},
fail() {
window.vm.showToast('已取消支付')
resolve()
},
cancel() {
window.vm.showToast('支付失败,请重试')
resolve()
}
})
})
}
}
export default WxConfig