MapApi.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { DataModel } from '@imengyu/js-request-transform';
  2. import { MengyuServerRequestModule } from '../RequestModules';
  3. export interface CityItem {
  4. first_letter: string;
  5. code: string;
  6. name: string;
  7. /**
  8. * 0:省
  9. * 1:市
  10. * 2:区
  11. */
  12. type: number;
  13. children?: CityItem[];
  14. }
  15. /** 行政区划层级(与服务端 MapCityData.level 一致) */
  16. export type MapCityDataLevel = 'province' | 'city' | 'district';
  17. /**
  18. * 省市区数据模型
  19. */
  20. export class MapCityData extends DataModel<MapCityData> {
  21. constructor() {
  22. super(MapCityData, '省市区数据');
  23. }
  24. adcode = '';
  25. parentAdCode: string | null = null;
  26. citycode = '';
  27. name = '';
  28. center = '';
  29. polyline = '';
  30. pinyin = '';
  31. level = '' as MapCityDataLevel;
  32. }
  33. export class MapApi extends MengyuServerRequestModule<MapCityData> {
  34. /**
  35. * 快速获取地区编码
  36. * @param name 地区名称
  37. * @param city 城市名称(可选,用于消歧)
  38. */
  39. simpleGetRegionCode(name: string, city?: string) {
  40. return this.get<string>(
  41. `/content/map/simpleGetRegionCode`,
  42. '快速获取地区编码',
  43. { name, city },
  44. )
  45. .then(res => res.requireData() as string)
  46. .catch(e => { throw e });
  47. }
  48. /**
  49. * 简单获取地区(扁平地区信息)
  50. * @param name 地区名称
  51. * @param city 城市名称(可选,用于消歧)
  52. */
  53. simpleGetRegion(name: string, city?: string) {
  54. return this.get<MapCityData>(
  55. `/content/map/simpleGetRegion`,
  56. '简单获取地区',
  57. { name, city },
  58. MapCityData,
  59. );
  60. }
  61. /**
  62. * 按编码获取地区详情
  63. * @param adcode 地区编码
  64. */
  65. getRegionInfo(adcode: string) {
  66. return this.get<MapCityData>(
  67. `/content/map/getRegionInfo`,
  68. '按编码获取地区详情',
  69. { adcode },
  70. );
  71. }
  72. /**
  73. * 获取子级地区列表
  74. * @param adcode 地区编码(可选;不传则返回省级列表)
  75. */
  76. getRegionChildren(adcode?: string) {
  77. return this.get<MapCityData[]>(
  78. `/content/map/getRegionChildren`,
  79. '获取子级地区列表',
  80. { adcode },
  81. );
  82. }
  83. loadCityData(full = false) : Promise<CityItem[]> {
  84. return new Promise((resolve, reject) => {
  85. uni.request({
  86. url: `https://mn.wenlvti.net/app_static/xiangyuan/data/ChinaCityData${full ? '' : '.slim'}.json`,
  87. method: 'GET',
  88. success(result) {
  89. if (result.statusCode === 200) {
  90. resolve(result.data as CityItem[]);
  91. } else {
  92. reject(new Error(`请求失败,状态码:${result.statusCode}`));
  93. }
  94. },
  95. fail(error) {
  96. reject(error);
  97. }
  98. })
  99. });
  100. }
  101. }
  102. export default new MapApi();