'钱包提现', '2' => '平台转账', '10' => '余额充值', '11' => '余额充值', '12' => '积分充值', '13' => '积分兑换', '16' => '球杆检测', '21' => '球杆检测', '22' => '球杆检测', '33' => '预定包厢', '19' => '商品下单', '99' => '店长入账记录', ]; } public static function getOrderType($type) { $labels = self::orderTypeLabels(); return isset($labels[$type]) ? $labels[$type] : "未知"; } /** * 生成唯一订单号,YYYYMMDDHHIISS+订单类型+随机数+校验码(2位) * @param string $order_type 订单类型,识别码三位数字 * @param Model $class Model * @param integer $len 订单号长度,最小值为20 * retrun string|null * throw Exception */ public static function generate($order_type, $class = null, $len = 24, $retry = 1, $retry_max = 3, $unique_field = 'order_no') { $cache_key = "u_o_n_g_{$order_type}"; $cache_val = FlashStorage::getCache($cache_key); if ($cache_val !== false) { sleep(1); $retry += 1; return $retry <= $retry_max ? self::generate($order_type, $class, $len, $retry, $retry_max, $unique_field) : null; } FlashStorage::setCache($cache_key, $cache_key); $order_no = null; if ($class == null) { $order_no = self::_generate($order_type, $len); FlashStorage::deleteCache($cache_key); return $order_no; } if (!class_exists($class)) { throw new \Exception("{$class}不存在"); } while (true) { $order_no = self::_generate($order_type, $len); $exists = $class::find()->where([$unique_field => $order_no])->exists(); if (!$exists) { FlashStorage::deleteCache($cache_key); break; } } return $order_no; } public static function _generate($order_type, $len = 32) { $order_no_main = date('YmdHis', time()) . $order_type; $rand_str_len = $len - strlen($order_no_main) - 2; $rand_str = ""; if ($rand_str_len > 0) { $rand_min = pow(10, $rand_str_len - 1); $rand_max = pow(10, $rand_str_len) - 1; $rand_str = mt_rand($rand_min, $rand_max); } $order_no_main .= $rand_str; $order_no_mian_len = strlen($order_no_main); $order_no_main_sum = 0; for ($i = 0; $i < $order_no_mian_len; $i++) { $order_no_main_sum += (int)(substr($order_no_main, $i, 1)); } $order_no = $order_no_main . str_pad((100 - $order_no_main_sum % 100) % 100, 2, '0', STR_PAD_LEFT); //并发情况生成唯一订单号 $cache_key = "u_o_n_g_{$order_type}_{$order_no}"; $cache_val = FlashStorage::getCache($cache_key); if ($cache_val !== false) { usleep(500); return self::_generate($order_type, $len); } FlashStorage::setCache($cache_key, $cache_key, 60); return $order_no; } }