import store from '@/store/index' export default { _caller: null, // 调用方指针 mapOptions: null, // holdPlayersData: [{ // 暂存玩家数据 用于reload页面后重新恢复数据 // id: 0, // 玩家ID // trailData: [], // 玩家轨迹信息(数据会追加并定时清理过期数据) // }], // *** 非指针复制 *** storePlayers: [{ // 玩家信息 生存周期为当前页面的生存周期,页面重载或离开后数据会销毁 id: 0, // 玩家ID marker: null, // 玩家标识 trail: null, // 玩家轨迹 trailData: [], // 玩家轨迹信息(数据会追加并定时清理过期数据) interval_creatCircleMarker: null, interval_showTrail: null, }], map: null, map_layer: null, mapUrl: null, // centPoint: null, preloadBounds: null, preloadTileMap: [], players: [], // 玩家信息(指针复制 每次grpc获取数据后之前旧数据会丢弃) players_position: [], // 玩家当前位置信息(指针复制 每次grpc获取数据后之前旧数据会丢弃) focusPlayerId: 0, // 当前选中的玩家ID checkPoints: [], // 检查点列表(指针复制 每次grpc获取数据后之前旧数据会丢弃) routes: [], // 路线列表(指针复制 每次grpc获取数据后之前旧数据会丢弃) init() { this._caller = null this.mapOptions = null this.map = null this.map_layer = null this.preloadBounds = null this.focusPlayerId = 0 if (this.storePlayers != null) this.storePlayers.length = 0 // 清空数组 if (this.preloadTileMap != null) this.preloadTileMap.length = 0 // 清空数组 if (this.players != null) this.players.length = 0 // 清空数组 if (this.players_position != null) this.players_position.length = 0 // 清空数组 if (this.checkPoints != null) this.checkPoints.length = 0 // 清空数组 if (this.routes != null) this.routes.length = 0 // 清空数组 }, setCaller(data) { this._caller = data // 指针复制,是同一个对象 }, setMapUrl(data) { this.mapUrl = data }, // setCentPoint(data) { // this.centPoint = data // }, setPreloadBounds(data) { this.preloadBounds = data }, setPreloadTileMap(data) { this.preloadTileMap.push(data) }, setPlayers(data) { this.players = data // 指针复制,是同一个对象 }, setPlayersPosition(data) { this.players_position = data // 指针复制,是同一个对象 }, // *** 非指针复制 *** // storePlayersTrail(data) { // this.storePlayers.trail.push(data) // }, setCheckPoints(data) { this.checkPoints = data // 指针复制,是同一个对象 }, setRoutes(data) { this.routes = data // 指针复制,是同一个对象 }, getCaller() { return this._caller }, getMapUrl() { return this.mapUrl }, // getCentPoint() { // return this.centPoint // }, getPreloadBounds() { return this.preloadBounds }, getPreloadTileMapByZoom(zoom) { if (!(zoom > 0)) return var preload = null if (this.preloadTileMap != null && this.preloadTileMap.length > 0) { preload = this.preloadTileMap.find(o => o.zoom === zoom) } // console.log("[getPreloadTileMapByZoom]", zoom, preload) return preload }, getPlayerById(playerId) { if (!(playerId > 0)) return var player = null if (this.players != null && this.players.length > 0) { player = this.players.find(o => o.id === playerId) } // console.log("[getPlayerById]", playerId, player) return player }, getPlayerPositionById(playerId) { if (!(playerId > 0)) return var player_position = null if (this.players_position != null && this.players_position.length > 0) { player_position = this.players_position.find(o => o.id === playerId) } // console.log("[getPlayerPositionById]", playerId, player_position) return player_position }, getStorePlayersById(playerId) { if (!(playerId > 0)) return var storePlayer = null if (this.storePlayers != null && this.storePlayers.length > 0) { storePlayer = this.storePlayers.find(o => o.id === playerId) } // console.log("[getStorePlayersById]", playerId, storePlayer) return storePlayer }, getCheckPointById(cpId) { if (!(cpId > 0)) return var checkPoint = null if (this.checkPoints != null && this.checkPoints.length > 0) { checkPoint = this.checkPoints.find(o => o.cp_id === cpId) } // console.log("[getCheckPointById]", cpId, checkPoint) return checkPoint }, getRouteById(rtId) { if (!(rtId > 0)) return var route = null if (this.routes != null && this.routes.length > 0) { route = this.routes.find(o => o.id === rtId) } // console.log("[getCheckPointById]", rtId, route) return route }, // 处理过期数据 dealStaleData() { for (let i = 0; i < this.storePlayers.length; i++) { var storePlayer = this.storePlayers[i] if (storePlayer == null || !(storePlayer.id > 0)) { continue } // 查找最新的玩家记录列表 var player = this.getPlayerById(storePlayer.id) if (player == null) { // 未找到 说明记录已过期 // console.warn('[dealStaleData] 发现过期数据', storePlayer) if (storePlayer.marker != null) { storePlayer.marker.removeFrom(storePlayer.markerLayerGroup) storePlayer.marker = null } if (storePlayer.trail != null) { storePlayer.trail.removeFrom(storePlayer.trailLayerGroup) storePlayer.trail2.removeFrom(storePlayer.trailLayerGroup) storePlayer.trail = null storePlayer.trail2 = null storePlayer.trailData = [] } if (storePlayer.interval_creatCircleMarker != null) { clearInterval(storePlayer.interval_creatCircleMarker) } if (storePlayer.interval_creatCircleMarker != null) { clearInterval(storePlayer.interval_showTrail) } // delete删除元素之后数组长度不变,只是被删除元素被置为empty // delete this.storePlayers[i] } } }, // 页面reLoad前调用本方法,将用户数据先保存到Vuex,页面重载后再进行数据恢复 savePlayersData() { // 先清空旧缓存 store.commit('clearHoldPlayersData') store.commit('setHoldTime', new Date()) for (let i = 0; i < this.storePlayers.length; i++) { var storePlayer = this.storePlayers[i] if (storePlayer == null || !(storePlayer.id > 0)) { continue } // 查找最新的玩家记录列表 var player = this.getPlayerById(storePlayer.id) if (player != null) { // 找到记录 进行保存 console.warn('[storePlayersData] 发现待保存数据', storePlayer) var storeData = { id: storePlayer.id, // 玩家ID trailData: storePlayer.trailData, // 玩家轨迹信息(数据会追加并定时清理过期数据) } store.commit('pushHoldPlayersData', storeData) } } }, // 获取Vuex中暂存的用户数据,用于页面重载后的数据恢复 fetchPlayersDataById(playerId) { if (!(playerId > 0)) return var holdTime = store.state.holdTime if (holdTime != null) { var now = new Date() var expireTime = 3000 // 过期时间 毫秒 if ((now - holdTime) >= expireTime) { // 暂存的记录已过期,需清理 store.commit('clearHoldPlayersData') store.commit('setHoldTime', null) console.warn('[fetchPlayersDataById] 暂存的记录已过期,清理完毕') return } } var holdPlayersData = store.state.holdPlayersData // console.warn("[fetchPlayersDataById] holdPlayersData", holdPlayersData) var playerData = null if (holdPlayersData != null && holdPlayersData.length > 0) { playerData = holdPlayersData.find(o => o.id === playerId) } // console.log("[fetchPlayersDataById]", playerId, playerData) return playerData }, }