优化代码

main
gzydong 2021-09-12 21:15:38 +08:00
parent 5f669d156b
commit b42523a25f
7 changed files with 83 additions and 83 deletions

View File

@ -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));
}

View File

@ -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,
// ]);
}

View File

@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
namespace App\Repository;
use App\Model\Robot;
class RobotRepository extends BaseRepository
{
public function __construct(Robot $model)
{
parent::__construct($model);
}
/**
* 通过机器人类型获取关联ID
*
* @param int $type
* @return int
*/
public function findTypeByUserId(int $type): int
{
return (int)$this->value(['type' => $type], 'user_id');
}
}

View File

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
namespace App\Service;
@ -10,17 +11,6 @@ use Hyperf\Utils\Str;
class RobotService
{
/**
* 获取机器人关联的用户ID
*
* @param int $robot_type
* @return int
*/
public function getRootUserID(int $robot_type): int
{
return (int)Robot::where('type', $robot_type)->value('user_id');
}
/**
* 创建机器人
*

View File

@ -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;
}
}

View File

@ -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');

View File

@ -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,