Store.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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, $shop_supplier_id = 0)
  22. {
  23. $model = $this;
  24. // 是否支持自提核销
  25. $is_check && $model = $model->where('is_check', '=', $is_check);
  26. // 商家id
  27. $shop_supplier_id && $model = $model->where('shop_supplier_id', '=', $shop_supplier_id);
  28. // 获取数量
  29. $limit != false && $model = $model->limit($limit);
  30. // 获取门店列表数据
  31. $data = $model->where('is_delete', '=', '0')
  32. ->where('status', '=', '1')
  33. ->order(['sort' => 'asc', 'create_time' => 'desc'])
  34. ->select();
  35. // 根据距离排序
  36. if (!empty($longitude) && !empty($latitude)) {
  37. return $this->sortByDistance($data, $longitude, $latitude);
  38. }
  39. return $data;
  40. }
  41. /**
  42. * 根据距离排序
  43. */
  44. private function sortByDistance(&$data, $longitude, $latitude)
  45. {
  46. // 根据距离排序
  47. $list = $data->isEmpty() ? [] : $data->toArray();
  48. $sortArr = [];
  49. foreach ($list as &$store) {
  50. // 计算距离
  51. $distance = self::getDistance($longitude, $latitude, $store['longitude'], $store['latitude']);
  52. // 排序列
  53. $sortArr[] = $distance;
  54. $store['distance'] = $distance;
  55. if ($distance >= 1000) {
  56. $distance = bcdiv($distance, 1000, 2);
  57. $store['distance_unit'] = $distance . 'km';
  58. } else
  59. $store['distance_unit'] = $distance . 'm';
  60. }
  61. // 根据距离排序
  62. array_multisort($sortArr, SORT_ASC, $list);
  63. return $list;
  64. }
  65. /**
  66. * 获取两个坐标点的距离
  67. */
  68. private static function getDistance($ulon, $ulat, $slon, $slat)
  69. {
  70. // 地球半径
  71. $R = 6378137;
  72. // 将角度转为狐度
  73. $radLat1 = deg2rad($ulat);
  74. $radLat2 = deg2rad($slat);
  75. $radLng1 = deg2rad($ulon);
  76. $radLng2 = deg2rad($slon);
  77. // 结果
  78. $s = acos(cos($radLat1) * cos($radLat2) * cos($radLng1 - $radLng2) + sin($radLat1) * sin($radLat2)) * $R;
  79. // 精度
  80. $s = round($s * 10000) / 10000;
  81. return round($s);
  82. }
  83. /**
  84. * 根据门店id集获取门店列表
  85. */
  86. public function getListByIds($storeIds)
  87. {
  88. $model = $this;
  89. // 筛选条件
  90. $filter = ['store_id' => ['in', $storeIds]];
  91. if (!empty($storeIds)) {
  92. $model = $model->orderRaw('field(store_id, ' . implode(',', $storeIds) . ')');
  93. }
  94. // 获取商品列表数据
  95. return $model->with(['logo'])
  96. ->where('is_delete', '=', '0')
  97. ->where('status', '=', '1')
  98. ->where($filter)
  99. ->select();
  100. }
  101. }