先说问题,uniapp运行到小程序模拟器时可正常解密,真机调试报错,网上搜的一些编码解码问题等等无法解决,(如果模拟器都无法正常解密,请检查uni.getuser是否在uni.login之前)

错误代码:

<button class="enterpriseLogin" @click="toGetPhoneNumber" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
toGetPhoneNumber(){
				let self = this;
				uni.request({
					method:'GET',
					url:'url',
					success(idres) {
						self.appid = idres.data[0].appid;
						uni.login({
							provider: 'weixin',
							success: (loginRes) => {
								self.jsCode = loginRes.code;//保存登录后的code
								//获取session_key
								uni.request({
									url: 'https://api.weixin.qq.com/sns/jscode2session',
									method:'GET',
									data: {
									appid: idres.data[0].appid,        //你的小程序的APPID
									secret: idres.data[0].session,    //你的小程序秘钥secret,  
									js_code: self.jsCode,    //wx.login 登录成功后的code
									grant_type:'authorization_code'
									},
									success: (cts) => {
										self.openid=cts.data.openid    //openid 用户唯一标识
										self.session_key=cts.data.session_key    //session_key  会话密钥
									}
								});
							}
						});
					}
				})
			},
			getPhoneNumber(e) {
				let self = this;
				if(e.detail.errMsg==`getPhoneNumber:ok`){
					let pc = new WXBizDataCrypt(`${self.appid}`,self.session_key);
					let data = pc.decryptData(e.detail.encryptedData,e.detail.iv);
					uni.request({
						method:'GET',
						url:`url`,
						data:{
							"flag":1,
							"val":data.phoneNumber
						},
						success(res) {
							if(res.data.length == 0){
								uni.showToast({
									title: '未注册',
									duration: 2000,
									icon: 'exception'
								});
							}else{
								uni.setStorage({
									key:'logininformation',
									data:res.data[0]
								});
								uni.switchTab({
									url:'/pages/my/my'
								});
							}
						}
					})
				}
			},

正确代码:

<button class="enterpriseLogin" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
onLoad() {
			this.toGetPhoneNumber();
		},
toGetPhoneNumber(){
				let self = this;
				uni.request({
					method:'GET',
					url:'url',
					success(idres) {
						self.appid = idres.data[0].appid;
						uni.login({
							provider: 'weixin',
							success: (loginRes) => {
								self.jsCode = loginRes.code;//保存登录后的code
								//获取session_key
								uni.request({
									url: 'https://api.weixin.qq.com/sns/jscode2session',
									method:'GET',
									data: {
									appid: idres.data[0].appid,        //你的小程序的APPID
									secret: idres.data[0].session,    //你的小程序秘钥secret,  
									js_code: self.jsCode,    //wx.login 登录成功后的code
									grant_type:'authorization_code'
									},
									success: (cts) => {
										self.openid=cts.data.openid    //openid 用户唯一标识
										self.session_key=cts.data.session_key    //session_key  会话密钥
									}
								});
							}
						});
					}
				})
			},
			getPhoneNumber(e) {
				let self = this;
				if(e.detail.errMsg==`getPhoneNumber:ok`){
					let pc = new WXBizDataCrypt(`${self.appid}`,self.session_key);
					let data = pc.decryptData(e.detail.encryptedData,e.detail.iv);
					uni.request({
						method:'GET',
						url:`url`,
						data:{
							"flag":1,
							"val":data.phoneNumber
						},
						success(res) {
							if(res.data.length == 0){
								uni.showToast({
									title: '未注册',
									duration: 2000,
									icon: 'exception'
								});
							}else{
								uni.setStorage({
									key:'logininformation',
									data:res.data[0]
								});
								uni.switchTab({
									url:'/pages/my/my'
								});
							}
						}
					})
				}
			},

原因:uni.login一定要在getPhoneNumber之前,之前在onload时就执行。

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