设为首页 加入收藏

TOP

(二)网页授权获取用户基本信息(三)
2017-10-10 10:09:42 】 浏览:10087
Tags:网页 授权 获取 用户 基本 信息
t;conf['appid'], 29 'secret' => $this->conf['token'], 30 'grant_type' => 'client_credential' 31 ]; 32 $url = $this->conf['mp_authorize_url']; 33 } else { 34 $data = [ 35 'appid' => $this->conf['appid'], //公众号唯一标识 36 'redirect_uri' => urlencode($this->conf['redirect_uri'] . $route), //授权后重定向的回调链接地址 37 'response_type' => 'code', //返回类型,此处填写code 38 'scope'=>$this->conf['scope'], //应用授权作用域 39 'state'=>$state, //重定向后带上state参数,开发者可以填写任意参数 40 '#wechat_redirect'=>'' //直接在微信打开链接,可不填,做页面302重定向时必须带此参数 41 ]; 42 $url = $this->conf['authorize_url']; 43 } 44 45 $send = new HttpsTool; 46 //var_dump($url . $send->create_url($data));exit; 47 return $url . $send->create_url($data); 48 } 49 50 /** 51 * 获取 access token 52 * @param string 用于换取access token的code,微信提供 53 */ 54 public function access_token($code) { 55 56 $data = [ 57 'appid' => $this->conf['appid'], 58 'secret' => $this->conf['secret'], 59 'code' => $code, 60 'grant_type' => 'authorization_code' 61 ]; 62 // 生成授权url 63 $url = $this->conf['access_token_url']; 64 65 $send = new HttpsTool; 66 return $send->send_request($url, $data); 67 } 68 69 /** 70 * 获取用户信息 71 * @param string access token 72 * @param string 用户的open id 73 */ 74 public function userinfo($token, $openid) { 75 76 $data = [ 77 'access_token' => $token, 78 'openid' => $openid, 79 'lang' => $this->conf['lang'] 80 ]; 81 // 生成授权url 82 $url = $this->conf['userinfo_url']; 83 84 $send = new HttpsTool; 85 return $send->send_request($url, $data); 86 } 87 88 }

             d. 授权基类调用及用户数据处理(在控制器调用前,先对用户数据存入或更新)

 1 <?php
 2 namespace wechat\controllers\classes;
 3 
 4 use common\tools\wechat\OauthTool;
 5 use common\models\User;
 6 use common\tools\EmojiTool;
 7 
 8 /**
 9  * 微信用户基本信息获取
10  */
11 class UserinfoClass {
12  
13     /**
14      * 用户授权并获取code 
15      * @return string 用户code
16      */
17     public function getCode($route, $state){
18 
19         $re = new OauthTool;
20         $request = $re->authorize_addr($route, $state);
21         $code = isset($_GET['code']) ? $_GET['code'] : '';
22         
23         return [$request,$code];
24     }
25     
26     /**
27      * 获取用户信息并写入数据库(之后加参数传给code)
28      */
29     public function info($code) {
30         $re = new OauthTool;
31         //获取access token
32         $access = $re->access_token($code);
33         $token = json_decode($access,true);
34         //header("Content-type: text/html; charset=gbk"); 
35         //获取用户信息
36         if(count($token) != 2) {
37             $response = $re->userinfo($token['access_token'], $token['openid']);
38             $user = json_decode($response,true);
39             //用户昵称转换
40             //$user['nickname'] = EmojiTool::emoji_trans($user['nickname']);
41 
42             if($model = User::findOne(['openid' => $user['openid'] ])) {            //用户已存在更新数据
43                 $model->attributes = $user;
44                 $model->modify_time = time();
45                 $model->save(false);
46             }else{                                                                  //用户不存在写入
47                 $model = new User;
48                 $model->attributes = $user;
49                 $model->create_time = time();
50                 $model->save(false);
51             }
52         }
53         return isset($model->id) ? $model->id : '';
54     }
55     
56 }

             e. 控制器调用(这里只贴其中一个方法)

 1 /**
 2      * 产品列表
 3      * @return object 所有可用产品信息
 4      */
 5     public function actionInd
首页 上一页 1 2 3 4 下一页 尾页 3/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇linux 环境下安装mysql5.6 下一篇HTTP状态管理机制之Cookie

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目