thinkphp实现微信登录授权、菜单、信息接收与处理

thinkphp 用第三方类库 wechat.class.php 实现微信菜单、微信信息接收与返回、微信网页登录静默授权和用户手动授权。主要是功能实现,帮助一些入门选手实现基本功能。
thinkphp 用第三方类库 wechat.class.php 实现微信菜单、微信信息接收与返回、微信网页登录静默授权和用户手动授权。主要是功能实现,帮助一些入门选手实现基本功能。
  1.     protected $AppID         = '';     // AppID(应用ID)    
  2.     protected $AppSecret     = '';     // AppSecret
  3.     protected $Token         = '';     // 微信后台填写的TOKEN
  4.     protected $Crypt         = '';   // 消息加密KEY(EncodingAESKey)
  5.     /*
  6.      微信第三方开发者模式 使用方法
  7.      服务器地址    : http://你的域名/index.php/Wx/wechat.html
  8.      Token        : 自己定义
  9.      AppID        : 微信公众账户后台的AppID
  10.      AppSecret    : 微信公众账户后台的AppSecret
  11.      Crypt        : 微信公众账户后台的EncodingAESKey
  12.      */
  13.     public function wechat(){
  14.         // 第三方发送消息给公众平台
  15.         $options = array(
  16.                 'token'                => $this->Token,         // 填写你设定的key
  17.                 'encodingaeskey'    => $this->Crypt,
  18.                 'appid'                => $this->AppID,
  19.                 'appsecret'            => $this->AppSecret,
  20.                 'debug'                => false,
  21.                 'logcallback'        => false,
  22.             );
  23.         import("@.Library.wechat");
  24.         $weObj = new \Wechat($options);
  25.         $weObj->valid();
  26.         $type = $weObj->getRev()->getRevType();
  27.         //设置菜单
  28.         $newmenu =  array(
  29.             "button"=>
  30.                 array(
  31.                     array('type'=>'view','name'=>'客户案例','url'=>'http://www.vmonsion.com'),
  32.                     array('name'=>'我的','sub_button'=>array(
  33.                             array('type'=>'view','name'=>'我的网站','url'=>'http://www.vmonsion.com'),
  34.                             array('type'=>'view','name'=>'个人资料','url'=>'http://www.vmonsion.com'),
  35.                         )
  36.                     ),
  37.                     array('type'=>'view','name'=>'关于我们','url'=>'http://www.vmonsion.com/index.php'),
  38.                     )
  39.             );
  40.         // 公众号菜单更新
  41.         //$weObj->getMenu($newmenu);
  42.         $weObj->createMenu($newmenu);
  43.         //$weObj->deleteMenu($newmenu);
  44.         //分解数据获得常用字段
  45.         //$this->openid     = $weObj->getRev()->getRevFrom();
  46.         $this->type     = $weObj->getRev()->getRevType();
  47.         $this->data     = $weObj->getRev()->getRevData();
  48.         $content = '[捂脸]'.$weObj->getRev()->getRevContent();
  49.         switch($type) {
  50.             // 文本消息
  51.             case \Wechat::MSGTYPE_TEXT:
  52.                     file_put_contents('test.txt','TextMessage',FILE_APPEND);
  53.                     $weObj->text($content)->reply();
  54.                     exit;
  55.                     break;
  56.             // 图片消息
  57.             case \Wechat::MSGTYPE_IMAGE:
  58.                     $weObj->text('This is a Picture?')->reply();
  59.                     break;
  60.             // 位置消息
  61.             case \Wechat::MSGTYPE_LOCATION:
  62.                     $weObj->text('what?GPS?')->reply();
  63.                     exit;
  64.                     break;
  65.             // 连接消息
  66.             case \Wechat::MSGTYPE_LINK:
  67.                     break;
  68.             // 音乐消息
  69.             case \Wechat::MSGTYPE_MUSIC:
  70.                     break;
  71.             // 图文消息(推送过来的应该不存在这种类型,但是可以给用户回复该类型消息)
  72.             case \Wechat::MSGTYPE_NEWS:
  73.                     break;
  74.             // 音频消息
  75.             case \Wechat::MSGTYPE_VOICE:
  76.                     break;
  77.             // 视频消息
  78.             case \Wechat::MSGTYPE_VIDEO:
  79.                     break;
  80.             // 短视频
  81.             case \Wechat::MSGTYPE_SHORTVIDEO:
  82.                     break;
  83.             //事件消息:五种
  84.             case \Wechat::MSGTYPE_EVENT:
  85.                     $event = $weObj->getRev()->getRevEvent();
  86.                     $reply = $this->messageEvent($event);
  87.                     break;
  88.             default:
  89.                     $weObj->text("你好,我是微信小助手,很高兴为您服务~")->reply();
  90.         }
  91.     }
  92.     // Wechat事件处理
  93.     public function messageEvent($event){
  94.         switch($event) {    
  95.             // 订阅        
  96.             case \Wechat::EVENT_SUBSCRIBE:
  97.                 break; 
  98.             // 取消订阅    
  99.             case \Wechat::EVENT_UNSUBSCRIBE:
  100.                 break;
  101.             // 扫描带参数二维码        
  102.             case \Wechat::EVENT_SCAN:
  103.                 break;  
  104.             // 上报地理位置         
  105.             case \Wechat::EVENT_LOCATION:
  106.                 break;
  107.             // 菜单 - 点击菜单跳转链接     
  108.             case \Wechat::EVENT_MENU_VIEW:
  109.                 break;
  110.             // 菜单 - 点击菜单拉取消息
  111.             case \Wechat::EVENT_MENU_CLICK:
  112.                 break;
  113.             // 菜单 - 扫码推事件(客户端跳URL)    
  114.             case \Wechat::EVENT_MENU_SCAN_PUSH:
  115.                 break;
  116.             // 菜单 - 扫码推事件(客户端不跳URL)
  117.             case \Wechat::EVENT_MENU_SCAN_WAITMSG:
  118.                 break;
  119.             // 菜单 - 弹出系统拍照发图
  120.             case \Wechat::EVENT_MENU_PIC_SYS:
  121.                 break;
  122.             // 菜单 - 弹出拍照或者相册发图
  123.             case \Wechat::EVENT_MENU_PIC_PHOTO:
  124.                 break;
  125.             // 菜单 - 弹出微信相册发图器
  126.             case \Wechat::EVENT_MENU_PIC_WEIXIN:
  127.                 break;
  128.             // 菜单 - 弹出地理位置选择器
  129.             case \Wechat::EVENT_MENU_LOCATION:
  130.                 break;
  131.             default:
  132.                     break;
  133.         }
  134.     }
