uniapp微信小程序获取微信运动步数(保姆级教程)

1、用户点击按钮,在小程序中触发getuserinfo方法,获取用户信息

<u-button type="success" open-type="getUserInfo" @getuserinfo="getStep">去完成</u-button>

2、在uniapp项目中,新建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,就是一些,微信步数加密后的数据

//获取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.stepInfoList[30].step)
					},
					fail: function(res) {
						wx.showModal({
							title: '提示',
							content: '开发者未开通微信运动,请关注“微信运动”公众号后重试',
							showCancel: false,
							confirmText: '知道了'
						})
					}
				})
			}
		}
	})
}

4、其中用到了**[开放能力 /用户信息 /开放数据校验与解密]

官方文档
https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/signature.html
可以先了解一下,然后下载对应的解密文件
在这里插入图片描述
![在这里插入图片描述](https://img-blog.csdnimg.cn/a9dbc3ffbb8a48479369619eb7a780cc.png

找到对应的解密语言版本,老师这里用的uniapp,只能选择node版
在这里插入图片描述
在这里插入图片描述
这里有具体的案例,可以参考一下,直接在代码中导入,在上面的getStepInfo方法中直接实例化,即可直接使用

5、最终可打印出用户近30的微信运动步数数据,需要那一天直接拿出来用就行。

在这里插入图片描述

uniapp开发微信小程序中,可以通过微信授权的方式获取用户的信息,包括头像、昵称和手机号。具体步骤如下: 1. 使用uni.login()方法获取微信提供的code码。 2. 使用获取到的code码,可以获取用户的唯一标识openid和会话密钥sessionkey,用于解密获取手机的加密信息。 3. 使用微信提供的getphonenumber方法,获取encryptedData和iv两个字段的内容。 4. 使用微信提供的解密方法,可以得到最终的手机号。 5. 要获取用户的头像、昵称等信息,可以直接调用uni.getUserProfile()方法。 通过以上步骤,就可以在uniapp微信小程序获取用户的信息了。引用\[2\]\[3\] #### 引用[.reference_title] - *1* *3* [uniapp小程序中登录,获取用户信息,获取手机号逻辑记录](https://blog.csdn.net/u011608672/article/details/124713671)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [uniapp小程序登录,获取用户信息,获取手机号等流程](https://blog.csdn.net/tutodelinglimgho/article/details/131321975)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

onenessxy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值