微信运动属于敏感数据,如果直接调用wx.getWeRunData接口的话获取到的数据是加密后的,所以用云开发的话,步骤如下:
1. 开通云开发后,新建一个Node.js云函数:
2. 获取开放数据必须得用户授权登录,因此要先调用wx.login接口,建好云函数后,获取微信运动数据的完整代码如下:
Page({
data: {
step:null
},
onLoad: function (options) {
var that = this;
wx.login({
success:function(resLonin){
console.log(resLonin)
console.log(resLonin.code)
wx.getWeRunData({
success:function(resRun){
console.log("微信运动密文:")
console.log(resRun)
wx.cloud.callFunction({
name:'weRun',//云函数的文件名
data:{
weRunData: wx.cloud.CloudID(resRun.cloudID),
obj:{
shareInfo: wx.cloud.CloudID(resRun.cloudID)
}
},
success: function (res) {
console.log("云函数接收到的数据:")
console.log(res)
let step = res.result.event.weRunData.data.stepInfoList[30].step
that.setData({
step:step
})
console.log("得到的今日步数:",that.data.step)
}
})
}
})
}
})
},
})