qifengquan 1 年之前
父節點
當前提交
4917825829
共有 3 個文件被更改,包括 84 次插入0 次删除
  1. 66 0
      application/api/controller/Common.php
  2. 1 0
      application/api/controller/Index.php
  3. 17 0
      application/common/model/Apply.php

+ 66 - 0
application/api/controller/Common.php

@@ -361,5 +361,71 @@ class Common extends Api
         curl_close($curl);
         return $res;
     }
+    
+    /**
+     * 发送微信模板消息
+     * @param $order_id
+     * @param $template
+     * @return false|string|\think\response\Json
+     */
+    public function sendWechatMessage($openid,$template_id,$data,$url)
+    {
+        $data = array(
+            "touser"        =>  $openid,        //openid
+            "template_id"   =>  $template_id,             //模板id
+            "url"           =>  $url,
+            "data"          =>  $data      //模板数据
+        );
+        //下单用户发送
+        $access_token = $this->access_token();
+        if ($access_token == 400){
+            return ['code'=>0,'message'=>'获取access_token失败'];
+        }
+        $urls = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;
+        $result = $this->http_post_json($urls,json_encode($data));//发送请求
+        if($result[0] == 200){
+            return ['code'=>1,'data'=>'推送成功'];
+        }
+        return ['code'=>0,'message'=>$result[1]['errmsg']];
+    }
+
+
+    public function http_post_json($url, $jsonStr)
+    {
+        $ch = curl_init();
+        curl_setopt($ch, CURLOPT_POST, 1);
+        curl_setopt($ch, CURLOPT_URL, $url);
+        curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
+                'Content-Type: application/json; charset=utf-8',
+                'Content-Length: ' . strlen($jsonStr)
+            )
+        );
+        $response = curl_exec($ch);
+        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
+        curl_close($ch);
+        return [$httpCode,$response];
+        // return json_encode(array($httpCode, $response),true);
+        //"[200,"{\"errcode\":0,\"errmsg\":\"ok\",\"msgid\":2696398438942572550}"]"
+    }
+
+    //获取access_token
+    public function access_token(){
+        $appId = 'wx5e111b790c719cae';
+        $appSecret = 'a4e2a6d2a4fd06bd9e4ba279fd95e2d8';
+        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appId."&secret=".$appSecret;
+        $ch = curl_init();//初始化curl
+        curl_setopt($ch, CURLOPT_URL,$url); //要访问的地址
+        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
+        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//跳过证书验证
+        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
+        $data = json_decode(curl_exec($ch),true);
+        if(curl_errno($ch)){
+            var_dump(curl_error($ch)); //打印错误信息
+        }
+        curl_close($ch);//关闭curl
+        return $data['access_token'];
+    }
 
 }

+ 1 - 0
application/api/controller/Index.php

@@ -198,6 +198,7 @@ class Index extends Api
                 ];
                 Apply::insert($apply);
                 user::money(-$cost,$uid,'申请认识支出');
+                Apply::wxMessage(input('nid'),Date('Y-m-d H:i:s'));
                 Db::commit();
                 $this->success('解锁成功');
             }catch (Exception $e){

+ 17 - 0
application/common/model/Apply.php

@@ -2,6 +2,7 @@
 
 namespace app\common\model;
 
+use app\api\controller\Common;
 use think\Model;
 
 
@@ -57,6 +58,22 @@ class Apply extends Model
             Apply::update(['u_is_read'=>1],['id'=>$v['id']]);
         }
     }
+
+    /**
+     * 微信服务号通知
+     */
+    public function wxMessage($id,$time){
+        $user_name = User::get(['id'=>$id]);
+        $data = [
+            'keyword1'=>[
+                'value'=>$user_name['username']
+            ],
+            'keyword2'=>[
+                'value'=>$time
+            ],
+        ];
+        (new \app\api\controller\Common())->sendWechatMessage($user_name['open_id'],'SDrPJvTOgm03PnQHibVlguD3lra3nK9pknojcMjSXq8',$data,"http://xiangduilun.hdlkeji.com/h5/#/pages/xiaoxi/shenqing-me");
+    }