wugg il y a 7 mois
Parent
commit
e57b8536dd
1 fichiers modifiés avec 51 ajouts et 24 suppressions
  1. 51 24
      app/bug/listener/WxPush.php

+ 51 - 24
app/bug/listener/WxPush.php

@@ -12,34 +12,63 @@ class WxPush
     protected $appsecret;
     protected $wechat;
     protected $uid=[];
+    protected $bugMsg='';
+
+    public function __construct()
+    {
+        $this->appid = env('WECHAT.APPID');
+        $this->appsecret = env('WECHAT.APPSECRET');
+        $this->wechat = new \Wechat(['appid' => $this->appid, 'appsecret' => $this->appsecret]);
+    }
     /**
      * 事件监听处理
      *
      * @return mixed
      */
-    public function handle($event)
+    public function handle(array $event): bool
     {
-        if(empty($event)) return false;
-        $data=$event['data'];
-        $this->uid=Talk::$talkUid;
-        if($event['type']=='bug'&& in_array($data['status'],[0,1,4,5])){
-            $this->uid=$data['status']==0?$this->uid:$data['apply_id'];
-            $this->check($data,$data['status']);
+        if (empty($event)) {
+            return false;
         }
-        if($event['type']=="talk"){
-            if(!isset($data['bugNo'])|| $data['bugNo']=='') return false;
-            $note=Note::where(['bugNo'=>$data['bugNo']])->findOrEmpty()->toArray();
-            if(empty($note)) return false;
-            if(in_array($data['talk_id'],$this->uid))$this->uid=$note['apply_id'];
-            $this->check($note);
+        $data = $event['data'];
+        $this->uid = Talk::$talkUid;
+
+        try {
+            switch ($event['type']) {
+                case 'bug':
+                    if (in_array($data['status'], [0, 1, 4, 5])) {
+                        $this->uid = $data['status'] == 0 ? $this->uid : $data['apply_id'];
+                        $this->bugMsg = Note::$statusCn[$data['status']]??"未知";
+                        $this->sendMessage($data, $data['status']);
+                    }
+                    break;
+                case 'talk':
+                    if (!isset($data['bugNo']) || $data['bugNo'] == '') {
+                        return false;
+                    }
+                    $note = Note::where(['bugNo' => $data['bugNo']])->findOrEmpty()->toArray();
+                    if (empty($note)) {
+                        return false;
+                    }
+                    if (in_array($data['talk_id'], $this->uid)) {
+                        $this->uid = $note['apply_id'];
+                    }
+                    $this->bugMsg ='已有回复请查看';
+                    $this->sendMessage($note);
+                    break;
+                default:
+                    // 处理未知类型
+                    Log::warning("Unhandled event type: {$event['type']}");
+                    return false;
+            }
+        } catch (\Exception $e) {
+            Log::error('Error handling event: ' . $e->getMessage());
+            return false;
         }
+
+        return true;
     }
-    public function check($data,$status=1){
-        $options=['appid'=>env('WECHAT.APPID'),'appsecret'=>env('WECHAT.APPSECRET')];
-        $this->wechat=new \Wechat($options);
-        $this->sendMessage($data,$status);
-    }
-    public function sendMessage($data,$status){
+    public function sendMessage($data,$status=1){
         if(empty($data)) return false;
         $template=[
                 [
@@ -62,9 +91,10 @@ class WxPush
                     'example'=> "工单编号:11\n工单名称:这是一个测试工单\n处理结果:审核通过\n完成时间:2022-12-12 14:00\n"
                  ]
                 ];
+        $openidArr = User::where(['account_id'=>$this->uid])->column('openId,account_id,nickname');
+        if(empty($openidArr)) return false;
         switch ($status){
             case 0:
-                $openidArr = User::where('account_id',$this->uid)->column('openId,account_id,nickname');
                 $template_id = $template[0]['template_id'];
                 $template_name=$template[0]['title'];
                 $temp =  [
@@ -78,18 +108,16 @@ class WxPush
             case 1:
                 $template_id = $template[1]['template_id'];
                 $template_name=$template[1]['title'];
-                $openidArr = User::where('account_id',$this->uid)->column('openId,account_id,nickname');
                 $temp =  [
                     'character_string18'=>['value'=>$data['bugNo']],
                     'thing2'=>['value'=>$data['title']],
                     'time3'=>['value'=>$data['addtime']],
                     'thing8'=>['value'=>$data['apply_name']],
-                    'const23'=>['value'=>'进行中']
+                    'const23'=>['value'=>$this->bugMsg ]
                 ];
                 break;
             case 4:
             case 5:
-                 $openidArr = User::where('account_id',$this->uid)->column('openId,account_id,nickname');
                  $template_id = $template['2']['template_id'];
                  $template_name=$template[2]['title'];
                  $temp =  [
@@ -100,7 +128,6 @@ class WxPush
                 ];
                 break;
         }
-        if(empty($openId)) return false;
         $add=[];
         foreach ($openidArr as $item){
           $res=$this->pushMessage($item['openId'],$template_id,$temp);