聪少爱学堂 专注分享全网引流精准引流方法及自媒体运营干货
本篇文章主要介绍了微信小程序微信运动步数的实例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
现在运动计步很火,无论是蚂蚁森林,还是微信上都很火爆,本文介绍了微信小程序微信运动步数的实例代码,分享给大家
微信小程序API-微信运动
思路: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 . "
");
} else {
print($errCode . "
");
}
以上就是微信小程序如何实现获取微信运动步数的案例(图)的详细内容,更多请关注php中文网其它相关文章!
微信分享
声明:本文原创发布php中文网,转载请注明出处,感谢您的尊重!如有疑问,请联系admin@php.cn处理
相关标签:小程序 运动 获取上一篇:微信小程序之页面跳转传值的案例下一篇:微信小程序中实现摇一摇功能的方法介绍
聪少爱学堂,专注分享全网精准引流方法及自媒体赚钱运营干货。
聪少私人微信:80110557,暗号:8
送见面礼:价值980元自媒体运营与抖音热门教程礼包一份。
或微信扫描下面二维码,马上添加
干货分享:微信小程序引流推广的四种方法2021-06-29
微信小程序店铺如何做引流,带来转化?2021-06-29
2019升级版微信引流 精准 人脉 互粉程序系统平台 网站2021-06-29
海外房地产企业如何利用微信小程序拓客引流?2021-06-29
小程序分销商城能满足商家哪些需求?2021-06-29
小程序怎么引流吸粉?做好微信小程序的五大技巧(3)2021-06-29
餐饮行业怎样利用微信小程序引流2021-06-29
微信小程序怎么做秒杀活动引流到店?2021-06-29
微信公众号如何结合小程序引流?-2021-06-29
微信小程序商城如何引流?2021-06-29