-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwechat.js
87 lines (85 loc) · 2.74 KB
/
wechat.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
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
import QueryString from 'querystring'
import Url from 'url'
export default {
isWechat () {
return window.navigator.userAgent.toLowerCase().indexOf('micromessenger') !== -1
},
authorizeRedirect (appId, redirectPath, scope) {
let endpoint = 'https://open.weixin.qq.com/connect/oauth2/authorize'
let defaultScope = 'snsapi_userinfo'
if (scope) defaultScope = scope
let currentUrl = Url.parse(window.location.href)
let queryString = QueryString.stringify({
appid: appId,
redirect_uri: currentUrl.protocol + '//' + currentUrl.host + '/',
response_type: 'code',
scope: defaultScope,
state: redirectPath
})
window.location.href = endpoint + '?' + queryString + '#wechat_redirect'
},
handleAuthorizationCodeRedirect (next) {
let currentUrl = Url.parse(window.location.href)
let query = QueryString.parse(currentUrl.query)
if (query.code && query.state) {
window.history.replaceState(null, null, currentUrl.protocol + '//' + currentUrl.host + '/' + currentUrl.hash)
next({path: query.state + '/wechat/' + query.code, replace: true})
return true
} else {
return false
}
},
connectDrupal (http, clientId, appId, authCode) {
return http.post('api/rest/wechat_connect/connect?_format=json', {
client_id: clientId,
app_id: appId,
code: authCode
})
},
registerDrupal (http, clientId, appId, openId, phone) {
return http.post('api/rest/wechat_connect/register?_format=json', {
client_id: clientId,
app_id: appId,
open_id: openId,
phone
})
},
createWechatPayment (http, gateway, cartId) {
return http.post('api/rest/wechat-pay/wechat-payment?_format=json', {
gateway,
cart_id: cartId
})
},
launchMediaPlatformWechatPay (config, callback) {
let onBridgeReady = function () {
WeixinJSBridge.invoke( // eslint-disable-line
'getBrandWCPayRequest', {
'appId': config.appId,
'timeStamp': config.timeStamp,
'nonceStr': config.nonceStr,
'package': config.package,
'signType': config.signType,
'paySign': config.paySign
},
callback
)
}
if (typeof WeixinJSBridge === 'undefined') {
if (document.addEventListener) {
document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false)
} else if (document.attachEvent) {
document.attachEvent('WeixinJSBridgeReady', onBridgeReady)
document.attachEvent('onWeixinJSBridgeReady', onBridgeReady)
}
} else {
onBridgeReady()
}
},
getJSSDKConfig (http, appId, url, APIs) {
return http.post('api/rest/wechat_connect/js-sdk-config?_format=json', {
app_id: appId,
url,
apis: APIs
})
}
}