From 007b8ca2bfb18cf61986dc9c77c1c4e40b955571 Mon Sep 17 00:00:00 2001 From: lihan <3398341942@qq.com> Date: Wed, 10 Oct 2018 14:19:16 +0800 Subject: [PATCH] 微信授权测试 --- app/index/controller/IndexController.php | 10 ++++++---- simplewind/extend/WeChatCommon.php | 11 +++++------ 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/app/index/controller/IndexController.php b/app/index/controller/IndexController.php index f19c3d3..71ee8a9 100644 --- a/app/index/controller/IndexController.php +++ b/app/index/controller/IndexController.php @@ -18,15 +18,17 @@ class IndexController extends HomeBaseController $wx->code(); } else { $code = request()->param('code'); - $userInfo = $wx->getInfo($code); + $userInfo = $wx->getOpenid($code); if(Db::name('user')->where(['openid'=>$userInfo['openid']])->count() == 0) { //注册新用户 + //拉去用户信息 + $return = $wx->getUserInfo($userInfo['openid']); $data = [ 'user_type' => 2, 'create_time' => time(), - 'user_nickname' => $userInfo['nickname'], - 'sex' => $userInfo['sex'], - 'avatar' => $userInfo['headimgurl'] + 'user_nickname' => $return['nickname'], + 'sex' => $return['sex'], + 'avatar' => $return['headimgurl'] ]; if(Db::name('user')->insert($data)) { $userId = Db::name('user')->getLastInsID(); diff --git a/simplewind/extend/WeChatCommon.php b/simplewind/extend/WeChatCommon.php index f015e4d..b64fd0d 100644 --- a/simplewind/extend/WeChatCommon.php +++ b/simplewind/extend/WeChatCommon.php @@ -30,7 +30,7 @@ class WeChatCommon { * @param $code * @return mixed */ - public function getInfo($code){ + public function getOpenid($code){ $get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.config('AppID').'&secret='.config('AppSecret').'&code='.$code .'&grant_type=authorization_code'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $get_token_url); @@ -40,8 +40,8 @@ class WeChatCommon { $res = curl_exec($ch); curl_close($ch); $json_obj = json_decode($res, true); - return $json_obj; - + $openid = $json_obj['openid']; + return $openid; } /** @@ -79,7 +79,7 @@ class WeChatCommon { * @return array */ public function getUserInfo($openid){ - $url='https://api.weixin.qq.com/cgi-bin/user/info?access_token='.$this->getAccessToken().'&openid='.$openid; + $url='https://api.weixin.qq.com/sns/userinfo?access_token='.$this->getAccessToken().'&openid='.$openid.'&lang=zh_CN'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); @@ -88,8 +88,7 @@ class WeChatCommon { $output = curl_exec($ch); curl_close($ch); $jsoninfo = json_decode($output, true); - $data=array($jsoninfo['nickname'], $jsoninfo['headimgurl']); - return $data; + return $jsoninfo; } /** -- libgit2 0.24.0