167 lines
5.3 KiB
JavaScript
167 lines
5.3 KiB
JavaScript
|
|
/**
|
|||
|
|
* 国家地址配置
|
|||
|
|
* 定义各国地址字段规则和邮编格式
|
|||
|
|
*/
|
|||
|
|
|
|||
|
|
// 货币代码到国家代码的映射
|
|||
|
|
export const currencyToCountry = {
|
|||
|
|
'USD': 'US',
|
|||
|
|
'SGD': 'SG',
|
|||
|
|
'MYR': 'MY',
|
|||
|
|
'PHP': 'PH',
|
|||
|
|
'THB': 'TH',
|
|||
|
|
'VND': 'VN',
|
|||
|
|
'CNY': 'CN',
|
|||
|
|
'GBP': 'GB',
|
|||
|
|
'EUR': 'DE'
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 国家配置
|
|||
|
|
export const countryConfigs = {
|
|||
|
|
SG: {
|
|||
|
|
code: 'SG',
|
|||
|
|
name: '新加坡',
|
|||
|
|
nameEn: 'Singapore',
|
|||
|
|
phoneCode: '+65',
|
|||
|
|
postcodeLength: 6,
|
|||
|
|
postcodePattern: /^\d{6}$/,
|
|||
|
|
requiredFields: ['shippingName', 'shippingPhone', 'shippingCountry', 'shippingCity',
|
|||
|
|
'shippingAddressLine1', 'shippingBlockNumber', 'shippingUnitNumber', 'shippingPostcode'],
|
|||
|
|
specialFields: ['shippingBlockNumber', 'shippingUnitNumber'],
|
|||
|
|
fieldLabels: {
|
|||
|
|
shippingBlockNumber: '组屋号 (Block Number)',
|
|||
|
|
shippingUnitNumber: '单元号 (Unit Number)',
|
|||
|
|
shippingAddressLine1: '详细地址1 (Address Line 1)',
|
|||
|
|
shippingAddressLine2: '详细地址2 (Address Line 2)',
|
|||
|
|
shippingCity: '城市 (City)',
|
|||
|
|
shippingPostcode: '邮编 (Postcode)'
|
|||
|
|
},
|
|||
|
|
addressFormat: 'Blk 123 Jurong West St 41 #12-345, Singapore 640123'
|
|||
|
|
},
|
|||
|
|
MY: {
|
|||
|
|
code: 'MY',
|
|||
|
|
name: '马来西亚',
|
|||
|
|
nameEn: 'Malaysia',
|
|||
|
|
phoneCode: '+60',
|
|||
|
|
postcodeLength: 5,
|
|||
|
|
postcodePattern: /^\d{5}$/,
|
|||
|
|
requiredFields: ['shippingName', 'shippingPhone', 'shippingCountry', 'shippingCity',
|
|||
|
|
'shippingStateMalaysia', 'shippingAddressLine1', 'shippingPostcode'],
|
|||
|
|
specialFields: ['shippingStateMalaysia'],
|
|||
|
|
fieldLabels: {
|
|||
|
|
shippingStateMalaysia: '州属 (State)',
|
|||
|
|
shippingAddressLine1: '详细地址1 (Address Line 1)',
|
|||
|
|
shippingAddressLine2: '详细地址2 (Address Line 2)',
|
|||
|
|
shippingCity: '城市 (City)',
|
|||
|
|
shippingPostcode: '邮编 (Postcode)'
|
|||
|
|
},
|
|||
|
|
addressFormat: '123 Jalan Abdullah, 05-01 Menara A, Kuala Lumpur, Selangor 50300'
|
|||
|
|
},
|
|||
|
|
PH: {
|
|||
|
|
code: 'PH',
|
|||
|
|
name: '菲律宾',
|
|||
|
|
nameEn: 'Philippines',
|
|||
|
|
phoneCode: '+63',
|
|||
|
|
postcodeLength: 4,
|
|||
|
|
postcodePattern: /^\d{4}$/,
|
|||
|
|
requiredFields: ['shippingName', 'shippingPhone', 'shippingCountry', 'shippingCity',
|
|||
|
|
'shippingState', 'shippingBarangay', 'shippingAddressLine1', 'shippingPostcode'],
|
|||
|
|
specialFields: ['shippingBarangay'],
|
|||
|
|
fieldLabels: {
|
|||
|
|
shippingBarangay: 'Barangay(社区编号)',
|
|||
|
|
shippingState: '省 (Province)',
|
|||
|
|
shippingCity: '市 (City)',
|
|||
|
|
shippingAddressLine1: '详细地址1 (Address Line 1)',
|
|||
|
|
shippingAddressLine2: '详细地址2 (Address Line 2)',
|
|||
|
|
shippingPostcode: '邮编 (Postcode)'
|
|||
|
|
},
|
|||
|
|
addressFormat: '123 Main St, Barangay 12, Manila, Metro Manila 1000'
|
|||
|
|
},
|
|||
|
|
TH: {
|
|||
|
|
code: 'TH',
|
|||
|
|
name: '泰国',
|
|||
|
|
nameEn: 'Thailand',
|
|||
|
|
phoneCode: '+66',
|
|||
|
|
postcodeLength: 5,
|
|||
|
|
postcodePattern: /^\d{5}$/,
|
|||
|
|
requiredFields: ['shippingName', 'shippingPhone', 'shippingCountry', 'shippingCity',
|
|||
|
|
'shippingState', 'shippingAddressLine1', 'shippingPostcode', 'shippingAddressThai'],
|
|||
|
|
specialFields: ['shippingAddressThai', 'shippingAdministrativeArea'],
|
|||
|
|
fieldLabels: {
|
|||
|
|
shippingAddressThai: '泰文地址 (Thai Address)',
|
|||
|
|
shippingAddressLine1: '英文地址 (English Address)',
|
|||
|
|
shippingAddressLine2: '详细地址2 (Address Line 2)',
|
|||
|
|
shippingState: '府 (Changwat)',
|
|||
|
|
shippingCity: '县 (Amphoe)',
|
|||
|
|
shippingAdministrativeArea: '区 (Tambon)',
|
|||
|
|
shippingPostcode: '邮编 (Postcode)'
|
|||
|
|
},
|
|||
|
|
addressFormat: '123 Soi Sukhumvit 101, Khlong Toei, Bangkok 10110'
|
|||
|
|
},
|
|||
|
|
VN: {
|
|||
|
|
code: 'VN',
|
|||
|
|
name: '越南',
|
|||
|
|
nameEn: 'Vietnam',
|
|||
|
|
phoneCode: '+84',
|
|||
|
|
postcodeLength: 5,
|
|||
|
|
postcodePattern: /^\d{5}$/,
|
|||
|
|
requiredFields: ['shippingName', 'shippingPhone', 'shippingCountry', 'shippingProvince',
|
|||
|
|
'shippingDistrict', 'shippingWard', 'shippingAddressLine1', 'shippingPostcode'],
|
|||
|
|
specialFields: ['shippingProvince', 'shippingDistrict', 'shippingWard'],
|
|||
|
|
fieldLabels: {
|
|||
|
|
shippingProvince: '省 (Tỉnh)',
|
|||
|
|
shippingDistrict: '市/郡 (Thành phố/Huyện)',
|
|||
|
|
shippingWard: '区/坊 (Quận/Phường)',
|
|||
|
|
shippingAddressLine1: '详细地址1 (Địa chỉ chi tiết 1)',
|
|||
|
|
shippingAddressLine2: '详细地址2 (Địa chỉ chi tiết 2)',
|
|||
|
|
shippingPostcode: '邮编 (Postcode)'
|
|||
|
|
},
|
|||
|
|
addressFormat: '123 Đường Nguyễn Huệ, Phường Bến Nghé, Quận 1, Thành phố Hồ Chí Minh 70000'
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 根据国家代码获取配置
|
|||
|
|
*/
|
|||
|
|
export function getCountryConfig(countryCode) {
|
|||
|
|
return countryConfigs[countryCode] || null
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 根据货币代码推断国家
|
|||
|
|
*/
|
|||
|
|
export function getCountryByCurrency(currency) {
|
|||
|
|
return currencyToCountry[currency] || null
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 验证邮编格式
|
|||
|
|
*/
|
|||
|
|
export function validatePostcode(countryCode, postcode) {
|
|||
|
|
if (!postcode || !postcode.trim()) {
|
|||
|
|
return false
|
|||
|
|
}
|
|||
|
|
const config = getCountryConfig(countryCode)
|
|||
|
|
if (!config) {
|
|||
|
|
return true // 未知国家不验证
|
|||
|
|
}
|
|||
|
|
return config.postcodePattern.test(postcode.trim())
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 获取必填字段
|
|||
|
|
*/
|
|||
|
|
export function getRequiredFields(countryCode) {
|
|||
|
|
const config = getCountryConfig(countryCode)
|
|||
|
|
return config ? config.requiredFields : []
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 获取特殊字段
|
|||
|
|
*/
|
|||
|
|
export function getSpecialFields(countryCode) {
|
|||
|
|
const config = getCountryConfig(countryCode)
|
|||
|
|
return config ? config.specialFields : []
|
|||
|
|
}
|
|||
|
|
|