MCPcopy Create free account
hub / github.com/debbide/GeoFill / selectLocationByCity

Function selectLocationByCity

scripts/generators.js:977–1014  ·  view source on GitHub ↗

* 根据 IP 检测的城市和州设置位置(直接使用 IP 返回的信息)

(country, cityName, regionName)

Source from the content-addressed store, hash-verified

975 * 根据 IP 检测的城市和州设置位置(直接使用 IP 返回的信息)
976 */
977function selectLocationByCity(country, cityName, regionName) {
978 const locations = CITY_STATE_MAP[country] || CITY_STATE_MAP['United States'];
979
980 // 如果 IP 返回了城市信息,直接使用
981 if (cityName && cityName !== 'Unknown') {
982 // 尝试在数据库中查找邮编前缀
983 let zipPrefix = '';
984
985 // 精确匹配城市获取邮编
986 const exactMatch = locations.find(loc =>
987 loc.city.toLowerCase() === cityName.toLowerCase()
988 );
989 if (exactMatch) {
990 zipPrefix = exactMatch.zip;
991 } else if (regionName) {
992 // 匹配同州城市获取邮编
993 const stateMatch = locations.find(loc =>
994 loc.state.toLowerCase() === regionName.toLowerCase() ||
995 loc.state.toLowerCase().includes(regionName.toLowerCase())
996 );
997 if (stateMatch) {
998 zipPrefix = stateMatch.zip;
999 }
1000 }
1001
1002 // 直接使用 IP 返回的城市和州
1003 currentLocation = {
1004 city: cityName,
1005 state: regionName || '',
1006 zip: zipPrefix
1007 };
1008
1009 return currentLocation;
1010 }
1011
1012 // 没有 IP 城市信息,随机选择
1013 return selectLocation(country);
1014}
1015
1016/**
1017 * 获取当前位置信息

Callers 2

generateAllInfoFunction · 0.85

Calls 1

selectLocationFunction · 0.85

Tested by

no test coverage detected