hejianhao
2026-03-06 b8f3899ffaf7ef471899bc06a178ed8f352bc201
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import amapFile from '../libs/amap-wx.130.js';
import { AMAP_KEY, _ROBOTAXI_HOST } from '../constants/api';
import Taro from '@tarojs/taro';
import request from './request';
import { getFenceCheck, getNearestSite } from '../robotaxi/api/home';
import { getOpenCity } from '../robotaxi/api/select-city';
 
// 辅助函数:计算两点之间的距离(单位:米)
export const getDistance = (lat1: number, lng1: number, lat2: number, lng2: number) => {
  const radLat1 = lat1 * Math.PI / 180.0;
  const radLat2 = lat2 * Math.PI / 180.0;
  const a = radLat1 - radLat2;
  const b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
  let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
  s = s * 6378.137; // 地球半径
  s = Math.round(s * 10000) / 10000;
  return s * 1000;
};
 
// 格式化距离
export const formatDistance = (val: any) => {
  if (!val) return '0m';
  const d = Number(val);
  if (Number.isNaN(d)) return val;
  if (d >= 1000) {
    return (d / 1000).toFixed(2) + 'km';
  }
  return d.toFixed(2) + 'm';
}
 
// 查找最近的标记点
export const findNearestMarker = (lat: number, lng: number, markers: any[]) => {
  let minDistance = Infinity;
  let targetMarker: any = null;
 
  markers.forEach(marker => {
    const distance = getDistance(lat, lng, marker.latitude, marker.longitude);
    if (distance < minDistance) {
      minDistance = distance;
      targetMarker = marker;
    }
  });
 
  return { targetMarker, minDistance };
};
 
// 解析路线数据为坐标点数组
export const parseRouteToPoints = (routeData: any) => {
  const points: any[] = [];
  if (routeData.paths && routeData.paths[0] && routeData.paths[0].steps) {
    const steps = routeData.paths[0].steps;
    for (let i = 0; i < steps.length; i++) {
      const poLen = steps[i].polyline.split(';');
      for (let j = 0; j < poLen.length; j++) {
        points.push({
          longitude: parseFloat(poLen[j].split(',')[0]),
          latitude: parseFloat(poLen[j].split(',')[1])
        })
      }
    }
  }
  return points;
};
 
// 格式化站点为标记点
export const formatSiteToMarker = (sites: any[]) => {
  return sites.map((site, index) => ({
    ...site,
    // id: index + 1,
    iconPath: 'https://huishou-1323682843.cos.ap-nanjing.myqcloud.com/images/a2b43790-9317-4b3b-b29f-d82cc10830e9.png',
    width: 15,
    height: 15,
    anchor: { x: 0.5, y: 0.5 }
  }));
};
 
// 获取位置的逆地理编码
export const getAddressByLocation = (longitude: number, latitude: number): Promise<any> => {
  return new Promise((resolve, reject) => {
    const myAmapFun = new amapFile.AMapWX({ key: AMAP_KEY });
    myAmapFun.getRegeo({
      location: `${longitude},${latitude}`,
      success: (data) => {
        if (data && data[0]) {
          resolve(data[0]);
        } else {
          reject('No data found');
        }
      },
      fail: (info) => {
        reject(info);
      }
    });
  });
};
 
// 格式化电子围栏数据
export const formatGeofencePolygons = (geofenceList: any[]) => {
  if (!geofenceList || !Array.isArray(geofenceList)) {
    return [];
  }
  return geofenceList.map(item => {
    return {
      points: item.points.map(p => ({
        latitude: p.lat,
        longitude: p.lng
      })),
      strokeWidth: 2,
      strokeColor: '#A1B1FF',
      fillColor: '#6d7fd933',
      zIndex: 1
    }
  });
};
 
// 提取多边形所有顶点
export const extractPointsFromPolygons = (polygons: any[]) => {
  const points: any[] = [];
  if (!polygons || !Array.isArray(polygons)) return points;
 
  polygons.forEach(polygon => {
    if (polygon.points && Array.isArray(polygon.points)) {
      points.push(...polygon.points);
    }
  });
  return points;
};
 
// 转换 adcode 为 cityCode
export const convertAdcodeToCityCode = (adcode: string, cityName: string, province: string) => {
  let cityCode = adcode;
  if (adcode && typeof adcode === 'string' && adcode.length === 6) {
    if (cityName === province) {
      // 直辖市:前2位 + 0000
      cityCode = adcode.substring(0, 2) + '0000';
    } else {
      // 普通城市:前4位 + 00
      cityCode = adcode.substring(0, 4) + '00';
    }
  }
  return cityCode;
};
 
