137 lines
4.1 KiB
JavaScript
137 lines
4.1 KiB
JavaScript
/**
|
||
* 区域数据服务
|
||
* 提供省市区数据查询功能
|
||
*/
|
||
|
||
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 ? city.name : province.name // 只返回市名或省名
|
||
};
|
||
}
|
||
|
||
return null;
|
||
} catch (error) {
|
||
console.error('[AreaService] 根据代码查询地区失败:', error);
|
||
return null;
|
||
}
|
||
}
|
||
}
|
||
|
||
export default AreaService;
|