欢迎来到258分享网,纯净的网络源码分享基地!

258资源分享网

全部作品
全部作品
网站源码
微信源码
素材特效
源码插件
视频教程
建站学院
热门搜索: 织梦  农业种植  农业  安全设置  官方
258资源分享 > 建站学院 > 微信开发 > 微信小程序--获取微信运动步数的实例代码

推荐下载

HTML5响应式自适应网咯设计

2020-05-12   浏览:740

高端HTML5响应式企业通用网

2020-05-06   浏览:521

html5响应式外贸网站英文版

2020-05-08   浏览:510

HTML5自适应律师工作室类网

2020-04-04   浏览:504

HTML5影视传媒文化公司类网

2020-05-12   浏览:500

微信小程序--获取微信运动步数的实例代码

发布时间:2020-10-30  

现在运动计步很火,无论是蚂蚁森林,还是微信上都很火爆,本文介绍了微信小程序微信运动步数的实例代码,分享给大家

微信小程序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");