global.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. // console.log("[setPlayersPosition] data", data)
  69. this.players_position = data // 指针复制,是同一个对象
  70. },
  71. // *** 非指针复制 ***
  72. // storePlayersTrail(data) {
  73. // this.storePlayers.trail.push(data)
  74. // },
  75. setCheckPoints(data) {
  76. this.checkPoints = data // 指针复制,是同一个对象
  77. },
  78. setRoutes(data) {
  79. this.routes = data // 指针复制,是同一个对象
  80. },
  81. getCaller() {
  82. return this._caller
  83. },
  84. getMapUrl() {
  85. return this.mapUrl
  86. },
  87. // getCentPoint() {
  88. // return this.centPoint
  89. // },
  90. getPreloadBounds() {
  91. return this.preloadBounds
  92. },
  93. getPreloadTileMapByZoom(zoom) {
  94. if (!(zoom > 0)) return
  95. var preload = null
  96. if (this.preloadTileMap != null && this.preloadTileMap.length > 0) {
  97. preload = this.preloadTileMap.find(o => o.zoom === zoom)
  98. }
  99. // console.log("[getPreloadTileMapByZoom]", zoom, preload)
  100. return preload
  101. },
  102. getPlayerById(playerId) {
  103. if (!(playerId > 0)) return
  104. var player = null
  105. if (this.players != null && this.players.length > 0) {
  106. player = this.players.find(o => o.id === playerId)
  107. }
  108. // console.log("[getPlayerById]", playerId, player)
  109. return player
  110. },
  111. getPlayerPositionById(playerId) {
  112. if (!(playerId > 0)) return
  113. var player_position = null
  114. if (this.players_position != null && this.players_position.length > 0) {
  115. player_position = this.players_position.find(o => o.id === playerId)
  116. }
  117. // console.log("[getPlayerPositionById]", playerId, player_position)
  118. return player_position
  119. },
  120. getStorePlayersById(playerId) {
  121. if (!(playerId > 0)) return
  122. var storePlayer = null
  123. if (this.storePlayers != null && this.storePlayers.length > 0) {
  124. storePlayer = this.storePlayers.find(o => o.id === playerId)
  125. }
  126. // console.log("[getStorePlayersById]", playerId, storePlayer)
  127. return storePlayer
  128. },
  129. getCheckPointById(cpId) {
  130. if (!(cpId > 0)) return
  131. var checkPoint = null
  132. if (this.checkPoints != null && this.checkPoints.length > 0) {
  133. checkPoint = this.checkPoints.find(o => o.cp_id === cpId)
  134. }
  135. // console.log("[getCheckPointById]", cpId, checkPoint)
  136. return checkPoint
  137. },
  138. getRouteById(rtId) {
  139. if (!(rtId > 0)) return
  140. var route = null
  141. if (this.routes != null && this.routes.length > 0) {
  142. route = this.routes.find(o => o.id === rtId)
  143. }
  144. // console.log("[getCheckPointById]", rtId, route)
  145. return route
  146. },
  147. // 处理过期数据
  148. dealStaleData() {
  149. for (let i = 0; i < this.storePlayers.length; i++) {
  150. var storePlayer = this.storePlayers[i]
  151. if (storePlayer == null || !(storePlayer.id > 0)) {
  152. continue
  153. }
  154. // 查找最新的玩家记录列表
  155. var player = this.getPlayerById(storePlayer.id)
  156. if (player == null) { // 未找到 说明记录已过期
  157. // console.warn('[dealStaleData] 发现过期数据', storePlayer)
  158. if (storePlayer.marker != null) {
  159. storePlayer.marker.removeFrom(storePlayer.markerLayerGroup)
  160. storePlayer.marker = null
  161. }
  162. if (storePlayer.trail != null) {
  163. storePlayer.trail.removeFrom(storePlayer.trailLayerGroup)
  164. storePlayer.trail2.removeFrom(storePlayer.trailLayerGroup)
  165. storePlayer.trail = null
  166. storePlayer.trail2 = null
  167. storePlayer.trailData = []
  168. }
  169. if (storePlayer.interval_creatCircleMarker != null) {
  170. clearInterval(storePlayer.interval_creatCircleMarker)
  171. }
  172. if (storePlayer.interval_creatCircleMarker != null) {
  173. clearInterval(storePlayer.interval_showTrail)
  174. }
  175. // delete删除元素之后数组长度不变,只是被删除元素被置为empty
  176. // delete this.storePlayers[i]
  177. }
  178. }
  179. },
  180. // 页面reLoad前调用本方法,将用户数据先保存到Vuex,页面重载后再进行数据恢复
  181. savePlayersData() {
  182. // 先清空旧缓存
  183. store.commit('clearHoldPlayersData')
  184. store.commit('setHoldTime', new Date())
  185. for (let i = 0; i < this.storePlayers.length; i++) {
  186. var storePlayer = this.storePlayers[i]
  187. if (storePlayer == null || !(storePlayer.id > 0)) {
  188. continue
  189. }
  190. // 查找最新的玩家记录列表
  191. var player = this.getPlayerById(storePlayer.id)
  192. if (player != null) { // 找到记录 进行保存
  193. console.warn('[storePlayersData] 发现待保存数据', storePlayer)
  194. var storeData = {
  195. id: storePlayer.id, // 玩家ID
  196. trailData: storePlayer.trailData, // 玩家轨迹信息(数据会追加并定时清理过期数据)
  197. }
  198. store.commit('pushHoldPlayersData', storeData)
  199. }
  200. }
  201. },
  202. // 获取Vuex中暂存的用户数据,用于页面重载后的数据恢复
  203. fetchPlayersDataById(playerId) {
  204. if (!(playerId > 0)) return
  205. var holdTime = store.state.holdTime
  206. if (holdTime != null) {
  207. var now = new Date()
  208. var expireTime = 3000 // 过期时间 毫秒
  209. if ((now - holdTime) >= expireTime) { // 暂存的记录已过期,需清理
  210. store.commit('clearHoldPlayersData')
  211. store.commit('setHoldTime', null)
  212. console.warn('[fetchPlayersDataById] 暂存的记录已过期,清理完毕')
  213. return
  214. }
  215. }
  216. var holdPlayersData = store.state.holdPlayersData
  217. // console.warn("[fetchPlayersDataById] holdPlayersData", holdPlayersData)
  218. var playerData = null
  219. if (holdPlayersData != null && holdPlayersData.length > 0) {
  220. playerData = holdPlayersData.find(o => o.id === playerId)
  221. }
  222. // console.log("[fetchPlayersDataById]", playerId, playerData)
  223. return playerData
  224. },
  225. }