pearadmin - layui - area 省市区联动 js插件 省市区编码更新到最新方法
从这个开源项目 https://gitee.com/xiangyuecn/AreaCity-JsSpider-StatsGov 获取最新的json数据
https://xiangyuecn.github.io/AreaCity-JsSpider-StatsGov/#tools
导出为自定义按钮 导出 area_format_user.json 文件
执行 area.php 提示 area_data.js 文件已更新。
打开 area_data.js 复制新生成的areaList对象,替换掉 area.js 里的areaList 即可更新到当前最新的(2025年)的省市区数据。
2025-07-18 ff
<?php
// 读取 area.json 文件 - 注意文件必须utf-8不带bom头的否则报错
$jsonContent = file_get_contents('area_format_user.json');
// 判断文件读取是否成功
if ($jsonContent === false) {
// 文件读取失败,输出错误信息
die('无法读取文件,请确认文件路径和权限是否正确');
} else {
// 文件读取成功,继续执行后续操作
$areaJson = json_decode($jsonContent, true);
// 判断JSON解析是否成功
if (json_last_error() !== JSON_ERROR_NONE) {
die('JSON解析失败:' . json_last_error_msg());
}
// 初始化省、市、区(县)列表
$provinceList = [];
$cityList = [];
$countyList = [];
// 处理省、市、区(县)数据
function processData($data, &$provinceList, &$cityList, &$countyList) {
foreach ($data as $province) {
$provinceId = substr($province['ext_id'], 0, 6);
$provinceName = $province['ext_name'];
$provinceList[$provinceId] = $provinceName;
if (isset($province['childs'])) {
foreach ($province['childs'] as $city) {
$cityId = substr($city['ext_id'], 0, 6);
$cityName = $city['ext_name'];
$cityList[$cityId] = $cityName;
if (isset($city['childs'])) {
foreach ($city['childs'] as $county) {
$countyId = substr($county['ext_id'], 0, 6);
$countyName = $county['ext_name'];
$countyList[$countyId] = $countyName;
}
}
}
}
}
}
processData($areaJson, $provinceList, $cityList, $countyList);
// 生成新的 area_data.js 内容
$jsContent = "var areaList = {\n";
$jsContent .= " province_list: {\n";
foreach ($provinceList as $id => $name) {
$jsContent .= " \"$id\": \"$name\",\n";
}
$jsContent = rtrim($jsContent, ",\n") . "\n },\n";
$jsContent .= " city_list: {\n";
foreach ($cityList as $id => $name) {
$jsContent .= " \"$id\": \"$name\",\n";
}
$jsContent = rtrim($jsContent, ",\n") . "\n },\n";
$jsContent .= " county_list: {\n";
foreach ($countyList as $id => $name) {
$jsContent .= " \"$id\": \"$name\",\n";
}
$jsContent = rtrim($jsContent, ",\n") . "\n }\n};";
// 将新内容写入 area_data.js 文件
file_put_contents('area_data.js', $jsContent);
echo "area_data.js 文件已更新。";
}
?>源码:邮箱 309 搜索标题 pearadmin-layui-area 省市区联动 js插件 省市区编码更新到最新方法