
- 新增 get_contacts 方法,用于获取指定微信 ID 的联系人名称 - 在 WeChatBot 控制器中集成 RedisService 以使用 Redis 缓存 - 修改 MerchantBalanceMonitor 中获取联系人名称的逻辑,使用新的 get_contacts 方法 - 优化 WeChatBot 中处理消息通知的逻辑,使用新的 get_contacts 方法获取联系人名称
393 lines
17 KiB
PHP
393 lines
17 KiB
PHP
<?php
|
||
|
||
namespace app\private_api\controller\robotApi;
|
||
|
||
use app\channel\service\ChannelService;
|
||
use app\core\Service;
|
||
use app\gateway\service\RedisService;
|
||
use app\merchant\service\MerchantService;
|
||
use app\merchant\service\OrderLastweekHistoryService;
|
||
use app\order\service\OrderService;
|
||
use dever\Log;
|
||
use think\exception\HttpResponseException;
|
||
use app\robot\controller\WeChatBot as WeChatBotC;
|
||
use app\robot\service\WeChatService;
|
||
|
||
|
||
/**
|
||
* 接口处理
|
||
* Class Handle
|
||
* @package app\gateway\api
|
||
*/
|
||
class WeChatBot extends Core
|
||
{
|
||
# 是否检测数据
|
||
protected bool $check = false;
|
||
|
||
protected array $robotInfo = [];
|
||
|
||
|
||
|
||
|
||
# 获取机器人信息
|
||
public function getRobotInfo($token)
|
||
{
|
||
$redisKey = 'wechat_robot_info_' . $token;
|
||
$robotInfo = $this->redis->get($redisKey);
|
||
if ($robotInfo === false) {
|
||
$WeChatService = WeChatService::instance();
|
||
|
||
|
||
// 如果 Redis 连接失败或没有找到机器人信息,从数据库中获取
|
||
$robotInfo = $WeChatService->getInfo($token);
|
||
if($robotInfo && $robotInfo['status'] == 1){
|
||
// 将获取到的信息存入 Redis,以便下次快速访问
|
||
$this->redis->set($redisKey, json_encode($robotInfo), 3600);
|
||
|
||
} else {
|
||
return false;
|
||
}
|
||
|
||
} else {
|
||
$robotInfo = json_decode($robotInfo, true);
|
||
}
|
||
|
||
return $robotInfo;
|
||
|
||
}
|
||
|
||
|
||
public function Call()
|
||
{
|
||
$RobotService = new WeChatBotC($this->app);
|
||
$data = input();
|
||
// if(isset($data['token'])){
|
||
// $robotInfo = $this->getRobotInfo($data['token']);
|
||
// if($robotInfo){
|
||
// $this->robotInfo = $robotInfo;
|
||
// }else{
|
||
// return $this->response(['code'=>-1,'msg'=>'机器人不存在']);
|
||
// }
|
||
// }else{
|
||
// return $this->response(['code'=>-1,'msg'=>'参数错误']);
|
||
// }
|
||
|
||
# 判断是否为群组
|
||
if($data['is_group']){
|
||
$roomid = $data['roomid'];
|
||
Log::write('robot', 'wechat', $data);
|
||
// if($roomid == '47576792968@chatroom' && str_contains($data['xml'],'wxid_9iv1hha8g3ok29')){
|
||
// $AllContacts = $RobotService->get_all_contacts();
|
||
//// var_dump($AllContacts);die;
|
||
// $Contacts_array = json_decode($AllContacts,true);
|
||
// $acc = $Contacts_array['data']['contacts'];
|
||
//// var_dump($acc);die;
|
||
// $ac = '';
|
||
// foreach ($acc as $k=>$v){
|
||
// if($v['wxid'] == $data['sender']){
|
||
// $ac = $v['name'];
|
||
// }
|
||
// }
|
||
//// var_dump($data['sender']);die;
|
||
// return $RobotService->send_text('测试自动回复 @'.$ac,$data['roomid'],$data['sender']);
|
||
// }
|
||
// if($roomid == '47576792968@chatroom' && str_contains($data['xml'],'wxid_9iv1hha8g3ok29')){
|
||
// $AllContacts = $RobotService->get_all_contacts();
|
||
//// var_dump($AllContacts);die;
|
||
// $Contacts_array = json_decode($AllContacts,true);
|
||
// $acc = $Contacts_array['data']['contacts'];
|
||
//// var_dump($acc);die;
|
||
// $ac = '';
|
||
// foreach ($acc as $k=>$v){
|
||
// if($v['wxid'] == $data['sender']){
|
||
// $ac = $v['name'];
|
||
// }
|
||
// }
|
||
//// var_dump($data['sender']);die;
|
||
// return $RobotService->send_text('测试自动回复 @'.$ac,$data['roomid'],$data['sender']);
|
||
// }
|
||
|
||
|
||
|
||
|
||
if( str_contains($data['xml'],'wxid_9iv1hha8g3ok29')){
|
||
$merchantService = MerchantService::instance();
|
||
$check_merchant = $merchantService->db()->whereLike('other_param', '%'.$roomid.'%')->findOrEmpty();
|
||
if($check_merchant){
|
||
$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'], '通知全部 ');
|
||
if ($newString) {
|
||
$newString = substr($newString, strlen('通知全部 '));
|
||
}else{
|
||
$newString = strstr($data['content'], '通知 ');
|
||
if ($newString) {
|
||
$newString = substr($newString, strlen('通知 '));
|
||
}else{
|
||
$this->response(['code'=>1]);
|
||
}
|
||
}
|
||
$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;
|
||
foreach ($merchantsList as $merchant){
|
||
$other_param = json_decode($merchant['other_param'],true);
|
||
$roomids = $other_param['QYWX_roomid'];
|
||
$sender = $other_param['QYWX_sender'];
|
||
|
||
if($roomids == $roomid){
|
||
continue;
|
||
}
|
||
// $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++;
|
||
|
||
}
|
||
$merchant_name = implode("\n", $tip_info);
|
||
return $RobotService->send_text('通知完毕 共通知了'.$num."个商户:\n".$merchant_name.'@'.$ac,$data['roomid'],$data['sender']);
|
||
}
|
||
if(str_ends_with($data['content'], '余额') ){
|
||
$channelService = ChannelService::instance();
|
||
|
||
$channelList = $channelService->db()->where(['status'=>1])->whereLike('other_data','%Monitor_Balance_status%')->select()->toArray();
|
||
if(!$channelList)return $RobotService->send_text('暂无可查询渠道@'.$ac,$data['roomid'],$data['sender']);
|
||
$count_sum= 0;
|
||
$day = date('Y-m-d', strtotime('-1 day'));
|
||
$tip_info=[];
|
||
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'];
|
||
$tip_info[]='渠道:['.$channel['name']."] \n余额:【".$balance.'】元';
|
||
$count_sum++;
|
||
}
|
||
}catch (\Exception $e){
|
||
|
||
}
|
||
}
|
||
if(!$tip_info)return $RobotService->send_text('暂无可查询渠道@'.$ac,$data['roomid'],$data['sender']);
|
||
|
||
$errorMsg = implode("\n", $tip_info);
|
||
return $RobotService->send_text("渠道余额查询完毕,共查询了".$count_sum."个渠道:\n".$errorMsg."\n@".$ac,$data['roomid'],$data['sender']);
|
||
}
|
||
|
||
}
|
||
if(str_ends_with($data['content'], '余额') || str_ends_with($data['content'],'查余额')){
|
||
return $RobotService->send_text("商户[".$check_merchant['name']."] : \n 余额为:".$check_merchant['account_surplus']." @".$ac,$data['roomid'],$data['sender']);
|
||
}
|
||
|
||
if(str_starts_with($data['content'], '@发财-庚辰小秘1')||str_starts_with($data['content'], '@发财-庚辰小秘 1')){
|
||
$newString = strstr($data['content'], '1');
|
||
|
||
if (preg_match('/\b1[3-9]\d{9}\b/', $newString)) {
|
||
// Log::write('Robot', 'wechat', $data);
|
||
#查询订单
|
||
$msg = [];
|
||
$orderService = OrderService::instance();
|
||
$order = $orderService->db()->field('cid,pid,mid,order_id,merchant_order_id,param,account,cash,status,channel_callback_at,create_at')->where(['account'=>$newString,'mid'=>$check_merchant['id']])->select()->toArray();
|
||
if(!$order){
|
||
$orderLastweekHistoryService = OrderLastweekHistoryService::instance();
|
||
$order = $orderLastweekHistoryService->db()->field('cid,pid,mid,order_id,merchant_order_id,param,account,cash,status,channel_callback_at,create_at')->where(['account'=>$newString,'mid'=>$check_merchant['id']])->select()->toArray();
|
||
}
|
||
if(!$order)return $RobotService->send_text("手机号格式正确,但是未找到订单,暂时支持当天和最近一周的订单筛选,更多请前往后台查询 @".$ac,$data['roomid'],$data['sender']);
|
||
if (count($order) == 1) {
|
||
$orders = $order[0];
|
||
$infos =$orderService->getOrderInfo($orders['cid'],$orders['mid'],$orders['pid']);
|
||
$msg = [
|
||
'系统订单号:' . $orders['order_id'],
|
||
'产品:' . $infos['p']['name']??'',
|
||
'商户订单号:' . $orders['merchant_order_id'],
|
||
'手机号:' . $orders['account'],
|
||
'订单金额:' . $orders['cash'],
|
||
'订单状态:' . ($orders['status'] == 2 ? '成功' : ($orders['status'] == 3 ? '失败' : '处理中')),
|
||
'订单时间:' . $orders['create_at'],
|
||
];
|
||
if(str_contains($orders['param'], 'mnp_isp_array')){
|
||
$param_data = json_decode($orders['param'],true);
|
||
|
||
if($param_data['mnp_isp_array']['old_isp'] == $param_data['mnp_isp_array']['new_isp']){
|
||
$msg[] = '携转检测:未携转';
|
||
}else{
|
||
$msg[] = '携转检测:已携转';
|
||
$isp_name = [
|
||
1=>'移动',
|
||
2=>'联通',
|
||
3=>'电信',
|
||
];
|
||
|
||
$msg[] = '旧运营商:'.$isp_name[$param_data['mnp_isp_array']['old_isp']??0] ?? '';
|
||
$msg[] = '新运营商:'.$isp_name[$param_data['mnp_isp_array']['new_isp']??0] ?? '';
|
||
}
|
||
}
|
||
if(isset($orders['channel_callback_at']) && $orders['channel_callback_at']){
|
||
$msg[] = '完成时间:'.$orders['channel_callback_at'];
|
||
}
|
||
$m_msg= implode("\n", $msg);
|
||
return $RobotService->send_text("已为您查询到订单信息:\n".$m_msg." \n@".$ac, $data['roomid'], $data['sender']);
|
||
|
||
} else {
|
||
return $RobotService->send_text("手机号格式正确,查询到多条订单,暂时只支持单条查询,更多请前往后台查询 @".$ac,$data['roomid'],$data['sender']);
|
||
// foreach ($order as $singleOrder) {
|
||
// // 在这里处理每一条订单数据
|
||
// // 例如:$RobotService->send_text("订单信息: " . $singleOrder['some_field'], $data['roomid'], $data['sender']);
|
||
// }
|
||
}
|
||
|
||
}
|
||
}
|
||
|
||
if(str_starts_with($data['content'], '@发财-庚辰小秘检测 ')||str_starts_with($data['content'], '@发财-庚辰小秘 检测 ')){
|
||
$newString = strstr($data['content'], '1');
|
||
|
||
|
||
if (!preg_match('/\b1[3-9]\d{9}\b/', $newString)) return $RobotService->send_text("暂时只支持手机号码检测,其他请联系管理员 @".$ac,$data['roomid'],$data['sender']);
|
||
|
||
|
||
$settingPhoneMNPStatus = sysconf('settingPhoneMNPStatus');
|
||
if ($settingPhoneMNPStatus == 1) {
|
||
$mnp_cid = sysconf('PhoneMnp_cid');
|
||
$param = [
|
||
'mobile' => $newString,
|
||
];
|
||
if ($mnp_cid) {
|
||
$mnp_array = ChannelService::instance()->call('phone_mnp', $mnp_cid, $param);
|
||
|
||
|
||
if (is_array($mnp_array) && isset($mnp_array['new_isp'])) {
|
||
$msg = [
|
||
'手机号:' . $newString,
|
||
'是否携转:' . ($mnp_array['new_isp'] == $mnp_array['old_isp'] ? '否' : '是'),
|
||
'旧运营商:' . ($mnp_array['old_isp'] == 1 ? '移动' : ($mnp_array['old_isp'] == 2 ? '联通' : '电信')),
|
||
'新运营商:' . ($mnp_array['new_isp'] == 1 ? '移动' : ($mnp_array['new_isp'] == 2 ? '联通' : '电信')),
|
||
];
|
||
$m_msg= implode("\n", $msg);
|
||
return $RobotService->send_text("已经为您查询到手机号信息:\n".$m_msg." \n@".$ac, $data['roomid'], $data['sender']);
|
||
|
||
}
|
||
|
||
|
||
}
|
||
|
||
}
|
||
|
||
$isp = Service::instance()->isp($newString);
|
||
$msg = [
|
||
'手机号:' . $newString,
|
||
'是否携转:' . '未携转或者无法查询',
|
||
'运营商:' . ($isp == 1 ? '移动' : ($isp == 2 ? '联通' : '电信')),
|
||
|
||
];
|
||
$m_msg= implode("\n", $msg);
|
||
return $RobotService->send_text("已经为您查询到手机号信息:\n".$m_msg." \n@", $data['roomid'], $data['sender']);
|
||
|
||
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
return $RobotService->send_text('测试自动回复 @'.$ac,$data['roomid'],$data['sender']);
|
||
}
|
||
}
|
||
|
||
|
||
$this->response(['code'=>1]);
|
||
}
|
||
|
||
|
||
$this->response($data);
|
||
|
||
}
|
||
|
||
|
||
|
||
public function response($result)
|
||
{
|
||
throw new HttpResponseException(json($result));
|
||
|
||
}
|
||
|
||
protected function maketime($v): float|false|int|string
|
||
{
|
||
if (!$v) {
|
||
return '';
|
||
}
|
||
|
||
if (is_numeric($v)) {
|
||
return $v;
|
||
}
|
||
|
||
if (is_array($v)) {
|
||
$v = $v[1];
|
||
}
|
||
|
||
if (strstr($v, ' ')) {
|
||
$t = explode(' ', $v);
|
||
$v = $t[0];
|
||
$s = explode(':', $t[1]);
|
||
} else {
|
||
$s = array(0, 0, 0);
|
||
}
|
||
|
||
if (!isset($s[1])) {
|
||
$s[1] = 0;
|
||
}
|
||
|
||
if (!isset($s[2])) {
|
||
$s[2] = 0;
|
||
}
|
||
|
||
if (strstr($v, '-')) {
|
||
$t = explode('-', $v);
|
||
} elseif (strstr($v, '/')) {
|
||
$u = explode('/', $v);
|
||
$t[0] = $u[2];
|
||
$t[1] = $u[0];
|
||
$t[2] = $u[1];
|
||
}
|
||
|
||
if (!isset($t)) {
|
||
$t = array(0, 0, 0);
|
||
}
|
||
|
||
if (!isset($t[1])) {
|
||
$t[1] = 0;
|
||
}
|
||
|
||
if (!isset($t[2])) {
|
||
$t[2] = 0;
|
||
}
|
||
|
||
$v = mktime($s[0], $s[1], $s[2], $t[1], $t[2], $t[0]);
|
||
|
||
return $v;
|
||
}
|
||
|
||
} |