Accept Merge Request #202: (feature/hant -> develop)
Merge Request: 修改订单编号 Created By: @todayswind Accepted By: @todayswind URL: https://g-bcrc3009.coding.net/p/allocatr/d/allocatr/git/merge/202?initial=true
This commit is contained in:
commit
c154532736
|
|
@ -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('订单号已满');
|
||||
|
|
|
|||
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