刷题小程序–掌上题库小程序更新授权登录

微信改版了,又又又改版了

getUserInfo改成getUserProfile函数使用了      以下是刷题题库小程序的微信登录授权更新代码段

解决小程序前端

1、在pages/mine/mine.wxml中找到<button class="header_img" style="top:rpx;line-height: 140rpx;text-align: center;font-size: 16px;color: #333333;" bindgetuserinfo="getUserInfo" openType="getUserInfo" wx:else>登录</button>修改为<button class="header_img" style="top:rpx;line-height: 140rpx;text-align: center;font-size: 16px;color: #333333;" bindtap="getUserProfile" openType="getUserInfo" wx:else>登录</button>

2、在pages/mine/mine.js中添加以下函数,记得函数前后加上半角逗号(,)

getUserProfile: function() {
        wx.getUserProfile({
            desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
            success: (res) => {
                wx.login({
                  success:(l)=>{
                    console.log(l),dataApi.login2({
                        code:l.code,
                        is_bind:2,
                        encryptedData:res.encryptedData,
                        iv:res.iv,
                        nickName:res.userInfo.nickName,
                        city:res.userInfo.city,
                        province:res.userInfo.province,
                        avatarUrl:res.userInfo.avatarUrl,
                    }).then(function(tn){
                        console.log(tn),wx.hideLoading(),wx.showToast({ 
                            title: tn.message,
                            icon: "none"
                        }) ,0 == tn.errno && (wx.setStorageSync("uid", tn.data.uid)  ,setTimeout(function() { 
                            wx.reLaunch({
                                url: "/pages/home/home"
                            });
                        }, 1500)); 
                    }).catch(function(tn) {
                        console.log(tn), wx.hideLoading(), wx.showToast({
                            title: tn.message,
                            icon: "none"
                        });
                    });
                  }
                })
              
            }
          })
    }

3、找到provider/dataApi.js添加以下函数

function login2(e) {
    return api.post("login2", e);
}
// 找到最下面的modele.exports 添加 login2: login2
module.exports = {
	login: login, // 在他的下面添加就行
	login2: login2,
}

4、修改另一个页面的获取用户信息 找到 pages/bindWechat/bindWechat.wxml 找到 <button bindgetuserinfo="getUserInfo" class="bind_tips" openType="getUserInfo" wx:if="">绑定微信</button> 修改成 <button bindtap="getUserProfile" class="bind_tips" openType="getUserInfo" wx:if="">绑定微信</button>

5、找到pages/bindWechat/bindWechat.js 添加以下函数

getUserProfile: function() {
        var e = this;
        wx.getUserProfile({
            desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
            success: (res) => {
                wx.login({
                  success:(l)=>{
                    console.log(l),dataApi.login2({
                        code:l.code,
                        is_bind:2,
                        encryptedData:res.encryptedData,
                        iv:res.iv,
                        nickName:res.userInfo.nickName,
                        city:res.userInfo.city,
                        province:res.userInfo.province,
                        avatarUrl:res.userInfo.avatarUrl,
                    }).then(function(tn){
                        console.log(tn),wx.hideLoading(),wx.showToast({ 
                            title: tn.message,
                            icon: "none"
                        }) ,0 == tn.errno && (wx.setStorageSync("uid", tn.data.uid)  ,setTimeout(function() { 
                            wx.reLaunch({
                                url: "/pages/home/home"
                            });
                        }, 1500)); 
                    }).catch(function(t) {
                        console.log(t), wx.hideLoading(), wx.showToast({
                            title: t.message,
                            icon: "none"
                        });
                    });
                  }
                })
              
            }
          })
    },

修改后端

1、找到addons/goouc_fullexam/wxapp.php,加入以下函数

