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