收藏
回答

使用window.location.href或replace跳转微信授权怎么才能不生成一多的页面栈?

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) => {
            },
          });
        }
      }
    }
回答关注问题邀请回答
收藏

1 个回答

  • 智能回答 智能回答 本次回答由AI生成
    2025-12-12
    有用
登录 后发表内容