场景:A小程序跳转至B小程序,A小程序的用户点击某一个入口,跳转到了B小程序,此时如何获取A小程序带来的uid?

参考api:

打开另一个小程序:https://developers.weixin.qq.com/miniprogram/dev/api/open-api/miniprogram-navigate/wx.navigateToMiniProgram.html

获取小程序启动时的参数:https://developers.weixin.qq.com/miniprogram/dev/api/base/app/life-cycle/wx.getLaunchOptionsSync.html

在app.js

App({
  onLaunch: function(options) {
    console.log(options)
    // 展示本地存储能力
    if (options.referrerInfo.extraData && options.referrerInfo.extraData.appName) {
      this.globalData.sourceData.extraData.uid = options.referrerInfo.extraData.uid;
      this.globalData.sourceData.extraData.dvid = options.referrerInfo.extraData.dvid;
      this.globalData.sourceData.extraData.appName = options.referrerInfo.extraData.appName;
    }
  },
  globalData: {
    sourceData: {
      uid: null, //用户标识 此处模拟用户没登录的情况
      dvid: null, //腾讯返回的unionid
      appName:'',
    }
  }
})

在页面的onload

    let localUid = wx.getStorageSync('uid');
    if (!localUid) {
      if (app.globalData.sourceData.ycApp && app.globalData.sourceData.ycApp.uid) {
        wx.setStorage({
          key: 'uid',
          data: app.globalData.sourceData.ycApp.uid
        });
        that.setData({ //是否显示消息提示选项
          uid: app.globalData.sourceData.ycApp.uid,
        })
      }
    } else {
      that.setData({ //是否显示消息提示选项
        uid: localUid,
      })
    }
    if (that.data.uid) { //存在uid
      that.setData({ //是否显示消息提示选项
        notifyBar: true,
      })
    } else {
      that.setData({
        notifyBar: false
      })
    }

  

02-12 05:57