From 21e4c1e37a45487d943c13f8633968cf8c18611f Mon Sep 17 00:00:00 2001 From: mzeros Date: Mon, 6 Jan 2025 13:08:15 +0800 Subject: [PATCH] =?UTF-8?q?feat(queue):=20=E6=B7=BB=E5=8A=A0=E6=B8=A0?= =?UTF-8?q?=E9=81=93=E4=BD=99=E9=A2=9D=E7=9B=91=E6=8E=A7=E9=A2=84=E8=AD=A6?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 ChannelBalanceMonitor 类实现渠道余额监控 - 在 MerchantBalanceMonitor 中增加余额恢复提醒功能 - 添加 WxWorkBot 类用于发送微信工作台消息- 在 Test 控制器中整合渠道余额监控逻辑 - 更新 sys.php 添加新的渠道余额监控命令 --- app/openapi/controller/Test.php | 107 +++++++------- .../command/monitor/ChannelBalanceMonitor.php | 131 ++++++++++++++++++ .../monitor/MerchantBalanceMonitor.php | 12 +- app/queue/sys.php | 1 + app/robot/controller/WxWorkBot.php | 128 +++++++++++++++++ 5 files changed, 317 insertions(+), 62 deletions(-) create mode 100644 app/queue/command/monitor/ChannelBalanceMonitor.php create mode 100644 app/robot/controller/WxWorkBot.php diff --git a/app/openapi/controller/Test.php b/app/openapi/controller/Test.php index 70579fd..2b48586 100644 --- a/app/openapi/controller/Test.php +++ b/app/openapi/controller/Test.php @@ -38,6 +38,7 @@ use app\order\service\Kami91OrderService; use app\order\service\OrderService as OrderBase; use app\robot\controller\api\Qqbot; use app\robot\controller\WeChatBot as WeChatBotC; +use app\robot\controller\WxWorkBot; use app\setting\service\BlackCardService; use app\setting\service\StatService; use dever\Log; @@ -1056,17 +1057,24 @@ class Test extends Core public function execute() { + ini_set('memory_limit', '1024M'); $this->redis(); - $merchantService = MerchantService::instance(); - $RobotService = new WeChatBotC($this->app); - $merchantsList = $merchantService->db()->where(['merchant_type'=>1,'status'=>1])->whereLike('other_param','%Monitor_Balance_status%')->select()->toArray(); - $merchantsList_num = count($merchantsList); + $channelService = ChannelService::instance(); + $RobotService = new WxWorkBot($this->app); + $channelList = $channelService->db()->where(['status'=>1])->whereLike('other_data','%Monitor_Balance_status%')->select()->toArray(); + + $channelList_num = count($channelList); $count_sum = 0; + $day = date('Y-m-d', strtotime('-1 day')); - foreach($merchantsList as $merchants) { - $other_param = json_decode($merchants['other_param'],true); + $tip_info=[]; + $key = '41b42bd4-c9f9-4617-9531-0a358dd97a82'; + + + foreach($channelList as $channel) { + $other_param = json_decode($channel['other_data'],true); if(!$other_param){ continue; } @@ -1074,77 +1082,60 @@ class Test extends Core if(!$Monitor_Balance){ continue; } - $roomid = $other_param['QYWX_roomid']; - $sender = $other_param['QYWX_sender']; - $count_sum ++; + try{ + $account_data = $channelService->call('account', $channel['id'], $day); + + if (is_array($account_data) && isset($account_data['account'])) { + $balance = $account_data['account']; + if( $balance <= $Monitor_Balance){ + + $count_sum ++; + $this->redis->set('Check_channel_account-'.$channel['id'],1); + $tip_info[] = $channel['name']."余额不足,只剩下:".$balance.""; - if($merchants['account_surplus'] <= $Monitor_Balance){ + $Monitor_Balance_TOP = $other_param['Monitor_Balance_TOP'] ?? null; + if($Monitor_Balance_TOP && $balance <= $Monitor_Balance_TOP){ + if(!$this->redis->get('Check_channel_account_TOP-'.$channel['id'])){ + $count_sum ++; + $this->redis->set('Check_channel_account_TOP-'.$channel['id'],1); + $tip_info[] = $channel['name']."余额不足,只剩下:".$balance.""; - #进入预警流程 - #设置redis,根据mid设置redis, - if(!$this->redis->get('Check_merchant_account-'.$merchants['id'])){ - $AllContacts = $RobotService->get_all_contacts(); - $Contacts_array = json_decode($AllContacts,true); - $acc = $Contacts_array['data']['contacts']; - $ac = ''; - foreach ($acc as $k=>$v){ - if($v['wxid'] == $sender){ - $ac = $v['name']; - } - } - $this->redis->set('Check_merchant_account-'.$merchants['id'],1); - $RobotService->send_text('余额预警: 您的账户现在只剩下'.$Monitor_Balance.'元,请及时充值, @'.$ac,$roomid,$sender); - } - if($merchants['account_surplus'] <= 500){ - if(!$this->redis->get('Check_merchant_account-TOP-'.$merchants['id'])){ - if(isset($ac)){ - $AllContacts = $RobotService->get_all_contacts(); - $Contacts_array = json_decode($AllContacts,true); - $acc = $Contacts_array['data']['contacts']; - $ac = ''; - foreach ($acc as $k=>$v){ - if($v['wxid'] == $sender){ - $ac = $v['name']; - } } } + }else{ + if($this->redis->get('Check_channel_account-'.$channel['id'])){ + $count_sum ++; - $this->redis->set('Check_merchant_account-TOP-'.$merchants['id'],1); - $RobotService->send_text('余额预警: 您的账户现在只剩下'.$Monitor_Balance.'元,已经严重不足,请关注账户, @'.$ac,$roomid,$sender); - } - } - }else{ - if($this->redis->get('Check_merchant_account-'.$merchants['id'])){ - $AllContacts = $RobotService->get_all_contacts(); - $Contacts_array = json_decode($AllContacts,true); - $acc = $Contacts_array['data']['contacts']; - $ac = ''; - foreach ($acc as $k=>$v){ - if($v['wxid'] == $sender){ - $ac = $v['name']; + $this->redis->delete('Check_channel_account-'.$channel['id']); + $tip_info[] = $channel['name']."余额已经恢复,现在余额为:".$balance.""; } + if($this->redis->get('Check_channel_account_TOP-'.$channel['id'])){ + $this->redis->delete('Check_channel_account_TOP-'.$channel['id']); + } + } - $this->redis->del('Check_merchant_account-'.$merchants['id']); - $RobotService->send_text('余额预警: 您的账户已经恢复正常,现在余额为'.$merchants['account_surplus'].'元,请关注账户, @'.$ac,$roomid,$sender); - } - if($this->redis->get('Check_merchant_account-TOP-'.$merchants['id'])){ - $this->redis->del('Check_merchant_account-TOP-'.$merchants['id']); } + }catch (\Exception $e){ + } + } + if(!empty($tip_info)){ + $errorMsg = "渠道余额监控预警".$count_sum ."列,请相关同事注意。\n +>"; + $errorMsg .= implode(">", $tip_info); + $errorMsgs =$RobotService->send_markdown($errorMsg,$key); + } - - - - + var_dump($errorMsgs); } diff --git a/app/queue/command/monitor/ChannelBalanceMonitor.php b/app/queue/command/monitor/ChannelBalanceMonitor.php new file mode 100644 index 0000000..b369a6e --- /dev/null +++ b/app/queue/command/monitor/ChannelBalanceMonitor.php @@ -0,0 +1,131 @@ +setName('xQueue:ChannelBalanceMonitor')->setDescription('[ 监控系统 ] 渠道余额监控预警'); + } + + /** + * @param Input $input + * @param Output $output + * @throws \think\admin\Exception + */ + protected function execute(Input $input, Output $output) + { + ini_set('memory_limit', '1024M'); + $this->redis(); + + $channelService = ChannelService::instance(); + $RobotService = new WxWorkBot($this->app); + $channelList = $channelService->db()->where(['status'=>1])->whereLike('other_data','%Monitor_Balance_status%')->select()->toArray(); + if(!$channelList) $this->setQueueSuccess("未找到支持监控余额的渠道"); + + $channelList_num = count($channelList); + $count_sum = 0; + + $day = date('Y-m-d', strtotime('-1 day')); + + $tip_info=[]; + $key = '41b42bd4-c9f9-4617-9531-0a358dd97a82'; + + + foreach($channelList as $channel) { + $other_param = json_decode($channel['other_data'],true); + if(!$other_param){ + continue; + } + $Monitor_Balance = $other_param['Monitor_Balance'] ?? null; + if(!$Monitor_Balance){ + continue; + } + + + + try{ + $account_data = $channelService->call('account', $channel['id'], $day); + + if (is_array($account_data) && isset($account_data['account'])) { + $balance = $account_data['account']; + if( $balance <= $Monitor_Balance){ + + if(!$this->redis->get('Check_channel_account-'.$channel['id'])){ + $count_sum ++; + $this->redis->set('Check_channel_account-'.$channel['id'],1); + $tip_info[] = $channel['name']."余额不足,只剩下:".$balance.""; + + } + $Monitor_Balance_TOP = $other_param['Monitor_Balance_TOP'] ?? null; + if($Monitor_Balance_TOP && $balance <= $Monitor_Balance_TOP){ + if(!$this->redis->get('Check_channel_account_TOP-'.$channel['id'])){ + $count_sum ++; + $this->redis->set('Check_channel_account_TOP-'.$channel['id'],1); + $tip_info[] = $channel['name']."余额不足,只剩下:".$balance.""; + + } + } + }else{ + if($this->redis->get('Check_channel_account-'.$channel['id'])){ + $count_sum ++; + + $this->redis->delete('Check_channel_account-'.$channel['id']); + $tip_info[] = $channel['name']."余额已经恢复,现在余额为:".$balance.""; + } + if($this->redis->get('Check_channel_account_TOP-'.$channel['id'])){ + $this->redis->delete('Check_channel_account_TOP-'.$channel['id']); + } + + } + + } + }catch (\Exception $e){ + + } + + } + + if(!empty($tip_info)){ + $errorMsg = "渠道余额监控预警".$count_sum ."列,请相关同事注意。\n +>"; + $errorMsg .= implode(">", $tip_info); + $RobotService->send_markdown($errorMsg,$key); + + } + + + $this->setQueueSuccess("共处理 {$channelList_num} 个渠道, 完成{$count_sum} 个查询处理!"); + + + } + + protected function redis() + { + if (!$this->redis) { + $this->redis = RedisService::getInstance(); + } + } + + + + +} + + + diff --git a/app/queue/command/monitor/MerchantBalanceMonitor.php b/app/queue/command/monitor/MerchantBalanceMonitor.php index eea8bb5..b915585 100644 --- a/app/queue/command/monitor/MerchantBalanceMonitor.php +++ b/app/queue/command/monitor/MerchantBalanceMonitor.php @@ -58,7 +58,7 @@ class MerchantBalanceMonitor extends Command - $count_sum ++; + if($merchants['account_surplus'] <= $Monitor_Balance){ @@ -66,6 +66,7 @@ class MerchantBalanceMonitor extends Command #进入预警流程 #设置redis,根据mid设置redis, if(!$this->redis->get('Check_merchant_account-'.$merchants['id'])){ + $count_sum ++; $AllContacts = $RobotService->get_all_contacts(); $Contacts_array = json_decode($AllContacts,true); $acc = $Contacts_array['data']['contacts']; @@ -81,6 +82,7 @@ class MerchantBalanceMonitor extends Command } if($merchants['account_surplus'] <= 500){ if(!$this->redis->get('Check_merchant_account-TOP-'.$merchants['id'])){ + $count_sum ++; if(!isset($ac)){ $AllContacts = $RobotService->get_all_contacts(); $Contacts_array = json_decode($AllContacts,true); @@ -100,6 +102,7 @@ class MerchantBalanceMonitor extends Command } }else{ if($this->redis->get('Check_merchant_account-'.$merchants['id'])){ + $count_sum ++; $AllContacts = $RobotService->get_all_contacts(); $Contacts_array = json_decode($AllContacts,true); $acc = $Contacts_array['data']['contacts']; @@ -109,18 +112,19 @@ class MerchantBalanceMonitor extends Command $ac = $v['name']; } } - $this->redis->del('Check_merchant_account-'.$merchants['id']); + $this->redis->delete('Check_merchant_account-'.$merchants['id']); $RobotService->send_text('余额预警: 您的账户已经恢复正常,现在余额为'.$merchants['account_surplus'].'元,请关注账户, @'.$ac,$roomid,$sender); } if($this->redis->get('Check_merchant_account-TOP-'.$merchants['id'])){ - $this->redis->del('Check_merchant_account-TOP-'.$merchants['id']); + $count_sum ++; + $this->redis->delete('Check_merchant_account-TOP-'.$merchants['id']); } } } - $this->setQueueSuccess("共处理 {$merchantsList_num} 个商家, 完成{$count_sum} 个订单查询新状态更新!"); + $this->setQueueSuccess("共处理 {$merchantsList_num} 个商家, 完成{$count_sum} 个查询处理!"); } diff --git a/app/queue/sys.php b/app/queue/sys.php index ff07dc7..d36ffaf 100644 --- a/app/queue/sys.php +++ b/app/queue/sys.php @@ -34,6 +34,7 @@ if ($app->request->isCli()) { $console->addCommand(\app\queue\command\taobao\TaobaoFailOrderSecondNotify::class); #监控 $console->addCommand(\app\queue\command\monitor\MerchantBalanceMonitor::class); + $console->addCommand(\app\queue\command\monitor\ChannelBalanceMonitor::class); }); diff --git a/app/robot/controller/WxWorkBot.php b/app/robot/controller/WxWorkBot.php new file mode 100644 index 0000000..12efbec --- /dev/null +++ b/app/robot/controller/WxWorkBot.php @@ -0,0 +1,128 @@ +'暂无订单', + -2=>'暂无卡密', + 1=>'成功' + ); + + protected string $host = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send'; + + + public function initialize(): void + { + #47609e509f946ecbfdba27f16db341c4 + + parent::initialize(); + + #获取wechat信息 + $this->key='47609e509f946ecbfdba27f16db341c4'; + } + + #发送文本 + public function send_text($content,$key = false,$receiver = false) + { + $text = [ + 'content' =>$content, + + ]; + + if($receiver){ + $text['mentioned_mobile_list'] = $receiver; + } + $request = [ + 'msgtype'=>'text', + 'text'=>$text + ]; + + $url = $this->host.'?key='.$this->key; + if($key){ + $url = $this->host.'?key='.$key; + } + + return $this->curl('post', $url,$request,true); + + } + + #发送markdown + + public function send_markdown($content,$key = false) + { + $markdown = [ + 'content' =>$content, + + ]; + + + $request = [ + 'msgtype'=>'markdown', + 'markdown'=>$markdown + ]; + $url = $this->host.'?key='.$this->key; + if($key){ + $url = $this->host.'?key='.$key; + } +// var_dump($request);die; + + return $this->send_post($url,$request,'POST'); + } + + + + + + + + + public function send_post($notify_url, $post_data, $type): mixed + { + $postdate = json_encode($post_data); + +// $postdate = http_build_query($post_data); + + $options = array( + 'http' => array( + 'method' => $type, + 'header' => 'Content-type:application/json', + 'content' => $postdate, + 'timeout' => 15 * 60 // 超时时间(单位:s) + ) + ); + $context = stream_context_create($options); + return file_get_contents($notify_url, false, $context); + } + + protected function curl($method, $url, $param = array(), $json = false, $header = false):mixed + { + if ($param) { + $log['type'] = 'request'; + $log['url'] = $url; + $log['param'] = $param; + $this->log($log); + } + + $curl = CurlService::getInstance($url, $param, $method, $json, $header); + $curl->setTimeOut(3600); + return $curl->result(); + } + + protected static function log($data, $type = 'request'):void + { + \dever\Log::write('WxWork', $type, $data); +// \dever\Log::write('jingdong', $type, $data); + } + + + + +} \ No newline at end of file