diff --git a/LumenIM 同步SQL.sql b/LumenIM 同步SQL.sql index c3451dd..ce61447 100644 --- a/LumenIM 同步SQL.sql +++ b/LumenIM 同步SQL.sql @@ -112,3 +112,19 @@ CREATE TABLE `lar_talk_records_vote_answer` ( PRIMARY KEY (`id`), KEY `idx_vote_id_user_id` (`vote_id`,`user_id`) USING BTREE ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='聊天对话记录(投票消息统计表)'; + + +CREATE TABLE `lar_talk_records_login` ( + `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '登录ID', + `record_id` int(11) unsigned DEFAULT '0' COMMENT '消息记录ID', + `user_id` int(11) unsigned DEFAULT '0' COMMENT '用户ID', + `ip` varchar(20) NOT NULL DEFAULT '' COMMENT 'IP地址', + `platform` varchar(20) NOT NULL DEFAULT '' COMMENT '登录平台[h5,ios,windows,mac,web]', + `agent` varchar(255) NOT NULL DEFAULT '' COMMENT '设备信息', + `address` varchar(100) NOT NULL DEFAULT '' COMMENT 'IP所在地', + `reason` varchar(100) NOT NULL DEFAULT '' COMMENT '登录异常提示', + `created_at` datetime NOT NULL COMMENT '登录时间', + PRIMARY KEY (`id`), + UNIQUE KEY `uk_record_id` (`record_id`) USING BTREE, + KEY `idx_user_id` (`user_id`) USING BTREE +) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COMMENT='聊天对话记录(登录日志)'; diff --git a/app/Command/TestCommand.php b/app/Command/TestCommand.php index 9882b0a..059ef4f 100644 --- a/app/Command/TestCommand.php +++ b/app/Command/TestCommand.php @@ -10,6 +10,8 @@ use Hyperf\Command\Command as HyperfCommand; use Hyperf\Command\Annotation\Command; use Hyperf\DbConnection\Db; use Psr\Container\ContainerInterface; +use Hyperf\Guzzle\ClientFactory; +use function _HumbugBox39a196d4601e\RingCentral\Psr7\build_query; /** * @Command @@ -36,6 +38,26 @@ class TestCommand extends HyperfCommand public function handle() { - VoteStatisticsCache::getInstance()->updateVoteCache(15); + //VoteStatisticsCache::getInstance()->updateVoteCache(15); + + $api = config('juhe_api.ip'); + $options = []; + $client = di()->get(ClientFactory::class)->create($options); + $params = [ + 'ip' => '47.105.180.123', + 'key' => $api['key'], + ]; + + $address = ''; + $response = $client->get($api['api'] . '?' . http_build_query($params)); + if ($response->getStatusCode() == 200) { + $result = json_decode($response->getBody()->getContents(), true); + if ($result['resultcode'] == 200) { + unset($result['result']['Isp']); + $address = join(' ', $result['result']); + } + } + + var_dump($address); } } diff --git a/app/Constants/RobotConstant.php b/app/Constants/RobotConstant.php new file mode 100644 index 0000000..085fdd2 --- /dev/null +++ b/app/Constants/RobotConstant.php @@ -0,0 +1,16 @@ +request->input('user', 'Hyperf'); $method = $this->request->getMethod(); + var_dump($this->request->all()); + return [ 'method' => $method, 'message' => "Hello {$user}.", diff --git a/app/Event/LoginEvent.php b/app/Event/LoginEvent.php index 48fe5a3..370df23 100644 --- a/app/Event/LoginEvent.php +++ b/app/Event/LoginEvent.php @@ -27,6 +27,11 @@ class LoginEvent */ public $ip; + /** + * @var string + */ + public $agent; + /** * UserLogin constructor. * @@ -39,6 +44,8 @@ class LoginEvent $this->platform = $request->input('platform', ''); + $this->agent = $request->getHeaderLine('user-agent'); + $this->ip = $this->getClientRealIp($request); } diff --git a/app/Listener/LoginListener.php b/app/Listener/LoginListener.php index a3c722a..f2ac974 100644 --- a/app/Listener/LoginListener.php +++ b/app/Listener/LoginListener.php @@ -3,9 +3,11 @@ declare(strict_types=1); namespace App\Listener; +use App\Service\TalkMessageService; use Hyperf\Event\Contract\ListenerInterface; use App\Event\LoginEvent; use Hyperf\Event\Annotation\Listener; +use Hyperf\Guzzle\ClientFactory; /** * @Listener @@ -25,7 +27,35 @@ class LoginListener implements ListenerInterface */ public function process(object $event) { - // 推送登录提示信息 - stdout_log()->notice('登录事件回调 ' . $event->user->mobile . ':' . $event->platform . ':' . $event->ip); + //$reason = '正常登录'; + //$address = ''; + // + //$api = config('juhe_api.ip'); + // + //$client = di()->get(ClientFactory::class)->create([]); + //$params = [ + // 'ip' => $event->ip, + // 'key' => $api['key'], + //]; + // + //$response = $client->get($api['api'] . '?' . http_build_query($params)); + //if ($response->getStatusCode() == 200) { + // $result = json_decode($response->getBody()->getContents(), true); + // if ($result['resultcode'] == 200) { + // unset($result['result']['Isp']); + // $address = join(' ', $result['result']); + // } + //} + // + //di()->get(TalkMessageService::class)->insertLoginMessage([ + // 'user_id' => $event->user->id, + //], [ + // 'user_id' => $event->user->id, + // 'ip' => $event->ip, + // 'platform' => $event->platform, + // 'agent' => $event->agent, + // 'address' => $address, + // 'reason' => $reason, + //]); } } diff --git a/app/Model/Talk/TalkRecordsLogin.php b/app/Model/Talk/TalkRecordsLogin.php new file mode 100644 index 0000000..b0e26d6 --- /dev/null +++ b/app/Model/Talk/TalkRecordsLogin.php @@ -0,0 +1,40 @@ + 'integer', + 'user_id' => 'integer', + ]; +} diff --git a/app/Model/Talk/TalkRecordsVote.php b/app/Model/Talk/TalkRecordsVote.php index 6b1f90f..60a0a41 100644 --- a/app/Model/Talk/TalkRecordsVote.php +++ b/app/Model/Talk/TalkRecordsVote.php @@ -11,7 +11,7 @@ use App\Model\BaseModel; * * @property int $id * @property int $record_id 消息记录ID - * @property int $user_id 通知类型[1:入群通知;2:自动退群;3:管理员踢群] + * @property int $user_id 用户ID * @property string $title 投票标题 * @property int $answer_mode 投票模式 * @property string $answer_option 投票选项 diff --git a/app/Service/TalkListService.php b/app/Service/TalkListService.php index 778918d..fb8815a 100644 --- a/app/Service/TalkListService.php +++ b/app/Service/TalkListService.php @@ -19,7 +19,7 @@ class TalkListService * @param int $talk_type 创建类型[1:私聊;2:群聊;] * @return array */ - public function create(int $user_id, int $receiver_id, int $talk_type) + public function create(int $user_id, int $receiver_id, int $talk_type, bool $is_robot = false) { $result = TalkList::updateOrCreate([ 'talk_type' => $talk_type, @@ -29,6 +29,7 @@ class TalkListService 'is_top' => 0, 'is_delete' => 0, 'is_disturb' => 0, + 'is_robot' => intval($is_robot), 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s') ]); diff --git a/app/Service/TalkMessageService.php b/app/Service/TalkMessageService.php index d9ef802..3e8fc4a 100644 --- a/app/Service/TalkMessageService.php +++ b/app/Service/TalkMessageService.php @@ -5,11 +5,15 @@ namespace App\Service; use App\Cache\LastMessage; use App\Cache\VoteCache; use App\Cache\VoteStatisticsCache; +use App\Constants\RobotConstant; use App\Constants\TalkEventConstant; use App\Constants\TalkMessageType; +use App\Constants\TalkModeConstant; use App\Event\TalkEvent; use App\Model\Group\GroupMember; +use App\Model\Talk\TalkList; use App\Model\Talk\TalkRecordsCode; +use App\Model\Talk\TalkRecordsLogin; use App\Model\Talk\TalkRecordsVote; use App\Model\Talk\TalkRecordsVoteAnswer; use App\Support\UserRelation; @@ -244,4 +248,57 @@ class TalkMessageService return [true, $cache]; } + + /** + * 添加登录消息 + * + * @param array $message + * @param array $loginParams + * @return bool + */ + public function insertLoginMessage(array $message, array $loginParams) + { + Db::beginTransaction(); + try { + $message['receiver_id'] = RobotConstant::LOGIN_ROBOT; + $message['talk_type'] = TalkModeConstant::PRIVATE_CHAT; + $message['msg_type'] = TalkMessageType::USER_LOGIN_MESSAGE; + $message['created_at'] = date('Y-m-d H:i:s'); + $message['updated_at'] = date('Y-m-d H:i:s'); + + $insert = TalkRecords::create($message); + if (!$insert) { + throw new Exception('插入聊天记录失败...'); + } + + $loginParams['record_id'] = $insert->id; + $loginParams['created_at'] = date('Y-m-d H:i:s'); + + if (!TalkRecordsLogin::create($loginParams)) { + throw new Exception('插入聊天记录(登录消息)失败...'); + } + + Db::commit(); + } catch (Exception $e) { + Db::rollBack(); + return false; + } + + // 创建对话列表 + di()->get(TalkListService::class)->create($insert->user_id, $insert->receiver_id, $insert->talk_type, true); + + 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 + ])); + + return true; + } } diff --git a/config/config.php b/config/config.php index a575a0e..5d8b776 100644 --- a/config/config.php +++ b/config/config.php @@ -28,6 +28,13 @@ return [ // 管理员邮箱 'admin_email' => env('ADMIN_EMAIL', ''), + 'juhe_api' => [ + 'ip' => [ + 'api' => 'http://apis.juhe.cn/ip/ipNew', + 'key' => env('JUHE_IP_KEY', '') + ], + ], + StdoutLoggerInterface::class => [ 'log_level' => [ LogLevel::ALERT,