编辑WXBizDataCrypt.js文件,在需要解析的页面使用
WXBizDataCrypt.js
var crypto = require('crypto')
function WXBizDataCrypt(appId, sessionKey) {
this.appId = appId
this.sessionKey = sessionKey
}
WXBizDataCrypt.prototype.decryptData = function (encryptedData, iv) {
// base64 decode
var sessionKey = new Buffer(this.sessionKey, 'base64')
encryptedData = new Buffer(encryptedData, 'base64')
iv = new Buffer(iv, 'base64')
try {
// 解密
var decipher = crypto.createDecipheriv('aes-128-cbc', sessionKey, iv)
// 设置自动 padding 为 true,删除填充补位
decipher.setAutoPadding(true)
var decoded = decipher.update(encryptedData, 'binary', 'utf8')
decoded += decipher.final('utf8')
decoded = JSON.parse(decoded)
} catch (err) {
// throw new Error('Illegal Buffer')
}
// if (decoded.watermark.appid !== this.appId) {
// throw new Error('Illegal Buffer')
// }
return decoded
}
module.exports = WXBizDataCrypt
index.vue
<template>
<view style="background-color: aliceblue;height: 100vh;overflow: auto;padding-top: 8rpx;">
<u-notice-bar text="由于微信仅提供用户近三十天的步数信息,请您每三十天内打开一次小程序。"></u-notice-bar>
<view class="" style="text-align: center;font-size: 1.5rem;font-weight: bold;margin: 1rem 0;">
微信步数
</view>
<uni-table ref="table" :loading="loading" stripe emptyText="暂无更多数据">
<uni-tr>
<uni-th width="100" align="center">时间</uni-th>
<uni-th width="100" align="center">步数</uni-th>
</uni-tr>
<uni-tr v-for="(item, index) in arr" :key="index">
<uni-td align="center">
<view class="" style="display: flex;justify-content: center;"
:style=" {color:date==item.timestamp?'#000':''}">
<!-- clock -->
<u-icon name="clock"></u-icon>
<view class="" style="margin-left: 12rpx;">
{{item.timestamp}}
</view>
</view>
</uni-td>
<uni-td align="center">
<view style="display: flex;justify-content: center;"
:style="{color:item.step>=8000?'#19be6b':date!=item.timestamp&&item.step<8000?'#909399':'#000' }">
<image src="../../static/img/index/步数.svg" mode=""
style="width: 36rpx;height: 36rpx;margin-right: 12rpx;"></image>
<view>{{ item.step }}</view>
<view class="" v-if="item.step>=8000">
(已达标)
</view>
</view>
</uni-td>
</uni-tr>
</uni-table>
<u-toast ref="uToast"></u-toast>
<u-popup :show="show1" mode="center" :round="10">
<view style="bottom:0;width:600rpx">
<view class="" style="padding: 48rpx;">
<view class="" style="text-align: center;font-weight: bold;">
提示
</view>
<view class="" style="color:red;line-height: 1.5rem;padding-top: 1rem;">
由于微信仅提供用户近三十天的步数信息,请您每三十天内打开一次小程序。
</view>
</view>
<u-button :text="buttonText" :disabled="disabledButton" @click="offButton"></u-button>
</view>
</u-popup>
</view>
</template>
<script>
import WXBizDataCrypt from './WXBizDataCrypt.js'
import {
synchronizationStep,
getUser,
getUser_wx
} from '../../api/index.js'
export default {
data() {
return {
title: 'Hello',
appid: '',
secret: '',
session_key: '',
arr: [],
loading: true,
show: false,
show1: false,
date: new Date().toISOString().slice(0, 10),
userId: '',
buttonText:'确定(5)',
dateText:5,
disabledButton:true,
userIv:''
}
},
onLoad() {
},
onShow() {
this.login()
},
methods: {
offButton() {
this.show1 = false
if (!this.userId) {
this.$refs.uToast.show({
type: 'default',
message: "请进行身份绑定!",
})
}
},
login() {
var that = this;
const accountInfo = uni.getAccountInfoSync();
this.appid = accountInfo.miniProgram.appId
wx.login({
success: function(res) {
getUser_wx({
code: res.code
}).then(res => {
var session_key = res.session_key;
that.getStepInfo(that.appid, session_key);
wx.setStorageSync('openid', res.openid)
}).catch(err => {
//隐藏加载框
that.loading = false
})
}
})
},
//获取encryptedData(没有解密的步数)和iv(加密算法的初始向量)
getStepInfo: function(appid, session_key) {
let that = this
wx.getSetting({
success: function(resSetting) {
// 未开启微信运动授权 scope.userRunData
// 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) {
var encryptedData = res.encryptedData;
var iv = res.iv;
this.userIv = res.iv
console.log(this.userIv);
var pc = new WXBizDataCrypt(appid, session_key);
var data = pc.decryptData(encryptedData, iv)
that.arr = data.stepInfoList.reverse().map(res => {
return {
timestamp: that.timeFun(res.timestamp),
step: res.step
}
})
wx.setStorageSync('bindList', data.stepInfoList.reverse())
that.loading = false
that.getUser()
},
fail: function(res) {
if (res.err_code) {
wx.showModal({
title: '提示',
content: '获取微信运动步数,需要开启计步权限',
success: function(res) {
if (res.confirm) {
//跳转去设置
wx.openSetting({
success: function(
res) {}
})
} else {
uni.reLaunch({
url: '/pages/index/index'
})
}
},
fail: function() {
// uni.reLaunch({
// url: '/pages/index/index'
// })
}
})
} else if (res.errMsg == 'getWeRunData:fail') {
wx.showModal({
title: '提示',
content: '未开通微信运动,请关注“微信运动”公众号后重试',
showCancel: false,
confirmText: '知道了'
})
}
}
})
}
// }
})
},
// 同步步数
synchronization(arr) {
synchronizationStep(wx.getStorageSync('openid'), arr).then()
},
// end
// 身份获取
getUser() {
getUser({
wxChatId: wx.getStorageSync('openid')
}).then(res => {
this.userId = res.data.userId
// this.date = 5
let aaa = setInterval(()=>{
this.buttonText = `确定(${this.dateText})`
this.dateText--
if(this.dateText < 0){
clearInterval(aaa)
this.disabledButton = false
this.buttonText = `确定`
}
},1000)
this.show1 = true
if (res.data.userId) {
this.arr.iv = this.userIv
console.log(this.userIv);
// this.synchronization(this.arr)
} else {
// this.show1 = true
}
})
},
// end
timeFun(time) {
const date = new Date(time * 1000);
// 提取年、月、日
const year = date.getFullYear();
const month = ('0' + (date.getMonth() + 1)).slice(-2); // 月份从0开始计数,需要加1,并确保格式为两位数
const day = ('0' + date.getDate()).slice(-2); // 确保格式为两位数
return `${year}-${month}-${day}`;
}
}
}
</script>
<style>
.u-safe-bottom {
padding-bottom: 0;
}
button{
color: #3c9cff !important;
}
</style>