161 lines
3.8 KiB
PHP
161 lines
3.8 KiB
PHP
<?php
|
||
|
||
namespace app\private_api\controller\robotApi;
|
||
|
||
use app\channel\service\ChannelService;
|
||
use app\gateway\service\RedisService;
|
||
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'];
|
||
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']);
|
||
}
|
||
|
||
$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;
|
||
}
|
||
|
||
} |