修改订单编号

This commit is contained in:
hant 2025-06-30 22:38:27 +08:00
parent 7d866b8513
commit 28633df9cd
2 changed files with 31 additions and 5 deletions

View File

@ -3,6 +3,7 @@
namespace app\admin\model;
use app\admin\library\Auth;
use app\common\services\RedisService;
use think\Cache;
use think\Model;
use traits\model\SoftDelete;
@ -289,11 +290,8 @@ class Order extends Model
$date = date('ymd'); // 6 位日期
$key = 'order_inc:' . date('Ymd');
$inc = Cache::store('redis')->inc($key); // 自增
// 设置 key 过期(防止 key 永久存在)
$handler = Cache::store('redis')->handler();
$handler->expire($key, 86400); // 设置 1 天过期
$redis = new RedisService(0);
$inc = $redis->incrWithExpire($key);
if ($inc > 9999) {
throw new \Exception('订单号已满');

View File

@ -0,0 +1,28 @@
<?php
namespace app\common\services;
use think\Cache;
class RedisService
{
protected $redis;
/**
* @param int $db Redis数据库编号默认 0
*/
public function __construct(int $db = 0)
{
$this->redis = Cache::store('redis')->handler();
$this->redis->select($db);
}
public function incrWithExpire(string $key, int $expireSeconds = 86400)
{
$val = $this->redis->incr($key);
if ($val === 1) {
$this->redis->expire($key, $expireSeconds);
}
return $val;
}
}