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.

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