OrderHelper.php 831 B

12345678910111213141516171819202122232425262728293031323334
  1. <?php
  2. namespace app\common\util;
  3. use think\db\exception\DataNotFoundException;
  4. use think\db\exception\DbException;
  5. use think\db\exception\ModelNotFoundException;
  6. use think\facade\Db;
  7. class OrderHelper
  8. {
  9. /**
  10. * 生成订单号
  11. * @param $user_id
  12. * @return string|null
  13. * @throws DataNotFoundException
  14. * @throws DbException
  15. * @throws ModelNotFoundException
  16. */
  17. public static function createOrderNo($user_id): ?string
  18. {
  19. for ($i = 0; $i < 10000; $i++) {
  20. //订单号规则 年月日时分秒+4位随机数+用户ID
  21. $no = date('YmdHis') . rand(1000, 9999) . $user_id;
  22. $exist = (new Db)->name('order')->where(["order_no" => $no])->find();
  23. if (!$exist) {
  24. return $no;
  25. }
  26. }
  27. return null;
  28. }
  29. }