Area.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace app\common\model;
  3. use think\Cache;
  4. use think\db\Query;
  5. use think\Model;
  6. /**
  7. * 地区数据模型
  8. * @property bool has_air_coordinate
  9. * @method static static shi($name=null)
  10. * @method static static area()
  11. */
  12. class Area extends Model
  13. {
  14. const LEVEL_COUNTY=3;
  15. /**
  16. * 根据经纬度获取当前地区信息
  17. *
  18. * @param string $lng 经度
  19. * @param string $lat 纬度
  20. * @return Area 城市信息
  21. */
  22. public static function getAreaFromLngLat($lng, $lat, $level = 3)
  23. {
  24. $namearr = [1 => 'geo:province', 2 => 'geo:city', 3 => 'geo:district'];
  25. $rangearr = [1 => 15000, 2 => 1000, 3 => 200];
  26. $geoname = isset($namearr[$level]) ? $namearr[$level] : $namearr[3];
  27. $georange = isset($rangearr[$level]) ? $rangearr[$level] : $rangearr[3];
  28. // 读取范围内的ID
  29. $redis = Cache::store('redis')->handler();
  30. $georadiuslist = [];
  31. if (method_exists($redis, 'georadius')) {
  32. $georadiuslist = $redis->georadius($geoname, $lng, $lat, $georange, 'km', ['WITHDIST', 'COUNT' => 5, 'ASC']);
  33. }
  34. if ($georadiuslist) {
  35. list($id, $distance) = $georadiuslist[0];
  36. }
  37. $id = isset($id) && $id ? $id : 3;
  38. return self::get($id);
  39. }
  40. /**
  41. * 根据经纬度获取省份
  42. *
  43. * @param string $lng 经度
  44. * @param string $lat 纬度
  45. * @return Area
  46. */
  47. public static function getProvinceFromLngLat($lng, $lat)
  48. {
  49. $provincedata = null;
  50. $citydata = self::getCityFromLngLat($lng, $lat);
  51. if ($citydata) {
  52. $provincedata = self::get($citydata['pid']);
  53. }
  54. return $provincedata;
  55. }
  56. /**
  57. * 根据经纬度获取城市
  58. *
  59. * @param string $lng 经度
  60. * @param string $lat 纬度
  61. * @return Area
  62. */
  63. public static function getCityFromLngLat($lng, $lat)
  64. {
  65. $citydata = null;
  66. $districtdata = self::getDistrictFromLngLat($lng, $lat);
  67. if ($districtdata) {
  68. $citydata = self::get($districtdata['pid']);
  69. }
  70. return $citydata;
  71. }
  72. /**
  73. * 根据经纬度获取地区
  74. *
  75. * @param string $lng 经度
  76. * @param string $lat 纬度
  77. * @return Area
  78. */
  79. public static function getDistrictFromLngLat($lng, $lat)
  80. {
  81. $districtdata = self::getAreaFromLngLat($lng, $lat, 3);
  82. return $districtdata;
  83. }
  84. public function scopeLevel(Query $query,$level){
  85. $query->where('level',$level);
  86. }
  87. public function scopePro(Query $query){
  88. $query->level(1);
  89. }
  90. public function scopeShi(Query $query,$name=null){
  91. $query->level(2);
  92. if($name!==null){
  93. $query->where('name|shortname','like',"%{$name}%");
  94. }
  95. }
  96. public function scopeArea(Query $query){
  97. $query->level(3);
  98. }
  99. public function province(){
  100. return $this->hasMany(User::class,'province_id');
  101. }
  102. public function city(){
  103. return $this->hasMany(User::class,'city_id');
  104. }
  105. public function county(){
  106. return $this->hasMany(User::class,'county_id');
  107. }
  108. public function getMergenameAttr($a){
  109. return str_replace(',','',str_replace('中国,','',$a));
  110. }
  111. public static function getIdByName($name,$level=null){
  112. $map=[];
  113. if($level){
  114. $map['level']=$level;
  115. }
  116. return self::where('name|shortname',$name)->where($map)->value('id');
  117. }
  118. public static function getTreeId($id){
  119. if(is_numeric($id)) {
  120. $county = self::where('id', $id)->area()->find();
  121. }else{
  122. $county= self::where('name|shortname',$id)->find();
  123. }
  124. if(!$county){
  125. throw_user('区县不存在');
  126. }
  127. $cityId=$county['pid'];
  128. $provinceId=self::where('id',$cityId)->value('pid');
  129. if(!$provinceId){
  130. throw_user('省不存在');
  131. }
  132. return [$provinceId,$cityId,$county['id']];
  133. }
  134. public static function getNameString($id,$sp=''){
  135. if(!$id){
  136. return '';
  137. }
  138. $cacheName='area_'.serialize($id).md5($sp);
  139. $res=Cache::get($cacheName);
  140. if(!$res){
  141. $res=implode($sp,self::whereIn('id',$id)->column('name'));
  142. Cache::set($cacheName,$res,0);
  143. }
  144. return $res;
  145. }
  146. public static function shouldSend($county_id){
  147. $config=config('site.disable_send_province');
  148. $config=explode(',',$config)?:[];
  149. $provinceId=self::getTreeId($county_id)[0];
  150. if(in_array($provinceId,$config)){
  151. throw_user('此区域无法购买此产品');
  152. }
  153. }
  154. }