onShareAppMessage 分享取消后,app.js的onshow方法再次加密path?
发布于 3 年前 作者 yanchao 14415 次浏览 来自 问答

业务场景:客户通过扫码小程序码领取优惠券,小程序码带参为99000002%3AC%3A100000139,解密后就是99000002:C:100000139,然后我再讲优惠券分享给朋友时,进行取消操作,此时app.js的onshow方法会再调用一次,参数则改变为99000002%253AC%253A100000139,请问这种情况要怎么规避。

3 回复
// 获取参数 app.js
onLaunch(options) {
  this.globalData.query = options.query || {}
  if (options.referrerInfo) {
     this.globalData.query = Object.assign(this.globalData.query, options.referrerInfo.extraData)
  }
},
onShow(options) { // 这里打印并且解密你的二维码里的参数
  if (Object.keys(options.query).length > 0) {
      // 解决连续扫两次带参数的码,参数丢失的问题
      this.globalData.query = options.query || {}
      // 处理通过小程序码进来的参数,包括渠道码等,放置到全局数据中
      if (options.query.scene) {
        let queryObj = {}
        var strs = decodeURIComponent(options.query.scene).split('&') //以&分割
        for (var i = 0; i < strs.length; i++) {
          queryObj[strs[i].split('=')[0]] = unescape(strs[i].split('=')[1])
        }
        Object.assign(this.globalData.query, queryObj)
      }
    }
  if (options.referrerInfo) {
     this.globalData.query = Object.assign(this.globalData.query, options.referrerInfo.extraData)
  }
},
globalData: {
  query: {}
}
// 页面js
onLoad(options) {
   // 先从上个页面获取,再从app.js里获取,都没获取到给个空字符串做保护。
   this.setData({
     shareId: options.id || app.globalData.query.id || ''
   })
}

代码逻辑卡就可以了,比如,当有解密参数,不进行再次解密

回到顶部