瀏覽代碼

格式化获取城市数据完整列表

evoxwht 2 年之前
父節點
當前提交
36a893d5a8

+ 1 - 1
crmeb/app/adminapi/controller/v1/setting/SystemCity.php

@@ -151,6 +151,6 @@ class SystemCity extends AuthController
      */
     public function fullList()
     {
-        return app('json')->success($this->services->fullList('parent_id,city_id,id,name,name as label,city_id as value'));
+        return app('json')->success($this->services->fullList('parent_id,name as label,city_id as value'));
     }
 }

+ 1 - 1
crmeb/app/dao/shipping/SystemCityDao.php

@@ -96,6 +96,6 @@ class SystemCityDao extends BaseDao
      */
     public function fullList($field = '*')
     {
-        return $this->getModel()->with('children')->order('id asc')->field($field)->select()->toArray();
+        return $this->getModel()->order('id asc')->field($field)->select()->toArray();
     }
 }

+ 25 - 2
crmeb/app/services/shipping/SystemCityServices.php

@@ -153,13 +153,36 @@ class SystemCityServices extends BaseServices
     }
 
     /**
-     * 获取城市数据
+     * 获取城市数据完整列表
      * @return mixed
      */
     public function fullList($field = '*')
     {
         return CacheService::remember('CITY_FULL_LIST', function () use ($field) {
-            return $this->dao->fullList($field);
+            return $this->fullListTree($this->dao->fullList($field));
         }, 0);
     }
+
+    /**
+     * 格式化获取城市数据完整列表
+     * @param $data
+     * @param int $pid
+     * @param array $navList
+     * @return array|mixed
+     * @author 吴汐
+     * @email 442384644@qq.com
+     * @date 2023/04/10
+     */
+    function fullListTree($data, $pid = 0, $navList = [])
+    {
+        foreach ($data as $k => $menu) {
+            if ($menu['parent_id'] == $pid) {
+                unset($menu['parent_id']);
+                unset($data[$k]);
+                $menu['children'] = $this->fullListTree($data, $menu['value']);
+                $navList[] = $menu;
+            }
+        }
+        return $navList;
+    }
 }