123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <?php
- namespace app\admin\model\institution;
- use think\Exception;
- use think\Model;
- class Ade extends Model
- {
-
-
- // 表名
- protected $name = 'ade';
-
- // 自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
- protected $deleteTime = false;
- // 追加属性
- protected $append = [
- 'status_text'
- ];
-
- public function getStatusList()
- {
- return ['-1' => __('Status -1'), '0' => __('Status 0'), '1' => __('Status 1')];
- }
- public function getStatusTextAttr($value, $data)
- {
- $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
- $list = $this->getStatusList();
- return isset($list[$value]) ? $list[$value] : '';
- }
- /**
- * 获取排序sql
- * @param $latitude
- * @param $longitude
- * @return mixed
- */
- public static function distanceSql($latitude, $longitude)
- {
- $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";
- return $field;
- }
- /**
- * 门店列表
- * @return mixed
- */
- public static function lst($cid,$latitude, $longitude, $page, $limit)
- {
- $model = new self();
- $model = $model->where('status', 1);
- if($cid>0) $model = $model->where('cid',$cid);
- if ($latitude && $longitude) {
- $model = $model->field(['*', self::distanceSql($latitude, $longitude)])->order('distance asc');
- }
- $list = $model->page((int)$page, (int)$limit)
- ->select();
- if ($latitude && $longitude) {
- foreach ($list as &$value) {
- //计算距离
- if($value['latitude'] && $value['longitude']) {
- $value['distance'] = self::getDistance($latitude, $longitude, trim($value['latitude']), trim($value['longitude']));
- //转换单位
- $value['range'] = bcdiv($value['distance'], 1000, 1);
- }
- }
- }
- return $list;
- }
- public static function getDistance($lat1, $lng1, $lat2, $lng2)
- {
- try {
- $earthRadius = 6367000; //approximate radius of earth in meters
- $lat1 = ($lat1 * pi()) / 180;
- $lng1 = ($lng1 * pi()) / 180;
- $lat2 = ($lat2 * pi()) / 180;
- $lng2 = ($lng2 * pi()) / 180;
- $calcLongitude = $lng2 - $lng1;
- $calcLatitude = $lat2 - $lat1;
- $stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);
- $stepTwo = 2 * asin(min(1, sqrt($stepOne)));
- $calculatedDistance = $earthRadius * $stepTwo;
- return round($calculatedDistance);
- }catch (Exception $exception)
- {
- @file_put_contents("getDistance.txt",$lat2.'--'.$lng2);
- return 0;
- }
- }
- }
|