xxxrrrdddd 3 年之前
父节点
当前提交
1626cbd9a7
共有 1 个文件被更改,包括 38 次插入1 次删除
  1. 38 1
      application/api/controller/Common.php

+ 38 - 1
application/api/controller/Common.php

@@ -8,15 +8,17 @@ use app\common\exception\UploadException;
 use app\common\library\Upload;
 use app\common\model\Ad;
 use app\common\model\Area;
+use think\Cache;
 use think\Config;
 use think\File;
+use function app\api\controller\area;
 
 /**
  * 公共接口
  */
 class Common extends Api
 {
-    protected $noNeedLogin = ['agreement','ads','area','area_sort'];
+    protected $noNeedLogin = ['agreement','ads','area','area_sort','area_tree'];
 
     /**
      * 获取各种协议
@@ -172,4 +174,39 @@ class Common extends Api
         }
         $this->success('',$a);
     }
+    /**
+     * 地区递归三级联动
+     */
+    public function area_tree(){
+        $areas=Area::where('level','<=',3)->field('id as value,name as label,pid')->select()->toArray();
+        $fina=[];
+        function area(&$data,$areas,$first=false){
+            foreach ($areas as $k=>$area){
+                if($first){
+                    if($area['pid']==0) {
+                        area($area, $areas);
+                        unset($area['pid']);
+                        $data[] = $area;
+                        unset($areas[$k]);
+                    }
+                }else{
+                    if($data['value']==$area['pid']){
+                        area($area, $areas);
+                        unset($area['pid']);
+                        $data['children'][]=$area;
+                        unset($areas[$k]);
+                    }
+                }
+            }
+        }
+
+        $fromCache=Cache::get('app_area');
+        if(!$fromCache){
+            area($fina,$areas,true);
+            $fromCache=$fina;
+            Cache::remember('app_area',$fina,0);
+        }
+
+        $this->success('',$fromCache);
+    }
 }