优化代码

main
gzydong 2021-07-25 22:57:41 +08:00
parent aca952f079
commit d75c33f9ec
11 changed files with 203 additions and 5 deletions

View File

@ -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='聊天对话记录(登录日志)';

View File

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

View File

@ -0,0 +1,16 @@
<?php
namespace App\Constants;
/**
* 定义系统机器人ID
*
* @package App\Constants
*/
class RobotConstant
{
// 登录机器人
const LOGIN_ROBOT = 1;
}

View File

@ -12,6 +12,8 @@ class IndexController extends AbstractController
$user = $this->request->input('user', 'Hyperf');
$method = $this->request->getMethod();
var_dump($this->request->all());
return [
'method' => $method,
'message' => "Hello {$user}.",

View File

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

View File

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

View File

@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
namespace App\Model\Talk;
use App\Model\BaseModel;
/**
* Class TalkRecordsLogin
*
* @property int $record_id 消息记录ID
* @property int $user_id 用户ID
* @property int $ip 登录IP
* @property int $platform 登录平台
* @property int $agent 设备信息
* @property int $address 登录地址
* @property int $reason 异常信息
* @property int $created_at 登录时间
* @package App\Model\Talk
*/
class TalkRecordsLogin extends BaseModel
{
protected $table = 'talk_records_login';
protected $fillable = [
'record_id',
'user_id',
'ip',
'platform',
'agent',
'address',
'reason',
'created_at',
];
protected $casts = [
'record_id' => 'integer',
'user_id' => 'integer',
];
}

View File

@ -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 投票选项

View File

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

View File

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

View File

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