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.

111 lines
3.4 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. <?php
  2. namespace App\Controller;
  3. use App\Constants\ErrorCode;
  4. use App\Constants\LogLabel;
  5. use App\Model\OrderMain;
  6. use App\Request\OrderOfflineRequest;
  7. use App\Request\OrderOnlineRequest;
  8. use App\Service\SeparateAccountsServiceInterface;
  9. use Hyperf\DbConnection\Db;
  10. use Hyperf\Di\Annotation\Inject;
  11. use App\Service\OrderServiceInterface;
  12. use Hyperf\HttpMessage\Stream\SwooleStream;
  13. use Hyperf\Validation\ValidationException;
  14. class OrderController extends BaseController
  15. {
  16. /**
  17. * @Inject
  18. * @var OrderServiceInterface
  19. */
  20. protected $orderService;
  21. /**
  22. * @Inject
  23. * @var SeparateAccountsServiceInterface
  24. */
  25. protected $separateAccountsService;
  26. public function addOnlineOrder(OrderOnlineRequest $request)
  27. {
  28. $orderMainId = $this->orderService->addOnlineOrder($request->validated());
  29. if (!is_int($orderMainId)) {
  30. return $this->result(ErrorCode::ORDER_FAILURE, '', $orderMainId);
  31. }
  32. return $this->success(['order_id' => $orderMainId]);
  33. }
  34. public function addOfflineOrder(OrderOfflineRequest $request)
  35. {
  36. $orderMainId = $this->orderService->addOfflineOrder($request->validated());
  37. if (!is_int($orderMainId)) {
  38. return $this->result(ErrorCode::ORDER_FAILURE, '', $orderMainId);
  39. }
  40. return $this->success(['order_id' => $orderMainId]);
  41. }
  42. /**
  43. * 用户完成订单-确认收货
  44. */
  45. public function userComp()
  46. {
  47. $validator = $this->validationFactory->make(
  48. $this->request->all(),
  49. [
  50. 'user_id' => 'required|nonempty|integer',
  51. 'order_id' => 'required|nonempty|numeric',
  52. ],
  53. [
  54. '*.*' => ':attribute 参数不正确',
  55. ]
  56. );
  57. if ($validator->fails()) {
  58. // Handle exception
  59. throw new ValidationException($validator);
  60. return;
  61. }
  62. $userId = $this->request->input('user_id');
  63. $orderId = $this->request->input('order_id'); // TODO 等新订单列表接口处理完毕后全面转换成global_order_id
  64. $orderMain = OrderMain::query()
  65. ->where(['id' => $orderId, 'state' => OrderMain::ORDER_STATE_DELIVERY, 'user_id' => $userId])
  66. ->first();
  67. if (empty($orderMain)) {
  68. $this->log->event(LogLabel::ONLINE_COMPLETE_LOG, ['order_not_found' => 'order_not_found']);
  69. return $this->result(ErrorCode::SEPARATE_ACCOUNTS_ERROR, '', '操作失败,订单异常或不存在');
  70. }
  71. Db::beginTransaction();
  72. try {
  73. $this->orderService->onlineCompleted($orderMain->global_order_id);
  74. $this->separateAccountsService->orderOnlineCompleted($orderMain->global_order_id);
  75. Db::commit();
  76. return $this->success('');
  77. } catch (\Exception $e) {
  78. Db::rollBack();
  79. $this->log->event(LogLabel::ONLINE_COMPLETE_LOG, ['exception' => $e->getMessage()]);
  80. return $this->result(ErrorCode::SEPARATE_ACCOUNTS_ERROR, '', '操作失败,请稍后重试');
  81. }
  82. }
  83. /**
  84. * 用户取消订单
  85. */
  86. public function onlineCancel(){
  87. $orderId = $this->request->input('order_id');
  88. $orderMain = OrderMain::where('id',$orderId)
  89. ->select('global_order_id')
  90. ->first();
  91. return $this->success($this->orderService->onlineCancel($orderMain->global_order_id));
  92. }
  93. }