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.

238 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
  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('user_id',$userId)
  82. ->orderByDesc('is_default')
  83. ->orderByDesc('updated_at')
  84. ->first();
  85. $res['location'] = [
  86. 'address' => $address,
  87. 'distribution_price' => 0
  88. ];
  89. //返回预约送达时间 数组
  90. $res['appointment_time'] = $this->appointmentTimeService->get($shopcartIds);
  91. $res['store_list'] = $this->shopCartService->getGoodsByShopcartId($shopcartIds);
  92. //获取用户优惠券
  93. $res['coupon'] = $this->couponRecService->allForOnlineOrderAvailable($userId, $marketId);
  94. //增值服务接口
  95. $res['value_added_service'] = [
  96. 'text' => '勾选本条款视为您同意接受懒族生活为您提供增值服务的费用,包括但不仅限于洗菜,切菜,装菜所产生的打包费用,送货上门等增值服务',
  97. 'select' => 1,
  98. 'price' => 3.50,
  99. 'original_price' => 0.00
  100. ];
  101. $total = 0;
  102. foreach ($res['store_list'] as $store)
  103. {
  104. $total = bcadd($total,$store['subtotal'],2);
  105. }
  106. $total = bcadd($total,$res['value_added_service']['price'],2);
  107. $total = bcadd($total,$res['location']['distribution_price'],2);
  108. $res['total'] = $total;
  109. return $this->success($res);
  110. }
  111. /**
  112. * 订单详情
  113. * 1、主订单信息,用户配送信息(地址、姓名、电话、配送时间、配送类型)、基础信息(订单ID、订单编号、下单时间、订单金额、付款时间、支付方式、红包优惠、服务站电话、增值服务费)
  114. * 2、子订单以及订单商品,按商户分组,有商户信息(ID、商户名、商户logo),商品信息(id、名字、封面、规格、tag、原价、售价、库存、总销、月销、是否失效、失效原因)
  115. * @param OrderOnlineDetailRequest $request
  116. * @return ResponseInterface
  117. */
  118. public function detailByUser(OrderOnlineDetailRequest $request)
  119. {
  120. $params = $request->validated();
  121. return $this->success(['detail' => $this->orderOnlineService->detailByUser($params['global_order_id'], $params['user_id'])]);
  122. }
  123. /**
  124. * 下单并进行支付,返回支付参数
  125. * 1、用户传参,用户地址ID,用于获取用户地址经纬度来计算配送费
  126. * 2、用户传参,购物车IDs,去获取购物车里的商品详情
  127. * 3、预约送达时间
  128. * 4、优惠券IDs
  129. * 5、订单总金额,用于校验比对
  130. * 6、下单成功,请求支付
  131. * @param OrderOnlineRequest $request
  132. * @return ResponseInterface
  133. */
  134. public function add(OrderOnlineRequest $request){
  135. // 下单
  136. $params = $request->validated();
  137. $couponIds = isset($params['coupon_ids'])&&$params['coupon_ids'] ? explode(',', $params['coupon_ids']) : [];
  138. $data = $this->orderOnlineService->do(
  139. $params['market_id'],
  140. $params['user_id'],
  141. $params['user_address_id'],
  142. json_decode($params['store_list']),
  143. $params['total_money'],
  144. $params['delivery_time_note'],
  145. $params['service_money'],
  146. $couponIds,
  147. $params['plat']
  148. );
  149. return $this->success(['data' => $data]);
  150. }
  151. /**
  152. * 待支付订单重新发起支付
  153. * 1、用户id、订单id
  154. * 2、发起支付
  155. * @param OrderOnlineStateRequest $request
  156. * @return ResponseInterface
  157. */
  158. public function pay(OrderOnlineStateRequest $request)
  159. {
  160. $params = $request->validated();
  161. $data = $this->orderOnlineService->doPay($params['global_order_id'], $params['user_id']);
  162. return $this->success(['data' => $data]);
  163. }
  164. /**
  165. * 取消订单
  166. * @param OrderOnlineStateRequest $request
  167. * @return ResponseInterface
  168. */
  169. public function cancel(OrderOnlineStateRequest $request)
  170. {
  171. $params = $request->validated();
  172. $this->orderOnlineService->undo($params['global_order_id'], $params['user_id']);
  173. return $this->success([]);
  174. }
  175. /**
  176. * 删除订单
  177. * @param OrderOnlineStateRequest $request
  178. * @return ResponseInterface
  179. */
  180. public function del(OrderOnlineStateRequest $request)
  181. {
  182. $params = $request->validated();
  183. $this->orderOnlineService->doDel($params['global_order_id'], $params['user_id']);
  184. return $this->success([]);
  185. }
  186. /**
  187. * 申请退款
  188. * @param OrderOnlineStateRequest $request
  189. * @return ResponseInterface
  190. */
  191. public function applyRefund(OrderOnlineStateRequest $request)
  192. {
  193. $params = $request->validated();
  194. $this->orderOnlineService->doApplyRefund($params['global_order_id'], $params['user_id']);
  195. return $this->success([]);
  196. }
  197. /**
  198. * 完成订单
  199. * @param OrderOnlineStateRequest $request
  200. * @return ResponseInterface
  201. */
  202. public function complete(OrderOnlineStateRequest $request)
  203. {
  204. Db::beginTransaction();
  205. try {
  206. $params = $request->validated();
  207. $this->orderOnlineService->doComplete($params['global_order_id'], $params['user_id']);
  208. $this->separateAccountsService->orderOnlineCompleted($params['global_order_id'], $params['user_id']);
  209. Db::commit();
  210. return $this->success([]);
  211. } catch (\Exception $e) {
  212. Db::rollBack();
  213. $this->log->event(LogLabel::ORDER_COMPLETE_LOG, ['exception' => $e->getMessage()]);
  214. throw new ErrorCodeException(ErrorCode::ORDER_COMPLETE_FAIL, $e->getMessage());
  215. }
  216. }
  217. }