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

This commit is contained in:
evoxwht 2023-04-10 11:24:32 +08:00
parent d6db8fabdd
commit 36a893d5a8
3 changed files with 27 additions and 4 deletions

View File

@ -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'));
}
}

View File

@ -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();
}
}

View File

@ -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;
}
}