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.

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