// 处理 Banner 点击跳转
export const handleBannerJump = (item: any) => {
  if (!item) return;
  const { jumpType, jumpUrl } = item;
  // jumpType: 1小程序内页面 2小程序地址 3网页地址
  if (jumpType === 1) {
    if (jumpUrl) {
      Taro.navigateTo({
        url: jumpUrl
      });
    }
  } else if (jumpType === 2) {
    if (jumpUrl) {
      Taro.navigateToMiniProgram({
        appId: jumpUrl,
        // path: '',
        success(res) {
          console.log('打开小程序成功', res);
        },
        fail(err) {
          console.error('打开小程序失败', err);
          Taro.showToast({ title: '跳转失败', icon: 'none' });
        }
      });
    }
  } else if (jumpType === 3) {
    if (jumpUrl) {
      Taro.navigateTo({
        url: `/pages/webpage/index?pageUrl=${encodeURIComponent(jumpUrl)}`
      });
    }
  }
};
 
// 防抖函数
export const debounce = (fn: Function, wait: number) => {
  let timer: any = null;
  return function (this: any, ...args: any[]) {
    if (timer) clearTimeout(timer);
    timer = setTimeout(() => {
      fn.apply(this, args);
    }, wait);
  }
}
 
// 节流函数
export const throttle = (fn: Function, wait: number) => {
  let _lastTime: any = null;
  return function (this: any, ...args: any[]) {
    let _nowTime = +new Date();
    if (!_lastTime || _nowTime - _lastTime > wait) {
      fn.apply(this, args);
      _lastTime = _nowTime;
    }
  }
}
 
// 预下载图片,返回本地临时路径(WeApp),失败或非WeApp环境返回原始URL
export const preloadImageToTemp = async (url: string): Promise<string> => {
  try {
    if (typeof Taro.getEnv !== 'function' || Taro.getEnv() !== Taro.ENV_TYPE.WEAPP) {
      return url;
    }
    const res = await Taro.downloadFile({ url });
    if (res?.statusCode === 200 && res.tempFilePath) {
      return res.tempFilePath;
    }
    const info = await Taro.getImageInfo({ src: url });
    if (info?.path) {
      return info.path;
    }
  } catch (e) {
  }
  return url;
}
 
// 获取地图数据(围栏检查+附近站点)
export const fetchMapData = async (lat: number, lng: number) => {
  try {
    // 1. 先检查是否在运营区域内
    const fenceRes: any = await getFenceCheck({ latitude: lat, longitude: lng }).catch(e => {
      return { status: 'inside' }; // 默认兜底
    });
 
    const isInside = fenceRes.status === 'inside';
    const electronicAreaId = fenceRes?.electronicAreaId || '';
 
    // 2. 只有在运营区域内才获取附近站点
    let sites: any = { points: [] };
    if (isInside) {
      sites = await getNearestSite({ latitude: lat, longitude: lng }).catch(e => {
        return { points: [] };
      });
      // 兼容处理
      if (!sites || !sites.points) {
        sites = { points: [] };
      }
    }
 
    const newMarkers = formatSiteToMarker(sites.points);
 
    return { isInside, electronicAreaId, newMarkers, sites: sites.points };
  } catch (error) {
    console.error('fetchMapData error', error);
    return { isInside: true, electronicAreaId: '', newMarkers: [], sites: [] };
  }
}
 
// 获取步行规划路线
export const getWalkingRoute = (origin: string, destination: string): Promise<{ points: any[], duration: number, distance: number }> => {
  return new Promise((resolve, reject) => {
    const myAmapFun = new amapFile.AMapWX({ key: AMAP_KEY });
    myAmapFun.getWalkingRoute({
      origin: origin,
      destination: destination,
      success: (data) => {
        const points = parseRouteToPoints(data);
        let duration = 0;
        let distance = 0;
        if (data.paths && data.paths[0]) {
          duration = Number(data.paths[0].duration);
          distance = Number(data.paths[0].distance);
        }
        resolve({ points, duration, distance });
      },
      fail: (info) => {
        reject(info);
      }
    });
  });
};
 
