WxPush.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\bug\listener;
  4. use app\bug\model\Note;
  5. use app\bug\model\Talk;use app\user\model\User;
  6. use think\facade\Cache;use think\facade\Log;
  7. class WxPush
  8. {
  9. protected $appid;
  10. protected $appsecret;
  11. protected $wechat;
  12. protected $uid=[];
  13. protected $bugMsg='';
  14. public function __construct()
  15. {
  16. $this->appid = env('WECHAT.APPID');
  17. $this->appsecret = env('WECHAT.APPSECRET');
  18. $this->wechat = new \Wechat(['appid' => $this->appid, 'appsecret' => $this->appsecret]);
  19. }
  20. /**
  21. * 事件监听处理
  22. *
  23. * @return mixed
  24. */
  25. public function handle(array $event): bool
  26. {
  27. if (empty($event)) {
  28. return false;
  29. }
  30. $data = $event['data'];
  31. $this->uid = Talk::$talkUid;
  32. try {
  33. switch ($event['type']) {
  34. case 'bug':
  35. if (in_array($data['status'], [0, 1, 4, 5])) {
  36. $this->uid = $data['status'] == 0 ? $this->uid : $data['apply_id'];
  37. $this->bugMsg = Note::$statusCn[$data['status']]??"未知";
  38. $this->sendMessage($data, $data['status']);
  39. }
  40. break;
  41. case 'talk':
  42. if (!isset($data['bugNo']) || $data['bugNo'] == '') {
  43. return false;
  44. }
  45. $note = Note::where(['bugNo' => $data['bugNo']])->findOrEmpty()->toArray();
  46. if (empty($note)) {
  47. return false;
  48. }
  49. if (in_array($data['talk_id'], $this->uid)) {
  50. $this->uid = $note['apply_id'];
  51. }
  52. $this->bugMsg ='已有回复请查看';
  53. $this->sendMessage($note);
  54. break;
  55. default:
  56. // 处理未知类型
  57. Log::warning("Unhandled event type: {$event['type']}");
  58. return false;
  59. }
  60. } catch (\Exception $e) {
  61. Log::error('Error handling event: ' . $e->getMessage());
  62. return false;
  63. }
  64. return true;
  65. }
  66. public function sendMessage($data,$status=1){
  67. if(empty($data)) return false;
  68. $template=[
  69. [
  70. 'template_id'=> 'qfnZuAz3luYNR3hZ9quhgxKlJ29lMhweDzpy4N_gV2M',
  71. 'title'=> '待处理工单提醒',
  72. 'content'=> "工单编号:{{character_string13.DATA}}\n工单名称:{{thing44.DATA}}\n工单类型:{{thing6.DATA}}\n提交人:{{thing14.DATA}}\n提交时间:{{time15.DATA}}\n",
  73. 'example'=> "工单编号:W865775674\n工单名称:XXXX单位电脑更换硬盘\n工单类型:设备保障类/设施设备/媒体播放\n提交人:小明\n提交时间:2022-12-24 12:54:12\n"
  74. ],
  75. [
  76. 'template_id'=> 'BPVhhVJZO3xrYDN_4IseD34iCgg60VnU80uthKzKVCA',
  77. 'title'=> '系统工单受理通知',
  78. 'content'=> "工单编号:{{character_string18.DATA}}\n工单标题:{{thing2.DATA}}\n发起时间:{{time3.DATA}}\n发起人:{{thing8.DATA}}\n处理状态:{{const23.DATA}}\n",
  79. 'example'=> "工单编号:GD20230606151125001\n工单标题:【充值申请单】\n发起时间:2022年11月 22日 16:17\n发起人:彭xx\n处理状态:待审批\n"
  80. ]
  81. ,
  82. [
  83. 'template_id'=> 'G-yQOPhuyfdN8F61JV1PGVJ5L-SfhQznk93YMCF6ipo',
  84. 'title'=> '工单完成通知',
  85. 'content'=> "工单编号:{{character_string2.DATA}}\n工单名称:{{thing12.DATA}}\n处理结果:{{phrase15.DATA}}\n完成时间:{{time11.DATA}}\n",
  86. 'example'=> "工单编号:11\n工单名称:这是一个测试工单\n处理结果:审核通过\n完成时间:2022-12-12 14:00\n"
  87. ]
  88. ];
  89. $openidArr = User::where(['account_id'=>$this->uid])->column('openId,account_id,nickname');
  90. if(empty($openidArr)) return false;
  91. switch ($status){
  92. case 0:
  93. $template_id = $template[0]['template_id'];
  94. $template_name=$template[0]['title'];
  95. $temp = [
  96. 'character_string13'=>['value'=>$data['bugNo']],
  97. 'thing44'=>['value'=> mb_substr( $data['title'], 0, 4)],
  98. 'thing6'=>['value'=>Note::$typeCn[$data['type']]??"未知"],
  99. 'thing14'=>['value'=>$data['apply_name']],
  100. 'time15'=>['value'=>$data['addtime']],
  101. ];
  102. break;
  103. case 1:
  104. $template_id = $template[1]['template_id'];
  105. $template_name=$template[1]['title'];
  106. $temp = [
  107. 'character_string18'=>['value'=>$data['bugNo']],
  108. 'thing2'=>['value'=>mb_substr( $data['title'], 0, 4)],
  109. 'time3'=>['value'=>$data['addtime']],
  110. 'thing8'=>['value'=>$data['apply_name']],
  111. 'const23'=>['value'=>$this->bugMsg ]
  112. ];
  113. break;
  114. case 4:
  115. case 5:
  116. $template_id = $template['2']['template_id'];
  117. $template_name=$template[2]['title'];
  118. $temp = [
  119. 'character_string2'=>['value'=>$data['bugNo']],
  120. 'thing12'=>['value'=>mb_substr( $data['title'], 0, 4)],
  121. 'phrase15'=>['value'=>Note::$statusCn[$data['status']]??"未知"],
  122. 'time11'=>['value'=>$data['addtime']],
  123. ];
  124. break;
  125. }
  126. foreach ($openidArr as $item){
  127. $temps=[
  128. "code"=>$data['bugNo'],
  129. "belong"=>4,
  130. "openId"=>$item['openId'],
  131. "template_id"=>$template_id,
  132. "template_name"=>$template_name,
  133. "action_name"=>'工单推送',
  134. "template_data"=>$temp,
  135. "uid"=>$item['account_id'],
  136. "uname"=>$item['nickname'],
  137. ];
  138. Cache::store('redis')->handler()->lpush('wxpush_queue',json_encode($temps,JSON_UNESCAPED_UNICODE));
  139. }
  140. }
  141. // public function pushMessage($openId,$template_id,$data){
  142. // $temp=[];
  143. // $temp['touser'] =$openId;
  144. // $temp['template_id'] = $template_id;
  145. // $temp['data']=$data;
  146. // $res= $this->wechat->sendTemplateMessage($temp);
  147. // if($res==false && $this->wechat->errCode==40001){
  148. // $this->wechat->resetAuth(env("WECHAT.APPID"));
  149. // $this->wechat->sendTemplateMessage($temp);
  150. // }
  151. // Log::write('data:'.json_encode($temp,JSON_UNESCAPED_UNICODE),'event');
  152. // Log::write('data:'.json_encode($res,JSON_UNESCAPED_UNICODE),'event');
  153. // return $res;
  154. // }
  155. }