From b42523a25f8a9aed36906bea574669fcac716f70 Mon Sep 17 00:00:00 2001 From: gzydong <837215079@qq.com> Date: Sun, 12 Sep 2021 21:15:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Cache/IpAddressCache.php | 2 +- app/Command/TestCommand.php | 16 +++--- app/Repository/RobotRepository.php | 25 +++++++++ app/Service/RobotService.php | 12 +--- app/Service/TalkMessageService.php | 89 ++++++++++-------------------- app/Support/IpAddress.php | 16 +++++- app/Support/Response.php | 6 +- 7 files changed, 83 insertions(+), 83 deletions(-) create mode 100644 app/Repository/RobotRepository.php diff --git a/app/Cache/IpAddressCache.php b/app/Cache/IpAddressCache.php index 8e11cd0..d363158 100644 --- a/app/Cache/IpAddressCache.php +++ b/app/Cache/IpAddressCache.php @@ -19,7 +19,7 @@ class IpAddressCache extends HashRedis return JsonHelper::decode($result); } - $result = di()->get(IpAddress::class)->read($ip); + $result = di()->get(IpAddress::class)->get($ip); if (!empty($result)) { $this->add($ip, JsonHelper::encode($result)); } diff --git a/app/Command/TestCommand.php b/app/Command/TestCommand.php index 002d000..fe36510 100644 --- a/app/Command/TestCommand.php +++ b/app/Command/TestCommand.php @@ -4,8 +4,10 @@ declare(strict_types=1); namespace App\Command; use App\Cache\IpAddressCache; +use App\Constants\RobotConstant; use App\Model\UsersFriend; use App\Repository\ExampleRepository; +use App\Repository\RobotRepository; use App\Service\RobotService; use App\Support\IpAddress; use Hyperf\Command\Command as HyperfCommand; @@ -40,13 +42,13 @@ class TestCommand extends HyperfCommand // $repository = di()->get(ExampleRepository::class); // $repository->where_case2(); - di()->get(RobotService::class)->create([ - 'robot_name' => "登录助手", - 'describe' => "异地登录助手", - 'logo' => '', - 'is_talk' => 0, - 'type' => 1, - ]); + // di()->get(RobotService::class)->create([ + // 'robot_name' => "登录助手", + // 'describe' => "异地登录助手", + // 'logo' => '', + // 'is_talk' => 0, + // 'type' => 1, + // ]); } diff --git a/app/Repository/RobotRepository.php b/app/Repository/RobotRepository.php new file mode 100644 index 0000000..2f893c8 --- /dev/null +++ b/app/Repository/RobotRepository.php @@ -0,0 +1,25 @@ +value(['type' => $type], 'user_id'); + } +} diff --git a/app/Service/RobotService.php b/app/Service/RobotService.php index 0c01e49..9d36531 100644 --- a/app/Service/RobotService.php +++ b/app/Service/RobotService.php @@ -1,4 +1,5 @@ value('user_id'); - } - /** * 创建机器人 * diff --git a/app/Service/TalkMessageService.php b/app/Service/TalkMessageService.php index c3aed57..d593216 100644 --- a/app/Service/TalkMessageService.php +++ b/app/Service/TalkMessageService.php @@ -17,6 +17,7 @@ use App\Model\Talk\TalkRecordsCode; use App\Model\Talk\TalkRecordsLogin; use App\Model\Talk\TalkRecordsVote; use App\Model\Talk\TalkRecordsVoteAnswer; +use App\Repository\RobotRepository; use App\Support\UserRelation; use Exception; use App\Constants\MediaTypeConstant; @@ -41,23 +42,7 @@ class TalkMessageService $result = TalkRecords::create($message); - // 判断是否私信 - if ($result->talk_type == TalkModeConstant::PRIVATE_CHAT) { - UnreadTalkCache::getInstance()->increment($result->user_id, $result->receiver_id); - } - - // 缓存最后一条聊天消息 - LastMessage::getInstance()->save($result->talk_type, $result->user_id, $result->receiver_id, [ - 'text' => mb_substr($result->content, 0, 30), - 'created_at' => date('Y-m-d H:i:s') - ]); - - event()->dispatch(new TalkEvent(TalkEventConstant::EVENT_TALK, [ - 'sender_id' => $result->user_id, - 'receiver_id' => $result->receiver_id, - 'talk_type' => $result->talk_type, - 'record_id' => $result->id - ])); + $this->handle($result, ['text' => mb_substr($result->content, 0, 30)]); return true; } @@ -94,17 +79,7 @@ class TalkMessageService return false; } - LastMessage::getInstance()->save($insert->talk_type, $insert->user_id, $insert->receiver_id, [ - 'text' => '[代码消息]', - 'created_at' => date('Y-m-d H:i:s') - ]); - - event()->dispatch(new TalkEvent(TalkEventConstant::EVENT_TALK, [ - 'sender_id' => $insert->user_id, - 'receiver_id' => $insert->receiver_id, - 'talk_type' => $insert->talk_type, - 'record_id' => $insert->id - ])); + $this->handle($insert, ['text' => '[代码消息]']); return true; } @@ -142,17 +117,7 @@ class TalkMessageService return false; } - LastMessage::getInstance()->save($insert->talk_type, $insert->user_id, $insert->receiver_id, [ - 'text' => '[图片消息]', - 'created_at' => date('Y-m-d H:i:s') - ]); - - event()->dispatch(new TalkEvent(TalkEventConstant::EVENT_TALK, [ - 'sender_id' => $insert->user_id, - 'receiver_id' => $insert->receiver_id, - 'talk_type' => $insert->talk_type, - 'record_id' => $insert->id - ])); + $this->handle($insert, ['text' => '[图片消息]']); return true; } @@ -195,17 +160,7 @@ class TalkMessageService return false; } - LastMessage::getInstance()->save($insert->talk_type, $insert->user_id, $insert->receiver_id, [ - 'text' => '[投票消息]', - 'created_at' => date('Y-m-d H:i:s') - ]); - - event()->dispatch(new TalkEvent(TalkEventConstant::EVENT_TALK, [ - 'sender_id' => $insert->user_id, - 'receiver_id' => $insert->receiver_id, - 'talk_type' => $insert->talk_type, - 'record_id' => $insert->id - ])); + $this->handle($insert, ['text' => '[投票消息]']); return true; } @@ -279,6 +234,7 @@ class TalkMessageService // 更新投票缓存 VoteCache::getInstance()->updateCache($record->vote_id); + $cache = VoteStatisticsCache::getInstance()->updateVoteCache($record->vote_id); // todo 推送消息 @@ -295,7 +251,7 @@ class TalkMessageService */ public function insertLogin(array $message, array $loginParams): bool { - $user_id = di()->get(RobotService::class)->getRootUserID(RobotConstant::LOGIN_ROBOT); + $user_id = di()->get(RobotRepository::class)->findTypeByUserId(RobotConstant::LOGIN_ROBOT); if ($user_id == 0) return false; @@ -328,18 +284,33 @@ class TalkMessageService // 创建对话列表 di()->get(TalkListService::class)->create($insert->receiver_id, $insert->user_id, $insert->talk_type, true); - LastMessage::getInstance()->save($insert->talk_type, $insert->user_id, $insert->receiver_id, [ - 'text' => '[登录提醒]', + $this->handle($insert, ['text' => '[登录提醒]']); + + return true; + } + + /** + * 处理数据 + * + * @param TalkRecords $record + * @param array $option + */ + private function handle(TalkRecords $record, array $option = []): void + { + if ($record->talk_type == TalkModeConstant::PRIVATE_CHAT) { + UnreadTalkCache::getInstance()->increment($record->user_id, $record->receiver_id); + } + + LastMessage::getInstance()->save($record->talk_type, $record->user_id, $record->receiver_id, [ + 'text' => $option['text'], 'created_at' => date('Y-m-d H:i:s') ]); event()->dispatch(new TalkEvent(TalkEventConstant::EVENT_TALK, [ - 'sender_id' => $insert->user_id, - 'receiver_id' => $insert->receiver_id, - 'talk_type' => $insert->talk_type, - 'record_id' => $insert->id + 'sender_id' => $record->user_id, + 'receiver_id' => $record->receiver_id, + 'talk_type' => $record->talk_type, + 'record_id' => $record->id ])); - - return true; } } diff --git a/app/Support/IpAddress.php b/app/Support/IpAddress.php index 7ad97c0..67c95a5 100644 --- a/app/Support/IpAddress.php +++ b/app/Support/IpAddress.php @@ -7,12 +7,24 @@ use Hyperf\Guzzle\ClientFactory; class IpAddress { - public function read(string $ip) + /** + * 获取IP地址信息 + * + * @param string $ip + * @return array + */ + public function get(string $ip): array { return $this->request($ip); } - private function request(string $ip) + /** + * 请求聚合数据IP查询接口 + * + * @param string $ip + * @return array + */ + private function request(string $ip): array { $api = config('juhe_api.ip'); diff --git a/app/Support/Response.php b/app/Support/Response.php index 3e7b386..39ac7db 100644 --- a/app/Support/Response.php +++ b/app/Support/Response.php @@ -34,7 +34,7 @@ class Response * @param string $message 响应提示 * @return PsrResponseInterface */ - public function success(array $data = [], $message = 'success'): PsrResponseInterface + public function success(array $data = [], string $message = 'success'): PsrResponseInterface { $code = ResponseCode::SUCCESS; return $this->response->json(compact('code', 'message', 'data')); @@ -48,7 +48,7 @@ class Response * @param int $code 错误码 * @return PsrResponseInterface */ - public function fail($message = 'fail', $data = [], $code = ResponseCode::FAIL): PsrResponseInterface + public function fail(string $message = 'fail', array $data = [], $code = ResponseCode::FAIL): PsrResponseInterface { return $this->response->json(compact('code', 'message', 'data')); } @@ -60,7 +60,7 @@ class Response * @param int $code 错误码 * @return PsrResponseInterface */ - public function error($message = '', $code = ResponseCode::SERVER_ERROR): PsrResponseInterface + public function error(string $message = '', $code = ResponseCode::SERVER_ERROR): PsrResponseInterface { return $this->response->withStatus(500)->json([ 'code' => $code,