吉安感知网项目-前端
罗广辉
2026-01-26 beb95fb5fc166804056abafd70fc01ac27de7621
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
/**
 * 位置管理工具类
 * 用于处理位置权限请求和实时位置获取
 */
import permission from './voiceCallByTX/TrtcCloud/permission.js'
 
// 平台判断
const isIos = () => {
  // #ifdef APP-PLUS
  return plus.os.name === "iOS";
  // #endif
  return false;
};
 
// 位置更新回调函数
let locationUpdateCallback = null;
// 位置监听器ID
let locationWatcherId = null;
 
/**
 * 请求位置权限
 * @returns {Promise<boolean>} 是否获取到权限
 */
export const requestLocationPermission = async () => {
  return new Promise(async (resolve) => {
    // #ifdef APP-PLUS
    if (isIos()) {
      // iOS平台
      const hasPermission = permission.judgeIosPermission('location');
      if (hasPermission) {
        resolve(true);
      } else {
        // iOS需要提示用户手动开启权限
        uni.showModal({
          title: '提示',
          content: '应用需要获取位置信息权限,请在设置中开启',
          confirmText: '去设置',
          cancelText: '取消',
          success: (res) => {
            if (res.confirm) {
              permission.gotoAppPermissionSetting();
              resolve(false);
            } else {
              resolve(false);
            }
          }
        });
      }
    } else {
      // Android平台
      const result = await permission.requestAndroidPermission('android.permission.ACCESS_FINE_LOCATION');
      if (result === 1) {
        resolve(true);
      } else if (result === 0) {
        // 拒绝本次申请
        resolve(false);
      } else if (result === -1) {
        // 永久拒绝
        uni.showModal({
          title: '提示',
          content: '应用需要获取位置信息权限,请在设置中开启',
          confirmText: '去设置',
          cancelText: '取消',
          success: (res) => {
            if (res.confirm) {
              permission.gotoAppPermissionSetting();
              resolve(false);
            } else {
              resolve(false);
            }
          }
        });
      }
    }
    // #endif
    // #ifdef H5
    // H5平台使用浏览器原生API请求权限
    if (navigator.geolocation) {
      navigator.permissions.query({ name: 'geolocation' }).then((result) => {
        if (result.state === 'granted') {
          resolve(true);
        } else if (result.state === 'prompt') {
          // 还未请求过权限,会在getLocation时提示
          resolve(true);
        } else {
          resolve(false);
        }
      }).catch(() => {
        resolve(false);
      });
    } else {
      resolve(false);
    }
    // #endif
    // #ifdef MP
    // 小程序平台
    uni.getSetting({
      success: (res) => {
        if (res.authSetting['scope.userLocation']) {
          resolve(true);
        } else {
          uni.authorize({
            scope: 'scope.userLocation',
            success: () => {
              resolve(true);
            },
            fail: () => {
              uni.showModal({
                title: '提示',
                content: '应用需要获取位置信息权限,请在设置中开启',
                confirmText: '去设置',
                cancelText: '取消',
                success: (res) => {
                  if (res.confirm) {
                    uni.openSetting();
                    resolve(false);
                  } else {
                    resolve(false);
                  }
                }
              });
            }
          });
        }
      }
    });
    // #endif
  });
};
 
/**
 * 获取当前位置信息
 * @returns {Promise<Object>} 位置信息对象
 */
export const getCurrentLocation = () => {
  return new Promise((resolve, reject) => {
    uni.getLocation({
      type: 'gcj02',
      altitude: true,
      success: (res) => {
        resolve({
          latitude: res.latitude,
          longitude: res.longitude,
          altitude: res.altitude,
          accuracy: res.accuracy,
          speed: res.speed,
          verticalAccuracy: res.verticalAccuracy,
          horizontalAccuracy: res.horizontalAccuracy
        });
      },
      fail: (err) => {
        console.error('获取位置失败:', err);
        reject(err);
      }
    });
  });
};
 
/**
 * 开始实时位置监听
 * @param {Function} callback 位置更新回调函数
 * @param {Object} options 监听选项
 * @returns {number} 监听器ID
 */
export const startLocationWatcher = (callback, options = {}) => {
  if (locationWatcherId) {
    stopLocationWatcher();
  }
  
  locationUpdateCallback = callback;
  
  locationWatcherId = uni.startLocationUpdate({
    success: () => {
      console.log('开始位置监听成功');
    },
    fail: (err) => {
      console.error('开始位置监听失败:', err);
      // 如果是因为权限问题,重新请求权限
      if (err.errCode === 12002) {
        requestLocationPermission();
      }
    }
  });
  
  // 监听位置更新
  uni.onLocationChange((res) => {
    const locationInfo = {
      latitude: res.latitude,
      longitude: res.longitude,
      altitude: res.altitude,
      accuracy: res.accuracy,
      speed: res.speed,
      verticalAccuracy: res.verticalAccuracy,
      horizontalAccuracy: res.horizontalAccuracy,
      timestamp: res.timestamp
    };
    
    if (locationUpdateCallback) {
      locationUpdateCallback(locationInfo);
    }
  });
  
  return locationWatcherId;
};
 
/**
 * 停止位置监听
 */
export const stopLocationWatcher = () => {
  if (locationWatcherId) {
    uni.stopLocationUpdate({
      success: () => {
        console.log('停止位置监听成功');
      },
      fail: (err) => {
        console.error('停止位置监听失败:', err);
      }
    });
    locationWatcherId = null;
  }
  
  // 移除位置更新监听
  uni.offLocationChange();
  locationUpdateCallback = null;
};
 
/**
 * 检查系统定位服务是否开启
 * @returns {Promise<boolean>} 是否开启
 */
export const checkSystemLocationEnabled = async () => {
  return new Promise((resolve) => {
    // #ifdef APP-PLUS
    const enabled = permission.checkSystemEnableLocation();
    resolve(enabled);
    // #endif
    // #ifdef H5
    resolve(navigator.geolocation ? true : false);
    // #endif
    // #ifdef MP
    uni.getSystemInfo({
      success: (res) => {
        // 小程序无法直接检查系统定位服务是否开启
        resolve(true);
      },
      fail: () => {
        resolve(false);
      }
    });
    // #endif
  });
};
 
export default {
  requestLocationPermission,
  getCurrentLocation,
  startLocationWatcher,
  stopLocationWatcher,
  checkSystemLocationEnabled
};