Ade.php 3.1 KB

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