Area.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace app\data\controller\api;
  3. use app\data\model\BasePostageRegion;
  4. use hg\apidoc\annotation\Param;
  5. use hg\apidoc\annotation\Returned;
  6. use hg\apidoc\annotation\Title;
  7. use think\admin\Controller;
  8. use think\Cache;
  9. use think\helper\Str;
  10. /**
  11. * @Title("地区模块")
  12. */
  13. class Area extends Controller
  14. {
  15. /**
  16. * @Title("获取地区信息")
  17. * @Param("level",desc="1省2市3区")
  18. * @Param("pid",desc="上级区域ID")
  19. * @Returned("id",desc="地区ID")
  20. * @Returned("name",desc="地区名称")
  21. * @Returned("short",desc="地区简短名称")
  22. * @Returned("pinyin",desc="地区拼音")
  23. * @Returned("first",desc="地区首字母")
  24. */
  25. public function area(){
  26. $data=$this->_vali([
  27. 'level.require'=>'级别必须',
  28. 'pid.require'=>"上级必须",
  29. ],'get');
  30. $this->success(
  31. '',
  32. BasePostageRegion::where('level',$data['level'])
  33. ->where('pid',$data['pid'])
  34. ->order('pinyin')
  35. ->select()
  36. );
  37. }
  38. /**
  39. * @Title("获取地区信息(按字母排序)")
  40. * @Param("level",desc="1省2市3区")
  41. * @Param("hot",desc="1是否获取热门城市")
  42. * @Param("name",desc="模糊搜索地区名称")
  43. */
  44. public function sort(){
  45. $data=$this->_vali([
  46. 'level.require'=>'级别必须',
  47. 'level.in:1,2,3'=>'级别必须',
  48. ],'get');
  49. $name=$this->request->get('name');
  50. $sortArea=$this->app->cache->remember("app_area_sort_{$data['level']}",function ()use ($data){
  51. $citys=BasePostageRegion::field(['id','name','first'])->where('level',$data['level'])->select();
  52. $a = [];
  53. for ($i = 65; $i < 91; $i++) {
  54. $one = [
  55. 'index' => chr($i),
  56. 'child' => []
  57. ];
  58. foreach ($citys as $key => &$city) {
  59. if ($city['first'] == $one['index']) {
  60. $one['child'][] = [
  61. 'title' => $city['name'],
  62. 'weight' => $city['id'],
  63. 'id' => $city['id'],
  64. ];
  65. unset($citys[$key]);
  66. }
  67. }
  68. $a[] = $one;
  69. }
  70. return $a;
  71. },0);
  72. if($name){
  73. foreach ($sortArea as $key=>&$area){
  74. $newChild=[];
  75. foreach ($area['child'] as $idx=>$one){
  76. if(Str::contains($one['title'],$name)){
  77. $newChild[]=$one;
  78. }
  79. }
  80. $area['child']=$newChild;
  81. }
  82. }
  83. $info=[
  84. 'areas'=>$sortArea,
  85. ];
  86. if(input('hot')){
  87. $info['hot']=BasePostageRegion::hot();
  88. }
  89. $this->success('',$info);
  90. }
  91. /**
  92. * @Title("地区递归三级联动")
  93. */
  94. public function tree(){
  95. function area(&$data,$areas,$first=false){
  96. foreach ($areas as $k=>$area){
  97. if($first){
  98. if($area['pid']==0) {
  99. area($area, $areas);
  100. unset($area['pid']);
  101. $data[] = $area;
  102. unset($areas[$k]);
  103. }
  104. }else{
  105. if($data['value']==$area['pid']){
  106. area($area, $areas);
  107. unset($area['pid']);
  108. $data['children'][]=$area;
  109. unset($areas[$k]);
  110. }
  111. }
  112. }
  113. }
  114. $cache=$this->app->cache;
  115. $fromCache=$cache->get('app_area');
  116. if(!$fromCache){
  117. $fina=[];
  118. $areas=BasePostageRegion::where('level','<=',3)->field('id as value,name as label,pid')->select()->toArray();
  119. area($fina,$areas,true);
  120. $fromCache=$fina;
  121. $cache->remember('app_area',$fina,0);
  122. }
  123. $this->success('',$fromCache);
  124. }
  125. /**
  126. * @Title("根据名称获取信息")
  127. * @Param("name",desc="城市名")
  128. */
  129. public function name(){
  130. $rs=null;
  131. $cache=$this->app->cache;
  132. if($name=input('name')){
  133. $cacheName="area_name_".$name;
  134. $rs=$cache->get($cacheName,null);
  135. if(!$rs){
  136. $rs=BasePostageRegion::where('name|short',$name)->find();
  137. $cache->set($cacheName,$rs);
  138. }
  139. }
  140. $this->success('',$rs);
  141. }
  142. }