public function dopageLogin2()
	{
		global $_W;
		global $_GPC;
		$contion = '';
		$params[':weid'] = $_W['uniacid'];
		$code = $_GPC['code'];
		
		$user_info['nickName'] = $_GPC['nickName'];
		$user_info['province'] = $_GPC['province'];
		$user_info['city'] = $_GPC['city'];
		$user_info['avatarUrl'] = $_GPC['avatarUrl'];
		
		if (empty($code)) {
			return $this->result(1, 'code值获取失败', ['message' => 'code值获取失败']);
		}

		$appid = $_W['account']['key'];
		$secret = $_W['account']['secret'];
		$wxapi = new wxapi($appid, $secret);
		$info = $wxapi->code2Session($code);

		if ($info['code'] == 4001) {
			return $this->result(1, '获取用户信息失败', ['message' => $info['message']]);
		}

		$contion .= ' AND openid = :openid ';
		$params[':openid'] = $info['openid'];
		$uinfo = pdo_fetch('select * from ' . tablename($this->t_user) . ' where ' . $this->contion . $contion, $params);
		$is_band = $_GPC['is_band'];
         
		if ($is_band == 1) { 
			$has = pdo_get($this->t_user, ['weid' => $_W['uniacid'], 'openid' => $info['openid']], ['id', 'status', 'phone']);

			if (!$has['id']) {
				return $this->result(1, '请先用手机号登录', ['code' => 1003]);
			} else	
		     if ($has['status'] == 0) {
				return $this->result(1, '用户被禁用,请联系管理员', ['code' => 1003, 'uid' => $has['id']]);
			}

			if ($uinfo['status'] != 1) {
				return $this->result(1, '您已被禁用,请联系管理员处理~', $uinfo);
			}

			$data['last_login_time'] = time();
			pdo_update($this->t_user, $data, ['weid' => $_W['uniacid'], 'id' => $uinfo['id']]);
			$uid = $uinfo['id'];

			if (!empty($uinfo['phone'])) {
				$info_status = 1;
			}

			return $this->result(0, '登录成功', ['uid' => $uid, 'info_status' => $info_status]);
		}
		else {
			$uid = $_GPC['uid'];
			$encryptedData = $_GPC['encryptedData'];
			$iv = $_GPC['iv'];
			$pc = new WXBizDataCrypt($appid, $info['session_key']);
			// $errCode = $pc->decryptData($encryptedData, $iv, $datas);
			$errCode = 0;
			if ($errCode == 0) {
				// $a = json_decode($datas, true);
				$user_info['openId'] = $info['openid'];
				$a = $user_info;

				$data['openid'] = $a['openId'];
				$data['nickname'] = $a['nickName'];
				$data['nativeplace'] = $a['province'] . ',' . $a['city'];
				$data['headimg'] = $a['avatarUrl'];
				$data['unionid'] = $a['unionId'];
				$data['createtime'] = time(); 
			} 
		 

			$data['weid'] = $_W['uniacid'];
			$data['last_login_time'] = time();
             


			if (6800 < (time() - cache_load('tokentime'))) {
				cache_delete('access_token');
				cache_delete('tokentime');
				$access_token = $wxapi->getAccess_Token();
				cache_write('access_token', $access_token);
				cache_write('tokentime', time());
			}
			else {
				$access_token = cache_load('access_token');
			}

			$path = '../addons/goouc_fullexam/Qrcode/';

			if (!is_dir($path)) {
				mkdir($path, 511, true);
			}

			$filename = 'qrcode_' . $uid . '.jpg';

			try {
				$res = $wxapi->getUnlimited($access_token, $uid, $path, $filename);

				if ($res) {
					$da['qrcode'] = $path . $filename;
					pdo_update($this->t_user, $da, ['id' => $uid]);
				}
			}
			catch (Exception $e) {
				return $this->result(1, '生成二维码错误了', $e);
			}
            $is_binded = pdo_get($this->t_user, ['openid' => $a['openId']], ['id','phone']);  
             if($is_binded['phone'] !=null){
                  pdo_update($this->t_user, $data, ['openid'=>$a['openId']]);
                  return $this->result(0, '微信绑定成功',['uid' => $is_binded['id'],'uinfo'=>$user_info]);
             }else{
                $result= pdo_insert($this->t_user, $data, ['weid' => $_W['uniacid']]);
                 if($result){
			       $checkM = pdo_get($this->t_user, ['openid' => $a['openId']], ['id', 'status']);
			     }
			     return $this->result(0, '绑定成功,请继续点击手机绑定',['uid' => $checkM['id'],'opid'=>$_GPC['user_info']]);
             }

		}
	}

自此,修改完成,去小程序开发者工具内清除缓存数据,然后试一下获取授权,就可以看到弹窗获取授权信息了,然后去个人中心也可以看到用户头像和昵称了

本站除非注明了原创,否则都是本站悉心编辑的文章,欢迎转载,但请留下转载链接,谢谢合作。
笑鹰资源 » 刷题小程序–掌上题库小程序更新授权登录

提供最优质的资源集合,网站建设服务,以及服务器配置优化

联系我们 关于我们