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.

239 lines
8.0 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
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\Request\v3\UserRequest;
  13. use App\Service\v3\Implementations\PaymentService;
  14. use App\Service\v3\Interfaces\CouponRecServiceInterface;
  15. use App\Service\v3\Interfaces\SeparateAccountsServiceInterface;
  16. use App\Service\v3\Interfaces\ShopCartServiceInterface;
  17. use App\Service\v3\Interfaces\UserAddressServiceInterface;
  18. use Hyperf\DbConnection\Db;
  19. use Hyperf\Di\Annotation\Inject;
  20. use App\Service\v3\Interfaces\OrderOnlineServiceInterface;
  21. use App\Service\v3\Interfaces\UserBindTelServiceInterface;
  22. use App\Service\v3\Interfaces\AppointmentTimeServiceInterface;
  23. use Psr\Http\Message\ResponseInterface;
  24. use App\Model\v3\UserAddress;
  25. class OrderOnlineController extends BaseController
  26. {
  27. /**
  28. * @Inject
  29. * @var UserBindTelServiceInterface
  30. */
  31. protected $userBindTelService;
  32. /**
  33. * @Inject
  34. * @var CouponRecServiceInterface
  35. */
  36. protected $couponRecService;
  37. /**
  38. * @Inject
  39. * @var AppointmentTimeServiceInterface
  40. */
  41. protected $appointmentTimeService;
  42. /**
  43. * @Inject
  44. * @var OrderOnlineServiceInterface
  45. */
  46. protected $orderOnlineService;
  47. /**
  48. * @Inject
  49. * @var ShopCartServiceInterface
  50. */
  51. protected $shopCartService;
  52. /**
  53. * @Inject
  54. * @var SeparateAccountsServiceInterface
  55. */
  56. protected $separateAccountsService;
  57. /**
  58. * @Inject
  59. * @var UserAddressServiceInterface
  60. */
  61. protected $userAddressService;
  62. /*
  63. * 如果没有绑手机号去绑定页面
  64. * 收货地址接口
  65. * 返回预约送达时间
  66. * 商品数据接口
  67. * 红包独立接口
  68. * 配送费独立接口 可根据距离动态计算费用
  69. * 增值服务接口
  70. * */
  71. public function review(UserRequest $request)
  72. {
  73. $userId = $this->request->input('user_id','');
  74. $marketId = $this->request->input('market_id','');
  75. $shopcartIds = $this->request->input('shopcart_ids','');
  76. //判断用户有没有绑定手机
  77. $telExists = $this->userBindTelService->check($userId);
  78. if(!$telExists){
  79. throw new ErrorCodeException(ErrorCode::NOT_BIND_TEL_ERROR);
  80. }
  81. //获取用户收货地址
  82. $address = UserAddress::query()->where('user_id',$userId)
  83. ->orderByDesc('is_default')
  84. ->orderByDesc('updated_at')
  85. ->first();
  86. $res['location'] = [
  87. 'address' => $address,
  88. 'distribution_price' => 0
  89. ];
  90. //返回预约送达时间 数组
  91. $res['appointment_time'] = $this->appointmentTimeService->get($shopcartIds);
  92. $res['store_list'] = $this->shopCartService->getGoodsByShopcartId($shopcartIds);
  93. //获取用户优惠券
  94. $res['coupon'] = $this->couponRecService->allForOnlineOrderAvailable($userId, $marketId);
  95. //增值服务接口
  96. $res['value_added_service'] = [
  97. 'text' => '增值服务包含洗菜、切菜、打包等所产生的人工成本费及包装费。',
  98. 'select' => 0,
  99. 'price' => 0,
  100. 'original_price' => 3.5
  101. ];
  102. $total = 0;
  103. foreach ($res['store_list'] as $store)
  104. {
  105. $total = bcadd($total,$store['subtotal'],2);
  106. }
  107. $total = bcadd($total,$res['value_added_service']['price'],2);
  108. $total = bcadd($total,$res['location']['distribution_price'],2);
  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['global_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['global_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['global_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['global_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['global_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['global_order_id'], $params['user_id']);
  209. $this->separateAccountsService->orderOnlineCompleted($params['global_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. }