@tmsfe/tms-core 0.0.14 → 0.0.18

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.
@@ -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: '' });
package/src/request.js CHANGED
@@ -20,7 +20,6 @@ import { getEnvInfo, getAuthInfo } from './env';
20
20
  */
21
21
  const seriesParam = (param) => {
22
22
  const keys = Object.keys(param)
23
- .filter(key => typeof param[key] !== 'undefined')
24
23
  .sort();
25
24
  const series = keys.map((key) => {
26
25
  const val = param[key];
@@ -73,6 +72,16 @@ const composeParam = async (param = {}, withAuth = true, baseParam = {}) => {
73
72
  { ...baseParam },
74
73
  { ...paramsWithAuth },
75
74
  );
75
+ // 清理undefined和NaN的参数
76
+ Object.keys(combinedParam)
77
+ .forEach((key) => {
78
+ if (typeof combinedParam[key] === 'number' && isNaN(combinedParam[key])) {
79
+ delete combinedParam[key];
80
+ }
81
+ if (typeof combinedParam[key] === 'undefined') {
82
+ delete combinedParam[key];
83
+ }
84
+ });
76
85
  return combinedParam;
77
86
  };
78
87