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.

215 lines
7.2 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. <?php
  2. namespace App\Controller\v3;
  3. use App\Constants\v3\ErrorCode;
  4. use App\Constants\v3\LogLabel;
  5. use App\Constants\v3\OrderState;
  6. use App\Controller\BaseController;
  7. use App\Exception\ErrorCodeException;
  8. use App\Model\v3\OrderMain;
  9. use App\Request\v3\OrderOnlineDetailRequest;
  10. use App\Request\v3\OrderOnlineRequest;
  11. use App\Request\v3\OrderOnlineStateRequest;
  12. use App\Service\v3\Implementations\PaymentService;
  13. use App\Service\v3\Interfaces\CouponRecServiceInterface;
  14. use App\Service\v3\Interfaces\SeparateAccountsServiceInterface;
  15. use App\Service\v3\Interfaces\ShopCartServiceInterface;
  16. use Hyperf\DbConnection\Db;
  17. use Hyperf\Di\Annotation\Inject;
  18. use App\Service\v3\Interfaces\OrderOnlineServiceInterface;
  19. use App\Service\v3\Interfaces\UserBindTelServiceInterface;
  20. use App\Service\v3\Interfaces\AppointmentTimeServiceInterface;
  21. use Psr\Http\Message\ResponseInterface;
  22. use App\Model\v3\UserAddress;
  23. class OrderOnlineController extends BaseController
  24. {
  25. /**
  26. * @Inject
  27. * @var UserBindTelServiceInterface
  28. */
  29. protected $userBindTelService;
  30. /**
  31. * @Inject
  32. * @var CouponRecServiceInterface
  33. */
  34. protected $couponRecService;
  35. /**
  36. * @Inject
  37. * @var AppointmentTimeServiceInterface
  38. */
  39. protected $appointmentTimeService;
  40. /**
  41. * @Inject
  42. * @var OrderOnlineServiceInterface
  43. */
  44. protected $orderOnlineService;
  45. /**
  46. * @Inject
  47. * @var ShopCartServiceInterface
  48. */
  49. protected $shopCartService;
  50. /**
  51. * @Inject
  52. * @var SeparateAccountsServiceInterface
  53. */
  54. protected $separateAccountsService;
  55. /*
  56. * 如果没有绑手机号去绑定页面
  57. * 收货地址接口
  58. * 返回预约送达时间
  59. * 商品数据接口
  60. * 红包独立接口
  61. * 配送费独立接口 可根据距离动态计算费用
  62. * 增值服务接口
  63. * */
  64. public function review()
  65. {
  66. $userId = $this->request->input('user_id');
  67. $marketId = $this->request->input('market_id');
  68. //判断用户有没有绑定手机
  69. // $telExists = $this->userBindTelService->check($params['user_id']);
  70. // if(!$telExists){
  71. // throw new ErrorCodeException(ErrorCode::NOT_BIND_TEL_ERROR);
  72. // }
  73. //获取用户收货地址
  74. $res['address'] = UserAddress::query()->find(15);
  75. //返回预约送达时间 数组
  76. $res['appointment_time'] = $this->appointmentTimeService->do();
  77. //
  78. $res['store_list'] = $this->shopCartService->do($userId,$marketId);
  79. //获取用户优惠券
  80. $res['coupon']['available'] = $this->couponRecService->allForOrderOlAvailable('181.02',$userId,'',1,'');
  81. $res['coupon']['not_available'] = $this->couponRecService->allForOrderOlAvailable('181.02',$userId,'',1,'');
  82. //获取配送费
  83. $res['distribution_price'] = '5.0';
  84. //增值服务接口
  85. $res['value_added_service'] = [
  86. 'select' => 1,
  87. 'price' => 3.50
  88. ];
  89. return $this->success($res);
  90. }
  91. /**
  92. * 订单详情
  93. * 1、主订单信息,用户配送信息(地址、姓名、电话、配送时间、配送类型)、基础信息(订单ID、订单编号、下单时间、订单金额、付款时间、支付方式、红包优惠、服务站电话、增值服务费)
  94. * 2、子订单以及订单商品,按商户分组,有商户信息(ID、商户名、商户logo),商品信息(id、名字、封面、规格、tag、原价、售价、库存、总销、月销、是否失效、失效原因)
  95. * @param OrderOnlineDetailRequest $request
  96. * @return ResponseInterface
  97. */
  98. public function detailByUser(OrderOnlineDetailRequest $request)
  99. {
  100. $params = $request->validated();
  101. return $this->success(['detail' => $this->orderOnlineService->detailByUser($params['order_id'], $params['user_id'])]);
  102. }
  103. /**
  104. * 下单并进行支付,返回支付参数
  105. * 1、用户传参,用户地址ID,用于获取用户地址经纬度来计算配送费
  106. * 2、用户传参,购物车IDs,去获取购物车里的商品详情
  107. * 3、预约送达时间
  108. * 4、优惠券IDs
  109. * 5、订单总金额,用于校验比对
  110. * 6、下单成功,请求支付
  111. * @param OrderOnlineRequest $request
  112. * @return ResponseInterface
  113. */
  114. public function add(OrderOnlineRequest $request){
  115. // 下单
  116. $params = $request->validated();
  117. $couponIds = isset($params['coupon_ids'])&&$params['coupon_ids'] ? explode(',', $params['coupon_ids']) : [];
  118. $data = $this->orderOnlineService->do(
  119. $params['market_id'],
  120. $params['user_id'],
  121. $params['user_address_id'],
  122. json_decode($params['store_list']),
  123. $params['total_money'],
  124. $params['delivery_time_note'],
  125. $params['service_money'],
  126. $couponIds,
  127. $params['plat']
  128. );
  129. return $this->success(['data' => $data]);
  130. }
  131. /**
  132. * 待支付订单重新发起支付
  133. * 1、用户id、订单id
  134. * 2、发起支付
  135. * @param OrderOnlineStateRequest $request
  136. * @return ResponseInterface
  137. */
  138. public function pay(OrderOnlineStateRequest $request)
  139. {
  140. $params = $request->validated();
  141. $data = $this->orderOnlineService->doPay($params['order_id'], $params['user_id']);
  142. return $this->success(['data' => $data]);
  143. }
  144. /**
  145. * 取消订单
  146. * @param OrderOnlineStateRequest $request
  147. * @return ResponseInterface
  148. */
  149. public function cancel(OrderOnlineStateRequest $request)
  150. {
  151. $params = $request->validated();
  152. $this->orderOnlineService->undo($params['order_id'], $params['user_id']);
  153. return $this->success([]);
  154. }
  155. /**
  156. * 删除订单
  157. * @param OrderOnlineStateRequest $request
  158. * @return ResponseInterface
  159. */
  160. public function del(OrderOnlineStateRequest $request)
  161. {
  162. $params = $request->validated();
  163. $this->orderOnlineService->doDel($params['order_id'], $params['user_id']);
  164. return $this->success([]);
  165. }
  166. /**
  167. * 申请退款
  168. * @param OrderOnlineStateRequest $request
  169. * @return ResponseInterface
  170. */
  171. public function applyRefund(OrderOnlineStateRequest $request)
  172. {
  173. $params = $request->validated();
  174. $this->orderOnlineService->doApplyRefund($params['order_id'], $params['user_id']);
  175. return $this->success([]);
  176. }
  177. /**
  178. * 完成订单
  179. * @param OrderOnlineStateRequest $request
  180. * @return ResponseInterface
  181. */
  182. public function complete(OrderOnlineStateRequest $request)
  183. {
  184. Db::beginTransaction();
  185. try {
  186. $params = $request->validated();
  187. $this->orderOnlineService->doComplete($params['order_id'], $params['user_id']);
  188. $this->separateAccountsService->orderOnlineCompleted($params['order_id'], $params['user_id']);
  189. Db::commit();
  190. return $this->success([]);
  191. } catch (\Exception $e) {
  192. Db::rollBack();
  193. $this->log->event(LogLabel::ORDER_COMPLETE_LOG, ['exception' => $e->getMessage()]);
  194. throw new ErrorCodeException(ErrorCode::ORDER_COMPLETE_FAIL, $e->getMessage());
  195. }
  196. }
  197. }