修复:IP定位接口路径统一管理,移除地图定位相关代码
This commit is contained in:
@@ -32,6 +32,9 @@ export const API_CONFIG = {
|
||||
|
||||
// 区域数据接口
|
||||
AREA_LIST: '/api/partnerh5/area_list',
|
||||
|
||||
// IP定位接口
|
||||
IP_LOCATION: '/api/partnerh5/ip_location',
|
||||
},
|
||||
|
||||
// 请求超时配置(毫秒)
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
*/
|
||||
|
||||
import { AreaService } from '../services/area.service.js';
|
||||
import { API_CONFIG } from '../config/api.config.js';
|
||||
|
||||
// 热门城市列表(12个核心城市)
|
||||
const HOT_CITIES = [
|
||||
@@ -660,7 +661,7 @@ export class CityPicker {
|
||||
|
||||
try {
|
||||
// 调用后端IP定位接口
|
||||
const response = await fetch('/pub/partnerh5/ip_location');
|
||||
const response = await fetch(API_CONFIG.ENDPOINTS.IP_LOCATION);
|
||||
const result = await response.json();
|
||||
|
||||
if (result.retcode === 0 && result.result) {
|
||||
@@ -705,80 +706,6 @@ export class CityPicker {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 逆地理编码,将经纬度转换为城市信息
|
||||
* 使用免费的逆地理编码服务
|
||||
* @param {number} latitude - 纬度
|
||||
* @param {number} longitude - 经度
|
||||
* @returns {Promise<Object|null>} - 城市信息对象
|
||||
*/
|
||||
async reverseGeocode(latitude, longitude) {
|
||||
try {
|
||||
// 使用免费的 Nominatim API(OpenStreetMap)
|
||||
// 注意:该服务有速率限制(每秒最多1次请求)
|
||||
const url = `https://nominatim.openstreetmap.org/reverse?format=json&lat=${latitude}&lon=${longitude}&accept-language=zh-CN`;
|
||||
|
||||
const response = await fetch(url, {
|
||||
headers: {
|
||||
'User-Agent': 'CityPicker/1.0'
|
||||
}
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (data && data.address) {
|
||||
const address = data.address;
|
||||
|
||||
// 获取城市信息(优先级:city > town > village > state)
|
||||
let cityName = address.city || address.town || address.village || address.state || '';
|
||||
let provinceName = address.state || '';
|
||||
|
||||
// 处理直辖市的特殊情况
|
||||
const municipalities = ['北京市', '上海市', '天津市', '重庆市'];
|
||||
if (municipalities.includes(provinceName)) {
|
||||
cityName = provinceName;
|
||||
}
|
||||
|
||||
// 如果城市名为空,尝试从county字段获取
|
||||
if (!cityName && address.county) {
|
||||
// 移除"市"、"区"、"县"等后缀
|
||||
cityName = address.county.replace(/[市区县]$/, '') + '市';
|
||||
}
|
||||
|
||||
if (cityName) {
|
||||
// 查找城市代码
|
||||
const cityCode = await this.findCityCode(provinceName, cityName);
|
||||
|
||||
if (cityCode) {
|
||||
return {
|
||||
name: cityName,
|
||||
province: provinceName,
|
||||
code: cityCode.cityCode,
|
||||
provinceCode: cityCode.provinceCode
|
||||
};
|
||||
} else {
|
||||
// 如果找不到代码,返回基本信息(代码为空)
|
||||
return {
|
||||
name: cityName,
|
||||
province: provinceName,
|
||||
code: '',
|
||||
provinceCode: ''
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
} catch (error) {
|
||||
console.error('[CityPicker] 逆地理编码失败:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 查找城市代码
|
||||
* @param {string} provinceName - 省份名称
|
||||
|
||||
BIN
static/image.zip
Normal file
BIN
static/image.zip
Normal file
Binary file not shown.
Reference in New Issue
Block a user