1、用戶點(diǎn)擊按鈕,在小程序中觸發(fā)getuserinfo方法,獲取用戶信息
<u-button type="success" open-type="getUserInfo" @getuserinfo="getStep">去完成</u-button>
2、在uniapp項(xiàng)目中,新建getStep方法,獲取獲取session_key,用來解密必須用到的
getStep() {
var that = this;
wx.login({
success: function(res) {
var appid = "wxeeb60e09bc7e8059";
var secret = "dfadcd1e92a2c35488e340c7e39a63b7";
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);
}
})
}
}
})
}
3、獲取encryptedData和iv,就是一些,微信步數(shù)加密后的數(shù)據(jù)
//獲取encryptedData(沒有解密的步數(shù))和iv(加密算法的初始向量)
getStepInfo: function(appid, session_key) {
wx.getSetting({
success: function(res) {
console.log(res);
// 未開啟微信運(yùn)動(dòng)授權(quán)
if (!res.authSetting['scope.werun']) {
wx.showModal({
title: '提示',
content: '獲取微信運(yùn)動(dòng)步數(shù),需要開啟計(jì)步權(quán)限',
success: function(res) {
if (res.confirm) {
//跳轉(zhuǎn)去設(shè)置
wx.openSetting({
success: function(res) {}
})
} else {
//不設(shè)置
}
}
})
} 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.stepInfoList[30].step)
},
fail: function(res) {
wx.showModal({
title: '提示',
content: '開發(fā)者未開通微信運(yùn)動(dòng),請(qǐng)關(guān)注“微信運(yùn)動(dòng)”公眾號(hào)后重試',
showCancel: false,
confirmText: '知道了'
})
}
})
}
}
})
}
4、其中用到了**[開放能力 /用戶信息 /開放數(shù)據(jù)校驗(yàn)與解密]
官方文檔
https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/signature.html
可以先了解一下,然后下載對(duì)應(yīng)的解密文件
找到對(duì)應(yīng)的解密語言版本,老師這里用的uniapp,只能選擇node版
這里有具體的案例,可以參考一下,直接在代碼中導(dǎo)入,在上面的getStepInfo方法中直接實(shí)例化,即可直接使用文章來源:http://www.zghlxwxcb.cn/news/detail-736462.html
5、最終可打印出用戶近30的微信運(yùn)動(dòng)步數(shù)數(shù)據(jù),需要那一天直接拿出來用就行。
文章來源地址http://www.zghlxwxcb.cn/news/detail-736462.html
到了這里,關(guān)于uniapp微信小程序獲取微信運(yùn)動(dòng)步數(shù)(保姆級(jí)教程)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!