//用于检查当前的url地址中是否含有code
getUrlParam(name) {
const reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
const r = window.location.search.substr(1).match(reg)
if (r != null) {
return unescape(r[2])
}
return null
}
// 获取code
async getWxCode() {
let appid = APPID //公众号的APPID
let code = this.getUrlParam('code') //检查url中是否存在code
let local = baseH5Url + '/#/promote' //测试路径,即当前h5页面的路径
if (code == null || code ==="") {
//不存在就打开下面的地址进行授权
window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${encodeURIComponent(local)}&response_type=code&scope=snsapi_base&state=1#wechat_redirect`
} else {
this.code = code
}
}