微信小程序分享出去的页面再点进来,如何取值并且在新用户未授权的情况下,授权后跳到当前页面


1、如何点击分享的页面进来,授权后跳转到当前页面

可以在授权成功后,将openid、头像、昵称入库成功之后,标记一下,及getStorageSync

  // 通过code获取openid
  getUserOpenId(code) {
    let postData = {
      code,
      appid: app.globalData.appid,
      parentUserid: wx.getStorageSync('parentUserid') || ''
    }
    app.requestData('您的接口', {
      ...postData
    }).then((res) => {
      if (res.result === 'success') {
        // wx.setStorageSync('sessionKey', res.data.sessionKey) // 缓存sessionKey
        wx.setStorageSync('openid', res.data.wxId) // 缓存openid
        this.setData({
          userinfoObj: {
            ...this.data.userinfoObj,
            openid: res.data.wxId
          }
        })
        console.log('wxId', res.data.wxId)
        this.insertUserInfo()
      } else {
        app.showTip(res.msg)
      }
    })
  },
  // 将 openid 头像、昵称 入库
  insertUserInfo() {
    app.requestData('您的接口', {
      nickName: this.data.userinfoObj.nickName,
      openid: this.data.userinfoObj.openid,
      avatarUrl: this.data.userinfoObj.avatarUrl,
      parentUserid: wx.getStorageSync('parentUserid') || ''
    }).then((res) => {
      if (res.result === 'success') {
        var value = wx.getStorageSync('urlWithArgs')
        app.checkIslogin(function () {
          if (value !== '') {
            wx.redirectTo({
              url: value,
            })
          } else {
            wx.switchTab({
              url: '../mine/index',
            })
          }
        })
      } else {
        app.showTip(res.msg)
      }
    })
  },

2、在分享的页面 onLoad函数

  onLoad: function (options) {     if (options.q) {       var qrUrl = decodeURIComponent(options.q);// 获取到二维码原始链接内容       var id = qrUrl.split('?')[1].split('&')[0].split('=')[1];//这里是获取分享链接上的参数id       // var flag = qrUrl.split('?')[1].split('&')[1].split('=')[1];       this.setData({         orderid: id,         flag: 0       })     } else {       this.setData({         orderid: options.id,         flag: options.flag       })     }     this.queryorderDetail(this.data.orderid)     //详情页的路由信息      const pages = getCurrentPages();     const currentPage = pages[pages.length - 1]     //路由     const url = currentPage.route;     //路由参数     const opt = currentPage.options;     var urlWithArgs = `/${url}?`     urlWithArgs += `id=${opt.id}&flag=0`//这里就是授权之后 跳转到当前页面     wx.setStorageSync('urlWithArgs', urlWithArgs);     wx.setNavigationBarColor({       frontColor: '#ffffff',       backgroundColor: '#3066FE',       animation: {         duration: 400,         timingFunc: 'easeIn'       }     });     let that = this;     wx.getSystemInfo({       success: function(res) {         //导航栏高度         that.setData({           windowHeight:res.windowHeight         })       }     });   },