不爱讲废话,直接贴代码(相关说明已在对应代码中加注解)
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;
}
autoH5Login() {
let mobile = this.getUrlParam('mobile') || Taro.getStorageSync('mobile') || null
let openId = this.getUrlParam('openId') || Taro.getStorageSync('openId') || null
if (this.getUrlParam('mobile')) {
Taro.setStorageSync('mobile', this.getUrlParam('mobile'))
}
if (this.getUrlParam('openId')) {
Taro.setStorageSync('openId', this.getUrlParam('openId'))
}
let targetPagePath = this.getUrlParam('targetPage')
let params = signature({
ReqTime: new Date().getTime(),
Mobile: mobile,
AppId: appId,
OpenId : openId
})
api.post({
url: 后端封装微信小程序的开放登录接口
data: params
}).then(res => {
if(res.data.IsSuccess) {
...
Taro.switchTab({
url: targetPagePath || '/pages/index/index'
})
} else {
...
}
})
}