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.

92 lines
2.8 KiB

6 years ago
6 years ago
6 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 Hyperf\DbConnection\Db;
  9. use Hyperf\Di\Annotation\Inject;
  10. use App\Service\OrderServiceInterface;
  11. use Hyperf\HttpMessage\Stream\SwooleStream;
  12. use Hyperf\Validation\ValidationException;
  13. class OrderController extends BaseController
  14. {
  15. /**
  16. * @Inject
  17. * @var OrderServiceInterface
  18. */
  19. protected $orderService;
  20. public function addOnlineOrder(OrderOnlineRequest $request)
  21. {
  22. $orderMainId = $this->orderService->addOnlineOrder($request->validated());
  23. if (!is_int($orderMainId)) {
  24. return $this->result(ErrorCode::ORDER_FAILURE, '', $orderMainId);
  25. }
  26. return $this->success(['order_id' => $orderMainId]);
  27. }
  28. public function addOfflineOrder(OrderOfflineRequest $request)
  29. {
  30. $orderMainId = $this->orderService->addOfflineOrder($request->validated());
  31. if (!is_int($orderMainId)) {
  32. return $this->result(ErrorCode::ORDER_FAILURE, '', $orderMainId);
  33. }
  34. return $this->success(['order_id' => $orderMainId]);
  35. }
  36. /**
  37. * 用户完成订单-确认收货
  38. */
  39. public function userComp()
  40. {
  41. $validator = $this->validationFactory->make(
  42. $this->request->all(),
  43. [
  44. 'user_id' => 'required|nonempty|integer',
  45. 'order_id' => 'required|nonempty|numeric',
  46. ],
  47. [
  48. '*.*' => ':attribute 参数不正确',
  49. ]
  50. );
  51. if ($validator->fails()) {
  52. // Handle exception
  53. throw new ValidationException($validator);
  54. return;
  55. }
  56. $userId = $this->request->input('user_id');
  57. $orderId = $this->request->input('order_id');
  58. $orderExist = OrderMain::query()
  59. ->where(['id' => $orderId, 'state' => OrderMain::ORDER_STATE_DELIVERY, 'user_id' => $userId])
  60. ->exists();
  61. if (!$orderExist) {
  62. $this->log->event(LogLabel::ONLINE_COMPLETE_LOG, ['order_not_found' => 'order_not_found']);
  63. return $this->result(ErrorCode::SEPARATE_ACCOUNTS_ERROR, '', '操作失败,订单异常或不存在');
  64. }
  65. Db::beginTransaction();
  66. try {
  67. $this->orderService->onlineCompleted($orderId);
  68. $this->separateAccountsService->orderOnlineCompleted($orderId);
  69. Db::commit();
  70. return $this->success(['order_id' => $orderId]);
  71. } catch (\Exception $e) {
  72. Db::rollBack();
  73. $this->log->event(LogLabel::ONLINE_COMPLETE_LOG, ['exception' => $e->getMessage()]);
  74. return $this->result(ErrorCode::SEPARATE_ACCOUNTS_ERROR, '', '操作失败,请稍后重试');
  75. }
  76. }
  77. }