Merge remote-tracking branch 'origin/develop' into develop

This commit is contained in:
xman 2025-07-01 10:54:21 +08:00
commit cdced64205
3 changed files with 52 additions and 9 deletions

View File

@ -471,15 +471,7 @@ class Order extends Backend
function generateOrderNumber($prefix = '')
{
// 获取当前时间戳(精确到毫秒)
$timestamp = microtime(true);
$date = date('YmdHis', (int)$timestamp); // 格式YYYYMMDDHHMMSS
// $randomNum = strtoupper(substr(md5($timestamp), 0, 4)); // 从时间戳生成一个随机字符串,取前四个字符
$code = str_pad(mt_rand(0, 999999), 6, '0', STR_PAD_LEFT);
// 组合生成的订单编号
$orderNumber = $prefix . $date . $code;
// 取 MD5 前10位
return substr(md5($orderNumber), 0, 10);
return \app\admin\model\Order::generateOrderNo();
}
use AmapTrait;

View File

@ -3,6 +3,8 @@
namespace app\admin\model;
use app\admin\library\Auth;
use app\common\services\RedisService;
use think\Cache;
use think\Model;
use traits\model\SoftDelete;
@ -279,4 +281,25 @@ class Order extends Model
return $this->belongsTo(Worker::class,'worker_id',);
}
/**
* 生成唯一订单号8位纯数字如25063001
* 格式yyMMdd + 当天自增序号
*/
public static function generateOrderNo()
{
$date = date('ymd'); // 6 位日期
$key = 'order_inc:' . date('Ymd');
$redis = new RedisService(0);
$inc = $redis->incrWithExpire($key);
if ($inc > 9999) {
throw new \Exception('订单号已满');
}
$suffix = str_pad($inc, 4, '0', STR_PAD_LEFT); // 可改为 4 位更安全
return $date . $suffix; // 最终结果:如 2506300001
}
}

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;
}
}