合同小程序前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.8 KiB

  1. // @ts-nocheck
  2. import _areaList from './city-china.json';
  3. export const areaList = _areaList
  4. // #ifndef UNI-APP-X
  5. type UTSJSONObject = Record<string, string>
  6. // #endif
  7. // #ifdef UNI-APP-X
  8. type Object = UTSJSONObject
  9. // #endif
  10. type AreaList = {
  11. province_list : Map<string, string>;
  12. city_list : Map<string, string>;
  13. county_list : Map<string, string>;
  14. }
  15. // type CascaderOption = {
  16. // text : string;
  17. // value : string;
  18. // children ?: CascaderOption[];
  19. // };
  20. const makeOption = (
  21. label : string,
  22. value : string,
  23. children ?: UTSJSONObject[],
  24. ) : UTSJSONObject => ({
  25. label,
  26. value,
  27. children,
  28. });
  29. export function useCascaderAreaData() : UTSJSONObject[] {
  30. const city = areaList['city_list'] as UTSJSONObject
  31. const county = areaList['county_list'] as UTSJSONObject
  32. const province = areaList['province_list'] as UTSJSONObject
  33. const provinceMap = new Map<string, UTSJSONObject>();
  34. Object.keys(province).forEach((code) => {
  35. provinceMap.set(code.slice(0, 2), makeOption(`${province[code]}`, code, []));
  36. });
  37. const cityMap = new Map<string, UTSJSONObject>();
  38. Object.keys(city).forEach((code) => {
  39. const option = makeOption(`${city[code]}`, code, []);
  40. cityMap.set(code.slice(0, 4), option);
  41. const _province = provinceMap.get(code.slice(0, 2));
  42. if (_province != null) {
  43. (_province['children'] as UTSJSONObject[]).push(option)
  44. }
  45. });
  46. Object.keys(county).forEach((code) => {
  47. const _city = cityMap.get(code.slice(0, 4));
  48. if (_city != null) {
  49. (_city['children'] as UTSJSONObject[]).push(makeOption(`${county[code]}`, code, null));
  50. }
  51. });
  52. // #ifndef APP-ANDROID || APP-IOS
  53. return Array.from(provinceMap.values());
  54. // #endif
  55. // #ifdef APP-ANDROID || APP-IOS
  56. const obj : UTSJSONObject[] = []
  57. provinceMap.forEach((value, code) => {
  58. obj.push(value)
  59. })
  60. return obj
  61. // #endif
  62. }