一、修改Redis配置文件
将redis.conf里面的notify-keyspace-events参数对应的值改为Ex,具体代码如下:
notify-keyspace-events Ex
二、创建控制台命令
在根目录下config文件夹下找到console.php文件修改,具体代码如下:
<?php
// +----------------------------------------------------------------------
// | 控制台配置
// +----------------------------------------------------------------------
return [
// 指令定义
'commands' => [
'redisClusterControlApiExpireToken' => 'app\command\ExpireToken',
],
];
三、创建数据库Redis配置文件
在根目录下config文件夹下创建rabbitmq.php,输入如下代码:
<?php
/**
* rabbitmq配置文件
* User: 龙哥·三年风水
* Date: 2024/12/11
* Time: 14:47
*/
return [
'is_formal' => '1',//正式环境时修改为0,测试环境时修改为1
'redis' => [
[
'host' => "192.168.0.157",
'port' => 6379,
'password' => "QXtr@@PxjoLenGon",
'select' => 126,
'temporary' => 125
],
[
'host' => "172.20.36.144",
'port' => 6379,
'password' => "QXtr@@PxjoLenGon",
'select' => 126,
'temporary' => 124
]
],
'mysql' => [
[
'host' => "192.168.0.161",
'port' => "3306",
'user' => "bjhy_base",
'password' => "jZ9e@F52V%pZ6RqP",
'dbname' => "sl_haoyou_base"
],
[
'host' => "172.20.36.143",
'port' => "3306",
'user' => "cluster_control",
'password' => "7%Qh3ar@TiA5Rt#gNPT",
'dbname' => "cluster_control"
]
],
];
四、创建命令文件
在根目录下app文件夹下创建command文件夹,创建控制台命令文件并命名ExpireToken,代码如下:
<?php
/**
* 过期清理token命令
* User: 龙哥·三年风水
* Date: 2024/11/13
* Time: 10:15
*/
namespace app\command;
use PHPMailer\PHPMailer\PHPMailer;
use think\console\Command;
use think\console\Input;
use think\console\Output;
class ExpireToken extends Command
{
protected function configure(){
// 指令配置
$this->setName('redisClusterControlApiExpireToken')->setDescription('the redisClusterControlApiExpireToken command');
}
protected function execute(Input $input, Output $output){
$isFormal = config('rabbitmq.is_formal');
//获取配置文件
$redisConfig = config("rabbitmq.redis.{$isFormal}");
$redis = new \Redis();
$redis->connect($redisConfig['host'],$redisConfig['port']);
$redis->auth($redisConfig['password']);
$redis->setOption(\Redis::OPT_READ_TIMEOUT, -1);
$redis->psubscribe(array("__keyevent@{$redisConfig['select']}__:expired"), function ($redis, $pattern, $channel, $msg){
$res = explode('_', $msg);
if(count($res) == 2){
$expireTime = time();
$isFormal = config('rabbitmq.is_formal');
$mysqlConfig = config("rabbitmq.mysql.{$isFormal}");
$dbconnect = new \PDO("mysql:host={$mysqlConfig['host']};dbname={$mysqlConfig['dbname']}", $mysqlConfig['user'], $mysqlConfig['password']);
if(!is_numeric($res[1])){//修改token退出时间
$sqlTokenUpdate = "Update nc_permission_token SET expire_time = {$expireTime} WHERE token = '{$res[1]}'";
$dbconnect->exec($sqlTokenUpdate);
}
if(is_numeric($res[1])){//向对应的管理员发送电子邮件
$sqlAdminFind = "SELECT `realname`,`email` FROM nc_permission_admin WHERE id = '{$res[1]}'";
$retAdminData = $dbconnect->query($sqlAdminFind)->fetch();
if(!empty($retAdminData)){
$sqlMailFind = "SELECT `username`,`account`,`smtp_address`,`smtp_port`,`smtp_password`,`smtp_protocol` FROM nc_param_mail WHERE id = 1";
$retMailData = $dbconnect->query($sqlMailFind)->fetch();
$expireDateTime = date("Y-m-d H:i:s",$expireTime);
$emailContent = $retAdminData['realname'].'于'.$expireDateTime.'自动退出系统';
$phpMailer = new PHPMailer(true);
$phpMailer->isSMTP();// 使用SMTP服务
$phpMailer->CharSet = 'utf8';// 编码格式为utf8,不设置编码的话,中文会出现乱码
$phpMailer->Host = $retMailData['smtp_address'];// 发送人的SMTP服务器地址
$phpMailer->SMTPAuth = true;// 是否使用身份验证
$phpMailer->Username = $retMailData['account'];// SMTP账号
$phpMailer->Password = $retMailData['smtp_password'];// SMTP密码
$phpMailer->SMTPSecure = $retMailData['smtp_protocol'];// 使用ssl协议方式
$phpMailer->Port = $retMailData['smtp_port'];// ssl协议方式端口号是465
$phpMailer->setFrom($retMailData['account'],$retMailData['username']);// 设置发件人信息,如邮件格式说明中的发件人,这里会显示为
$phpMailer->addAddress($retAdminData['email'],'自动退出系统');// 设置收件人信息,如邮件格式说明中的收件人
$phpMailer->isHTML(true);
$phpMailer->Subject = '自动退出系统';
$phpMailer->Body = $emailContent;
$phpMailer->send();
if(!empty($phpMailer->ErrorInfo)){
file_put_contents('redis_cluster_control_api_expire_token.txt', json_encode($phpMailer->ErrorInfo) . PHP_EOL, FILE_APPEND);
}
}
}
}
});
}
}
五、在环境里面执行命令
php think redisClusterControlApiExpireToken &