global.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import store from '@/store/index'
  2. export default {
  3. _caller: null, // 调用方指针
  4. mapOptions: null,
  5. // holdPlayersData: [{ // 暂存玩家数据 用于reload页面后重新恢复数据
  6. // id: 0, // 玩家ID
  7. // trailData: [], // 玩家轨迹信息(数据会追加并定时清理过期数据)
  8. // }],
  9. // *** 非指针复制 ***
  10. storePlayers: [{ // 玩家信息 生存周期为当前页面的生存周期,页面重载或离开后数据会销毁
  11. id: 0, // 玩家ID
  12. marker: null, // 玩家标识
  13. trail: null, // 玩家轨迹
  14. trailData: [], // 玩家轨迹信息(数据会追加并定时清理过期数据)
  15. interval_creatCircleMarker: null,
  16. interval_showTrail: null,
  17. }],
  18. map: null,
  19. map_layer: null,
  20. mapUrl: null,
  21. // centPoint: null,
  22. preloadBounds: null,
  23. preloadTileMap: [],
  24. players: [], // 玩家信息(指针复制 每次grpc获取数据后之前旧数据会丢弃)
  25. players_position: [], // 玩家当前位置信息(指针复制 每次grpc获取数据后之前旧数据会丢弃)
  26. focusPlayerId: 0, // 当前选中的玩家ID
  27. checkPoints: [], // 检查点列表(指针复制 每次grpc获取数据后之前旧数据会丢弃)
  28. routes: [], // 路线列表(指针复制 每次grpc获取数据后之前旧数据会丢弃)
  29. init() {
  30. this._caller = null
  31. this.mapOptions = null
  32. this.map = null
  33. this.map_layer = null
  34. this.preloadBounds = null
  35. this.focusPlayerId = 0
  36. if (this.storePlayers != null)
  37. this.storePlayers.length = 0 // 清空数组
  38. if (this.preloadTileMap != null)
  39. this.preloadTileMap.length = 0 // 清空数组
  40. if (this.players != null)
  41. this.players.length = 0 // 清空数组
  42. if (this.players_position != null)
  43. this.players_position.length = 0 // 清空数组
  44. if (this.checkPoints != null)
  45. this.checkPoints.length = 0 // 清空数组
  46. if (this.routes != null)
  47. this.routes.length = 0 // 清空数组
  48. },
  49. setCaller(data) {
  50. this._caller = data // 指针复制,是同一个对象
  51. },
  52. setMapUrl(data) {
  53. this.mapUrl = data
  54. },
  55. // setCentPoint(data) {
  56. // this.centPoint = data
  57. // },
  58. setPreloadBounds(data) {
  59. this.preloadBounds = data
  60. },
  61. setPreloadTileMap(data) {
  62. this.preloadTileMap.push(data)
  63. },
  64. setPlayers(data) {
  65. this.players = data // 指针复制,是同一个对象
  66. },
  67. setPlayersPosition(data) {
  68. this.players_position = data // 指针复制,是同一个对象
  69. },
  70. // *** 非指针复制 ***
  71. // storePlayersTrail(data) {
  72. // this.storePlayers.trail.push(data)
  73. // },
  74. setCheckPoints(data) {
  75. this.checkPoints = data // 指针复制,是同一个对象
  76. },
  77. setRoutes(data) {
  78. this.routes = data // 指针复制,是同一个对象
  79. },
  80. getCaller() {
  81. return this._caller
  82. },
  83. getMapUrl() {
  84. return this.mapUrl
  85. },
  86. // getCentPoint() {
  87. // return this.centPoint
  88. // },
  89. getPreloadBounds() {
  90. return this.preloadBounds
  91. },
  92. getPreloadTileMapByZoom(zoom) {
  93. if (!(zoom > 0)) return
  94. var preload = null
  95. if (this.preloadTileMap != null && this.preloadTileMap.length > 0) {
  96. preload = this.preloadTileMap.find(o => o.zoom === zoom)
  97. }
  98. // console.log("[getPreloadTileMapByZoom]", zoom, preload)
  99. return preload
  100. },
  101. getPlayerById(playerId) {
  102. if (!(playerId > 0)) return
  103. var player = null
  104. if (this.players != null && this.players.length > 0) {
  105. player = this.players.find(o => o.id === playerId)
  106. }
  107. // console.log("[getPlayerById]", playerId, player)
  108. return player
  109. },
  110. getPlayerPositionById(playerId) {
  111. if (!(playerId > 0)) return
  112. var player_position = null
  113. if (this.players_position != null && this.players_position.length > 0) {
  114. player_position = this.players_position.find(o => o.id === playerId)
  115. }
  116. // console.log("[getPlayerPositionById]", playerId, player_position)
  117. return player_position
  118. },
  119. getStorePlayersById(playerId) {
  120. if (!(playerId > 0)) return
  121. var storePlayer = null
  122. if (this.storePlayers != null && this.storePlayers.length > 0) {
  123. storePlayer = this.storePlayers.find(o => o.id === playerId)
  124. }
  125. // console.log("[getStorePlayersById]", playerId, storePlayer)
  126. return storePlayer
  127. },
  128. getCheckPointById(cpId) {
  129. if (!(cpId > 0)) return
  130. var checkPoint = null
  131. if (this.checkPoints != null && this.checkPoints.length > 0) {
  132. checkPoint = this.checkPoints.find(o => o.cp_id === cpId)
  133. }
  134. // console.log("[getCheckPointById]", cpId, checkPoint)
  135. return checkPoint
  136. },
  137. getRouteById(rtId) {
  138. if (!(rtId > 0)) return
  139. var route = null
  140. if (this.routes != null && this.routes.length > 0) {
  141. route = this.routes.find(o => o.id === rtId)
  142. }
  143. // console.log("[getCheckPointById]", rtId, route)
  144. return route
  145. },
  146. // 处理过期数据
  147. dealStaleData() {
  148. for (let i = 0; i < this.storePlayers.length; i++) {
  149. var storePlayer = this.storePlayers[i]
  150. if (storePlayer == null || !(storePlayer.id > 0)) {
  151. continue
  152. }
  153. // 查找最新的玩家记录列表
  154. var player = this.getPlayerById(storePlayer.id)
  155. if (player == null) { // 未找到 说明记录已过期
  156. // console.warn('[dealStaleData] 发现过期数据', storePlayer)
  157. if (storePlayer.marker != null) {
  158. storePlayer.marker.removeFrom(storePlayer.markerLayerGroup)
  159. storePlayer.marker = null
  160. }
  161. if (storePlayer.trail != null) {
  162. storePlayer.trail.removeFrom(storePlayer.trailLayerGroup)
  163. storePlayer.trail2.removeFrom(storePlayer.trailLayerGroup)
  164. storePlayer.trail = null
  165. storePlayer.trail2 = null
  166. storePlayer.trailData = []
  167. }
  168. if (storePlayer.interval_creatCircleMarker != null) {
  169. clearInterval(storePlayer.interval_creatCircleMarker)
  170. }
  171. if (storePlayer.interval_creatCircleMarker != null) {
  172. clearInterval(storePlayer.interval_showTrail)
  173. }
  174. // delete删除元素之后数组长度不变,只是被删除元素被置为empty
  175. // delete this.storePlayers[i]
  176. }
  177. }
  178. },
  179. // 页面reLoad前调用本方法,将用户数据先保存到Vuex,页面重载后再进行数据恢复
  180. savePlayersData() {
  181. // 先清空旧缓存
  182. store.commit('clearHoldPlayersData')
  183. store.commit('setHoldTime', new Date())
  184. for (let i = 0; i < this.storePlayers.length; i++) {
  185. var storePlayer = this.storePlayers[i]
  186. if (storePlayer == null || !(storePlayer.id > 0)) {
  187. continue
  188. }
  189. // 查找最新的玩家记录列表
  190. var player = this.getPlayerById(storePlayer.id)
  191. if (player != null) { // 找到记录 进行保存
  192. console.warn('[storePlayersData] 发现待保存数据', storePlayer)
  193. var storeData = {
  194. id: storePlayer.id, // 玩家ID
  195. trailData: storePlayer.trailData, // 玩家轨迹信息(数据会追加并定时清理过期数据)
  196. }
  197. store.commit('pushHoldPlayersData', storeData)
  198. }
  199. }
  200. },
  201. // 获取Vuex中暂存的用户数据,用于页面重载后的数据恢复
  202. fetchPlayersDataById(playerId) {
  203. if (!(playerId > 0)) return
  204. var holdTime = store.state.holdTime
  205. if (holdTime != null) {
  206. var now = new Date()
  207. var expireTime = 3000 // 过期时间 毫秒
  208. if ((now - holdTime) >= expireTime) { // 暂存的记录已过期,需清理
  209. store.commit('clearHoldPlayersData')
  210. store.commit('setHoldTime', null)
  211. console.warn('[fetchPlayersDataById] 暂存的记录已过期,清理完毕')
  212. return
  213. }
  214. }
  215. var holdPlayersData = store.state.holdPlayersData
  216. // console.warn("[fetchPlayersDataById] holdPlayersData", holdPlayersData)
  217. var playerData = null
  218. if (holdPlayersData != null && holdPlayersData.length > 0) {
  219. playerData = holdPlayersData.find(o => o.id === playerId)
  220. }
  221. // console.log("[fetchPlayersDataById]", playerId, playerData)
  222. return playerData
  223. },
  224. }