//在重定向之前和之后都会调用两次onload(疑似页面挂载了两次,而不是单一的加载了两次onload),导致页面调用了两次重定向、两次获取openid和登录的接口,导致页面的状态无法得到控制,如加载页的显示,实际状态已经变更,但页面还是在加载(两次挂载页面导致的状态不一致),以及接口调用失败
//希望能尽快帮忙看一下,已临近上线
onLoad (v) {
console.log('onload')
this.donorInfo = uni.getStorageSync("donor_info")
var storageOpenId = uni.getStorageSync('openid')
this.openid = (storageOpenId && storageOpenId.length > 11) ? storageOpenId : ''
//判断是否为微信浏览器打开
const isWechatBrowser = this.$store.state.app.userAgent.includes('micromessenger')
if (isWechatBrowser) {
if (!this.openid && !this.getQueryString('code')) {
this.redirectToWechatAuth(); // 没有用户信息,拉授权,重定向当前页面
} else {
this.checkBindStatus();
}
}
},
//重定向当前页面获取code
redirectToWechatAuth() {
try {
const redirectUri = encodeURIComponent(DS);//正常使用的域名,配置经过测试可以正常访问
const scope = 'snsapi_base';
const state = Math.random().toString(36).substring(7);
const authUrl = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${AppId}&redirect_uri=${redirectUri}&response_type=code&scope=${scope}&state=${state}#wechat_redirect`;
window.location.href = authUrl;
} catch (error) {
console.error('微信授权跳转失败:', error);
}
},
//执行登录校验
async checkBindStatus () {
var openid = this.openid;
if(!openid){
if(this.isLoadOpenId){return}
//获取openid
openid = await this.getOpenIdByCode();
this.redUrl+='3'+openid;
}
//调用后端接口判断是否当前openid能登录
queryHasBindDid(openid).then(res => {
if (res.code == 200 && res.login_dx_role) {
uni.setStorageSync('donor_info', { ...res.Info })
uni.reLaunch({
url: `/pages/home/homepage?onlyDid=1`,
success: () => { this.showLoadingPage = false},
fail: () => {this.showLoadingPage = false // 跳转失败时也要关闭loading}
})
//保存缓存等操作
.....
}else{
this.showLoadingPage = false;
}
}).catch(error => {
this.showLoadingPage = false;
})
},
//获取openid方法
getOpenIdByCode() {
return new Promise((resolve, reject) => {
this.isLoadOpenId = true;
const code = this.getQueryString('code');
if (code) {
getOpenId(code).then(res => {
if (res.code == 200) {
this.openid = res.msg
resolve(this.openid)
}
}).catch(error => {
this.error = error
this.showLoadingPage = false
console.log("获取openid失败: ", error)
}).finally(() => {
this.isLoadOpenId = false;
})
} else {
reject("code is null")
}
})
},

你好,使用xweb调试下呢?