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.

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