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.

307 lines
11 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
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. $distance = 0;
  97. if(!empty($address->lng) && !empty($address->lat)){
  98. $distance = $this->locationService->getDistanceByTencent($market->lng,$market->lat,$address->lng,$address->lat);
  99. }
  100. $deliveryDistance = config('distance.delivery_distance');
  101. if(isset($distance) && $distance < $deliveryDistance){
  102. if($distance >= 1000){
  103. $distance_text = '距您收货地址 ' . bcdiv($distance,1000,2) . 'km';
  104. }else{
  105. $distance_text = '距您收货地址 ' . $distance . 'm';
  106. }
  107. $distributionPrice = $this->distributionPriceService->do($distance);
  108. $originalPrice = $this->distributionPriceService->original($distance);
  109. $res['location'] = [
  110. 'address' => $address,
  111. 'distribution_price' => $distributionPrice,
  112. 'original_price' => $originalPrice,
  113. 'distance' => $distance,
  114. 'within' => true,
  115. 'style' => 'strike',
  116. // 'distribution_text' => '¥ '.$distributionPrice .'(' .$distance_text .')'
  117. 'distribution_text' => $distance_text,
  118. 'distribution_price_text' => '¥ '.$distributionPrice,
  119. 'original_price_text' => $originalPrice > $distributionPrice ? '¥ '.$originalPrice : ''
  120. ];
  121. }else{
  122. $res['location'] = [
  123. 'address' => '',
  124. 'distribution_price' => 0,
  125. 'original_price' => 0,
  126. 'distance' => $distance,
  127. 'within' => false,
  128. 'style' => 'strike',
  129. 'distribution_text' => '',
  130. 'distribution_price_text' => '¥ 0',
  131. 'original_price_text' => ''
  132. ];
  133. }
  134. //返回预约送达时间 数组
  135. $res['appointment_time'] = $this->appointmentTimeService->get($shopcartIds);
  136. $res['store_list'] = $this->shopCartService->getGoodsByShopcartId($shopcartIds);
  137. //获取用户优惠券
  138. $coupons = $this->couponRecService->allForOnlineOrderAvailable( $userId, $marketId, explode(',', $shopcartIds) );
  139. $res['coupon'] = [
  140. 'available' => [],
  141. 'not_available' => [],
  142. ];
  143. foreach ($coupons as $key => &$item) {
  144. foreach ($item as $k => &$rec) {
  145. for ($i=0; $i<$rec->number_remain; $i++) {
  146. if ($key == 'available') {
  147. $res['coupon']['available'][] = $rec;
  148. } elseif ($key == 'not_available') {
  149. $res['coupon']['not_available'][] = $rec;
  150. }
  151. }
  152. }
  153. }
  154. // $res['coupon'] = $coupons;
  155. //增值服务接口
  156. $res['value_added_service'] = [
  157. 'text' => '增值服务包含洗菜、切菜、打包等所产生的人工成本费及包装费。',
  158. 'select' => 0,
  159. 'price' => 0,
  160. 'original_price' => 3.5
  161. ];
  162. $total = 0;
  163. foreach ($res['store_list'] as $store)
  164. {
  165. $total = bcadd($total,$store['subtotal'],2);
  166. }
  167. $total = bcadd($total,$res['value_added_service']['price'],2);
  168. $total = bcadd($total,$res['location']['distribution_price'],2);
  169. $res['total'] = $total;
  170. // 新增返回市场信息
  171. $res['market'] = $market;
  172. return $this->success($res);
  173. }
  174. /**
  175. * 订单详情
  176. * 1、主订单信息,用户配送信息(地址、姓名、电话、配送时间、配送类型)、基础信息(订单ID、订单编号、下单时间、订单金额、付款时间、支付方式、红包优惠、服务站电话、增值服务费)
  177. * 2、子订单以及订单商品,按商户分组,有商户信息(ID、商户名、商户logo),商品信息(id、名字、封面、规格、tag、原价、售价、库存、总销、月销、是否失效、失效原因)
  178. * @param OrderOnlineDetailRequest $request
  179. * @return ResponseInterface
  180. */
  181. public function detailByUser(OrderOnlineDetailRequest $request)
  182. {
  183. $params = $request->validated();
  184. return $this->success(['detail' => $this->orderOnlineService->detailByUser($params['global_order_id'], $params['user_id'])]);
  185. }
  186. /**
  187. * 下单并进行支付,返回支付参数
  188. * 1、用户传参,用户地址ID,用于获取用户地址经纬度来计算配送费
  189. * 2、用户传参,购物车IDs,去获取购物车里的商品详情
  190. * 3、预约送达时间
  191. * 4、优惠券IDs
  192. * 5、订单总金额,用于校验比对
  193. * 6、下单成功,请求支付
  194. * @param OrderOnlineRequest $request
  195. * @return ResponseInterface
  196. */
  197. public function add(OrderOnlineRequest $request){
  198. // 下单
  199. $params = $request->validated();
  200. $couponIds = isset($params['coupon_ids'])&&$params['coupon_ids'] ? explode(',', $params['coupon_ids']) : [];
  201. $data = $this->orderOnlineService->do(
  202. $params['market_id'],
  203. $params['user_id'],
  204. ($params['user_address_id'] ?? 0),
  205. json_decode($params['store_list']),
  206. $params['total_money'],
  207. $params['delivery_time_note'],
  208. ($params['service_money'] ?? 0),
  209. $couponIds,
  210. $params['plat'] ?: '',
  211. ($params['self_take'] ?? 0)
  212. );
  213. return $this->success(['data' => $data]);
  214. }
  215. /**
  216. * 待支付订单重新发起支付
  217. * 1、用户id、订单id
  218. * 2、发起支付
  219. * @param OrderOnlineStateRequest $request
  220. * @return ResponseInterface
  221. */
  222. public function pay(OrderOnlineStateRequest $request)
  223. {
  224. $params = $request->validated();
  225. $data = $this->orderOnlineService->doPay($params['global_order_id'], $params['user_id']);
  226. return $this->success(['data' => $data]);
  227. }
  228. /**
  229. * 取消订单
  230. * @param OrderOnlineStateRequest $request
  231. * @return ResponseInterface
  232. */
  233. public function cancel(OrderOnlineStateRequest $request)
  234. {
  235. $params = $request->validated();
  236. $this->orderOnlineService->undo($params['global_order_id'], $params['user_id']);
  237. return $this->success([]);
  238. }
  239. /**
  240. * 删除订单
  241. * @param OrderOnlineStateRequest $request
  242. * @return ResponseInterface
  243. */
  244. public function del(OrderOnlineStateRequest $request)
  245. {
  246. $params = $request->validated();
  247. $this->orderOnlineService->doDel($params['global_order_id'], $params['user_id']);
  248. return $this->success([]);
  249. }
  250. /**
  251. * 申请退款
  252. * @param OrderOnlineStateRequest $request
  253. * @return ResponseInterface
  254. */
  255. public function applyRefund(OrderOnlineStateRequest $request)
  256. {
  257. $params = $request->validated();
  258. $this->orderOnlineService->doApplyRefund($params['global_order_id'], $params['user_id']);
  259. return $this->success([]);
  260. }
  261. /**
  262. * 完成订单
  263. * @param OrderOnlineStateRequest $request
  264. * @return ResponseInterface
  265. */
  266. public function complete(OrderOnlineStateRequest $request)
  267. {
  268. Db::beginTransaction();
  269. try {
  270. $params = $request->validated();
  271. $this->orderOnlineService->doComplete($params['global_order_id'], $params['user_id']);
  272. $this->separateAccountsService->orderOnlineCompleted($params['global_order_id'], $params['user_id']);
  273. Db::commit();
  274. return $this->success([]);
  275. } catch (\Exception $e) {
  276. Db::rollBack();
  277. $this->log->event(LogLabel::ORDER_COMPLETE_LOG, ['exception' => $e->getMessage()]);
  278. throw new ErrorCodeException(ErrorCode::ORDER_COMPLETE_FAIL);
  279. }
  280. }
  281. }