Area.php 4.2 KB

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