areaparse.ts 686 B

123456789101112131415161718192021
  1. import { areaData } from './area'
  2. export const addressParse = (provinceName: string, cityName: string, countryName: string) => {
  3. return new Promise((resolve, reject) => {
  4. try {
  5. const province = areaData.find((v) => v.name === provinceName)
  6. const { code: provinceCode } = province!
  7. const city = province!.children.find((v) => v.name === cityName)
  8. const { code: cityCode } = city!
  9. const country = city!.children.find((v) => v.name === countryName)
  10. const { code: districtCode } = country!
  11. resolve({
  12. provinceCode,
  13. cityCode,
  14. districtCode,
  15. })
  16. } catch (error) {
  17. reject('地址解析失败')
  18. }
  19. })
  20. }