sku完成,订单、商品上下架管理正在搞

This commit is contained in:
2025-05-21 17:04:10 +08:00
parent 035ab1105c
commit fc503cb95a
18 changed files with 239579 additions and 22 deletions

View File

@@ -185,15 +185,53 @@ class UtilsService
*/
public function genOrderNo(int $type = 0): string
{
$len = 9999999999;
switch ($type) {
case 0:
return 'DK' . date('Ymd') . str_pad(mt_rand(1, $len), 11, '0', STR_PAD_LEFT);
case 1:
return 'DD' . date('Ymd') . str_pad(mt_rand(1, $len), 11, '0', STR_PAD_LEFT);
default:
return '';
$prefix = ['NL', 'TX', 'TK'];
$datePart = date('Ymd');
// 确保len不超过PHP_INT_MAX避免整数溢出
$maxLen = PHP_INT_MAX;
// 使用bcmath扩展生成安全的大数随机数
if (function_exists('bcadd') && function_exists('bcmul')) {
// 生成一个10位的随机字符串作为基础
$randomBase = '';
for ($i = 0; $i < 10; $i++) {
$randomBase .= mt_rand(0, 9);
}
// 使用bcmath计算一个大数随机数
$randomPart = bcadd(
'1', // 确保最小值为1
bcmul(
$randomBase,
bcdiv((string)$maxLen, '10000000000', 0),
0
)
);
// 截取前11位数字
$randomStr = substr($randomPart, 0, 11);
} else {
// 备用方案使用mt_rand并处理溢出
$randomNum = mt_rand(1, $maxLen);
$randomStr = (string)$randomNum;
// 如果长度不足11位左侧补0
if (strlen($randomStr) < 11) {
$randomStr = str_pad($randomStr, 11, '0', STR_PAD_LEFT);
} else {
// 如果长度超过11位截取前11位
$randomStr = substr($randomStr, 0, 11);
}
}
// 确保随机部分长度为11
$paddedRandom = str_pad($randomStr, 11, '0', STR_PAD_LEFT);
$paddedRandom = substr($paddedRandom, -11); // 确保最终长度为11
// 返回格式化后的代码
return isset($prefix[$type]) ? $prefix[$type] . $datePart . $paddedRandom : '';
}
/**