复制代码
  1. /* 微信登录验证,可以选择方法一(静默授权)或者方法二(用户手动授权)
  2.        1、设置微信公众号的菜单,访问 http://你的域名/index.php/Wx/checkLogin.html 
  3.        2、返回redirect_uri的授权域名,获取$_GET['code']
  4.        3、通过code换取网页授权access_token和openID
  5.        4、function loginWx() 就是最终获取数据的方法
  6.      */     
  7.     public function checkLogin(){
  8.         // 方法一 Scope为snsapi_base基本授权
  9.         $result = $this->_baseAuth("http://www.kodjr.com/index.php/Wx/loginWx.html");
  10.         
  11.         // 方法二 Scope为snsapi_userinfo用户授权
  12.         //$result = $this->_userInfoAuth("http://www.kodjr.com/index.php/Wap/loginWx.html");
  13.     }
  14.     
  15.     // 验证是否邦定帐号了
  16.     public function loginWx(){
  17.         $code = $_GET['code'];
  18.         // 方法一
  19.         $tokenOpenID = $this->_baseToken($code);
  20.         ///dump($tokenOpenID);
  21.         // 设置session
  22.         if(!session('user_wechat_auth')){
  23.             session('user_wechat_auth', $tokenOpenID);
  24.         }else{
  25.                 
  26.         }    
  27.         // 方法二
  28.         //$userData = $this->_userInfoToken($code);
  29.         //$userInfo = $this->_getUserInfo($userData['access_token'],$userData['openid']);            
  30.     }
  31.     
  32.     
  33.     // 方法一:Scope为snsapi_base基本授权
  34.     public function _baseAuth($redirect_url){  
  35.         // 1.准备scope为snsapi_base网页授权页面  
  36.         $baseurl = urlencode($redirect_url);  
  37.         $snsapi_base_url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$this->AppID.'&redirect_uri='.$baseurl.'&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect';  
  38.         // 2.静默授权,获取code 
  39.         $code = $_GET['code'];
  40.         if( !isset($code) ){  
  41.             header('Location:'.$snsapi_base_url);  
  42.         }
  43.     }
  44.     public function _baseToken($code){
  45.         // 3.通过code换取网页授权access_token和openID  
  46.         $curl = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$this->AppID.'&secret='.$this->AppSecret.'&code='.$code.'&grant_type=authorization_code';  
  47.         $content = $this->http_curl($curl);  
  48.         $result = json_decode($content[1],true);  
  49.         $result['state'] = $content[0];
  50.         return $result;      
  51.     }
  52.     
  53.     // 方法二:Scope为snsapi_userinfo用户授权
  54.     public function _userInfoAuth($redirect_url){  
  55.         //1.准备scope为snsapi_userInfo网页授权页面  
  56.         $redirecturl = urlencode($redirect_url);  
  57.         $snsapi_userInfo_url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$this->AppID.'&redirect_uri='.$redirecturl.'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';  
  58.         //2.用户手动同意授权,同意之后,获取code  
  59.         $code = $_GET['code'];  
  60.         if( !isset($code) ){  
  61.             header('Location:'.$snsapi_userInfo_url);  
  62.         }  
  63.     }
  64.     public function _userInfoToken($code){
  65.         //3.通过code换取网页授权access_token  
  66.         $curl = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$this->AppID.'&secret='.$this->AppSecret.'&code='.$code.'&grant_type=authorization_code';  
  67.         $content             = $this->http_curl($curl);  
  68.         $result = json_decode($content[1],true);  
  69.         $result['state'] = $content[0];
  70.         //4.通过access_token和openid拉取用户信息  
  71.         $webAccess_token     = $result->access_token;  
  72.         $openid             = $result->openid;  
  73.         $userInfourl         = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$webAccess_token.'&openid='.$openid.'&lang=zh_CN ';  
  74.         $recontent             = $this->http_curl($userInfourl); 
  75.         $userInfo             = json_decode($recontent,true);  
  76.         return $userInfo;  
  77.     }  
  78.     public function http_curl($url, $method='POST', $postfields = null, $headers = array(), $debug = false)
  79.     {
  80.         $ci = curl_init();
  81.         /* Curl settings */
  82.         curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  83.         curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);
  84.         curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE);
  85.         curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, FALSE);
  86.         curl_setopt($ci, CURLOPT_TIMEOUT, 30);
  87.         curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
  88.         switch ($method) {
  89.             case 'POST':
  90.                 curl_setopt($ci, CURLOPT_POST, true);
  91.                 if (!empty($postfields)) {
  92.                     curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
  93.                     $this->postdata = $postfields;
  94.                 }
  95.                 break;
  96.         }
  97.         curl_setopt($ci, CURLOPT_URL, $url);
  98.         curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
  99.         curl_setopt($ci, CURLINFO_HEADER_OUT, true);
  100.         $response = curl_exec($ci);
  101.         $http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
  102.         if ($debug) {
  103.             echo "=====post data======\r\n";
  104.             var_dump($postfields);
  105.             echo '=====info=====' . "\r\n";
  106.             print_r(curl_getinfo($ci));
  107.             echo '=====$response=====' . "\r\n";
  108.             print_r($response);
  109.         }
  110.         curl_close($ci);
  111.         return array($http_code, $response);
  112.     }
复制代码

附件 Application微信实现登录和信息处理.zip ( 36.1 KB 下载:82 次 )

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值