Ver código fonte

💊 修复地址选择无效问题

快乐的梦鱼 2 semanas atrás
pai
commit
e59337af75

+ 13 - 0
src/api/RequestModules.ts

@@ -169,6 +169,19 @@ export class TencentMengyuServerRequestModule<T extends DataModel> extends BaseA
 }
 
 /**
+ * 地图服务请求模块
+ */
+export class AMapServerRequestModule<T extends DataModel> extends BaseAppServerRequestModule<T> {
+  constructor() {
+    super('https://restapi.amap.com');
+    this.config.requestInterceptor = (url, req) => {
+      url = appendGetUrlParams(url, 'key', ApiCofig.amapServerKey);
+      return { newUrl: url, newReq: req };
+    };
+  }
+}
+
+/**
  * 更新服务请求模块
  */
 export class UpdateServerRequestModule<T extends DataModel> extends BaseAppServerRequestModule<T> {

+ 87 - 0
src/api/map/AMapApi.ts

@@ -0,0 +1,87 @@
+import { DataModel, transformArrayDataModel, type NewDataModel } from '@imengyu/js-request-transform';
+import { AMapServerRequestModule } from '../RequestModules';
+import { type QueryParams } from '@imengyu/imengyu-utils';
+
+export class Poi extends DataModel<Poi> {
+  constructor() {
+    super(Poi, "Poi");
+    this.setNameMapperCase('Camel', 'Snake');
+    this._convertTable = {
+      location: { clientSide: 'splitCommaArray' },
+    };
+    this._convertKeyType = (key, direction) => {
+      if (key.endsWith('Time'))
+        return {
+          clientSide: 'date',
+          serverSide: 'string',
+        };
+      return undefined;
+    };
+  }
+  name = '';
+  location = [] as number[];
+  type = '';
+  address = '';
+  cityname = '';
+}
+
+export class AMapApi extends AMapServerRequestModule<DataModel> {
+
+  constructor() {
+    super();
+  }
+
+
+  searchPoi(name: string, page = 1, size = 10, querys?: QueryParams) {
+    return this.get(`/v3/place/text?keywords=${encodeURIComponent(name)}&citylimit=true&offset=${size}&page=${page}`, `搜索POI ${name} 第${page}页`, {
+      ...querys,
+    })
+      .then(res => transformArrayDataModel(Poi, res.data ?? [], ''))
+      .catch(e => { throw e });
+  }
+  regeo(lat: number, lng: number, querys?: QueryParams) {
+    return this.get(`/v3/geocode/regeo`, `查询经纬度(${lat}, ${lng})所属区县`, {
+      location: `${lng},${lat}`,
+      ...querys,
+    })
+      .then(res => (res.data as any).regeocode.addressComponent as {
+        country: string,
+        province: string,
+        city: string,
+        district: string,
+        adcode: string,
+        township: string,
+        citycode: string,
+        towncode: string,
+      })
+      .catch(e => { throw e });
+  }
+  regeoAddress(lat: number, lng: number, querys?: QueryParams) {
+    return this.get(`/v3/geocode/regeo`, `查询经纬度(${lat}, ${lng})地址`, {
+      location: `${lng},${lat}`,
+      ...querys,
+    })
+      .then(res => (res.data as any).regeocode.formatted_address as string)
+      .catch(e => { throw e });
+  }
+  loadCityData() {
+    return new Promise((resolve, reject) => {
+      uni.request({
+        url: 'https://mn.wenlvti.net/app_static/xiangyuan/data/ChinaCityData.slim.json',
+        method: 'GET',
+        success(result) {
+          if (result.statusCode === 200) {
+            resolve(result.data);
+          } else {
+            reject(new Error(`请求失败,状态码:${result.statusCode}`));
+          }
+        },
+        fail(error) {
+          reject(error);
+        }
+      })
+    });
+  }
+}
+
+export default new AMapApi();

+ 3 - 2
src/common/components/dynamicf/ComponentConfigs.ts

@@ -1,3 +1,4 @@
+import AMapApi from "@/api/map/AMapApi";
 import MapApi from "@/api/map/MapApi";
 import type { IDynamicFormComponentAdditionalDefine } from "@/components/dynamic";
 
@@ -9,13 +10,13 @@ export default [
     needArrow: true,
     props: {
       loadCityData: () => MapApi.loadCityData(),
-      loadDistrictInfo: (latlon: [number, number]) => MapApi.regeo(latlon[0], latlon[1]),
+      loadDistrictInfo: (latlon: [number, number]) => AMapApi.regeo(latlon[0], latlon[1]),
     },
   },
   {
     name: 'select-address',
     props: {
-      loadFormattedAddress: (latlon: [number, number]) => MapApi.regeoAddress(latlon[0], latlon[1]),
+      loadFormattedAddress: (latlon: [number, number]) => AMapApi.regeoAddress(latlon[0], latlon[1]),
     },
   },
 ] as IDynamicFormComponentAdditionalDefine[];

+ 1 - 0
src/common/config/ApiCofig.ts

@@ -9,6 +9,7 @@ export default {
   },
   mainBodyId: 1,
   platformId: 330,
+  amapServerKey: '8fd09264c33678141f609588c432df0e',
   mapKey: 'LDXBZ-JIWWC-IXW2S-AUDZS-26VC2-GRBC4',
   bugReport: {
     server: 'https://update-server1.imengyu.top/bug-submit',

+ 2 - 1
src/pages/dig/forms/data/spots.ts

@@ -3,6 +3,7 @@ import { villageCommonContent } from "./common";
 import type { FieldProps } from "@/components/form/Field.vue";
 import type { SingleForm } from "../forms";
 import MapApi from "@/api/map/MapApi";
+import AMapApi from "@/api/map/AMapApi";
 
 export const villageInfoSpotsFormItems : SingleForm = [CommonInfoModel, (form) => ({
   formItems: [
@@ -28,7 +29,7 @@ export const villageInfoSpotsFormItems : SingleForm = [CommonInfoModel, (form) =
             onChange(val: [number, number]) {
               if (form.value.getValueByPath('address'))
                 return;
-              MapApi.regeoAddress(val[0], val[1]).then(res => {
+              AMapApi.regeoAddress(val[0], val[1]).then(res => {
                 form.value.setValueByPath('address', res);
               });
             },