重构地址处理逻辑,使用 addressData 替代 address,增强状态缩写的准确性

This commit is contained in:
严浩
2025-07-16 01:05:19 +08:00
parent 8f3b3618e9
commit a9504c0cf6

View File

@@ -17,7 +17,7 @@ export default {
async function handleRequest(request) {
const { searchParams } = new URL(request.url);
const state = searchParams.get('state') || getRandomState();
let address, phone, country;
let phone, country, addressData;
const remoteProvinces = ['NL', 'NT', 'NU', 'YT']; // Remote Canadian Provinces/Territories
@@ -35,20 +35,20 @@ async function handleRequest(request) {
if (data.address.country_code === 'us') {
if (data.address.house_number && data.address.road && data.address.city) {
country = 'US';
address = formatAddress(data.address, state, country);
addressData = data.address;
break;
}
} else if (data.address.country_code === 'ca') {
if (remoteProvinces.includes(state)) {
// For remote provinces, allow partial addresses
country = 'CA';
address = formatAddress(data.address, state, country);
addressData = data.address;
break;
} else {
// For other provinces, require detailed address
if (data.address.house_number && data.address.road && data.address.city) {
country = 'CA';
address = formatAddress(data.address, state, country);
addressData = data.address;
break;
}
}
@@ -56,7 +56,7 @@ async function handleRequest(request) {
}
}
if (!address) {
if (!addressData) {
return new Response(
JSON.stringify({ error: 'Failed to retrieve detailed address' }),
{
@@ -82,17 +82,86 @@ async function handleRequest(request) {
phone = getRandomPhoneNumber(country, state);
}
// Parse address components
const addressParts = parseAddressComponents(address, state);
// Ensure we have the correct state abbreviation
const stateAbbreviations = {
// US States
Alabama: 'AL',
Alaska: 'AK',
Arizona: 'AZ',
Arkansas: 'AR',
California: 'CA',
Colorado: 'CO',
Connecticut: 'CT',
Delaware: 'DE',
Florida: 'FL',
Georgia: 'GA',
Hawaii: 'HI',
Idaho: 'ID',
Illinois: 'IL',
Indiana: 'IN',
Iowa: 'IA',
Kansas: 'KS',
Kentucky: 'KY',
Louisiana: 'LA',
Maine: 'ME',
Maryland: 'MD',
Massachusetts: 'MA',
Michigan: 'MI',
Minnesota: 'MN',
Mississippi: 'MS',
Missouri: 'MO',
Montana: 'MT',
Nebraska: 'NE',
Nevada: 'NV',
'New Hampshire': 'NH',
'New Jersey': 'NJ',
'New Mexico': 'NM',
'New York': 'NY',
'North Carolina': 'NC',
'North Dakota': 'ND',
Ohio: 'OH',
Oklahoma: 'OK',
Oregon: 'OR',
Pennsylvania: 'PA',
'Rhode Island': 'RI',
'South Carolina': 'SC',
'South Dakota': 'SD',
Tennessee: 'TN',
Texas: 'TX',
Utah: 'UT',
Vermont: 'VT',
Virginia: 'VA',
Washington: 'WA',
'West Virginia': 'WV',
Wisconsin: 'WI',
Wyoming: 'WY',
// Canadian Provinces and Territories
Alberta: 'AB',
'British Columbia': 'BC',
Manitoba: 'MB',
'New Brunswick': 'NB',
'Newfoundland and Labrador': 'NL',
'Nova Scotia': 'NS',
Ontario: 'ON',
'Prince Edward Island': 'PE',
Quebec: 'QC',
Saskatchewan: 'SK',
'Northwest Territories': 'NT',
Nunavut: 'NU',
Yukon: 'YT',
};
// Get the correct state abbreviation - use addressData.state if available, otherwise use the original state parameter
const stateAbbr = stateAbbreviations[addressData.state] || state;
const jsonResponse = {
firstName: firstName,
lastName: lastName,
streetAddress: addressParts.streetAddress,
aptSuiteBldg: addressParts.aptSuiteBldg,
city: addressParts.city,
state: addressParts.state,
zipCode: addressParts.zipCode,
streetAddress: `${addressData.house_number || ''} ${addressData.road || ''}`.trim(),
aptSuiteBldg: '', // Could be enhanced if needed
city: addressData.city || addressData.town || addressData.village || '',
state: stateAbbr,
zipCode: addressData.postcode || '',
phoneNumber: phone
};
@@ -369,91 +438,6 @@ function getRandomLocationInState(state) {
return { lat, lng };
}
function formatAddress(address, state, country) {
const stateAbbreviations = {
// US States
Alabama: 'AL',
Alaska: 'AK',
Arizona: 'AZ',
Arkansas: 'AR',
California: 'CA',
Colorado: 'CO',
Connecticut: 'CT',
Delaware: 'DE',
Florida: 'FL',
Georgia: 'GA',
Hawaii: 'HI',
Idaho: 'ID',
Illinois: 'IL',
Indiana: 'IN',
Iowa: 'IA',
Kansas: 'KS',
Kentucky: 'KY',
Louisiana: 'LA',
Maine: 'ME',
Maryland: 'MD',
Massachusetts: 'MA',
Michigan: 'MI',
Minnesota: 'MN',
Mississippi: 'MS',
Missouri: 'MO',
Montana: 'MT',
Nebraska: 'NE',
Nevada: 'NV',
'New Hampshire': 'NH',
'New Jersey': 'NJ',
'New Mexico': 'NM',
'New York': 'NY',
'North Carolina': 'NC',
'North Dakota': 'ND',
Ohio: 'OH',
Oklahoma: 'OK',
Oregon: 'OR',
Pennsylvania: 'PA',
'Rhode Island': 'RI',
'South Carolina': 'SC',
'South Dakota': 'SD',
Tennessee: 'TN',
Texas: 'TX',
Utah: 'UT',
Vermont: 'VT',
Virginia: 'VA',
Washington: 'WA',
'West Virginia': 'WV',
Wisconsin: 'WI',
Wyoming: 'WY',
// Canadian Provinces and Territories
Alberta: 'AB',
'British Columbia': 'BC',
Manitoba: 'MB',
'New Brunswick': 'NB',
'Newfoundland and Labrador': 'NL',
'Nova Scotia': 'NS',
Ontario: 'ON',
'Prince Edward Island': 'PE',
Quebec: 'QC',
Saskatchewan: 'SK',
'Northwest Territories': 'NT',
Nunavut: 'NU',
Yukon: 'YT',
};
const stateAbbr = stateAbbreviations[address.state] || state;
let formattedAddress = '';
if (address.house_number && address.road && address.city) {
if (country === 'US') {
formattedAddress = `${address.house_number} ${address.road}, ${address.city}, ${stateAbbr} ${address.postcode}, United States`;
} else if (country === 'CA') {
formattedAddress = `${address.house_number} ${address.road}, ${address.city}, ${stateAbbr} ${address.postcode}, Canada`;
}
} else {
// For partial addresses in remote provinces
formattedAddress = `${address.city}, ${stateAbbr} ${address.postcode}, ${country === 'US' ? 'United States' : 'Canada'}`;
}
return formattedAddress;
}
function getRandomPhoneNumber(country, state) {
const areaCodesUS = {
AL: ['205', '251', '256', '334', '938'],
@@ -540,50 +524,6 @@ function getRandomPhoneNumber(country, state) {
return `(${areaCode}) ${exchangeCode}-${lineNumber}`;
}
function parseAddressComponents(fullAddress, state) {
// Parse the formatted address to extract components
const parts = fullAddress.split(', ');
let streetAddress = '';
let aptSuiteBldg = '';
let city = '';
let stateCode = '';
let zipCode = '';
if (parts.length >= 3) {
streetAddress = parts[0] || '';
city = parts[1] || '';
// Handle state and zip from the third part
if (parts[2]) {
const stateZipMatch = parts[2].match(/^([A-Z]{2})\s*([A-Z0-9\s-]*)/);
if (stateZipMatch) {
stateCode = stateZipMatch[1];
zipCode = stateZipMatch[2] ? stateZipMatch[2].trim() : '';
} else {
stateCode = state;
}
}
} else {
// For partial addresses
if (parts.length === 2) {
city = parts[0] || '';
const stateZipMatch = parts[1].match(/^([A-Z]{2})\s*([A-Z0-9\s-]*)/);
if (stateZipMatch) {
stateCode = stateZipMatch[1];
zipCode = stateZipMatch[2] ? stateZipMatch[2].trim() : '';
}
}
}
return {
streetAddress: streetAddress,
aptSuiteBldg: aptSuiteBldg, // Empty for now, could be enhanced
city: city,
state: stateCode || state,
zipCode: zipCode
};
}
function getRandomState() {
const states = [
// US States