field($field) ->where($where) ->order($orderBy) ->select() ->toArray(); } /** * 获取热门城市列表 * @param int $limit * @return array */ public static function getHotCities($limit = 10) { // 预设热门城市ID(可根据实际业务配置) $hotCityIds = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; // 默认北京、上海、广州、深圳等 return Db::name('Region') ->field('id,pid,name,level,letters') ->where(['id' => $hotCityIds, 'is_enable' => 1]) ->order('sort asc,id asc') ->select() ->toArray(); } /** * 获取省份列表 * @return array */ public static function getProvinces() { return self::getCityList(['level' => 1]); } /** * 根据父级ID获取子城市列表 * @param int $pid * @return array */ public static function getCitiesByPid($pid) { return self::getCityList(['pid' => intval($pid)]); } /** * 获取城市名称 * @param int $cityId * @return string|null */ public static function getCityName($cityId) { if (empty($cityId)) { return null; } $name = Db::name('Region') ->where(['id' => intval($cityId), 'is_enable' => 1]) ->value('name'); return $name ?: null; } /** * 获取当前选中的城市信息 * @param int $cityId * @return array|null */ public static function getSelectedCity($cityId) { if (empty($cityId)) { return null; } $city = Db::name('Region') ->field('id,pid,name,level,letters') ->where(['id' => intval($cityId), 'is_enable' => 1]) ->find(); return $city ?: null; } /** * 构建城市选择器所需的三级联动数据结构 * @return array */ public static function getCitySelectorData() { // 省份(一级) $provinces = self::getProvinces(); // 标记有子城市的城市为非叶子节点 if (!empty($provinces)) { foreach ($provinces as &$province) { // 查询是否有子城市 $hasChildren = Db::name('Region') ->where(['pid' => $province['id'], 'is_enable' => 1]) ->count(); $province['has_children'] = $hasChildren > 0 ? 1 : 0; } } return [ 'provinces' => $provinces ?: [], ]; } /** * 获取城市树形结构(用于级联选择) * @param int $pid * @return array */ public static function getRegionTree($pid = 0) { $pid = intval($pid); $regions = Db::name('Region') ->field('id,pid,name,level,letters') ->where(['pid' => $pid, 'is_enable' => 1]) ->order('sort asc,id asc') ->select() ->toArray(); if (!empty($regions)) { foreach ($regions as &$region) { // 递归获取子节点 $children = self::getRegionTree($region['id']); $region['children'] = $children ?: []; } } return $regions; } }