使用uniapp进行微信用户信息获取

小程序登录、用户信息相关接口调整说明:https://developers.weixin.qq.com/community/develop/doc/000cacfa20ce88df04cb468bc52801
考虑到近期开发者对小程序登录、用户信息相关接口调整的相关反馈,为优化开发者调整接口的体验,回收wx.getUserInfo接口可获取用户授权的个人信息能力的截止时间由2021年4月13日调整至2021年4月28日24时。

所以无法使用wx.getUserInfo来做微信授权了,而新增加了getUserProfil

`

<button type="primary" @click="getInfo()">获取信息</button>
<button @click="login">登录</button>
            // 手动授权方法
            login(e) {

				uni.login({
				  success (res) {
					 console.log(res);
						if (res.code) {
						  //发起网络请求
						  
						  uni.request({
							// 请求路径
							url: 'api/user/login',
							// 请求参数code
							data: {
							  jsCode: res.code
							},
							
							method: 'GET',
							success(res){
								console.log(res)
								uni.setStorage({
									key: 'token',
									data: res.data.token,
								})
							}
						  })
						} else {
						  console.log('登录失败!' + res.errMsg)
						}
					}
				})
                    // fail() {
                    //     console.log("获取用户信息失败");
                    // }
                
            },
			getInfo(){
				uni.showModal({
					title: '温馨提示',
					content: '亲,授权微信登录后才能正常使用小程序功能',
					success(){
				          uni.getUserProfile({
				          		desc: '用于完善会员资料',
				          	    success(res) {
				          			const token = uni.getStorageSync('token');
				          	        uni.request({
				          				// 请求路径
				          				url: 'api/user/info',
				          				// 请求参数code
				          				header:{
				          					token:token
				          				},
				          				data: {
				          				  encryptedData: res.encryptedData,
				          				  iv:res.iv,
				          				  rawData:res.rawData,
				          				  signature:res.signature
				          				},
				          				method: 'GET',
				          				success(res){
				          					// 请求成功后获取openid和session_key
				          					console.log(res)
				          				},
				          				fail(rej){
				          					console.log(rej)
				          				}
				          	        })
				          	    },
				          	    fail(rej) {
				          			console.log(rej)
				          	        console.log("获取用户信息失败")
				          	    }
				          	})
						
					},
					fail() {
						// console.log(3);
						uni.showToast({
							title: '您拒绝了请求,不能正常使用小程序',
							icon: 'error',
							duration: 2000
						});
						return;
					}
					})
				          
			},

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值