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.

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