修改订单编号
This commit is contained in:
parent
7d866b8513
commit
28633df9cd
|
|
@ -3,6 +3,7 @@
|
||||||
namespace app\admin\model;
|
namespace app\admin\model;
|
||||||
|
|
||||||
use app\admin\library\Auth;
|
use app\admin\library\Auth;
|
||||||
|
use app\common\services\RedisService;
|
||||||
use think\Cache;
|
use think\Cache;
|
||||||
use think\Model;
|
use think\Model;
|
||||||
use traits\model\SoftDelete;
|
use traits\model\SoftDelete;
|
||||||
|
|
@ -289,11 +290,8 @@ class Order extends Model
|
||||||
$date = date('ymd'); // 6 位日期
|
$date = date('ymd'); // 6 位日期
|
||||||
$key = 'order_inc:' . date('Ymd');
|
$key = 'order_inc:' . date('Ymd');
|
||||||
|
|
||||||
$inc = Cache::store('redis')->inc($key); // 自增
|
$redis = new RedisService(0);
|
||||||
|
$inc = $redis->incrWithExpire($key);
|
||||||
// 设置 key 过期(防止 key 永久存在)
|
|
||||||
$handler = Cache::store('redis')->handler();
|
|
||||||
$handler->expire($key, 86400); // 设置 1 天过期
|
|
||||||
|
|
||||||
if ($inc > 9999) {
|
if ($inc > 9999) {
|
||||||
throw new \Exception('订单号已满');
|
throw new \Exception('订单号已满');
|
||||||
|
|
|
||||||
28
application/common/services/RedisService.php
Normal file
28
application/common/services/RedisService.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user