海南旅游SAAS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

589 lines
20 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Common\PayType;
  4. use App\Common\ProductStatus;
  5. use App\Http\Controllers\Controller;
  6. use App\Models\AdminSetting;
  7. use App\Models\Agent;
  8. use App\Models\AgentProduct;
  9. use App\Models\AgentSetting;
  10. use App\Models\Coupon;
  11. use App\Models\OrderProductItem;
  12. use App\Models\Product;
  13. use App\Models\ProductSpec;
  14. use App\Models\SystemSetting;
  15. use App\Models\User;
  16. use App\Models\Order;
  17. use App\Service\OpenPlatform;
  18. use EasyWeChat\Factory;
  19. use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
  20. use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
  21. use EasyWeChat\Kernel\Http\StreamResponse;
  22. use GuzzleHttp\Exception\GuzzleException;
  23. use Illuminate\Http\Request;
  24. use Illuminate\Support\Facades\DB;
  25. use App\Common\OrderStatus as Status;
  26. use Illuminate\Support\Facades\Storage;
  27. /**
  28. * 订单
  29. * Class OrderController
  30. * @package App\Http\Controllers\Api
  31. */
  32. class OrderController extends Controller
  33. {
  34. //订单列表
  35. public function index(Request $request)
  36. {
  37. $formData = $request->only(['page', 'status']);
  38. $request->validate([
  39. 'page' => 'regex:/^\d+$/',
  40. 'status' => 'nullable|regex:/^\d+(,\d+)*$/'
  41. ], [
  42. 'page.regex' => '页码错误',
  43. 'status.regex' => '订单状态错误'
  44. ]);
  45. $order_list = Order::where('user_id', $this->user_id);
  46. if (isset($formData['status'])) {
  47. if (preg_match('/^\d+$/', $formData['status'])) {
  48. $order_list = $order_list->where('status', $formData['status']);
  49. } else {
  50. $order_list = $order_list->whereIn('status', explode(',', $formData['status']));
  51. }
  52. }
  53. $order_list = $order_list->select('id', 'agent_product_id', 'product_id', 'title', 'picture', 'price', 'num', 'status', 'timeout', 'created_at')
  54. ->orderBy('id', 'DESC')
  55. ->simplePaginate(15)
  56. ->toArray();
  57. $time = time();
  58. $prefix = Storage::disk('public')->url('');
  59. foreach ($order_list['data'] as &$v) {
  60. //图片加上域名
  61. if (strpos($v['picture'], $prefix) === false) {
  62. $v['picture'] = $prefix . $v['picture'];
  63. }
  64. if (strpos($v['picture'], $prefix) === false) {
  65. $v['picture'] = $prefix . $v['picture'];
  66. }
  67. if (!empty($v['pictures']) && is_array($v['pictures'])) {
  68. $v['pictures'] = array_map(function($item) use ($prefix) {
  69. return strpos($item, $prefix) === false ? $prefix . $item : $item;
  70. }, $v['pictures']);
  71. }
  72. //未付款订单提示剩余付款时间
  73. if ($v['timeout'] !== null) {
  74. $second = strtotime($v['timeout']) - $time;
  75. if ($second > 0) {
  76. $text_arr = [
  77. Status::UNPAID => '付款',
  78. Status::OFFLINE_UNPAID => '线下付款',
  79. Status::PAY_EARNEST => '付尾款',
  80. ];
  81. if (isset($text_arr[$v['status']])) {
  82. $v['status_text'] = '请在' . ceil($second / 60) . "分钟内" . ($text_arr[$v['status']] ?? '付款');
  83. }
  84. } else if ($second < 0 && $v['status'] == Status::PAY_EARNEST) {
  85. $v['status_text'] = '尾款支付已超时';
  86. } /*else { //此部分由定时处理
  87. $timeout_ids[] = $v['id'];
  88. $v['status'] = Status::CANCEL;
  89. $v['status_text'] = '已取消';
  90. //此部分已由定时任务处理
  91. Product::query()->find($v['product_id'])->increment('stock', $v['num']);
  92. }*/
  93. }
  94. }
  95. return $this->success($order_list);
  96. }
  97. //提交订单
  98. public function create(Request $request)
  99. {
  100. $formData = $request->input();
  101. $formData = array_map(fn($v) => trim($v), $formData); //过滤,删除首尾空
  102. //表单验证
  103. $pay_type_values = join(',', array_keys(PayType::array()));
  104. $request->validate([
  105. 'id' => ['required', 'regex:/^\d+$/'],
  106. 'name' => ['required', 'between:2,20'],
  107. 'mobile' => ['required', 'regex:/^1[3-9]\d{9}$/'],
  108. 'pay_type' => ['required', 'in:' . $pay_type_values],
  109. 'num' => ['required', 'min:1'],
  110. 'spec_id' => ['required', 'min:1', 'integer'],
  111. 'id_card' => ['regex:/^\d{17}[\dXx]$/'],
  112. ], [
  113. 'id.required' => '未指定产品ID',
  114. 'name.required' => '请输入联系人姓名',
  115. 'mobile.required' => '请输入联系手机号',
  116. 'id.regex' => '产品ID错误',
  117. 'name.between' => '联系人姓名在2~20字符之间',
  118. 'mobile.regex' => '请输入11位手机号',
  119. 'pay_type.required' => '请选择支付方式',
  120. 'pay_type.in' => '不存在此支付方式',
  121. 'num.required' => '请输入购买数量',
  122. 'num.min' => '购买数量输入错误',
  123. 'spec_id.*' => '请选择产品规格',
  124. 'id_card.regex' => '身份证号输入不正确',
  125. ]);
  126. $ap = AgentProduct::query()
  127. ->with(['coupon', 'product.spec', 'agentCloudProduct:id,price'])
  128. ->where('stock', '>=', $formData['num'])
  129. ->where(['id' => $formData['id'], 'status' => ProductStatus::ON_SALE, 'agent_id' => $this->agent_id]) //判断agent_id,防止新入驻小程序的演示产品被下单
  130. ->whereDoesntHave('agentProductItem', function ($query) {
  131. return $query->whereHas('product', function ($query) {
  132. return $query->where('stock', '<=', 0)->orWhere('status', '<>', ProductStatus::ON_SALE);
  133. });
  134. })
  135. ->first();
  136. if (!$ap || !$ap->product) {
  137. return $this->error('产品已下架或库存不足');
  138. }
  139. if (is_null($ap->product->spec) || !array_filter($ap->product->spec->toArray(), fn($v) => $v['id'] == $formData['spec_id'])) {
  140. return $this->error('你选择的产品规格不存在');
  141. }
  142. $order_info = [];
  143. //0:单品销售;1:组合销售
  144. if ($ap->type == 0) {
  145. //0:旅游线路、1:酒店、2:景区、3:餐厅、4:车队、5:单项 => admin_trans('product.options.publish_type')
  146. switch ($ap->product->type) {
  147. case 0:
  148. if (empty($formData['departure_time']) || !strtotime($formData['departure_time'])) {
  149. return $this->error('请选择出发时间');
  150. }
  151. if (empty($formData['return_time']) || !strtotime($formData['return_time'])) {
  152. return $this->error('请选择回程时间');
  153. }
  154. if (empty($formData['id_card'])) {
  155. return $this->error('请输入身份证号');
  156. }
  157. $order_info['departure_time'] = $formData['departure_time']; //出发时间
  158. $order_info['return_time'] = $formData['return_time']; //回程时间
  159. $order_info['id_card'] = $formData['id_card']; //身份证号
  160. break;
  161. case 1:
  162. if (empty($formData['check_in_time']) || !strtotime($formData['check_in_time'])) {
  163. return $this->error('请选择入住时间');
  164. }
  165. if (empty($formData['check_out_time']) || !strtotime($formData['check_out_time'])) {
  166. return $this->error('请选择离店时间');
  167. }
  168. if (empty($formData['arrival_time']) || !strtotime($formData['arrival_time'])) {
  169. return $this->error('请选择到店时间');
  170. }
  171. if (empty($formData['id_card'])) {
  172. return $this->error('请输入身份证号');
  173. }
  174. $order_info['check_in_time'] = $formData['departure_time']; //入住时间
  175. $order_info['check_out_time'] = $formData['return_time']; //离店时间
  176. $order_info['arrival_time'] = $formData['arrival_time']; //到店时间
  177. $order_info['id_card'] = $formData['id_card']; //身份证号
  178. break;
  179. case 2:
  180. if (empty($formData['enter_time']) || !strtotime($formData['enter_time'])) {
  181. return $this->error('请选择入园时间');
  182. }
  183. if (empty($formData['id_card'])) {
  184. return $this->error('请输入身份证号');
  185. }
  186. $order_info['enter_time'] = $formData['enter_time']; //到店时间
  187. $order_info['id_card'] = $formData['id_card']; //身份证号
  188. break;
  189. }
  190. } else if ($ap->type == 1) {
  191. }
  192. //支付小程序的产品不允许购买
  193. if (AdminSetting::val('payee_appid') == Agent::where('id', $this->agent_id)->value('appid')) {
  194. return $this->error('系统出错了,购买失败~~');
  195. }
  196. $coupon_ids = [];
  197. if ($ap->coupon) {
  198. foreach ($ap->coupon as $v) {
  199. $coupon_ids[] = $v['id'];
  200. }
  201. }
  202. DB::beginTransaction();
  203. try {
  204. $price = $this->calc($ap->price, $formData['num'], $formData['pay_type'], $ap);
  205. //供应商产品表减库存
  206. $product_ids = explode(',', $ap->product_ids);
  207. $affect_row = Product::query()
  208. ->where('stock', '>=', $formData['num']) //乐观锁
  209. ->whereIn('id', $product_ids)
  210. ->decrement('stock', $formData['num']);
  211. if ($affect_row != count($product_ids)) {
  212. throw new \Exception('供应产品库存不足');
  213. }
  214. //产品规格减表减库存
  215. $affect_row = ProductSpec::query()
  216. ->where('stock', '>=', $formData['num']) //乐观锁
  217. ->where('id', $formData['spec_id'])
  218. ->decrement('stock', $formData['num']);
  219. if (!$affect_row) {
  220. throw new \Exception('你选择的产品规格库存不足');
  221. }
  222. //代理商产品表减库存
  223. $affect_row = AgentProduct::query()
  224. ->where('stock', '>=', $formData['num']) //乐观锁
  225. ->where('id', $ap->id)
  226. ->decrement('stock', $formData['num']);
  227. if (!$affect_row) {
  228. throw new \Exception('商户产品库存不足');
  229. }
  230. //未付款时定金/订金的超时时间依然使用默认的订单超时时间,付定金/订金之后才使用定金超时时间
  231. $order_timeout = AgentSetting::val($this->agent_id, 'order_timeout') ?? 60; //默认60分钟
  232. $timeout = date('Y-m-d H:i:s', time() + $order_timeout * 60); //60 * 分钟转为秒
  233. //处理预付金额
  234. if ($formData['pay_type'] == PayType::DEPOSIT_PAY) {
  235. //订金支付
  236. $prepayPrice = $ap->deposit;
  237. $prepayTimeout = $ap->deposit_timeout;
  238. } else if($formData['pay_type'] == PayType::EARNEST_PAY) {
  239. //定金支付
  240. $prepayPrice = $ap->earnest;
  241. $prepayTimeout = $ap->earnest_timeout;
  242. }
  243. // 存入订单表
  244. $order = Order::query()->create([
  245. 'user_id' => $this->user_id,
  246. 'agent_id' => $this->agent_id,
  247. 'order_no' => $this->getOrderNo(),
  248. 'num' => $formData['num'],
  249. 'price' => $price,
  250. 'name' => $formData['name'],
  251. 'mobile' => $formData['mobile'],
  252. 'title' => $ap->title,
  253. 'picture' => $ap->picture,
  254. 'agent_product_id' => $ap->id,
  255. 'product_id' => $ap->product_id,
  256. 'product_ids' => $ap->product->product_ids ?? $ap->product_id,
  257. 'status' => $formData['pay_type'] == PayType::OFFLINE ? Status::OFFLINE_UNPAID : Status::UNPAID,
  258. 'pay_type' => $formData['pay_type'],
  259. 'coupon_id' => join(',', $coupon_ids),
  260. 'guide_id' => $ap->guide_id,
  261. 'guide_price' => $ap->guide_price,
  262. 'timeout' => $timeout,
  263. 'agent_cloud_pid' => $ap->agent_cloud_pid,
  264. 'agent_cloud_price' => $ap->agentCloudProduct->price ?? 0,
  265. 'prepay_price ' => $prepayPrice ?? 0,
  266. 'prepay_timeout' => $prepayTimeout ?? 0,
  267. 'service_persons' => SystemSetting::val('single', 'price'),
  268. 'info' => $order_info,
  269. ]);
  270. //存入订单产品表
  271. $supplier_product_info = Product::whereIn('id', $product_ids)
  272. ->orderBy('id')->get(['type', 'id AS product_id', 'supplier_id', 'price', 'service_persons'])->toArray();
  273. $order_id = $order->id;
  274. $agent_id = $this->agent_id;
  275. $agent_product_id = $ap->id;
  276. foreach ($supplier_product_info as &$v) {
  277. $v['order_id'] = $order_id;
  278. $v['agent_id'] = $agent_id;
  279. $v['agent_product_id'] = $agent_product_id;
  280. $v['num'] = $formData['num'];
  281. if ($v['type'] == 0) { //TODO 此处未处理组合产品
  282. $v['product_spec_id'] = $formData['spec_id'];
  283. }
  284. unset($v['type']);
  285. }
  286. OrderProductItem::insert($supplier_product_info);
  287. DB::commit();
  288. } catch (\Exception $e) {
  289. DB::rollBack();
  290. return $this->error($e->getMessage());
  291. }
  292. if ($formData['pay_type'] == PayType::OFFLINE) { //线下支付
  293. return $this->success(['id' => $order_id], '操作成功,请及时联系客服付款');
  294. } else { //在线支付或定金支付
  295. /*$config = $this->payConfig($order, $price);
  296. if (!empty($config['paySign'])) {
  297. return $this->success($config);
  298. } else {
  299. return $this->error($config['err_code_des'] ?? join(',', $config));
  300. }*/
  301. // 跳转支付专用
  302. return $this->success(['id' => $order->id, 'jump' => true, 'jump_appid' => AdminSetting::val('payee_appid')]);
  303. }
  304. }
  305. //申请退款
  306. public function refund(Request $request)
  307. {
  308. $formData = $request->only(['id', 'desc', 'pictures']);
  309. $request->validate([
  310. 'id' => 'required|integer',
  311. 'desc' => 'required|string',
  312. 'pictures' => 'nullable|array',
  313. ], [
  314. '*.required' => '内容输入不完整',
  315. 'pictures.array' => '图片必须是数组',
  316. ]);
  317. //去掉图片地址前的域名
  318. $prefix = Storage::disk('public')->url('');
  319. foreach ($formData['pictures'] as &$v) {
  320. $v = str_replace($prefix, '', $v);
  321. }
  322. $order = Order::firstWhere(['id' => $formData['id'], 'user_id' => $this->user_id]);
  323. if (!$order) {
  324. return $this->error('订单不存在');
  325. }
  326. //订金/定金/首付款不允许退款,只有付全款才能申请退款
  327. if (!in_array($order->status, [Status::PAID, Status::PAID_RETAINAGE])) {
  328. return $this->error('当前订单状态不允许退款');
  329. }
  330. $order->refund_info = [
  331. 'desc' => strip_tags($formData['desc']),
  332. 'refund_no' => $this->getOrderNo(), //退款单号
  333. 'pictures' => $formData['pictures'] ?? [],
  334. 'old_status' => $order->status,
  335. ];
  336. $order->status = Status::REFUNDING;
  337. $order->save();
  338. return $this->success();
  339. }
  340. //获取应付金额及相关产品信息
  341. public function getPrice(Request $request)
  342. {
  343. $formData = $request->only(['id', 'num', 'pay_type']);
  344. $request->validate([
  345. 'id' => 'required|integer',
  346. 'num' => 'required|integer',
  347. 'pay_type' => 'required|integer',
  348. ], [
  349. '*.required' => '参数缺失',
  350. '*.integer' => '参数类型错误',
  351. ]);
  352. if (!$formData['num'] || $formData['num'] < 1) {
  353. return $this->error('未指定产品数量');
  354. }
  355. $ap = AgentProduct::query()
  356. ->has('product')
  357. ->with('coupon:agent_product_id,type,detail,agent_id,tag,start_at,end_at')
  358. ->find($formData['id'], ['id', 'price', 'original_price', 'product_id', 'title', 'pictures', 'earnest', 'earnest_timeout', 'deposit', 'deposit_timeout']);
  359. if (!$ap) {
  360. return $this->error('产品信息不存在');
  361. }
  362. $prefix = Storage::disk('public')->url('');
  363. $ap->pictures = array_map(fn($v) => $prefix . $v, $ap->pictures);
  364. //如果是线下支付,显示的价格跟在线全款支付价格一样
  365. if ($formData['pay_type'] == PayType::OFFLINE) {
  366. $formData['pay_type'] = PayType::ONLINE;
  367. }
  368. $ap->final_price = $this->calc($ap->price, $formData['num'], $formData['pay_type'], $ap);
  369. $ap->num = $formData['num'];
  370. return $this->success($ap);
  371. }
  372. //订单支付(在订单列表发起)
  373. public function pay(Request $request)
  374. {
  375. $id = (int)request()->input('id');
  376. //订单信息
  377. $order = Order::query()
  378. ->with('agentProduct')
  379. ->where(['user_id' => $this->user_id, 'agent_id' => $this->agent_id])
  380. ->whereRaw('`timeout` >= NOW()')
  381. ->whereIn('status', [Status::UNPAID, Status::PAY_EARNEST])
  382. ->find($id);
  383. if (!$order) {
  384. return $this->error('订单已支付或已超时');
  385. }
  386. // 跳转支付专用
  387. return $this->success(['id' => $id, 'jump' => true, 'jump_appid' => AdminSetting::val('payee_appid')]);
  388. /*$ap = AgentProduct::with('coupon')->find($order->agent_product_id);
  389. //如果已经付定金或首付款,则仅支付尾款
  390. if ($order->status == Status::PAY_EARNEST) {
  391. $price = $order->price - $order->paid_money;
  392. } else {
  393. $price = $this->calc($order->price, $order->num, $order->pay_type, $ap);
  394. }
  395. $config = $this->payConfig($order, $price);
  396. if (!empty($config['paySign'])) {
  397. return $this->success($config);
  398. } else {
  399. return $this->error($config['err_code_des'] ?? join(',', $config));
  400. }*/
  401. }
  402. //获取支付配置信息
  403. private function payConfig($order, $price)
  404. {
  405. //用户openid
  406. $openid = User::query()->where('id', $this->user_id)->value('openid'); //此处要用where,value()用find有BUG
  407. //代理商信息
  408. $agent = Agent::query()->find($this->agent_id);
  409. $config = config('wechat.payment.default');
  410. $config = array_merge($config, [
  411. 'app_id' => $agent->appid,
  412. 'mch_id' => $agent->mchid,
  413. 'key' => $agent->mchkey,
  414. ]);
  415. $app = Factory::payment($config);
  416. try {
  417. $result = $app->order->unify([
  418. 'body' => mb_strcut($order->title, 0, 127),
  419. 'out_trade_no' => $order->order_no . '-' . $order->status, //后面加status,主要是为了方便微信支付回调时区分定金(首付款)和尾款支付
  420. 'total_fee' => round($price * 100), //支付金额单位为分
  421. 'notify_url' => route('wxpay_notify', ['agent_id' => $this->agent_id]), // 支付结果通知网址,如果不设置则会使用配置里的默认地址
  422. 'trade_type' => 'JSAPI',
  423. 'openid' => $openid,
  424. 'profit_sharing' => 'Y', //Y分账,N不分账,默认不分账,Y大写
  425. ]);
  426. } catch (InvalidArgumentException | InvalidConfigException | GuzzleException $e) {
  427. return ['error' => $e->getMessage(), 'file' => basename($e->getFile()), 'line' => $e->getLine()];
  428. }
  429. if (empty($result['prepay_id'])) {
  430. return $result;
  431. }
  432. $jssdk = $app->jssdk;
  433. return $jssdk->bridgeConfig($result['prepay_id'], false) + ['id' => $order->id, 'order_no' => $order->order_no]; // 返回数组
  434. }
  435. //订单详情
  436. public function show()
  437. {
  438. $id = (int)request()->input('id');
  439. $fields = ['id', 'agent_id', 'order_no', 'agent_product_id', 'num', 'price', 'name', 'mobile', 'title', 'picture', 'status',
  440. 'pay_type', 'coupon_id', 'paid_money', 'paid_at', 'refund_info', 'verify_code', 'created_at'];
  441. $order = Order::with('agent:id,appid,appsecret')
  442. ->where('user_id', $this->user_id)
  443. ->find($id, $fields);
  444. if (!$order) {
  445. return $this->error('订单不存在');
  446. }
  447. //订单ID和核销码拼接,查询时通过订单ID和核销码来查询,这样核销码不用建索引
  448. $order->verify_code = $order->verify_code ? $order->id . '-' . $order->verify_code : '';
  449. //如果有核销码,生成核销二维码
  450. if ($order->verify_code) {
  451. $app = new OpenPlatform();
  452. $refreshToken = $app->refreshToken($order->agent->appid);
  453. if (!$refreshToken) {
  454. return $this->error('获取refresh_token失败');
  455. }
  456. $app = $app->miniProgram($order->agent->appid, $refreshToken);
  457. //由于参数最多只能32个字符,故通过下面这种方式传参
  458. //pt表示使用普通订单,使用api/verification/verify接口核销;
  459. //hy表示行业产品订单,使用api/verification/industry_verify接口核销
  460. $response = $app->app_code->getUnlimit('pt' . $order->verify_code, ['page' => 'pages/verification/index']);
  461. if ($response instanceof StreamResponse) {
  462. $filename = $response->saveAs(storage_path('app/public/verify_code'), $order->verify_code);
  463. $order->verify_qrcode = Storage::disk('public')->url('verify_code/' . $filename);
  464. }
  465. }
  466. unset($order->agent, $order->agent_id); //必须unset掉$order->agent,否则会造成appsecret泄漏
  467. $order->coupon = Coupon::query()
  468. ->whereIn('id', $order->coupon_id)
  469. ->where(['agent_id' => $this->agent_id, 'agent_product_id' => $order->agent_product_id,])
  470. ->get(['tag']);
  471. return $this->success($order);
  472. }
  473. /**
  474. * 计算最终价格(扣除优惠券之后的价格)
  475. * $price:原价;$coupon:优惠券;$num:产品数量;$pay_type:支付方式
  476. * @param float $price
  477. * @param float $num
  478. * @param int $pay_type
  479. * @param AgentProduct $agent_product
  480. * @return float
  481. */
  482. private function calc(float $price, float $num, int $pay_type, AgentProduct $agent_product): float
  483. {
  484. /** 修改需要同步修改sharePay里面的 */
  485. //根据支付方式计算价格
  486. if (in_array($pay_type, [PayType::DEPOSIT_PAY, PayType::EARNEST_PAY, PayType::DOWN_PAYMENT])) {
  487. if ($pay_type == PayType::DEPOSIT_PAY && $agent_product->deposit && $agent_product->deposit_timeout) {
  488. return $agent_product->deposit;
  489. } else if ($pay_type == PayType::EARNEST_PAY && $agent_product->earnest && $agent_product->earnest_timeout) {
  490. return $agent_product->earnest;
  491. }
  492. }
  493. $total_price = $price * $num;
  494. /*//没有任何优惠券时直接返回最终价
  495. if ($coupon && $coupon->isEmpty()) {
  496. return $total_price;
  497. }
  498. $coupon = $coupon->toArray();
  499. foreach ($coupon as $v) {
  500. // 未判断优惠券有效期
  501. if ($v['type'] == 1 && !empty($v['detail']['full']) && !empty($v['detail']['reduction'])) { //满减
  502. if ($total_price >= $v['detail']['full']) {
  503. $total_price -= $v['detail']['reduction'];
  504. }
  505. } else if ($v['type'] == 2 && !empty($v['detail']['discount'])) { //打折
  506. $total_price *= $v['detail']['discount'];
  507. }
  508. }*/
  509. return round($total_price, 2);
  510. }
  511. // 生成订单号
  512. private function getOrderNo(): string
  513. {
  514. list($micro, $sec) = explode(' ', microtime());
  515. $micro = str_pad(floor($micro * 1000000), 6, 0, STR_PAD_LEFT);
  516. return date('ymdHis', $sec) . $micro . mt_rand(1000, 9999);
  517. }
  518. }