可以通過微信接口: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: { //傳給後臺參數code
code: resLogin.code
},
success: function (resSession) {
//2、調用小程序API: wx.getWeRunData獲取微信運動數據(加密的);
wx.getWeRunData({
success(resRun) {
const encryptedData = resRun
console.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
}
]
}
timestamp | number | 時間戳,表示數據對應的時間 |
step | number | 微信運動步數 |
參考:https://blog.csdn.net/a389483637/article/details/79544645
參考:https://blog.csdn.net/joyce_lcy/article/details/83412426