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.

164 lines
4.9 KiB

6 years ago
6 years ago
5 years ago
6 years ago
6 years ago
6 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 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
6 years ago
  1. <?php
  2. namespace App\Service\v3\Implementations;
  3. use App\Constants\v3\ErrorCode;
  4. use App\Exception\ErrorCodeException;
  5. use App\Model\v3\Market;
  6. use App\Model\v3\UserAddress;
  7. use App\Service\v3\Interfaces\DistributionPriceServiceInterface;
  8. use App\Service\v3\Interfaces\LocationServiceInterface;
  9. use App\Service\v3\Interfaces\ShopCartServiceInterface;
  10. use App\Service\v3\Interfaces\UserAddressServiceInterface;
  11. use App\Service\v3\SfExpress;
  12. /** @var Inject 注解使用 */
  13. use Hyperf\Di\Annotation\Inject;
  14. class UserAddressService implements UserAddressServiceInterface
  15. {
  16. /**
  17. * @Inject
  18. * @var LocationServiceInterface
  19. */
  20. protected $locationService;
  21. /**
  22. * @Inject
  23. * @var ShopCartServiceInterface
  24. */
  25. protected $shopCartService;
  26. /**
  27. * @Inject
  28. * @var DistributionPriceServiceInterface
  29. */
  30. protected $distributionPriceService;
  31. public function do($userAddressId,$user_id,$user_name,$address,$doorplate,$gender,$lat,$lng,$tel,$tags)
  32. {
  33. $userAddress = UserAddress::updateOrCreate(['id' => $userAddressId],
  34. [
  35. 'user_id' => $user_id,
  36. 'user_name' => $user_name,
  37. 'address' => $address,
  38. 'doorplate' => $doorplate,
  39. 'gender' => $gender,
  40. 'lat' => $lat,
  41. 'lng' => $lng,
  42. 'tel' => $tel,
  43. 'tags' => $tags
  44. ]);
  45. return $userAddress;
  46. }
  47. public function check()
  48. {
  49. }
  50. public function undo($userAddressId)
  51. {
  52. return UserAddress::destroy($userAddressId);
  53. }
  54. public function setDefault($userId,$userAddressId)
  55. {
  56. UserAddress::query()->where([
  57. ['user_id','=',$userId],
  58. ['is_default','=',1],
  59. ])->update(['is_default' => 0]);
  60. $userAddress = UserAddress::query()->find($userAddressId);
  61. $userAddress->is_default = 1;
  62. return $userAddress->save();
  63. }
  64. public function get($userAddressId)
  65. {
  66. $address['address'] = UserAddress::query()->find($userAddressId);
  67. $address['tags'] = $this->getTags();
  68. return $address;
  69. }
  70. public function getList($userId)
  71. {
  72. return UserAddress::query()->where('user_id',$userId)->get();
  73. }
  74. /**
  75. * @param $userAddressId
  76. * @param $marketId
  77. * @param string $shopCartIds
  78. * @param string $deliveryTimeNote
  79. * @return array
  80. */
  81. public function getAddressAndDistributionPrice($userAddressId, $marketId, string $shopCartIds, string $deliveryTimeNote)
  82. {
  83. $address = $this->get($userAddressId);
  84. $market = Market::find($marketId);
  85. if(empty($address['address']->lng) || empty($address['address']->lat) || empty($market->lng) || empty($market->lat)){
  86. throw new ErrorCodeException(ErrorCode::LOCATION_USER_ADDRESS);
  87. }
  88. $distance = $this->locationService->getDistanceByTencent($market->lng,$market->lat,$address['address']->lng,$address['address']->lat);
  89. $storeList = $this->shopCartService->getGoodsByShopcartId($shopCartIds);
  90. $weight = 0;
  91. foreach ($storeList as $item) {
  92. foreach ($item['shopping_cart'] as $v) {
  93. $weight += $v['goods']['weight'] * $v['num'];
  94. }
  95. }
  96. $sfParams = [
  97. 'user_lng' => $address['address']['lng'],
  98. 'user_lat' => $address['address']['lat'],
  99. 'user_address' => $address['address']['address'],
  100. 'weight' => $weight,
  101. 'shop' => [
  102. 'shop_name' => $market->name,
  103. 'shop_phone' => $market->tel,
  104. 'shop_address' => $market->address,
  105. 'shop_lng' => $market->lng,
  106. 'shop_lat' => $market->lat,
  107. ],
  108. ];
  109. # 预约单处理
  110. $sfParams = array_merge($sfParams, SfExpress::getInstance()->deliveryTimeNote2expectTime($deliveryTimeNote));
  111. $distributionPrice = SfExpress::getInstance()->getDeliveryCost($sfParams);
  112. $originalPrice = SfExpress::getInstance()->getOriginDeliveryCost($distributionPrice);
  113. if($distance >= 1000){
  114. $distance_text = '距您收货地址 ' . bcdiv($distance,1000,2) . 'km';
  115. }else{
  116. $distance_text = '距您收货地址 ' . $distance . 'm';
  117. }
  118. /**
  119. * distributionPrice 配送费
  120. * originalPrice 配送费原价
  121. * style 前端输出样式
  122. */
  123. $res['address'] = $address;
  124. $res['delivery_distance'] = $distance;
  125. $res['distribution_price'] = $distributionPrice;
  126. $res['original_price'] = $originalPrice;
  127. $res['style'] = 'strike';
  128. // $res['distribution_text'] = '¥ '.$distributionPrice .'(' .$distance_text .')';
  129. $res['distribution_text'] = $distance_text;
  130. $res['distribution_price_text'] = '¥ '.$distributionPrice;
  131. $res['original_price_text'] = $originalPrice > $distributionPrice ? '¥ '.$originalPrice : '';
  132. return $res;
  133. }
  134. public function getTags()
  135. {
  136. return [
  137. ['id' => 1, 'name' => '家'],
  138. ['id' => 2, 'name' => '父母家'],
  139. ['id' => 3, 'name' => '岳父母家'],
  140. ['id' => 4, 'name' => '公司'],
  141. ['id' => 5, 'name' => '朋友家'],
  142. ];
  143. }
  144. }