Store.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace app\api\model\store;
  3. use app\common\model\store\Store as StoreModel;
  4. /**
  5. * 商家门店模型
  6. */
  7. class Store extends StoreModel
  8. {
  9. /**
  10. * 隐藏字段
  11. */
  12. protected $hidden = [
  13. 'is_delete',
  14. 'app_id',
  15. 'create_time',
  16. 'update_time'
  17. ];
  18. /**
  19. * 获取门店列表
  20. */
  21. public function getList($is_check = null, $longitude = '', $latitude = '', $limit = false)
  22. {
  23. $model = $this;
  24. // 是否支持自提核销
  25. $is_check && $model = $model->where('is_check', '=', $is_check);
  26. // 获取数量
  27. $limit != false && $model = $model->limit($limit);
  28. // 获取门店列表数据
  29. $data = $model->where('is_delete', '=', '0')
  30. ->where('status', '=', '1')
  31. ->order(['sort' => 'asc', 'create_time' => 'desc'])
  32. ->select();
  33. // 根据距离排序
  34. if (!empty($longitude) && !empty($latitude)) {
  35. return $this->sortByDistance($data, $longitude, $latitude);
  36. }
  37. return $data;
  38. }
  39. /**
  40. * 根据距离排序
  41. */
  42. private function sortByDistance($data, $longitude, $latitude)
  43. {
  44. // 根据距离排序
  45. $list = $data->isEmpty() ? [] : $data->toArray();
  46. $sortArr = [];
  47. foreach ($list as &$store) {
  48. // 计算距离
  49. $distance = self::getDistance($longitude, $latitude, $store['longitude'], $store['latitude']);
  50. // 排序列
  51. $sortArr[] = $distance;
  52. $store['distance'] = $distance;
  53. if ($distance >= 1000) {
  54. $distance = bcdiv($distance, 1000, 2);
  55. $store['distance_unit'] = $distance . 'km';
  56. } else
  57. $store['distance_unit'] = $distance . 'm';
  58. }
  59. // 根据距离排序
  60. array_multisort($sortArr, SORT_ASC, $list);
  61. return $list;
  62. }
  63. /**
  64. * 获取两个坐标点的距离
  65. */
  66. private static function getDistance($ulon, $ulat, $slon, $slat)
  67. {
  68. // 地球半径
  69. $R = 6378137;
  70. // 将角度转为狐度
  71. $radLat1 = deg2rad($ulat);
  72. $radLat2 = deg2rad($slat);
  73. $radLng1 = deg2rad($ulon);
  74. $radLng2 = deg2rad($slon);
  75. // 结果
  76. $s = acos(cos($radLat1) * cos($radLat2) * cos($radLng1 - $radLng2) + sin($radLat1) * sin($radLat2)) * $R;
  77. // 精度
  78. $s = round($s * 10000) / 10000;
  79. return round($s);
  80. }
  81. /**
  82. * 根据门店id集获取门店列表
  83. */
  84. public function getListByIds($storeIds)
  85. {
  86. // 获取商品列表数据
  87. return $this->with(['logo'])
  88. ->where('is_delete', '=', '0')
  89. ->where('status', '=', '1')
  90. ->where('store_id', 'in', $storeIds)
  91. ->select();
  92. }
  93. }