CreateOrderSnTrait.php 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. /****************************************************************
  3. * Author: libaojia
  4. * Date: 2017/7/19 下午4:41
  5. * Email: livsyitian@163.com
  6. * QQ: 995265288
  7. * User: 芸众商城 www.yunzshop.com
  8. ****************************************************************/
  9. namespace app\common\traits;
  10. trait CreateOrderSnTrait
  11. {
  12. /**
  13. * 生成唯一单号
  14. *
  15. * @param $prefix //前缀一般为两个大写字母
  16. * @param string $field //字段不为 order_sn 时需要参数field
  17. * @param int $length //日期后随机数长度
  18. * @param bool $numeric //受否为纯数字
  19. * @return string
  20. */
  21. public static function createOrderSn($prefix, $field = 'order_sn', $length = 6, $numeric = true)
  22. {
  23. $orderSn = createNo($prefix, $length, $numeric);
  24. while (1) {
  25. if (!self::where($field, $orderSn)->first()) {
  26. break;
  27. }
  28. $orderSn = createNo($prefix, $length, $numeric);
  29. }
  30. return $orderSn;
  31. }
  32. }