wupengfei 3 年之前
父節點
當前提交
edf5470aed
共有 2 個文件被更改,包括 58 次插入22 次删除
  1. 3 4
      application/api/controller/Qc.php
  2. 55 18
      application/api/controller/WeChart.php

+ 3 - 4
application/api/controller/Qc.php

@@ -1,13 +1,12 @@
 <?php
 
 namespace app\api\controller;
+use app\api\controller\WeChart;
 class Qc{
 
     function index(){
-        $app_id ='wx7a5f15ddeda9ef5a';
-        $app_secret ='a4c33b42917949652c31447e42132de0';
-        $url=  "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$app_id}&secret={$app_secret}";
-        var_dump(http_curl($url));
+        $we = new WeChart();
+        var_dump($we->getAccessToken());
     }
 
 

+ 55 - 18
application/api/controller/WeChart.php

@@ -1,46 +1,83 @@
 <?php
 namespace app\api\controller;
 use EasyWeChat\Factory;
+use  think\cache\driver\Redis;
+use  think\Session;
 class WeChart {
 
-    protected $app_id ='';
-    protected $app_secret = '';
+    protected $app_id ='wx7a5f15ddeda9ef5a';
+    protected $app_secret = 'a4c33b42917949652c31447e42132de0';
 
 
     /**
-     * 是否关注公众号
-     * @param $code
-     * @return int|mixed
-     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
-     */
-    public function checkAttention($code)
-    {
-        $app = Factory::miniProgram(config('app.sy_wechat'));
-        $data = $app->auth->session($code);
-        return isset($data['subscribe']) ? $data['subscribe'] : 0;
-    }
-
-    /**
      * 获取token
      * @return mixed|string
      */
     public function  getAccessToken()
     {
-        $url=  "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->app_id}&secret={$this->app_secret}";
-        return http_curl($url);
+        $session = new Session();
+        $over_time = $session->get('token_over_time');
+        if($over_time < time()) {
+            $url=  "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->app_id}&secret={$this->app_secret}";
+            $ret = http_curl($url);
+            $session->set('access_token',$ret['access_token']);
+            $session->set('token_over_time',time() +$ret['expires_in'] );
+        }
+        return  $session->get('access_token');
     }
 
 
     // 获取详情
-    function getUserInfo($access_token,$openid)
+    function getUserInfo()
     {
+        $redis = new Redis();
         $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={$access_token}&openid={$openid}&lang=zh_CN";
         $data = http_curl($url);
         return isset($data['subscribe']) ? $data['subscribe'] : 0;
     }
 
 
+    public function send_message($order_sn=''){
 
+        $url='https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token='.$access_token;
+        $order = Db::name('StoreOrder')->field('id,order_sn,create_at,num,take_time,user_id,goods_id')->where('order_sn',$order_sn)->find();
+        $member = Db::name('storeMember')->field('id,vip_level,openid,username,nickname')->where('id',$order['user_id'])->find();
+        $book_book = Db::name('book_book')->field('id,title')->where('id',$order['goods_id'])->find();
+        $data=[
+            'touser'=>$member['openid'],
+            'mp_template_msg'=>[
+                "appid"=>"wx3346d52f010b611d",
+                "template_id"=>"SH6KcRTVDiU0-7cSDujHsCg8MhbgzGpHb8US1QiPYW0",
+                "url"=>"http://www.baidu.com",
+                'miniprogram'=>[
+                    "appid"=>"wx2b03c73ff0547cc3",
+                ],
+                'data'=>[
+                    'first'=>[
+                        "value"=>'恭喜你已借阅到该图书',
+                        "color"=>"#173177"
+                    ],
+                    'keyword1'=>[
+                        "value"=>$book_book['title'],
+                        "color"=>"#173177"
+                    ],
+                    'keyword2'=>[
+                        "value"=>$member['username']?$member['username']:$member['nickname'],
+                        "color"=>"#173177"
+                    ],
+                    'keyword3'=>[
+                        "value"=>date('Y-m-d H:i:s',time()),
+                        "color"=>"#173177"
+                    ],
+                    'remark'=>[
+                        "value"=>'谢谢你对哩哈教育的支持!',
+                        "color"=>"#173177"
+                    ]
+                ]
+            ],
+        ];
+        $res = requestPost($url,json_encode($data));
+    }