zoukankan      html  css  js  c++  java
  • Tp5 微信公众号 获取用户信息 EasyWeChat使用

    安装用composer 安装 EasyWeChat,这个就不说了!

    安装后直接use使用;

    以下代码是Fastadmin 框架里面写的   如果没用框架 就不用继承第5行

    直接访问

    test_send_template这个方法的地址即可实现授权微信登录然后发送模版消息(模版id等参数自行修改)

    代码如下

      1 <?php
      2 
      3 namespace appindexcontroller;
      4 
      5 use appcommoncontrollerFrontend;
      6 use EasyWeChatFactory;
      7 use thinkConfig;
      8 use thinkDb;
      9 use thinkSession;
     10 
     11 class Index extends Frontend
     12 {
     13 
     14     protected $noNeedLogin = '*';
     15     protected $noNeedRight = '*';
     16     protected $layout = '';
     17 
     18     public function index()
     19     {
     20         return $this->view->fetch();
     21     }
     22 
     23     /**
     24      * 获取微信公众号
     25      *
     26      * @return void
     27      */
     28     public function get_wx_gzh_app()
     29     {
     30          $config = [
     31              'app_id' => Config('site.wx_app_id'),//微信公众号的appid
     32              'secret' => Config('site.wx_secret'),//微信公众号的密钥
     33          
     34              // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
     35              'response_type' => 'array',
     36          
     37              //...
     38          ];
     39          
     40          //微信公众号的方法
     41          $app = Factory::officialAccount($config);
     42 
     43          return $app;
     44     }
     45 
     46     /**
     47      * 获取用户信息,没有授权的自动授权,类似登录
     48      * 
     49      * @return void
     50      */
     51     public function get_user()
     52     {
     53         
     54    
     55         $app = $this->get_wx_gzh_app();
     56         $oauth = $app->oauth;
     57 
     58         // 未登录或没openid数据的
     59         if (empty(Session::get('wechat_user'))) {
     60 
     61             
     62             $url = $this->request->domain().'/index/index/code';//改成当前类的 访问路径,可以写成封装方法,怕一些用户看不懂
     63             $oauth->scopes(['snsapi_userinfo'])->redirect($url);//获取用户信息跳转
     64             //非Tp框架的可能要return
     65 
     66             // 这里不一定是return,如果你的框架action不是返回内容的话你就得使用
     67             $oauth->redirect()->send();
     68         }
     69             
     70         $user = Session::get('wechat_user');
     71         
     72 
     73         
     74         // if(empty($user)){
     75         //     $url = $this->request->domain().'/index/index/code';
     76           
     77         //     $oauth->scopes(['snsapi_userinfo'])->redirect($url);
     78         //     $oauth->redirect()->send();
     79         //     // $oauth->redirect()->send();
     80         // }
     81         // 已经登录过 QQ496631085
     82         // halt($user);
     83         return $user;
     84     }
     85 
     86 
     87 
     88     
     89     /* 
     90     * @Description: 公众号测试登录可以直接用获取用户信息来(get_user方法已实现此方法)
     91     * @return: 
     92     */     
     93     public function login()
     94     {
     95         $url = $this->request->domain().'/index/index/code';
     96         $app = $this->get_wx_gzh_app();
     97         $app->oauth->scopes(['snsapi_userinfo'])->redirect($url);
     98 
     99         //这里不一定是return,如果你的框架action不是返回内容的话你就得使用
    100         $oauth->redirect()->send();
    101 
    102     }
    103 
    104     /* 
    105     * @Description: 公众号获取code
    106     * @return: 
    107     */   
    108     public function code()
    109     {
    110         $app = $this->get_wx_gzh_app();
    111         $oauth = $app->oauth;
    112         $user = $oauth->user();
    113         // halt($user);//可以自己打印看下
    114         Session::set('wechat_user',$user->original);//获取到用户信息 自己可以存储
    115         // var_dump($user->original);
    116     }
    117 
    118 
    119     /**
    120      * 测试发送模版消息
    121      *
    122      * @param string $com_openid 微信公众号的openid
    123      * @param string $keyword1 参数1
    124      * @param [type] $keyword2 参数2
    125      * @param string $path 跳转小程序路径
    126      * @return void
    127      */
    128     public function test_send_template($com_openid='ooB8g6Hu3fa5P0oeMSpURFPVZRS4',$keyword1="测试订单消息",$keyword2=null,$path='/pages/u_xjdS/u_xjdS')
    129     {
    130 
    131         $user = $this->get_user();
    132 
    133         $com_openid = $user['openid'];
    134         if(!$keyword2){
    135             $keyword2 = date('Y-m-d H:i:d');
    136         }
    137         $app = $this->get_wx_gzh_app();    
    138         $data = $app->template_message->send([
    139             'touser' => $com_openid,//微信openid,
    140             'template_id' => '_es-MdHZUO-voldjdevPADHj28JQ_t9bPzxowh_M8LM',
    141             'url' => 'https://he4966.cn/',
    142             'miniprogram' => [
    143                     'appid' => Config('site.app_id'),
    144                     'pagepath' => $path,
    145             ],
    146             'data' => [
    147                 'first' =>['您好,测试发送订单信息,成功!','#173177'],
    148                 'keyword1' =>[$keyword1,'#223177'],//订单编号
    149                 'keyword2' =>[$keyword2,'#333177'],//订单时间
    150                 // 'keyword3' =>[$service,'#173177'],
    151                 'remark' =>['请忽略本条测试信息','#173199'],
    152 
    153             ],
    154         ]);
    155         if($data['errmsg']=='ok'){
    156             $this->success('发送成功!,可正常收到微信模版消息','/');
    157         }else{
    158             $this->error('发送失败'.$data['errmsg']);
    159         }
    160         // return $data;
    161     }
    162 
    163 }
  • 相关阅读:
    js
    DOM
    css
    html
    java虚拟机
    java并发
    java容器
    java基础
    计算机网络面试题
    计算机操作系统-设备管理
  • 原文地址:https://www.cnblogs.com/xiaohe520/p/14544250.html
Copyright © 2011-2022 走看看