小程序微信登录——

	<button type="primary" open-type="getUserInfo" @getuserinfo="getuserinfo" withCredentials="true">微信登录</button>
	<!-- #endif -->
	<!-- #ifdef APP-PLUS -->
	<button type="primary" open-type="getUserInfo" @click="getuserinfoh5appwx" withCredentials="true">微信登录</button>
	<!-- #endif -->
	<button style="margin-top:50px;">手机号码登录</button>

getuserinfo: function(res1) {
console.log(res1);
//如果只需要opendid 和非加密数据至此登录完成

			wx.login({
				success: function(res2) {
					console.log(res2);
					// 						//获取 sessionKey
					wx.request({
						url: 'https:///hoa.hcoder.net/xcxencode/?c=sk&appid=wx59704652556221e6&secret=8e282b8ad3fa81c14cfddb5fa82d7414&code=' +
							res2.code,
						success: function(res3) {
							console.log(res3);
							// 								//记录到本地
							try {
								uni.setStorageSync('sk', res3.data.session_key);
								uni.setStorageSync('openid', res3.data.openid);
							} catch (e) {
								//TODO handle the exception
							}
							uni.hideLoading();
							//以下步骤可以获取加密信息,需要授权
							//获取加密信息
							if (!res1.detail.iv) {
								uni.showToast({
									title: "您取消了授权,登录失败",
									icon: "none"
								});
								return false;
							}
							try {
								var sessionKey = uni.getStorageSync('sk');
								console.log(sessionKey);
							} catch (e) {
								//TODO handle the exception
							}
							uni.request({
								/**
								* $appid         = $_POST['appid'];
								 $sessionKey    = $_POST['sessionKey'];
								 $encryptedData = $_POST['encryptedData'];
								 $iv            = $_POST['iv'];
								*/
								method: "POST",
								url: 'https:///hoa.hcoder.net/xcxencode/',
								header: {
									'content-type': 'application/x-www-form-urlencoded'
								},
								data: {
									appid: "wx59704652556221e6",
									sessionKey: sessionKey,
									iv: res1.detail.iv,
									encryptedData: res1.detail.encryptedData
								},
								success: function(res4) {
									//"{"openId":"oS6of0V0rdp9nY_BuvCnQUasOHYc","nickName":"深海",
									//"gender":1,"language":"zh_CN","city":"Xi'an","province":"Shaanxi",
									//"country":"China","avatarUrl":"https://wx.qlogo.cn/mmopen/vi_32/7iags6YD4enyU"
									console.log(res4);
									//至此登录完成
								}
							});
						}
					})
				}
			});
		},
		getuserinfoh5appwx: function() {
			uni.login({
				success: function(rs1) {
					console.log(JSON.stringify(rs1) + '');
					uni.getUserInfo({
						success: function(rs2) {
							console.log(JSON.stringify(rs2) + '2');
						}
					})
				},
			});
		},
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值