thinkphp后台获取微信小程序用户信息
thinkphp后台获取微信小程序用户信息
微信小程序登录:
public function wx_login() { $obtain_token = new \app\api\controller\Token(); $code = input('code'); $encryptedData = input('encryptedData'); $iv = input('iv'); $rawData = input('rawData'); $signature = input('signature'); $appId = config_group('web_wx_gzh_config')['appId']; $appSecret = config_group('web_wx_gzh_config')['appSecret']; $url = "https://api.weixin.qq.com/sns/jscode2session?appid=$appId&secret=$appSecret&js_code=$code&grant_type=authorization_code"; $res = $this->sendRequest($url); $session_key = $res['session_key']; // 数字签名校验 if($signature != sha1($rawData.$session_key)){ return json(100,'数字签名校验失败!'); } $pc = new \app\api\controller\Wxbizdatacrypt($appId,$session_key); $errCode = $pc->decryptData($encryptedData,$iv,$wx_data); /**--------------测试开始---------*/ // $user_info['openId'] = 'oj2LN4jkh0MbTkFKUAoEReWGs7fw'; // $user_info['nickName'] = '时光乱了谁的心'; // $user_info['avatarUrl'] = 'https://wx.qlogo.cn/mmopen/vi_32/Bibqia5n05eDHeibESH9Yg3MqjxvEch9BUOEgcEMaFFjlnODTknrhZntC1UQSE2HCmWxZZqaoj9BTbO47UJSA8BNg/132'; // $errCode =0; /**--------------测试结束---------*/ if ($errCode == 0) { //解密成功 $user_info=json_decode($wx_data,true); $refer_id = input('refer_id'); //验证用户是否存在 $user = Db::name('user')->where('openid', $user_info['openId'])->find(); if (!$user) { $data = [ 'openid' => $user_info['openId'] , 'user_name' => $user_info['nickName'] , 'headimgurl' => $user_info['avatarUrl'] , 'token' => $obtain_token->settoken() , 'refer_id' => $refer_id // , 'unionid' => $res['unionid'] , 'time_out' => time() + 604800 , 'create_time' => time() , 'update_time' => time(), ]; try { Db::name('user')->insert($data); //获取用户信息 $result = Db::name('user')->where('openid', $user_info['openId'])->field('password',true)->find(); /**-------------- 用户登录记录 --------------*/ $user_log = [ 'uid' => $result['id'] ,'create_time' => time() ]; Db::name('user_log')->insert($user_log); return json(200,'注册成功!',$result); } catch (Exception $e) { return json(100,'注册失败,请刷新重试!'); } }else{ $data = [ 'user_name' => $user_info['nickName'] , 'headimgurl' => $user_info['avatarUrl'] , 'token' => $obtain_token->settoken() , 'time_out' => time() + 604800 , 'update_time' => time(), ]; try { Db::name('user')->where('id',$user['id'])->update($data); //获取用户信息 $result = Db::name('user')->where('openid', $user_info['openId'])->field('password',true)->find(); $result['grade_type'] = Db::name('grade')->where('id',$result['grade_id'])->value('name'); $result['vip_end_time'] = date('Y-m-d H:i:s',$result['vip_end_time']); $result['off'] = Db::name('agent')->where('uid',$result['id'])->count(); /**-------------- 用户登录记录 --------------*/ $user_log = [ 'uid' => $result['id'] ,'create_time' => time() ]; Db::name('user_log')->insert($user_log); return json(200,'登录成功!',$result); } catch (Exception $e) { return json(100,'登录失败,请刷新重试!'); } } } else { return json(100,'解密失败!'); } }
APP端登录:
/** * 用户app端登录 */ public function app_login() { $obtain_token = new \app\api\controller\Token(); $openId = input('openid'); $username = input('username'); $avatarUrl = input('avatarUrl'); $unionid = input('unionid'); $refer_id = input('refer_id'); //验证用户是否存在 $user = Db::name('user')->where('openid', $openId)->find(); if (!$user) { $data = [ 'openid' => $openId , 'user_name' => $username , 'headimgurl' => $avatarUrl , 'token' => $obtain_token->settoken() , 'refer_id' => $refer_id , 'unionid' => $unionid , 'time_out' => time() + 604800 , 'create_time' => time() , 'update_time' => time() ]; try { Db::name('user')->insert($data); //获取用户信息 $result = Db::name('user')->where('openid', $openId)->field('password',true)->find(); /**-------------- 用户登录记录 --------------*/ $user_log = [ 'uid' => $result['id'] ,'create_time' => time() ]; Db::name('user_log')->insert($user_log); return json(200,'注册成功!',$result); } catch (Exception $e) { return json(100,'注册失败,请刷新重试!'); } }else{ $data = [ 'user_name' => $username , 'headimgurl' => $avatarUrl , 'token' => $obtain_token->settoken() , 'time_out' => time() + 604800 , 'update_time' => time(), ]; try { Db::name('user')->where('id',$user['id'])->update($data); //获取用户信息 $result = Db::name('user')->where('openid', $openId)->field('password',true)->find(); $result['grade_type'] = Db::name('grade')->where('id',$result['grade_id'])->value('name'); $result['vip_end_time'] = date('Y-m-d H:i:s',$result['vip_end_time']); $result['off'] = Db::name('agent')->where('uid',$result['id'])->count(); /**-------------- 用户登录记录 --------------*/ $user_log = [ 'uid' => $result['id'] ,'create_time' => time() ]; Db::name('user_log')->insert($user_log); return json(200,'登录成功!',$result); } catch (Exception $e) { return json(100,'登录失败,请刷新重试!'); } } }
发送请求
public function sendRequest($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); return json_decode($output, true); }