@tmsfe/tms-core 0.0.16 → 0.0.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -767,6 +767,7 @@ let cityTheUserAt = '';
767
767
  // 用户城市变化事件名
768
768
  const CityChangeEventName = 'loc_city_changed';
769
769
  const LocationType = 'gcj02'; // 获取经纬度时的坐标名称
770
+ let ipLocationPromise = null; // ip定位请求的promise
770
771
  /**
771
772
  * @class Location
772
773
  * @classdesc 基于LocationBase,封装业务侧位置的接口。 将用户经纬度转化为城市等展示信息
@@ -993,6 +994,32 @@ class Location extends LocationBase$1 {
993
994
  }
994
995
  return request.post('basic/lbs/direction', { from, to, mode });
995
996
  }
997
+ /**
998
+ * ip定位
999
+ * 原理:通过调手图接口查询当前IP所在位置,市级的准确率是91%
1000
+ * 注意:由于服务端对该查询服务有次数限制,所以本函数会缓存成功的promise
1001
+ * @param force 是否清除上次的缓存重新请求
1002
+ */
1003
+ getIpLocation(force = false) {
1004
+ if (ipLocationPromise === null || force) {
1005
+ ipLocationPromise = new Promise((resolve, reject) => {
1006
+ request.post('basic/lbs/decodeip')
1007
+ .then((res) => {
1008
+ if (res.errCode === 0) {
1009
+ resolve(res.resData);
1010
+ return;
1011
+ }
1012
+ reject({ erMsg: res.errMsg });
1013
+ ipLocationPromise = null;
1014
+ })
1015
+ .catch((e) => {
1016
+ reject(e);
1017
+ ipLocationPromise = null;
1018
+ });
1019
+ });
1020
+ }
1021
+ return ipLocationPromise;
1022
+ }
996
1023
  }
997
1024
  // 因为在构造函数中会用到wx的api,所以使用到时才实例化
998
1025
  let instance;
@@ -1136,11 +1163,11 @@ const getProvinceInfoByIp = () => {
1136
1163
  if (!ProvinceInfoCacheByIp) {
1137
1164
  ProvinceInfoCacheByIp = new Promise(async resolve => {
1138
1165
  try {
1139
- const loc = await R.post('basic/lbs/decodeip');
1166
+ const loc = await getLocInstance().getIpLocation();
1140
1167
  const {
1141
1168
  city,
1142
1169
  province
1143
- } = loc.resData.ad_info;
1170
+ } = loc.ad_info;
1144
1171
  resolve({
1145
1172
  cityName: city,
1146
1173
  province
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmsfe/tms-core",
3
- "version": "0.0.16",
3
+ "version": "0.0.17",
4
4
  "description": "tms运行时框架",
5
5
  "repository": {
6
6
  "type": "git",
@@ -16,6 +16,22 @@ interface UserLocationType {
16
16
  [key: string]: Promise<PostionType | void>
17
17
  }
18
18
 
19
+ interface IpLocationType {
20
+ ip: string,
21
+ ad_info: {
22
+ adcode: number,
23
+ city: string,
24
+ cityCode: string,
25
+ district: string,
26
+ nation: string,
27
+ province: string,
28
+ },
29
+ location: {
30
+ lat: number,
31
+ lng: number,
32
+ },
33
+ }
34
+
19
35
  const request = new Request();
20
36
  const event = new EventDispatcher();
21
37
 
@@ -27,6 +43,7 @@ let cityTheUserAt = '';
27
43
  // 用户城市变化事件名
28
44
  const CityChangeEventName = 'loc_city_changed';
29
45
  const LocationType = 'gcj02'; // 获取经纬度时的坐标名称
46
+ let ipLocationPromise: null | Promise<IpLocationType> = null; // ip定位请求的promise
30
47
 
31
48
  /**
32
49
  * @class Location
@@ -272,6 +289,33 @@ class Location extends LocationBase {
272
289
  }
273
290
  return request.post('basic/lbs/direction', { from, to, mode });
274
291
  }
292
+
293
+ /**
294
+ * ip定位
295
+ * 原理:通过调手图接口查询当前IP所在位置,市级的准确率是91%
296
+ * 注意:由于服务端对该查询服务有次数限制,所以本函数会缓存成功的promise
297
+ * @param force 是否清除上次的缓存重新请求
298
+ */
299
+ getIpLocation(force = false): Promise<IpLocationType> {
300
+ if (ipLocationPromise === null || force) {
301
+ ipLocationPromise = new Promise((resolve, reject) => {
302
+ request.post('basic/lbs/decodeip')
303
+ .then((res) => {
304
+ if (res.errCode === 0) {
305
+ resolve(res.resData as IpLocationType);
306
+ return;
307
+ }
308
+ reject({ erMsg: res.errMsg });
309
+ ipLocationPromise = null;
310
+ })
311
+ .catch((e) => {
312
+ reject(e);
313
+ ipLocationPromise = null;
314
+ });
315
+ });
316
+ }
317
+ return ipLocationPromise;
318
+ }
275
319
  }
276
320
 
277
321
  // 因为在构造函数中会用到wx的api,所以使用到时才实例化
package/src/report.js CHANGED
@@ -106,8 +106,8 @@ const getProvinceInfoByIp = () => {
106
106
  if (!ProvinceInfoCacheByIp) {
107
107
  ProvinceInfoCacheByIp = new Promise(async (resolve) => {
108
108
  try {
109
- const loc = await R.post('basic/lbs/decodeip');
110
- const { city, province } = loc.resData.ad_info;
109
+ const loc = await getLocInstance().getIpLocation();
110
+ const { city, province } = loc.ad_info;
111
111
  resolve({ cityName: city, province });
112
112
  } catch (_) {
113
113
  resolve({ cityName: '', province: '' });