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.

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