refactor(robot): 优化微信机器人获取联系人逻辑

- 新增 get_contacts 方法,用于获取指定微信 ID 的联系人名称
- 在 WeChatBot 控制器中集成 RedisService 以使用 Redis 缓存
- 修改 MerchantBalanceMonitor 中获取联系人名称的逻辑,使用新的 get_contacts 方法
- 优化 WeChatBot 中处理消息通知的逻辑,使用新的 get_contacts 方法获取联系人名称
This commit is contained in:
mzeros 2025-01-08 22:47:33 +08:00
parent a64d7deb9c
commit 47e579221e
3 changed files with 93 additions and 50 deletions

View File

@ -75,7 +75,7 @@ class WeChatBot extends Core
# 判断是否为群组 # 判断是否为群组
if($data['is_group']){ if($data['is_group']){
$roomid = $data['roomid']; $roomid = $data['roomid'];
// Log::write('robot', 'wechat', $data); Log::write('robot', 'wechat', $data);
// if($roomid == '47576792968@chatroom' && str_contains($data['xml'],'wxid_9iv1hha8g3ok29')){ // if($roomid == '47576792968@chatroom' && str_contains($data['xml'],'wxid_9iv1hha8g3ok29')){
// $AllContacts = $RobotService->get_all_contacts(); // $AllContacts = $RobotService->get_all_contacts();
//// var_dump($AllContacts);die; //// var_dump($AllContacts);die;
@ -114,16 +114,12 @@ class WeChatBot extends Core
$merchantService = MerchantService::instance(); $merchantService = MerchantService::instance();
$check_merchant = $merchantService->db()->whereLike('other_param', '%'.$roomid.'%')->findOrEmpty(); $check_merchant = $merchantService->db()->whereLike('other_param', '%'.$roomid.'%')->findOrEmpty();
if($check_merchant){ if($check_merchant){
$AllContacts = $RobotService->get_all_contacts(); $ac = $RobotService->get_contacts($data['sender']) ;
$Contacts_array = json_decode($AllContacts,true); if(!$ac){
$acc = $Contacts_array['data']['contacts']; $ac = '';
$ac = '';
foreach ($acc as $k=>$v){
if($v['wxid'] == $data['sender']){
$ac = $v['name'];
}
} }
if($roomid == '47576792968@chatroom'){ if($roomid == '47576792968@chatroom'){
if(str_starts_with($data['content'], '@发财-庚辰小秘通知 ') ||str_starts_with($data['content'], '@发财-庚辰小秘 通知 ')||str_starts_with($data['content'], '@发财-庚辰小秘通知全部 ')){ if(str_starts_with($data['content'], '@发财-庚辰小秘通知 ') ||str_starts_with($data['content'], '@发财-庚辰小秘 通知 ')||str_starts_with($data['content'], '@发财-庚辰小秘通知全部 ')){
$newString = strstr($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(); $merchantsList = $merchantService->db()->where(['merchant_type'=>1,'status'=>1])->whereLike('other_param','%Monitor_Balance_status%')->select()->toArray();
if(!$merchantsList)return $this->response(['code'=>1]); if(!$merchantsList)return $this->response(['code'=>1]);
$tip_info=[]; $tip_info=[];
$num = 0; $num = 0;
@ -145,16 +142,15 @@ class WeChatBot extends Core
$other_param = json_decode($merchant['other_param'],true); $other_param = json_decode($merchant['other_param'],true);
$roomids = $other_param['QYWX_roomid']; $roomids = $other_param['QYWX_roomid'];
$sender = $other_param['QYWX_sender']; $sender = $other_param['QYWX_sender'];
if($roomid == $data['roomid']){
if($roomids == $roomid){
continue; continue;
} }
$acm = ''; // $acm = '';
foreach ($acc as $k=>$v){ $acm = $RobotService->get_contacts($sender) ;
if($v['wxid'] == $sender){ if(!$acm){
$acm = $v['name']; $acm = '';
}
} }
$RobotService->send_text("通知 \n ".$newString."\n 时间:".date('Y-m-d H:i:s')."\n @".$acm,$roomids,$sender); $RobotService->send_text("通知 \n ".$newString."\n 时间:".date('Y-m-d H:i:s')."\n @".$acm,$roomids,$sender);
$tip_info[]=$merchant['name']; $tip_info[]=$merchant['name'];
$num++; $num++;

View File

@ -64,14 +64,9 @@ class MerchantBalanceMonitor extends Command
#设置redis,根据mid设置redis, #设置redis,根据mid设置redis,
if(!$this->redis->get('Check_merchant_account-'.$merchants['id'])){ if(!$this->redis->get('Check_merchant_account-'.$merchants['id'])){
$count_sum ++; $count_sum ++;
$AllContacts = $RobotService->get_all_contacts(); $ac = $RobotService->get_contacts($sender) ;
$Contacts_array = json_decode($AllContacts,true); if(!$ac){
$acc = $Contacts_array['data']['contacts']; $ac = '';
$ac = '';
foreach ($acc as $k=>$v){
if($v['wxid'] == $sender){
$ac = $v['name'];
}
} }
$this->redis->set('Check_merchant_account-'.$merchants['id'],1); $this->redis->set('Check_merchant_account-'.$merchants['id'],1);
$RobotService->send_text('余额预警: 您的账户现在只剩下'.$merchants['account_surplus'].'元,低于预警'.$Monitor_Balance.'元,请及时充值, @'.$ac,$roomid,$sender); $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'])){ if(!$this->redis->get('Check_merchant_account-TOP-'.$merchants['id'])){
$count_sum ++; $count_sum ++;
if(!isset($ac)){ if(!isset($ac)){
$AllContacts = $RobotService->get_all_contacts(); $ac = $RobotService->get_contacts($sender) ;
$Contacts_array = json_decode($AllContacts,true); if(!$ac){
$acc = $Contacts_array['data']['contacts']; $ac = '';
$ac = '';
foreach ($acc as $k=>$v){
if($v['wxid'] == $sender){
$ac = $v['name'];
}
} }
} }
@ -100,14 +90,9 @@ class MerchantBalanceMonitor extends Command
}else{ }else{
if($this->redis->get('Check_merchant_account-'.$merchants['id'])){ if($this->redis->get('Check_merchant_account-'.$merchants['id'])){
$count_sum ++; $count_sum ++;
$AllContacts = $RobotService->get_all_contacts(); $ac = $RobotService->get_contacts($sender) ;
$Contacts_array = json_decode($AllContacts,true); if(!$ac){
$acc = $Contacts_array['data']['contacts']; $ac = '';
$ac = '';
foreach ($acc as $k=>$v){
if($v['wxid'] == $sender){
$ac = $v['name'];
}
} }
$this->redis->delete('Check_merchant_account-'.$merchants['id']); $this->redis->delete('Check_merchant_account-'.$merchants['id']);
$RobotService->send_text('余额预警: 您的账户已经恢复正常,现在余额为'.$merchants['account_surplus'].'元,请关注账户, @'.$ac,$roomid,$sender); $RobotService->send_text('余额预警: 您的账户已经恢复正常,现在余额为'.$merchants['account_surplus'].'元,请关注账户, @'.$ac,$roomid,$sender);
@ -136,20 +121,13 @@ class MerchantBalanceMonitor extends Command
// 获取第一个匹配的元素 // 获取第一个匹配的元素
$merchantMid = reset($merchantMid); $merchantMid = reset($merchantMid);
$ac = '';
$other_params = json_decode($merchantMid['other_param'],true); $other_params = json_decode($merchantMid['other_param'],true);
$roomid = $other_params['QYWX_roomid']??null; $roomid = $other_params['QYWX_roomid']??null;
$sender = $other_params['QYWX_sender']??null; $sender = $other_params['QYWX_sender']??null;
if($roomid){ if($roomid){
if(!isset($acc)){ $ac = $RobotService->get_contacts($sender) ;
$AllContacts = $RobotService->get_all_contacts(); if(!$ac){
$Contacts_array = json_decode($AllContacts,true); $ac = '';
$acc = $Contacts_array['data']['contacts'];
}
foreach ($acc as $k=>$v){
if($v['wxid'] == $sender){
$ac = $v['name'];
}
} }
$RobotService->send_text("余额预警: \n 检测到您的账户余额有变动,\n 变动金额为:".$datas['num']."元, \n 变动描述:".$datas['desc']." \n 变动款时间:".$datas['time']." \n 请关注账户 @".$ac,$roomid,$sender); $RobotService->send_text("余额预警: \n 检测到您的账户余额有变动,\n 变动金额为:".$datas['num']."元, \n 变动描述:".$datas['desc']." \n 变动款时间:".$datas['time']." \n 请关注账户 @".$ac,$roomid,$sender);
$add_msg_count++; $add_msg_count++;

View File

@ -6,6 +6,7 @@ namespace app\robot\controller;
use app\channel\service\ChannelService; use app\channel\service\ChannelService;
use app\channel\service\ProductService; use app\channel\service\ProductService;
use app\gateway\service\CurlService; use app\gateway\service\CurlService;
use app\gateway\service\RedisService;
use app\kami\service\LockCardService; use app\kami\service\LockCardService;
use app\merchant\service\MerchantService; use app\merchant\service\MerchantService;
use app\merchant\service\OrderHistoryService; 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;
}
}
}