可以通过微信接口:wx.getWeRunData 获取用户过去三十天的微信运动步数。
前提:
使用这个接口有两个前提:
1. 需先调用wx.login接口进行登录 步数信息会在用户主动进入小程序时更新。
2. 调用前需要用户授权scope.werun
Page({data: {runData:[],},/*** 生命周期函数--监听页面加载*/onLoad: function (options) {//1、调用小程序API:wx.login获取code和sessionKey;var that = this;wx.login({success: function (resLogin) {if (resLogin.code) {wx.request({ url: 'http://localhost:9281/wxapp/onlogin',data: { //传给后台参数codecode: resLogin.code},success: function (resSession) {//2、调用小程序API: wx.getWeRunData获取微信运动数据(加密的);wx.getWeRunData({success(resRun) {const encryptedData = resRunconsole.info(resRun);//3、解密步骤2的数据;wx.request({url: 'http://localhost:9281/wxapp/decrypt',data: { //传给后台加密参数encryptedData: resRun.encryptedData,iv: resRun.iv,code: resLogin.code},method: 'GET', success: function (resDecrypt) {var runData = JSON.parse(resDecrypt.data.data)if (runData.stepInfoList){runData.stepInfoList = runData.stepInfoList.reverse()for (var i in runData.stepInfoList){runData.stepInfoList[i].date = util.formatTime(new Date(runData.stepInfoList[i].timestamp*1000))}that.setData({ runData: runData.stepInfoList });} }});}})}})} else {console.log('获取用户登录态失败!' + res.errMsg)}}});},})
步骤三中传递给后台加密参数后台解析加密数据传递给前台解密后json
{"stepInfoList": [{"timestamp": 1445866601,"step": 100},{"timestamp": 1445876601,"step": 120}]}
参考:/a389483637/article/details/79544645
参考:/joyce_lcy/article/details/83412426