新增:区域信息支持功能
1. 新增区域服务模块 - 新增area.service.js处理区域信息查询 - 优化城市选择器,支持区域信息获取 2. 完善表单基础信息页 - 优化基础信息表单验证规则 - 完善一键登录功能 - 优化草稿管理功能 3. 更新配置文件 - 更新API配置 - 更新应用配置 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
136
src/js/services/area.service.js
Normal file
136
src/js/services/area.service.js
Normal file
@@ -0,0 +1,136 @@
|
||||
/**
|
||||
* 区域数据服务
|
||||
* 提供省市区数据查询功能
|
||||
*/
|
||||
|
||||
import { API_CONFIG } from '../config/api.config.js';
|
||||
import { ApiClient } from '../core/api.js';
|
||||
|
||||
const CACHE_KEY_PREFIX = 'area_cache_';
|
||||
const CACHE_DURATION = 10 * 60 * 1000; // 10分钟
|
||||
|
||||
/**
|
||||
* 区域服务
|
||||
*/
|
||||
export class AreaService {
|
||||
/**
|
||||
* 获取区域列表
|
||||
* @param {string} provincecode - 省份代码(可选)
|
||||
* @returns {Promise<Array>} 区域列表 [{code, name}]
|
||||
*/
|
||||
static async getAreaList(provincecode) {
|
||||
// 检查缓存
|
||||
const cacheKey = `${CACHE_KEY_PREFIX}${provincecode || 'all'}`;
|
||||
const cached = localStorage.getItem(cacheKey);
|
||||
|
||||
if (cached) {
|
||||
const { data, timestamp } = JSON.parse(cached);
|
||||
if (Date.now() - timestamp < CACHE_DURATION) {
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
// 构建请求参数
|
||||
const params = {};
|
||||
if (provincecode) {
|
||||
params.provincecode = provincecode;
|
||||
}
|
||||
|
||||
// 使用 ApiClient 发送请求
|
||||
const result = await ApiClient.get(API_CONFIG.ENDPOINTS.AREA_LIST, params);
|
||||
|
||||
if (result.retcode !== 0) {
|
||||
throw new Error(result.retmsg || '获取区域数据失败');
|
||||
}
|
||||
|
||||
const areaList = result.result || [];
|
||||
|
||||
// 保存到缓存
|
||||
localStorage.setItem(cacheKey, JSON.stringify({
|
||||
data: areaList,
|
||||
timestamp: Date.now()
|
||||
}));
|
||||
|
||||
return areaList;
|
||||
} catch (error) {
|
||||
console.error('[AreaService] 获取区域数据失败:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有省份
|
||||
* @returns {Promise<Array>} 省份列表
|
||||
*/
|
||||
static async getProvinces() {
|
||||
return this.getAreaList();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定省份的市和区
|
||||
* @param {string} provincecode - 省份代码(如:13)
|
||||
* @returns {Promise<Array>} 市区列表
|
||||
*/
|
||||
static async getCities(provincecode) {
|
||||
if (!provincecode) {
|
||||
throw new Error('省份代码不能为空');
|
||||
}
|
||||
return this.getAreaList(provincecode);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除缓存
|
||||
*/
|
||||
static clearCache() {
|
||||
Object.keys(localStorage)
|
||||
.filter(key => key.startsWith(CACHE_KEY_PREFIX))
|
||||
.forEach(key => localStorage.removeItem(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据地区代码(身份证前6位)查询地区信息
|
||||
* @param {string} areaCode - 地区代码(6位)
|
||||
* @returns {Promise<Object|null>} - 地区信息 {province, city, district, value},如果未找到则返回 null
|
||||
*/
|
||||
static async getAreaByCode(areaCode) {
|
||||
if (!areaCode || areaCode.length < 6) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
const provincecode = areaCode.substring(0, 2);
|
||||
|
||||
// 并行查询省列表和该省的市区列表
|
||||
const [provinces, areas] = await Promise.all([
|
||||
this.getProvinces(),
|
||||
this.getAreaList(provincecode)
|
||||
]);
|
||||
|
||||
// 查找省、市、区
|
||||
const province = provinces.find(p => p.code === provincecode);
|
||||
const city = areas.find(a => a.code === areaCode.substring(0, 4));
|
||||
const district = areas.find(a => a.code === areaCode);
|
||||
|
||||
// 至少要有省份才返回
|
||||
if (province) {
|
||||
return {
|
||||
province: province.name,
|
||||
city: city ? city.name : '',
|
||||
district: district ? district.name : '',
|
||||
provinceCode: province.code,
|
||||
cityCode: city ? city.code : '',
|
||||
districtCode: district ? district.code : '',
|
||||
value: city ? `${province.name}/${city.name}` : province.name
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
} catch (error) {
|
||||
console.error('[AreaService] 根据代码查询地区失败:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default AreaService;
|
||||
Reference in New Issue
Block a user