解决小程序获取手机号时,checkSession通过但是获取手机号解密失败
发布于 3 年前 作者 fanglai 4513 次浏览 来自 分享
  • checkSession通过但是获取手机号解密失败
  • wx.login(),然后拿到新的session_key,再用此时的新key去解密出问题的概率还是很大
  • 用定时器定时登录获取session_key,99%解决
export default {
    data() {
      return {
        wxSession: '',
        wxLoginInterval: null
      };
    },
    onLoad() {
      this.getPlatformInfo();
    },
    onShow() {
      const _this = this;
      _this.checkWxSession(_this.wxSession).then((session) => {
        _this.wxSession = session;
      });
      _this.startWxLoginInterval();
    },
    /**
     * 生命周期函数--监听页面卸载
     * 退出本页面时停止计时器
     */
    onUnload: function () {
      this.clearWxLoginTimeInterval();
    },

    /**
     * 生命周期函数--监听页面隐藏
     * 在后台运行时停止计时器
     */
    onHide: function () {
      this.clearWxLoginTimeInterval();
    },
    computed: {},
    methods: {
	/**
	  * 结束wx定时登录
	*/
      clearWxLoginTimeInterval: function () {
        var that = this;
        if (!that.wxLoginInterval) {
          return;
        }
        clearInterval(that.wxLoginInterval);
        that.wxLoginInterval = "";
      },
      /**
       * 定时60s登录wx
       */
      startWxLoginInterval: function () {
        const _this = this;
        var interval = setInterval(function () {
          wx.login({
            success(res) {
              _this.wxSession = res.code;
            },
            fail(err) {}
          })
          console.log('定时60s登录');
        }, 60000);
        this.wxLoginInterval = interval;
      },
	  /**
       * 检测wxsession是否过期
       */
      checkWxSession(wxSession) {
        return new Promise(function (resolve, reject) {
          //没有session登录获取
          if (!wxSession) {
            wx.login({
              success(res) {
                resolve(res.code);
              },
              fail(err) {}
            })
            return;
          }
          wx.checkSession({
            success() {
              //session_key 未过期,并且在本生命周期一直有效
              console.log('没有过期', wxSession);
              resolve(wxSession);
            },
            fail() {
              console.log('过期,重新登录')
              // session_key 已经失效,需要重新执行登录流程
              wx.login({
                success(res) {
                  resolve(res.code);
                },
                fail(err) {
                  reject(err);
                }
              })
            }
          })
        });
      },
	  /**
       * 获取手机号
       */
      getPhone(e) {
        console.log("login info ", e);
        const _this = this;
        _this.checkWxSession(_this.wxSession).then((session) => {
          _this.wxSession = session;

          let encryptedData = encodeURIComponent(e.detail.encryptedData);
          let iv = encodeURIComponent(e.detail.iv);
          if (undefined == encryptedData || "undefined" == encryptedData) {
            console.log("用户拒绝授权手机号");
            return;
          }
          getUserInfoByPhone({
              iv: iv,
              encryptedData: encryptedData,
              jsCode: session,
              appid: process.env.VUE_APP_ID,
            })
            .then((res) => {
              console.log("解密之后的手机号",res);
              _this.wxSession = "";
            })
            .catch((err) => {
              console.log(err);
            });
        })
      },
    },
  };
回到顶部