2025-09-18 17:12:46 +08:00
|
|
|
|
const express = require('express')
|
|
|
|
|
|
const router = express.Router()
|
|
|
|
|
|
const {getDB} = require('../database')
|
|
|
|
|
|
|
2025-09-19 16:46:00 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @swagger
|
|
|
|
|
|
* tags:
|
|
|
|
|
|
* name: Common
|
|
|
|
|
|
* description: 公共接口
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2025-09-18 17:12:46 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @swagger
|
|
|
|
|
|
* /api/common/provinces:
|
2025-09-19 16:46:00 +08:00
|
|
|
|
* get:
|
|
|
|
|
|
* summary: 获取省市区列表
|
|
|
|
|
|
* tags: [Common]
|
2025-09-18 17:12:46 +08:00
|
|
|
|
* responses:
|
2025-09-19 16:46:00 +08:00
|
|
|
|
* 200:
|
2025-09-18 17:12:46 +08:00
|
|
|
|
* description: 成功获取分类列表
|
2025-09-19 16:46:00 +08:00
|
|
|
|
* content:
|
|
|
|
|
|
* application/json:
|
|
|
|
|
|
* schema:
|
|
|
|
|
|
* type: array
|
|
|
|
|
|
* items:
|
|
|
|
|
|
* properties:
|
|
|
|
|
|
* code:
|
|
|
|
|
|
* type: string
|
|
|
|
|
|
* description: 区域编码
|
|
|
|
|
|
* example: 110000
|
|
|
|
|
|
* label:
|
|
|
|
|
|
* type: string
|
|
|
|
|
|
* description: 区域名称
|
|
|
|
|
|
* example: 北京市
|
2025-09-18 17:12:46 +08:00
|
|
|
|
*/
|
|
|
|
|
|
router.get('/provinces', async (req, res) => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
// 按level分组数据
|
|
|
|
|
|
const regionsByLevel = {
|
|
|
|
|
|
1: [], // 省份
|
|
|
|
|
|
2: [], // 城市
|
|
|
|
|
|
3: [] // 区县
|
|
|
|
|
|
};
|
|
|
|
|
|
if (!global.provinces) {
|
|
|
|
|
|
// 一次性获取所有区域数据(省、市、区县)
|
|
|
|
|
|
const [allRegions] = await getDB().execute(
|
|
|
|
|
|
`SELECT code, name as label, level, parent_code
|
|
|
|
|
|
FROM china_regions
|
|
|
|
|
|
WHERE level <= 3
|
|
|
|
|
|
ORDER BY level, code`
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 创建code到region的映射,便于快速查找
|
|
|
|
|
|
const regionMap = {};
|
|
|
|
|
|
|
|
|
|
|
|
// 分组并建立映射
|
|
|
|
|
|
allRegions.forEach(region => {
|
|
|
|
|
|
region.children = []; // 初始化children数组
|
|
|
|
|
|
regionsByLevel[region.level].push(region);
|
|
|
|
|
|
regionMap[region.code] = region;
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 构建层级关系:先处理区县到城市的关系
|
|
|
|
|
|
regionsByLevel[3].forEach(district => {
|
|
|
|
|
|
const parentCity = regionMap[district.parent_code];
|
|
|
|
|
|
if (parentCity) {
|
|
|
|
|
|
parentCity.children.push(district);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 再处理城市到省份的关系
|
|
|
|
|
|
regionsByLevel[2].forEach(city => {
|
|
|
|
|
|
const parentProvince = regionMap[city.parent_code];
|
|
|
|
|
|
if (parentProvince) {
|
|
|
|
|
|
parentProvince.children.push(city);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
global.provinces = regionsByLevel[1];
|
|
|
|
|
|
} else {
|
|
|
|
|
|
console.log('1111')
|
|
|
|
|
|
regionsByLevel[1] = global.provinces;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 返回省份数据(已包含完整的层级结构)
|
|
|
|
|
|
res.json({
|
|
|
|
|
|
success: true,
|
|
|
|
|
|
data: regionsByLevel[1]
|
|
|
|
|
|
});
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('获取省份列表错误:', error);
|
|
|
|
|
|
res.status(500).json({message: '获取省份列表失败'});
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = router
|