uniapp vue2 h5端,跳转到微信授权后,当前页面返回上一页需要返回两次,要解决这个问题,又加入了window.history.back();又导致了back后ios系统不执行onLoad的问题,还发现有时候点击苹果下方物理返回按钮onUnload生命周期没有执行的问题。
onUnload() {
if (this.timer) {
clearInterval(this.timer);
this.timer = null;
}
uni.removeStorageSync('userCode')
uni.removeStorageSync('order_pay_data')
},
async onLoad(options) {
const userCode = uni.getStorageSync("userCode");
const code = this.getUrlParam('code');
this.xcxCode = code;
uni.setStorageSync('order_pay_data', {
orderNo: options.orderNo || '',
fromType: options.fromType || '',
});
if (!userCode && !this.xcxCode) {
this.getCode()
} else if (this.xcxCode && !userCode) {
uni.setStorageSync('userCode', code)
uni.setStorageSync('needRefresh', true)
setTimeout(() => {
window.history.back();
}, 500)
} else {
if (userCode && !this.isAuthCompleted) {
this.loadPage(options)
}
}
}
getCode() {
const appId = $service().STATIC_APPID;
const local = window.location.href;
let localUrl = encodeURIComponent(local);
const code = this.xcxCode;
const userCode = uni.getStorageSync('userCode')
if (!code && !userCode) {
let ua = navigator.userAgent.toLowerCase();
if (ua.includes('micromessenger')) {
window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${localUrl}&response_type=code&scope=snsapi_base&state=123#wechat_redirect`;
} else {
uni.showModal({
title: '提示',
content: '请在微信环境中打开',
confirmText: '确定',
success: (res) => {
},
});
}
}
}
