在小程序中获取用户微信运动步数
getStep() {
var that = this;
wx.login({
success: function(res) {
var appid = "小程序id";
var secret = "小程序secret";
if (res.code) {
wx.request({
url: 'https://api.weixin.qq.com/sns/jscode2session?appid=' +
appid +
'&secret=' + secret + '&js_code=' + res.code +
'&grant_type=authorization_code',
header: {
'content-type': 'json'
},
success: function(res) {
var session_key = res.data.session_key;
that.getStepInfo(appid, session_key);
}
})
}
}
})
},
//获取encryptedData(没有解密的步数)和iv(加密算法的初始向量)
getStepInfo: function(appid, session_key) {
wx.getSetting({
success: function(res) {
console.log(res);
// 未开启微信运动授权
if (!res.authSetting['scope.werun']) {
wx.showModal({
title: '提示',
content: '获取微信运动步数,需要开启计步权限',
success: function(res) {
if (res.confirm) {
//跳转去设置
wx.openSetting({
success: function(res) {}
})
} else {
//不设置
}
}
})
} else {
wx.getWeRunData({
success: function(res) {
// console.log(res);
// console.log("appid:" + appid + "session_key:" + session_key + "encryptedData:" + res.encryptedData + "iv:" + res.iv);
var encryptedData = res.encryptedData;
var iv = res.iv;
var pc = new WXBizDataCrypt(appid, session_key);
var data = pc.decryptData(encryptedData, iv)
console.log(data)
console.log(data.stepInfoList[30].step)
wx.showModal({
title: '提示',
content: '微信步数'+data.stepInfoList[30].step,
showCancel: false,
confirmText: '知道了'
})
},
fail: function(res) {
wx.showModal({
title: '提示',
content: '开发者未开通微信运动,请关注“微信运动”公众号后重试',
showCancel: false,
confirmText: '知道了'
})
}
})
}
}
})
},