Shop.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. *
  4. * @copyright ©2020 点小铺
  5. * @author hanj
  6. * @link: https://dyuit.com
  7. * Created by VSCode
  8. */
  9. namespace app\api\model\store;
  10. use app\common\model\store\Shop as ShopModel;
  11. /**
  12. * 商家门店模型
  13. * Class Shop
  14. * @package app\store\model\store
  15. */
  16. class Shop extends ShopModel
  17. {
  18. /**
  19. * 隐藏字段
  20. * @var array
  21. */
  22. protected $hidden = [
  23. 'is_delete',
  24. 'wxapp_id',
  25. 'create_time',
  26. 'update_time'
  27. ];
  28. /**
  29. * 获取门店列表
  30. * @param null $is_check
  31. * @param string $longitude
  32. * @param string $latitude
  33. * @param bool $limit
  34. * @return array|false|\PDOStatement|string|\think\Collection
  35. * @throws \think\db\exception\DataNotFoundException
  36. * @throws \think\db\exception\ModelNotFoundException
  37. * @throws \think\exception\DbException
  38. */
  39. public function getList($is_check = null, $longitude = '', $latitude = '', $limit = false)
  40. {
  41. // 是否支持自提核销
  42. $is_check && $this->where('is_check', '=', $is_check);
  43. // 获取数量
  44. $limit != false && $this->limit($limit);
  45. // 获取门店列表数据
  46. $data = $this->where('is_delete', '=', '0')
  47. ->where('status', '=', '1')
  48. ->order(['sort' => 'asc', 'create_time' => 'desc'])
  49. ->select();
  50. // 根据距离排序
  51. if (!empty($longitude) && !empty($latitude)) {
  52. return $this->sortByDistance($data, $longitude, $latitude);
  53. }
  54. return $data;
  55. }
  56. /**
  57. * 根据距离排序
  58. * @param string $longitude
  59. * @param string $latitude
  60. * @param \think\Collection|false|\PDOStatement|string $data
  61. * @return array
  62. * @throws
  63. */
  64. private function sortByDistance(&$data, $longitude, $latitude)
  65. {
  66. // 根据距离排序
  67. $list = $data->isEmpty() ? [] : $data->toArray();
  68. $sortArr = [];
  69. foreach ($list as &$shop) {
  70. // 计算距离
  71. $distance = self::getDistance($longitude, $latitude, $shop['longitude'], $shop['latitude']);
  72. // 排序列
  73. $sortArr[] = $distance;
  74. $shop['distance'] = $distance;
  75. if ($distance >= 1000) {
  76. $distance = bcdiv($distance, 1000, 2);
  77. $shop['distance_unit'] = $distance . 'km';
  78. } else
  79. $shop['distance_unit'] = $distance . 'm';
  80. }
  81. // 根据距离排序
  82. array_multisort($sortArr, SORT_ASC, $list);
  83. return $list;
  84. }
  85. /**
  86. * 获取两个坐标点的距离
  87. * @param $ulon
  88. * @param $ulat
  89. * @param $slon
  90. * @param $slat
  91. * @return float
  92. */
  93. private static function getDistance($ulon, $ulat, $slon, $slat)
  94. {
  95. // 地球半径
  96. $R = 6378137;
  97. // 将角度转为狐度
  98. $radLat1 = deg2rad($ulat);
  99. $radLat2 = deg2rad($slat);
  100. $radLng1 = deg2rad($ulon);
  101. $radLng2 = deg2rad($slon);
  102. // 结果
  103. $s = acos(cos($radLat1) * cos($radLat2) * cos($radLng1 - $radLng2) + sin($radLat1) * sin($radLat2)) * $R;
  104. // 精度
  105. $s = round($s * 10000) / 10000;
  106. return round($s);
  107. }
  108. /**
  109. * 根据门店id集获取门店列表
  110. * @param $shopIds
  111. * @return false|\PDOStatement|string|\think\Collection
  112. * @throws \think\db\exception\DataNotFoundException
  113. * @throws \think\db\exception\ModelNotFoundException
  114. * @throws \think\exception\DbException
  115. */
  116. public function getListByIds($shopIds)
  117. {
  118. // 筛选条件
  119. $filter = ['shop_id' => ['in', $shopIds]];
  120. if (!empty($shopIds)) {
  121. $this->orderRaw('field(shop_id, ' . implode(',', $shopIds) . ')');
  122. }
  123. // 获取商品列表数据
  124. return $this->with(['logo'])
  125. ->where('is_delete', '=', '0')
  126. ->where('status', '=', '1')
  127. ->where($filter)
  128. ->select();
  129. }
  130. }