Institution.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace app\admin\model\institution;
  3. use think\Model;
  4. class Institution extends Model
  5. {
  6. // 表名
  7. protected $name = 'institution';
  8. // 自动写入时间戳字段
  9. protected $autoWriteTimestamp = 'int';
  10. // 定义时间戳字段名
  11. protected $createTime = 'createtime';
  12. protected $updateTime = 'updatetime';
  13. protected $deleteTime = false;
  14. // 追加属性
  15. protected $append = [
  16. 'status_text'
  17. ];
  18. public function getStatusList()
  19. {
  20. return ['-1' => __('Status -1'), '0' => __('Status 0'), '1' => __('Status 1')];
  21. }
  22. public function getStatusTextAttr($value, $data)
  23. {
  24. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  25. $list = $this->getStatusList();
  26. return isset($list[$value]) ? $list[$value] : '';
  27. }
  28. /**
  29. * 获取排序sql
  30. * @param $latitude
  31. * @param $longitude
  32. * @return mixed
  33. */
  34. public static function distanceSql($latitude, $longitude)
  35. {
  36. $field = "(round(6367000 * 2 * asin(sqrt(pow(sin(((latitude * pi()) / 180 - ({$latitude} * pi()) / 180) / 2), 2) + cos(({$latitude} * pi()) / 180) * cos((latitude * pi()) / 180) * pow(sin(((longitude * pi()) / 180 - ({$longitude} * pi()) / 180) / 2), 2))))) AS distance";
  37. return $field;
  38. }
  39. /**
  40. * 门店列表
  41. * @return mixed
  42. */
  43. public static function lst($cid,$latitude, $longitude, $page, $limit)
  44. {
  45. $model = new self();
  46. $model = $model->where('status', 1);
  47. $model = $model->where('cid',$cid);
  48. if ($latitude && $longitude) {
  49. $model = $model->field(['*', self::distanceSql($latitude, $longitude)])->order('distance asc');
  50. }
  51. $list = $model->page((int)$page, (int)$limit)
  52. ->select();
  53. if ($latitude && $longitude) {
  54. foreach ($list as &$value) {
  55. //计算距离
  56. $value['distance'] = self::getDistance($latitude, $longitude, $value['latitude'], $value['longitude']);
  57. //转换单位
  58. $value['range'] = bcdiv($value['distance'], 1000, 1);
  59. }
  60. }
  61. return $list;
  62. }
  63. public static function getDistance($lat1, $lng1, $lat2, $lng2)
  64. {
  65. $earthRadius = 6367000; //approximate radius of earth in meters
  66. $lat1 = ($lat1 * pi() ) / 180;
  67. $lng1 = ($lng1 * pi() ) / 180;
  68. $lat2 = ($lat2 * pi() ) / 180;
  69. $lng2 = ($lng2 * pi() ) / 180;
  70. $calcLongitude = $lng2 - $lng1;
  71. $calcLatitude = $lat2 - $lat1;
  72. $stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);
  73. $stepTwo = 2 * asin(min(1, sqrt($stepOne)));
  74. $calculatedDistance = $earthRadius * $stepTwo;
  75. return round($calculatedDistance);
  76. }
  77. }