// 获取城市中心经纬度
export const getCityLocation = (cityName: string): Promise<{ latitude: number, longitude: number }> => {
  return new Promise((resolve, reject) => {
    const myAmapFun = new amapFile.AMapWX({ key: AMAP_KEY });
    myAmapFun.getInputtips({
      keywords: cityName,
      city: cityName,
      success: (data) => {
        if (data && data.tips && data.tips.length > 0) {
           // 优先取第一个有坐标的结果
           const tip = data.tips.find(t => t.location && t.location.length > 0);
           if (tip) {
             const locationArr = tip.location.split(',');
             if (locationArr.length === 2) {
                resolve({
                  longitude: Number(locationArr[0]),
                  latitude: Number(locationArr[1])
                });
                return;
             }
           }
           reject('No valid location found');
        } else {
          reject('No data found');
        }
      },
      fail: (info) => {
        reject(info);
      }
    });
  });
};
 
// 获取包含 ID 的城市信息
export const getCityInfoWithId = async (cityCode: string, cityName: string) => {
  try {
    const res: any = await getOpenCity();
 
    let cityId = 0;
    if (Array.isArray(res.cities)) {
      const target = res.cities.find(item => item.adcode === cityCode);
      if (target) {
        cityId = target.id;
      }
    }
    return {
      name: cityName,
      code: cityCode,
      id: cityId
    };
  } catch (error) {
    console.error('getOpenCity error', error);
    return {
      name: cityName,
      code: cityCode,
      id: 0
    };
  }
};
 
// 格式化秒数为时钟时间(分钟:秒)
export const formatDurationToClock = (sec: number) => {
  const safeSeconds = Math.max(0, sec);
  const m = Math.floor(safeSeconds / 60);
  const s = safeSeconds % 60;
  return `${String(m).padStart(2, '0')}:${String(s).padStart(2, '0')}`;
};
 
// 格式化日期时间
export const formatDateTime = (date: Date) => {
  const y = date.getFullYear();
  const mo = String(date.getMonth() + 1).padStart(2, '0');
  const d = String(date.getDate()).padStart(2, '0');
  const h = String(date.getHours()).padStart(2, '0');
  const mi = String(date.getMinutes()).padStart(2, '0');
  const s = String(date.getSeconds()).padStart(2, '0');
  return `${y}-${mo}-${d} ${h}:${mi}:${s}`;
};
 
// 清除订单行程存储
export const clearOrderTripStorage = () => {
  Taro.removeStorageSync('orderId');
  Taro.removeStorageSync('tripStatus');
};
 
/**
 * 通过 IP 获取当前位置信息(用于定位授权失败时的兜底)
 */
export const getLocationByIP = (): Promise<any> => {
  return new Promise(async (resolve, reject) => {
    try {
      // 1. 获取当前 network IP
      // 微信小程序没有直接获取公网 IP 的接口,通常由后端从请求头中获取,或者通过第三方接口获取
      // 这里模拟调用服务端提供的 IP 定位接口,假设接口会自动识别请求来源 IP
      const res: any = await request({
        url: _ROBOTAXI_HOST + '/map/base/v1/mini-program/point/getPointByIp',
        method: 'GET',
      });
 
      if (res && res.point && typeof res.point.latitude === 'number' && typeof res.point.longitude === 'number') {
        resolve({
          longitude: res.point.longitude,
          latitude: res.point.latitude,
          city: res.city || '',
          adcode: res.adcode || ''
        });
      } else {
        // 如果后端接口不可用或未返回数据,尝试使用第三方接口作为备选获取 IP
        Taro.request({
          url: 'https://api.ipify.org?format=json',
          success: (ipRes) => {
            const ip = ipRes.data?.ip;
            if (ip) {
              // 拿着获取到的 IP 去请求服务端接口
              request({
                url: _ROBOTAXI_HOST + '/map/base/v1/mini-program/point/getPointByIp',
                method: 'POST',
                payload: { ip }
              }).then((businessRes: any) => {
                if (businessRes && businessRes.point && typeof businessRes.point.latitude === 'number' && typeof businessRes.point.longitude === 'number') {
                  resolve({
                    longitude: businessRes.point.longitude,
                    latitude: businessRes.point.latitude,
                    city: businessRes.city || '',
                    adcode: businessRes.adcode || ''
                  });
                } else {
                  reject('Business IP location failed');
                }
              }).catch(reject);
            } else {
              reject('Failed to get network IP');
            }
          },
          fail: reject
        });
      }
    } catch (error) {
      reject(error);
    }
  });
};