海南旅游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.

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