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

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