123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- <?php
- declare (strict_types = 1);
- namespace app\bug\listener;
- use app\bug\model\Note;
- use app\bug\model\Talk;use app\user\model\User;
- use think\facade\Cache;use think\facade\Log;
- class WxPush
- {
- protected $appid;
- 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(array $event): bool
- {
- if (empty($event)) {
- return false;
- }
- $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 sendMessage($data,$status=1){
- if(empty($data)) return false;
- $template=[
- [
- 'template_id'=> 'qfnZuAz3luYNR3hZ9quhgxKlJ29lMhweDzpy4N_gV2M',
- 'title'=> '待处理工单提醒',
- 'content'=> "工单编号:{{character_string13.DATA}}\n工单名称:{{thing44.DATA}}\n工单类型:{{thing6.DATA}}\n提交人:{{thing14.DATA}}\n提交时间:{{time15.DATA}}\n",
- 'example'=> "工单编号:W865775674\n工单名称:XXXX单位电脑更换硬盘\n工单类型:设备保障类/设施设备/媒体播放\n提交人:小明\n提交时间:2022-12-24 12:54:12\n"
- ],
- [
- 'template_id'=> 'BPVhhVJZO3xrYDN_4IseD34iCgg60VnU80uthKzKVCA',
- 'title'=> '系统工单受理通知',
- 'content'=> "工单编号:{{character_string18.DATA}}\n工单标题:{{thing2.DATA}}\n发起时间:{{time3.DATA}}\n发起人:{{thing8.DATA}}\n处理状态:{{const23.DATA}}\n",
- 'example'=> "工单编号:GD20230606151125001\n工单标题:【充值申请单】\n发起时间:2022年11月 22日 16:17\n发起人:彭xx\n处理状态:待审批\n"
- ]
- ,
- [
- 'template_id'=> 'G-yQOPhuyfdN8F61JV1PGVJ5L-SfhQznk93YMCF6ipo',
- 'title'=> '工单完成通知',
- 'content'=> "工单编号:{{character_string2.DATA}}\n工单名称:{{thing12.DATA}}\n处理结果:{{phrase15.DATA}}\n完成时间:{{time11.DATA}}\n",
- '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:
- $template_id = $template[0]['template_id'];
- $template_name=$template[0]['title'];
- $temp = [
- 'character_string13'=>['value'=>$data['bugNo']],
- 'thing44'=>['value'=> mb_substr( $data['title'], 0, 4)],
- 'thing6'=>['value'=>Note::$typeCn[$data['type']]??"未知"],
- 'thing14'=>['value'=>$data['apply_name']],
- 'time15'=>['value'=>$data['addtime']],
- ];
- break;
- case 1:
- $template_id = $template[1]['template_id'];
- $template_name=$template[1]['title'];
- $temp = [
- 'character_string18'=>['value'=>$data['bugNo']],
- 'thing2'=>['value'=>mb_substr( $data['title'], 0, 4)],
- 'time3'=>['value'=>$data['addtime']],
- 'thing8'=>['value'=>$data['apply_name']],
- 'const23'=>['value'=>$this->bugMsg ]
- ];
- break;
- case 4:
- case 5:
- $template_id = $template['2']['template_id'];
- $template_name=$template[2]['title'];
- $temp = [
- 'character_string2'=>['value'=>$data['bugNo']],
- 'thing12'=>['value'=>mb_substr( $data['title'], 0, 4)],
- 'phrase15'=>['value'=>Note::$statusCn[$data['status']]??"未知"],
- 'time11'=>['value'=>$data['addtime']],
- ];
- break;
- }
- foreach ($openidArr as $item){
- $temps=[
- "code"=>$data['bugNo'],
- "belong"=>4,
- "openId"=>$item['openId'],
- "template_id"=>$template_id,
- "template_name"=>$template_name,
- "action_name"=>'工单推送',
- "template_data"=>$temp,
- "uid"=>$item['account_id'],
- "uname"=>$item['nickname'],
- ];
- Cache::store('redis')->handler()->lpush('wxpush_queue',json_encode($temps,JSON_UNESCAPED_UNICODE));
- }
- }
- // public function pushMessage($openId,$template_id,$data){
- // $temp=[];
- // $temp['touser'] =$openId;
- // $temp['template_id'] = $template_id;
- // $temp['data']=$data;
- // $res= $this->wechat->sendTemplateMessage($temp);
- // if($res==false && $this->wechat->errCode==40001){
- // $this->wechat->resetAuth(env("WECHAT.APPID"));
- // $this->wechat->sendTemplateMessage($temp);
- // }
- // Log::write('data:'.json_encode($temp,JSON_UNESCAPED_UNICODE),'event');
- // Log::write('data:'.json_encode($res,JSON_UNESCAPED_UNICODE),'event');
- // return $res;
- // }
- }
|