123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- <?php
- namespace app\common\model;
- use think\Cache;
- use think\db\Query;
- use think\Model;
- /**
- * 地区数据模型
- * @property bool has_air_coordinate
- * @method static static shi($name=null)
- * @method static static area()
- */
- class Area extends Model
- {
- const LEVEL_COUNTY=3;
- /**
- * 根据经纬度获取当前地区信息
- *
- * @param string $lng 经度
- * @param string $lat 纬度
- * @return Area 城市信息
- */
- public static function getAreaFromLngLat($lng, $lat, $level = 3)
- {
- $namearr = [1 => 'geo:province', 2 => 'geo:city', 3 => 'geo:district'];
- $rangearr = [1 => 15000, 2 => 1000, 3 => 200];
- $geoname = isset($namearr[$level]) ? $namearr[$level] : $namearr[3];
- $georange = isset($rangearr[$level]) ? $rangearr[$level] : $rangearr[3];
- // 读取范围内的ID
- $redis = Cache::store('redis')->handler();
- $georadiuslist = [];
- if (method_exists($redis, 'georadius')) {
- $georadiuslist = $redis->georadius($geoname, $lng, $lat, $georange, 'km', ['WITHDIST', 'COUNT' => 5, 'ASC']);
- }
- if ($georadiuslist) {
- list($id, $distance) = $georadiuslist[0];
- }
- $id = isset($id) && $id ? $id : 3;
- return self::get($id);
- }
- /**
- * 根据经纬度获取省份
- *
- * @param string $lng 经度
- * @param string $lat 纬度
- * @return Area
- */
- public static function getProvinceFromLngLat($lng, $lat)
- {
- $provincedata = null;
- $citydata = self::getCityFromLngLat($lng, $lat);
- if ($citydata) {
- $provincedata = self::get($citydata['pid']);
- }
- return $provincedata;
- }
- /**
- * 根据经纬度获取城市
- *
- * @param string $lng 经度
- * @param string $lat 纬度
- * @return Area
- */
- public static function getCityFromLngLat($lng, $lat)
- {
- $citydata = null;
- $districtdata = self::getDistrictFromLngLat($lng, $lat);
- if ($districtdata) {
- $citydata = self::get($districtdata['pid']);
- }
- return $citydata;
- }
- /**
- * 根据经纬度获取地区
- *
- * @param string $lng 经度
- * @param string $lat 纬度
- * @return Area
- */
- public static function getDistrictFromLngLat($lng, $lat)
- {
- $districtdata = self::getAreaFromLngLat($lng, $lat, 3);
- return $districtdata;
- }
- public function scopeLevel(Query $query,$level){
- $query->where('level',$level);
- }
- public function scopePro(Query $query){
- $query->level(1);
- }
- public function scopeShi(Query $query,$name=null){
- $query->level(2);
- if($name!==null){
- $query->where('name|shortname','like',"%{$name}%");
- }
- }
- public function scopeArea(Query $query){
- $query->level(3);
- }
- public function province(){
- return $this->hasMany(User::class,'province_id');
- }
- public function city(){
- return $this->hasMany(User::class,'city_id');
- }
- public function county(){
- return $this->hasMany(User::class,'county_id');
- }
- public function getMergenameAttr($a){
- return str_replace(',','',str_replace('中国,','',$a));
- }
- public static function getIdByName($name,$level=null){
- $map=[];
- if($level){
- $map['level']=$level;
- }
- return self::where('name|shortname',$name)->where($map)->value('id');
- }
- public static function getTreeId($id){
- if(is_numeric($id)) {
- $county = self::where('id', $id)->area()->find();
- }else{
- $county= self::where('name|shortname',$id)->find();
- }
- if(!$county){
- throw_user('区县不存在');
- }
- $cityId=$county['pid'];
- $provinceId=self::where('id',$cityId)->value('pid');
- if(!$provinceId){
- throw_user('省不存在');
- }
- return [$provinceId,$cityId,$county['id']];
- }
- public static function getNameString($id,$sp=''){
- if(!$id){
- return '';
- }
- $cacheName='area_'.serialize($id).md5($sp);
- $res=Cache::get($cacheName);
- if(!$res){
- $res=implode($sp,self::whereIn('id',$id)->column('name'));
- Cache::set($cacheName,$res,0);
- }
- return $res;
- }
- public static function shouldSend($county_id){
- $config=config('site.disable_send_province');
- $config=explode(',',$config)?:[];
- $provinceId=self::getTreeId($county_id)[0];
- if(in_array($provinceId,$config)){
- throw_user('此区域无法购买此产品');
- }
- }
- }
|