吉安感知网项目-前端
罗广辉
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
import { defineStore } from 'pinia'
import locationUtil from '@/utils/location.js'
 
const useLocationStore = defineStore('location', {
  state: () => ({
    // 当前位置信息
    currentLocation: null,
    // 位置权限状态
    hasPermission: false,
    // 系统定位服务是否开启
    systemLocationEnabled: true,
    // 位置更新间隔(毫秒)
    updateInterval: 5000,
    // 位置监听器ID
    watcherId: null
  }),
 
  getters: {
    /**
     * 获取当前位置
     * @returns {Object|null} 当前位置信息
     */
    getCurrentLocation: (state) => state.currentLocation,
 
    /**
     * 获取位置权限状态
     * @returns {boolean} 是否有位置权限
     */
    getPermissionStatus: (state) => state.hasPermission,
 
    /**
     * 获取系统定位服务状态
     * @returns {boolean} 系统定位服务是否开启
     */
    getSystemLocationStatus: (state) => state.systemLocationEnabled
  },
 
  actions: {
    /**
     * 初始化位置服务
     */
    async initLocationService() {
      try {
        // 检查系统定位服务是否开启
        this.systemLocationEnabled = await locationUtil.checkSystemLocationEnabled();
        if (!this.systemLocationEnabled) {
          console.warn('系统定位服务未开启');
        }
 
        // 请求位置权限
        this.hasPermission = await locationUtil.requestLocationPermission();
        if (this.hasPermission) {
          // 获取当前位置
          await this.updateCurrentLocation();
          // 开始位置监听
          this.startLocationWatch();
        }
      } catch (error) {
        console.error('初始化位置服务失败:', error);
      }
    },
 
    /**
     * 更新当前位置
     */
    async updateCurrentLocation() {
      try {
        const location = await locationUtil.getCurrentLocation();
        this.currentLocation = location;
        console.log('位置更新:', location);
        return location;
      } catch (error) {
        console.error('更新位置失败:', error);
        return null;
      }
    },
 
    /**
     * 开始位置监听
     */
    startLocationWatch() {
      if (this.watcherId) {
        this.stopLocationWatch();
      }
 
      this.watcherId = locationUtil.startLocationWatcher((location) => {
        this.currentLocation = location;
        // 可以在这里添加位置信息上报逻辑
        this.reportLocation(location);
      }, {
        interval: this.updateInterval
      });
    },
 
    /**
     * 停止位置监听
     */
    stopLocationWatch() {
      if (this.watcherId) {
        locationUtil.stopLocationWatcher();
        this.watcherId = null;
      }
    },
 
    /**
     * 重新请求位置权限
     */
    async reRequestPermission() {
      this.hasPermission = await locationUtil.requestLocationPermission();
      if (this.hasPermission) {
        await this.updateCurrentLocation();
        this.startLocationWatch();
      }
      return this.hasPermission;
    },
 
    /**
     * 位置信息上报(可根据实际需求实现)
     * @param {Object} location 位置信息
     */
    reportLocation(location) {
      // 这里可以添加位置信息上报到服务器的逻辑
      // 例如:api.uploadLocation(location)
      // console.log('位置上报:', location);
    },
 
    /**
     * 清理位置服务
     */
    cleanupLocationService() {
      this.stopLocationWatch();
      this.currentLocation = null;
    }
  }
})
 
export default useLocationStore