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.

214 lines
7.1 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
  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. $params = $this->request->all();
  67. //判断用户有没有绑定手机
  68. // $telExists = $this->userBindTelService->check($params['user_id']);
  69. // if(!$telExists){
  70. // throw new ErrorCodeException(ErrorCode::NOT_BIND_TEL_ERROR);
  71. // }
  72. //获取用户收货地址
  73. $res['address'] = UserAddress::query()->find(15);
  74. //返回预约送达时间 数组
  75. $res['appointment_time'] = $this->appointmentTimeService->do();
  76. //
  77. $res['store_list'] = $this->shopCartService->do();
  78. //获取用户优惠券
  79. $res['coupon']['available'] = $this->couponRecService->allForOrderOlAvailable('181.02',$params['user_id'],'',1,'','');
  80. $res['coupon']['not_available'] = $this->couponRecService->allForOrderOlAvailable('181.02',$params['user_id'],'',1,'','');
  81. //获取配送费
  82. $res['distribution_price'] = '5.0';
  83. //增值服务接口
  84. $res['value_added_service'] = [
  85. 'select' => 1,
  86. 'price' => 3.50
  87. ];
  88. return $this->success($res);
  89. }
  90. /**
  91. * 订单详情
  92. * 1、主订单信息,用户配送信息(地址、姓名、电话、配送时间、配送类型)、基础信息(订单ID、订单编号、下单时间、订单金额、付款时间、支付方式、红包优惠、服务站电话、增值服务费)
  93. * 2、子订单以及订单商品,按商户分组,有商户信息(ID、商户名、商户logo),商品信息(id、名字、封面、规格、tag、原价、售价、库存、总销、月销、是否失效、失效原因)
  94. * @param OrderOnlineDetailRequest $request
  95. * @return ResponseInterface
  96. */
  97. public function detailByUser(OrderOnlineDetailRequest $request)
  98. {
  99. $params = $request->validated();
  100. return $this->success(['detail' => $this->orderOnlineService->detailByUser($params['order_id'], $params['user_id'])]);
  101. }
  102. /**
  103. * 下单并进行支付,返回支付参数
  104. * 1、用户传参,用户地址ID,用于获取用户地址经纬度来计算配送费
  105. * 2、用户传参,购物车IDs,去获取购物车里的商品详情
  106. * 3、预约送达时间
  107. * 4、优惠券IDs
  108. * 5、订单总金额,用于校验比对
  109. * 6、下单成功,请求支付
  110. * @param OrderOnlineRequest $request
  111. * @return ResponseInterface
  112. */
  113. public function add(OrderOnlineRequest $request){
  114. // 下单
  115. $params = $request->validated();
  116. $couponIds = isset($params['coupon_ids'])&&$params['coupon_ids'] ? explode(',', $params['coupon_ids']) : [];
  117. $data = $this->orderOnlineService->do(
  118. $params['market_id'],
  119. $params['user_id'],
  120. $params['user_address_id'],
  121. json_decode($params['store_list']),
  122. $params['total_money'],
  123. $params['delivery_time_note'],
  124. $params['service_money'],
  125. $couponIds,
  126. $params['plat']
  127. );
  128. return $this->success(['data' => $data]);
  129. }
  130. /**
  131. * 待支付订单重新发起支付
  132. * 1、用户id、订单id
  133. * 2、发起支付
  134. * @param OrderOnlineStateRequest $request
  135. * @return ResponseInterface
  136. */
  137. public function pay(OrderOnlineStateRequest $request)
  138. {
  139. $params = $request->validated();
  140. $data = $this->orderOnlineService->doPay($params['order_id'], $params['user_id']);
  141. return $this->success(['data' => $data]);
  142. }
  143. /**
  144. * 取消订单
  145. * @param OrderOnlineStateRequest $request
  146. * @return ResponseInterface
  147. */
  148. public function cancel(OrderOnlineStateRequest $request)
  149. {
  150. $params = $request->validated();
  151. $this->orderOnlineService->undo($params['order_id'], $params['user_id']);
  152. return $this->success([]);
  153. }
  154. /**
  155. * 删除订单
  156. * @param OrderOnlineStateRequest $request
  157. * @return ResponseInterface
  158. */
  159. public function del(OrderOnlineStateRequest $request)
  160. {
  161. $params = $request->validated();
  162. $this->orderOnlineService->doDel($params['order_id'], $params['user_id']);
  163. return $this->success([]);
  164. }
  165. /**
  166. * 申请退款
  167. * @param OrderOnlineStateRequest $request
  168. * @return ResponseInterface
  169. */
  170. public function applyRefund(OrderOnlineStateRequest $request)
  171. {
  172. $params = $request->validated();
  173. $this->orderOnlineService->doApplyRefund($params['order_id'], $params['user_id']);
  174. return $this->success([]);
  175. }
  176. /**
  177. * 完成订单
  178. * @param OrderOnlineStateRequest $request
  179. * @return ResponseInterface
  180. */
  181. public function complete(OrderOnlineStateRequest $request)
  182. {
  183. Db::beginTransaction();
  184. try {
  185. $params = $request->validated();
  186. $this->orderOnlineService->doComplete($params['order_id'], $params['user_id']);
  187. $this->separateAccountsService->orderOnlineCompleted($params['order_id'], $params['user_id']);
  188. Db::commit();
  189. return $this->success([]);
  190. } catch (\Exception $e) {
  191. Db::rollBack();
  192. $this->log->event(LogLabel::ORDER_COMPLETE_LOG, ['exception' => $e->getMessage()]);
  193. throw new ErrorCodeException(ErrorCode::ORDER_COMPLETE_FAIL, $e->getMessage());
  194. }
  195. }
  196. }