<?php namespace app\common\util; use think\db\exception\DataNotFoundException; use think\db\exception\DbException; use think\db\exception\ModelNotFoundException; use think\facade\Db; class OrderHelper { /** * 生成订单号 * @param $user_id * @return string|null * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ public static function createOrderNo($user_id): ?string { for ($i = 0; $i < 10000; $i++) { //订单号规则 年月日时分秒+4位随机数+用户ID $no = date('YmdHis') . rand(1000, 9999) . $user_id; $exist = (new Db)->name('order')->where(["order_no" => $no])->find(); if (!$exist) { return $no; } } return null; } }