This commit is contained in:
ASUS 2025-10-16 19:04:48 +08:00
parent b470bf90db
commit 3fb7712f29
6 changed files with 127 additions and 80 deletions

View File

@ -44,8 +44,10 @@ class ApplySmsTemp extends Command
// p(replace_json_keys($a, json_encode($templateContent_arr, 256), []));
// $t_str = replace_placeholder_keys($str, $templateContent_arr);
// p($templateContent_arr, $t_str);
// $stortime = strtotime('2025-10-16 19:03:00');
// \support\Redis::setEx('expired:sms:9995', $stortime - time(), 1);
// Log::info('定时发送已存入redis');
// p(123123);
$host = RabbitMqConfig::$host;
$port = RabbitMqConfig::$port;
$user = RabbitMqConfig::$user;

View File

@ -0,0 +1,42 @@
<?php
namespace app\process;
use support\Log;
use Webman\RedisQueue\Redis;
use Workerman\Redis\Client;
// 消息推送
class ExpiredRedis
{
public function onWorkerStart()
{
$resis_host = config('redis.default.host');
$resis_port = config('redis.default.port');
$resis_pwd= config('redis.default.password');
$redis = new Client('redis://' .$resis_host. ':' . $resis_port);
// 如果Redis需要密码认证
$redis->auth($resis_pwd);
// 选择数据库默认0
$redis->select(0);
// 设置读取超时(-1表示不超时
$redis->setOption(\Redis::OPT_READ_TIMEOUT, -1);
// 订阅键过期事件频道
// __keyevent@0__:expired 表示监听0号数据库的键过期事件
$redis->psubscribe(['__keyevent@0__:expired'], function ($pattern, $channel, $key) use($redis){
// 这里可以添加你的业务逻辑
// 例如根据键名前缀处理不同业务
$str = 'expired:sms:';
if (strpos($key, $str) === 0) {
Log::info ("替换之前: " . $key);
$id = str_replace($str, '', $key);
Log::info ("替换过的ID: " . $id);
// 发给队列
Redis::send('send.mark.sms', '1,' . $id);
Log::info ("处理订单过期: {$id}\n{}");
}
});
}
}

View File

@ -7,6 +7,7 @@ use app\model\AlibabaSms;
use app\model\ShopInfo;
use ba\Exception;
use support\Log;
use support\Redis;
use support\think\Db;
use Webman\RedisQueue\Consumer;
@ -36,12 +37,13 @@ class SendMarkSms implements Consumer
// $record = Db::table('sms_push_event')->where(['id' => $sms_push_event_id, 'push_type' => 2])->find();
$record = Db::table('sms_push_event')->where(['id' => $sms_push_event_id])->find();
if($record) {
// 查询是否是定时发送
if($record['send_type'] == 2) {
}
if($record['send_type'] == 2 && $record['send_time'] > date('Y-m-d H:i:s')) {
// 推给redis
$stortime = strtotime($record['send_time']);
Redis::setEx('expired:sms:'.$record['id'], $stortime - time(), 1);
Log::info('定时发送已存入redis');
}else {
// 待发送
if($record['status'] == 0) {
Log::info('待发送');
@ -113,6 +115,7 @@ class SendMarkSms implements Consumer
Log::info('【发送营销短信】无目标用户---》' . $record['user_id']);
}
}
}
}else {
Log::info('发送营销短信未查询到');
}

View File

@ -61,7 +61,7 @@ return [
]
]
],
// 'MessagePushTask' => [
// 'handler' => MessagePushTask::class,
// ]
'ExpiredRedis' => [
'handler' => \app\process\ExpiredRedis::class,
]
];

View File

@ -242,7 +242,7 @@ class Base extends Model
print_r('redis保存client_id---->' . $client_id . "\r\n");
print_r('redis保存值---->' . $data . "\r\n");
print_r('redis保存时间---->' . self::REDIS_TIME . "\r\n");
$res = Redis::set($client_id, $data, self::REDIS_TIME);
$res = Redis::setex($client_id, self::REDIS_TIME, $data);
print_r('redis保存结果---->' . $res . "\r\n");
}
@ -293,7 +293,7 @@ class Base extends Model
print_r('推送了-----' . "\r\n");
// 推送以后新增推送次数
$msg_info_arr['send_num'] += 1;
Redis::set($client_id, json_encode($msg_info_arr), self::REDIS_TIME);
Redis::setex($client_id, self::REDIS_TIME, json_encode($msg_info_arr), );
return true;
}
}
@ -342,7 +342,7 @@ class Base extends Model
if(empty($msg_info_arr)) {
Redis::del($uid);
}else {
Redis::set($uid, json_encode($msg_info_arr), self::REDIS_TIME);
Redis::setex($uid, self::REDIS_TIME, json_encode($msg_info_arr), );
}
return true;
}
@ -372,7 +372,7 @@ class Base extends Model
$s_arr = array_values(array_intersect($client_id_arr, $redis_client_list));
$result[$type] = $s_arr;
}
Redis::set($redis_name, json_encode($result), 60 * 60 * 24);
Redis::setex($redis_name, 60 * 60 * 24, json_encode($result));
}
}
}
@ -762,7 +762,7 @@ class Base extends Model
}
}
if(isset($result_arr)) {
Redis::set($redis_name, json_encode($result_arr), 60 * 60 * 24);
Redis::setex($redis_name, 60 * 60 * 24, json_encode($result_arr));
}
if(!$type) {
Redis::del($client_id);

View File

@ -85,7 +85,7 @@ class Cashier extends Base
self::add_log_file('----离线后推送收银机订单打印无回执--' .date('Y-m-d H:i:s'). '--->' . $arr_json, 'cashier');
$arr_n[] = $arr;
}
Redis::set($uid, json_encode($arr_n), self::REDIS_TIME);
Redis::setex($uid,self::REDIS_TIME, json_encode($arr_n));
}
break;
// 新增