欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

微信小程序獲取微信運(yùn)動(dòng)步數(shù)的實(shí)例代碼

 更新時(shí)間:2017年07月20日 16:09:30   作者:風(fēng)流三月1  
本篇文章主要介紹了微信小程序微信運(yùn)動(dòng)步數(shù)的實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

現(xiàn)在運(yùn)動(dòng)計(jì)步很火,無論是螞蟻森林,還是微信上都很火爆,本文介紹了微信小程序微信運(yùn)動(dòng)步數(shù)的實(shí)例代碼,分享給大家

微信小程序API-微信運(yùn)動(dòng)
https://mp.weixin.qq.com/debug/wxadoc/dev/api/we-run.html#wxgetwerundataobject

思路:wx.login獲取的code請求獲取的session_key,wx.getWeRunData獲取的iv,encryptData,將它們一起發(fā)送到后臺(tái)解密就行了。

安全顧慮,因?yàn)橹皇鞘纠灾苯觽鬟fsession_key了,為了安全最好按照下圖的方式加密后存儲(chǔ)到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: {}, // 設(shè)置請求的 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({//解密微信運(yùn)動(dòng)
     success(res) {
      const wRunEncryptedData = res.encryptedData
      that.setData({ encryptedData: wRunEncryptedData })
      that.setData({ iv: res.iv })
      that.get3rdSession()//解密請求函數(shù)
     }
    })
   }
  })
 }

后臺(tái)這使用的是官方PHP版本Demo:先處理login的請求,login.php直接返回session_key,然后再一起請求decrypt.php進(jìn)行解密。

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");
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論