【微信小程序开发教程】解密微信运动数据

网友 7年前 3204 ℃ 导读

微信小程序开发API-微信运动https://mp.weixin.qq.com/debug/wxadoc/dev/api/we-run.html#wxgetwerundataobject思路:wx.login获取的code请求获取的session_key,wx.getWeRunData获取的iv,encryptData,将它们一起发送到后台解密就行了。安全顾...

微信小程序开发API-微信运动

https://mp.weixin.qq.com/debug/wxadoc/dev/api/we-run.html#wxgetwerundataobject

思路:wx.login获取的code请求获取的session_key,wx.getWeRunData获取的iv,encryptData,将它们一起发送到后台解密就行了。

安全顾虑,因为只是示例所以直接传递session_key了,为了安全最好按照下图的方式加密后存储到redis中再传递key。

【微信小程序开发教程】解密微信运动数据 小程序开发


小程序端代码

get3rdSession: function () {
    let that = this
    wx.request({
      url: 'https://localhost/login.php',
      data: {
        code: this.data.code
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      success: function (res) {
        var sessionId = res.data;
        that.setData({ sessionId: sessionId })
        wx.setStorageSync('sessionId', sessionId)
        that.decodeUserInfo()
      }
    })
  },
  decodeUserInfo: function () {
    let that = this
    wx.request({
      url: 'https://localhost/decrypt.php',
      data: {
        encryptedData: that.data.encryptedData,
        iv: that.data.iv,
        session: wx.getStorageSync('sessionId')
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function (res) {
        let todayStep = res.data.stepInfoList.pop()
        that.setData({
          step: todayStep.step
        });
      }
    })
  },
  onLoad: function () {
    let that = this
    wx.login({
      success: function (res) {
        let code = res.code
        that.setData({ code: code })
        wx.getWeRunData({//解密微信运动
          success(res) {
            const wRunEncryptedData = res.encryptedData
            that.setData({ encryptedData: wRunEncryptedData })
            that.setData({ iv: res.iv })
            that.get3rdSession()//解密请求函数
          }
        })
      }
    })
  }

后台这使用的是官方PHP版本Demo:先处理login的请求,login.php直接返回session_key,然后再一起请求decrypt.php进行解密。


login.php部分代码

$appid = '你的appid';
$appsecret = '你的appsecret';
$url = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$appid.'&secret='.$appsecret.'&js_code='.$_GET['code'].'&grant_type=authorization_code';
$content = file_get_contents($url);
$content = json_decode($content);
echo $content->session_key;


decrypt.php部分代码

$pc = new WXBizDataCrypt($appid, $sessionKey);
$errCode = $pc->decryptData($encryptedData, $iv, $data );
if ($errCode == 0) {
    print($data . "\n");
} else {
    print($errCode . "\n");
}


e3.gif

点击收藏【微信小程序开发教程】解密微信运动数据 | https://shyhot.com/kaifa/20170724382.html

瞎扯新时代

请使用手机自带浏览器扫描,一定不要使用QQ微信
微信打赏二维码宅男深夜APP
支付宝打赏二维码资源下载站

本站所收集的资源来源于互联网公开资料,转载的目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。本站部分作品是由网友自主投稿和发布,本站仅为交流平台,不为其版权负责。

小程序开发

100套精美PPT模板
500份优秀简历模板