From 47e579221eb85707019c91477073a5955ae23d24 Mon Sep 17 00:00:00 2001 From: mzeros Date: Wed, 8 Jan 2025 22:47:33 +0800 Subject: [PATCH] =?UTF-8?q?refactor(robot):=20=E4=BC=98=E5=8C=96=E5=BE=AE?= =?UTF-8?q?=E4=BF=A1=E6=9C=BA=E5=99=A8=E4=BA=BA=E8=8E=B7=E5=8F=96=E8=81=94?= =?UTF-8?q?=E7=B3=BB=E4=BA=BA=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 get_contacts 方法,用于获取指定微信 ID 的联系人名称 - 在 WeChatBot 控制器中集成 RedisService 以使用 Redis 缓存 - 修改 MerchantBalanceMonitor 中获取联系人名称的逻辑,使用新的 get_contacts 方法 - 优化 WeChatBot 中处理消息通知的逻辑,使用新的 get_contacts 方法获取联系人名称 --- .../controller/robotApi/WeChatBot.php | 28 ++++---- .../monitor/MerchantBalanceMonitor.php | 46 ++++--------- app/robot/controller/WeChatBot.php | 69 +++++++++++++++++++ 3 files changed, 93 insertions(+), 50 deletions(-) diff --git a/app/private_api/controller/robotApi/WeChatBot.php b/app/private_api/controller/robotApi/WeChatBot.php index e330500..57c5630 100644 --- a/app/private_api/controller/robotApi/WeChatBot.php +++ b/app/private_api/controller/robotApi/WeChatBot.php @@ -75,7 +75,7 @@ class WeChatBot extends Core # 判断是否为群组 if($data['is_group']){ $roomid = $data['roomid']; -// Log::write('robot', 'wechat', $data); + Log::write('robot', 'wechat', $data); // if($roomid == '47576792968@chatroom' && str_contains($data['xml'],'wxid_9iv1hha8g3ok29')){ // $AllContacts = $RobotService->get_all_contacts(); //// var_dump($AllContacts);die; @@ -114,16 +114,12 @@ class WeChatBot extends Core $merchantService = MerchantService::instance(); $check_merchant = $merchantService->db()->whereLike('other_param', '%'.$roomid.'%')->findOrEmpty(); if($check_merchant){ - $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'] == $data['sender']){ - $ac = $v['name']; - } + $ac = $RobotService->get_contacts($data['sender']) ; + if(!$ac){ + $ac = ''; } + if($roomid == '47576792968@chatroom'){ if(str_starts_with($data['content'], '@发财-庚辰小秘通知 ') ||str_starts_with($data['content'], '@发财-庚辰小秘 通知 ')||str_starts_with($data['content'], '@发财-庚辰小秘通知全部 ')){ $newString = strstr($data['content'], '通知全部 '); @@ -138,6 +134,7 @@ class WeChatBot extends Core } } $merchantsList = $merchantService->db()->where(['merchant_type'=>1,'status'=>1])->whereLike('other_param','%Monitor_Balance_status%')->select()->toArray(); + if(!$merchantsList)return $this->response(['code'=>1]); $tip_info=[]; $num = 0; @@ -145,16 +142,15 @@ class WeChatBot extends Core $other_param = json_decode($merchant['other_param'],true); $roomids = $other_param['QYWX_roomid']; $sender = $other_param['QYWX_sender']; - if($roomid == $data['roomid']){ + + if($roomids == $roomid){ continue; } - $acm = ''; - foreach ($acc as $k=>$v){ - if($v['wxid'] == $sender){ - $acm = $v['name']; - } +// $acm = ''; + $acm = $RobotService->get_contacts($sender) ; + if(!$acm){ + $acm = ''; } - $RobotService->send_text("通知 \n ".$newString."\n 时间:".date('Y-m-d H:i:s')."\n @".$acm,$roomids,$sender); $tip_info[]=$merchant['name']; $num++; diff --git a/app/queue/command/monitor/MerchantBalanceMonitor.php b/app/queue/command/monitor/MerchantBalanceMonitor.php index d8267a3..9ddea33 100644 --- a/app/queue/command/monitor/MerchantBalanceMonitor.php +++ b/app/queue/command/monitor/MerchantBalanceMonitor.php @@ -64,14 +64,9 @@ 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']; - $ac = ''; - foreach ($acc as $k=>$v){ - if($v['wxid'] == $sender){ - $ac = $v['name']; - } + $ac = $RobotService->get_contacts($sender) ; + if(!$ac){ + $ac = ''; } $this->redis->set('Check_merchant_account-'.$merchants['id'],1); $RobotService->send_text('余额预警: 您的账户现在只剩下'.$merchants['account_surplus'].'元,低于预警'.$Monitor_Balance.'元,请及时充值, @'.$ac,$roomid,$sender); @@ -81,14 +76,9 @@ class MerchantBalanceMonitor extends Command 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); - $acc = $Contacts_array['data']['contacts']; - $ac = ''; - foreach ($acc as $k=>$v){ - if($v['wxid'] == $sender){ - $ac = $v['name']; - } + $ac = $RobotService->get_contacts($sender) ; + if(!$ac){ + $ac = ''; } } @@ -100,14 +90,9 @@ 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']; - $ac = ''; - foreach ($acc as $k=>$v){ - if($v['wxid'] == $sender){ - $ac = $v['name']; - } + $ac = $RobotService->get_contacts($sender) ; + if(!$ac){ + $ac = ''; } $this->redis->delete('Check_merchant_account-'.$merchants['id']); $RobotService->send_text('余额预警: 您的账户已经恢复正常,现在余额为'.$merchants['account_surplus'].'元,请关注账户, @'.$ac,$roomid,$sender); @@ -136,20 +121,13 @@ class MerchantBalanceMonitor extends Command // 获取第一个匹配的元素 $merchantMid = reset($merchantMid); - $ac = ''; $other_params = json_decode($merchantMid['other_param'],true); $roomid = $other_params['QYWX_roomid']??null; $sender = $other_params['QYWX_sender']??null; if($roomid){ - if(!isset($acc)){ - $AllContacts = $RobotService->get_all_contacts(); - $Contacts_array = json_decode($AllContacts,true); - $acc = $Contacts_array['data']['contacts']; - } - foreach ($acc as $k=>$v){ - if($v['wxid'] == $sender){ - $ac = $v['name']; - } + $ac = $RobotService->get_contacts($sender) ; + if(!$ac){ + $ac = ''; } $RobotService->send_text("余额预警: \n 检测到您的账户余额有变动,\n 变动金额为:".$datas['num']."元, \n 变动描述:".$datas['desc']." \n 变动款时间:".$datas['time']." \n 请关注账户 @".$ac,$roomid,$sender); $add_msg_count++; diff --git a/app/robot/controller/WeChatBot.php b/app/robot/controller/WeChatBot.php index 3fdeb06..b07f746 100644 --- a/app/robot/controller/WeChatBot.php +++ b/app/robot/controller/WeChatBot.php @@ -6,6 +6,7 @@ namespace app\robot\controller; use app\channel\service\ChannelService; use app\channel\service\ProductService; use app\gateway\service\CurlService; +use app\gateway\service\RedisService; use app\kami\service\LockCardService; use app\merchant\service\MerchantService; use app\merchant\service\OrderHistoryService; @@ -69,6 +70,74 @@ class WeChatBot extends Controller } + public function send_image($content,$receiver,$aters =false) + { + + } + + public function get_contacts($wx_id) + { + $redis = RedisService::getInstance(); + try{ + + $data = $redis->get('Wechat_all_contacts'); + if($data){ + foreach ($data as $k=>$v){ + if($v['wxid'] == $wx_id){ + return $v['name']; + } + } + + $all_contacts = $this->get_all_contacts(); + $Contacts_array = json_decode($all_contacts,true); + if(!is_array($Contacts_array) || !isset( $Contacts_array['data']['contacts']) )return false; + + $acc = $Contacts_array['data']['contacts']; + foreach ($acc as $k=>$v){ + if($v['wxid'] == $wx_id){ + $redis->delete('Wechat_all_contacts'); + $redis->set('Wechat_all_contacts',$Contacts_array['data']['contacts']); + return $v['name']; + } + } + + }else{ + $all_contacts = $this->get_all_contacts(); + $Contacts_array = json_decode($all_contacts,true); + if(!is_array($Contacts_array) || !isset( $Contacts_array['data']['contacts']) )return false; + + $acc = $Contacts_array['data']['contacts']; + foreach ($acc as $k=>$v){ + if($v['wxid'] == $wx_id){ + $redis->set('Wechat_all_contacts',$Contacts_array['data']['contacts']); + return $v['name']; + } + } + } + return false; + }catch (\Exception $e){ + try{ + $all_contacts = $this->get_all_contacts(); + $Contacts_array = json_decode($all_contacts,true); + if(!is_array($Contacts_array) || !isset( $Contacts_array['data']['contacts']) )return false; + + $acc = $Contacts_array['data']['contacts']; + foreach ($acc as $k=>$v){ + if($v['wxid'] == $wx_id){ + return $v['name']; + } + } + return false; + }catch (\Exception $e){ + return false; + + } + + } + + + } +