欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

(2)网页授权获取用户基本信息

程序员文章站 2024-04-05 09:00:54
...
(二)网页授权获取用户基本信息

在公众号的配置过程中,许多开发者会在菜单中加入HTML5页面,有时在页面内需要访问页面的用户信息,此时就需要网页授权获取用户基本信息

PS:本博文所阐述的微信开发基于Yii2.0框架

1、设置授权回调域名:开发 ---> 接口权限

  找到“网页授权获取用户基本信息”,点击后面对应的“修改”,在弹框响应位置填写授权回调域名即可,此处的域名不需要加http:// (关于网页授权回调域名的说明详情可参考公众平台开发者文档)

(2)网页授权获取用户基本信息

(2)网页授权获取用户基本信息

2、获取授权

  关于OAuth2.0博主参考的是方倍工作室的博文http://www.cnblogs.com/txw1958/p/weixin71-oauth20.html(PS:方倍是一个微信开发大神,其中的微信开发内容还是比较详细的,推荐参考),其中详细剖析了微信官方文档的相关内容,也提供了获取授权的更详细思路和方案。

  实际上,获取用户信息的关键在于获取用户的openid。博主想要实现用户点击公众号菜单打开页面即可自动授权,从而针对该用户进行数据库操作,于是有下面两种方式: 

  (1)利用自定义菜单请求授权页面

    自定义菜单后面会单独写一篇博文,在这里先简述一下通过自定义菜单进行授权,该方法需要高级接口权限,且局限于关注公众号的用户直接从菜单进入页面。

 1 $menu = '{ 2   "button":[ 3     { 4       "type": "view", 5       "name": "商城", 6       "url": "https://open.weixin.qq.com/connect/oauth2/authorize?appid=xxx&redirect_uri=http://tx.heivr.com/index.php&response_type=code&scope=snsapi_base&state=1#wechat_redirect" 7     }, 8  9     {10       "name":"快递服务",11       "sub_button":[12         {13            "type":"click",14            "name":"发快递",15            "key":"express"16         },17         {18            "type":"click",19            "name":"快递查询",20            "key":"ww"21         }22       ]23     },24   ]25 }';

  需要授权的view直接在url处填写微信提供的授权请求地址,其中:

  • appid:填写微信公众平台基本配置中的AppID;
  • redirect_uri:填写授权完成后跳转的页面地址,即自己的html5页面;
  • state:跳转至回调页面所带参数;
  • response_type:网页授权的两种scope,微信官方文档中说明如下:
    1、以snsapi_base为scope发起的网页授权,是用来获取进入页面的用户的openid的,并且是静默授权并自动跳转到回调页的。用户感知的就是直接进入了回调页(往往是业务页面)2、以snsapi_userinfo为scope发起的网页授权,是用来获取用户的基本信息的。但这种授权需要用户手动同意,并且由于用户同意过,所以无须关注,就可在授权后获取该用户的基本信息。

    按照此方法点击“商城”即可接收到返回的openid,继而进行下一步用户信息的获取。 

      (2)利用JS自动请求授权页面

        这个方法相对而言比较笨拙,步骤略复杂,但目前能解决需求还没有研究简化方法,且由于页面的跳转多数情况下访问页面的时间会增加,但相比于前一个方法,该方法可以获取到非关注用户的基本信息。有些程序可能涉及到页面分享,程序没有强制关注但其他用户通过分享直接进入页面也需要记录用户信息,此时可以考虑该方法。(微信开发相关的代码博主封装成工具类调用,这里先贴用到的部分,以后整理完成会全部贴出来并附下载链接)

        该方法的思路为:js请求链接获取code ---> 利用code换取openid ---> 得到用户基本信息

    a. 编辑配置

    为了方便把用到的一些微信参数单独写入一个类,方便修改添加及调用

     1 php 2 namespace common\tools\wechat; 3  4 /** 5  * 微信请求相关配置类库 6  */ 7 class ConfigTool { 8  9     /**10      * 微信配置参数11      * @return array 配置参数12      */13     public function setConfig() {14 15         // 用于验证微信接口配置信息的Token,可以任意填写16         $config['token'] = '自己的token';17          18         // appID19         $config['appid'] = '自己的appid';20          21         // appSecret22         $config['secret'] = '自己的secret';23          24         // 回调链接地址25         $config['redirect_uri'] = 'http://tx.heivr.com/index.php?';26          27         // 是否以 HTTPS 安全协议访问接口28         $config['https_request'] = false;29          30         // 授权作用域,snsapi_base (不弹出授权页面,直接跳转,只能获取用户openid),31         // snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且,32         // 即使在未关注的情况下,只要用户授权,也能获取其信息)33         $config['scope'] = 'snsapi_userinfo';34          35         // 语言36         $config['lang'] = 'zh_CN'; // zh_CN 简体,zh_TW 繁体,en 英语37          38         // 微信公众账户授权地址39         $config['mp_authorize_url'] = 'https://api.weixin.qq.com/cgi-bin/token';40         // 微信公众账户js临时票据地址41         $config['jsapi_ticket_url'] = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket';42         // 授权地址43         $config['authorize_url'] = 'https://open.weixin.qq.com/connect/oauth2/authorize';44         // 获取access token 的地址45         $config['access_token_url'] = 'https://api.weixin.qq.com/sns/oauth2/access_token';46         // 刷新 token 的地址47         $config['refresh_token_url'] = 'https://api.weixin.qq.com/sns/oauth2/refresh_token';48         // 获取用户信息地址49         $config['userinfo_url'] = 'https://api.weixin.qq.com/sns/userinfo';50         // 验证access token51         $config['valid_token_url'] = 'https://api.weixin.qq.com/sns/auth';52         // 上传临时素材地址53         $config['media_temp_upload_url'] = 'https://api.weixin.qq.com/cgi-bin/media/upload?';54         // 上传永久素材地址55         $config['media_forever_upload_url'] = 'https://api.weixin.qq.com/cgi-bin/material/add_material?';56 57         return $config;58         59     }60 }

    b. https请求工具

     1 php 2 namespace common\tools; 3  4 /** 5  * https请求相关类库 6  */ 7 class HttpsTool { 8  9     const TIMEOUT = 5;                        // 设置超时时间10 11     private $ch;                                // curl对象12 13     /**14      * 发送curl请求,并获取请求结果15      * @param string 请求地址16      * @param array 如果是post请求则需要传入请求参数17      * @param string 请求方法,get 或者 post, 默认为get18      * @param bool 是否以https协议请求19      */20     public function send_request($requests, $params = null, $method = 'get', $https = true) {21         // 以get方式提交22         if ($method == 'get') {23             if($params){24                 $request = $requests . $this->create_url($params);25             }else{26                 $request = $requests;27             }28         }else{29             $request = $requests;30         }31 32         $this->ch = curl_init($request);33         curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);// 设置不显示结果,储存入变量34         curl_setopt($this->ch, CURLOPT_TIMEOUT, self::TIMEOUT); // 设置超时限制防止死循环35 36         // 判断是否以https方式访问37         if ($https) {38             curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查39             curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在40         }41 42         if ($method == 'post') {        // 以post方式提交43             //curl_setopt($this->ch, CURLOPT_SAFE_UPLOAD, false);     //php 5.6文件上传必加内容,5.4不需要44             curl_setopt($this->ch, CURLOPT_POST, 1); // 发送一个常规的Post请求45             curl_setopt($this->ch, CURLOPT_POSTFIELDS, $params); // Post提交的数据包46             curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);47         }48         49         $tmpInfo = curl_exec($this->ch); // 执行操作50         if (curl_errno($this->ch)) {51             echo 'Errno:'.curl_error($this->ch);//捕抓异常52         }53         curl_close($this->ch); // 关闭CURL会话54         //var_dump($tmpInfo);exit;55         return $tmpInfo; // 返回数据56     }57 58     /**59      * 生成url60      */61     public function create_url($data) {62         $temp = '?';63         foreach ($data as $key => $item) {64             $temp = $temp . $key . '=' . $item . '&';65         }66         return substr($temp, 0, -1);67     }68 }
    关于curl_setopt($this->ch, CURLOPT_SAFE_UPLOAD, false)会在微信图片资源上传博文中详细讲述它出现的心酸史,这里暂时用不到,不做解释

    c. 授权基类

     1 php   2 namespace common\tools\wechat; 3  4 use common\tools\wechat\ConfigTool; 5 use common\tools\HttpsTool; 6 /** 7 * Weixin_oauth 类库 8 */ 9 class OauthTool {10 11     public $conf;12 13     public function __construct(){14         $re = new ConfigTool;               15         $this->conf = $re->setConfig();16     } 17 18     /**19      * 生成用户授权的地址20      * @param string 自定义需要保持的信息21      * @param sting 请求的路由22      * @param bool 是否是通过公众平台方式认真23      */24     public function authorize_addr($route, $state='', $mp=false) {25 26         if ($mp) {27             $data = [28                 'appid' => $this->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',                                      //返回类型,此处填写code38                 '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 token52      * @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         // 生成授权url63         $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 token72      * @param string 用户的open id73      */74     public function userinfo($token, $openid) {75 76         $data = [77             'access_token' => $token,78             'openid' => $openid,79             'lang' => $this->conf['lang']80         ];81         // 生成授权url82         $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 用户code16      */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 token32         $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 actionIndex(){ 6         //判断页面是否自动刷新 7         if(isset($_GET['state'])) { 8             $refresh = 0; 9         }else{10             $refresh = 1;11         }12 13         //获取用户code14         $user = new UserinfoClass;15         $request = $user->getCode('r=store/index', 1);16 17         //该用户userid18         $userid = $user->info($request[1]);19 20         $model = new Product;21         $list = $model->find()->where(['status' => 1])->all();22 23         return $this->render('index',['list' => $list, 'refresh' => $refresh, 'userid' => $userid, 'request' => $request]);24     }

    程序要求用户打开产品列表即获取用户信息并存入数据库,其中设计了几个变量作用如下:

    $refresh:判断页面是否刷新,由于首次打开页面未进行oauth验证时才自动请求验证,避免反复刷新,这里用回调的state参数作为判断依据且设state=1(若有特定参数需要可将state赋值为所需值);

    $request:即为验证请求地址

    f. 视图自动刷新

    只需要在视图中添加以下js代码即可

     1 

    特此声明:相关文章均为查阅资料、阅读大神博文后结合实际开发情况遇到的问题整理而成,能找到原博的必会署名,找不到原博而引用的内容还望原博主海涵

    (2)网页授权获取用户基本信息

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

    相关文章

    相关视频


    网友评论

    文明上网理性发言,请遵守 新闻评论服务协议

    我要评论
  • (2)网页授权获取用户基本信息

专题推荐

作者信息
(2)网页授权获取用户基本信息

认证0级讲师

推荐视频教程
  • (2)网页授权获取用户基本信息javascript初级视频教程
  • (2)网页授权获取用户基本信息jquery 基础视频教程
  • 视频教程分类
    相关标签: nbsp gt this Token request

    上一篇: C#如何计算2个字符串相似度的示例代码分享

    下一篇: Photoshop辅助园林制图流程(二维的、三维的效果图)

    推荐阅读