Shop.php 3.8 KB

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