index.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. import browser from "@/utils/browser"
  4. import { deepClone } from "@/utils/other.js";
  5. Vue.use(Vuex)
  6. const store = new Vuex.Store({
  7. state: {
  8. userAvatar: '',
  9. userNickName: '',
  10. info: '',
  11. backupInfo: '',
  12. showInfo: '',
  13. hotspot: '',
  14. backupHotSpot: '',
  15. initScene: '',
  16. tablist: [],
  17. temptablist: [],
  18. vrlist: [],
  19. allVrlist: [],
  20. activeItem: '',
  21. isEditing: false,
  22. isShow: false,
  23. uploadStatusListAudio: [],
  24. uploadStatusListImage: [],
  25. uploadStatusListPano: [],
  26. uploadStatusListVideo: [],
  27. // 编辑器-导航-场景导航中,拖拽时要用到
  28. editorNavDragInfo: {
  29. type: '', // 'topologyGroupLevel1': 拓扑结构中一级分组;'topologyGroupLevel2': 拓扑结构中二级分组;'scene': 场景(全景图和三维场景)
  30. node: {},
  31. }
  32. },
  33. getters: {
  34. userAvatar: state => state.userAvatar,
  35. userNickName: state => state.userNickName,
  36. isEditing: state => state.isEditing,
  37. info: state => state.info,
  38. catalogTopology: (state) => {
  39. if (!state.info || !state.info.catalogRoot || !state.info.catalogs) {
  40. return
  41. }
  42. //四层:root,level1(一级分类),level2(二级分类或直属于一级分类的场景), level3(场景)
  43. let root = deepClone(state.info.catalogRoot)
  44. // 对于每个一级分类
  45. for (const itemLevel1 of root) {
  46. // 指定每个一级分类的下级
  47. itemLevel1.childrenTemp = []
  48. for (const itemLevel2Id of itemLevel1.children) {
  49. for (const catalogsItem of state.info.catalogs) {
  50. if (itemLevel2Id === catalogsItem.id) {
  51. const itemLevel2 = deepClone(catalogsItem)
  52. itemLevel2.parentId = itemLevel1.id // 看起来,vuex getter中的数据如果存在循环引用,在通过mutation转而再存储到vuex中时,会导致调用栈溢出,原因难道是vuex mutation在深拷贝时没有考虑循环引用的情况?所以这里不进行循环引用,只记录parent的id。
  53. itemLevel2.children = []
  54. itemLevel1.childrenTemp.push(itemLevel2)
  55. // 对于每个三级元素
  56. for (const sceneItem of state.info.scenes) {
  57. // 如果属于上述二级分类
  58. if (itemLevel2.id === sceneItem.category /* 注意拼写!!! */) {
  59. const itemLevel3 = deepClone(sceneItem)
  60. itemLevel3.parentId = itemLevel2.id // 看起来,vuex getter中的数据如果存在循环引用,在通过mutation转而再存储到vuex中时,会导致调用栈溢出,原因难道是vuex mutation在深拷贝时没有考虑循环引用的情况?所以这里不进行循环引用,只记录parent的id。
  61. itemLevel2.children.push(itemLevel3)
  62. }
  63. }
  64. // 按weight排序
  65. itemLevel2.children.sort((a, b) => {
  66. if (a.weight === undefined && b.weight === undefined) {
  67. return 0
  68. } else if (a.weight !== undefined && b.weight === undefined) {
  69. return -1
  70. } else if (a.weight === undefined && b.weight !== undefined) {
  71. return 1
  72. } else {
  73. return a.weight - b.weight
  74. }
  75. })
  76. break
  77. }
  78. }
  79. }
  80. itemLevel1.children = itemLevel1.childrenTemp
  81. delete itemLevel1.childrenTemp
  82. }
  83. return root
  84. },
  85. showInfo: state => state.showInfo,
  86. backupInfo: state => state.backupInfo,
  87. hotspot: state => state.hotspot,
  88. backupHotSpot: state => state.backupHotSpot,
  89. initScene: state => state.initScene,
  90. activeItem: state => state.activeItem,
  91. vrlist: state => state.vrlist,
  92. tablist: state => state.tablist,
  93. temptablist: state => state.temptablist,
  94. isShow: state => state.isShow,
  95. allVrlist: state => state.allVrlist,
  96. uploadStatusListAudio: state => state.uploadStatusListAudio,
  97. uploadStatusListImage: state => state.uploadStatusListImage,
  98. uploadStatusListPano: state => state.uploadStatusListPano,
  99. uploadStatusListVideo: state => state.uploadStatusListVideo,
  100. editorNavDragInfo: state => state.editorNavDragInfo,
  101. },
  102. mutations: {
  103. SetUserAvatar(state, avatar) {
  104. if (typeof avatar === 'string') {
  105. state.userAvatar = avatar
  106. }
  107. },
  108. SetUserNickName(state, nickName) {
  109. if (typeof nickName === 'string') {
  110. state.userNickName = nickName
  111. }
  112. },
  113. SetTabList(state, list) {
  114. state.tablist = list
  115. },
  116. SetTempTabList(state, list) {
  117. state.temptablist = list
  118. },
  119. SetVrList(state, list) {
  120. state.vrlist = list
  121. },
  122. UpdateIsShowState(state, isShow) {
  123. state.isShow = isShow
  124. },
  125. UpdateIsEditingState(state, isShow) {
  126. state.isEditing = isShow
  127. },
  128. SetAllVrlist(state, list) {
  129. state.allVrlist = list
  130. },
  131. SetInitScene(state, scene) {
  132. state.initScene = scene
  133. },
  134. SetActiveItem(state, scene) {
  135. state.activeItem = scene
  136. },
  137. SetShowInfo(state, data) {
  138. state.showInfo = data
  139. },
  140. SetInfo(state, data) {
  141. state.info = data
  142. this.commit("BackupInfo", browser.CloneObject(data))
  143. },
  144. BackupInfo(state, data) {
  145. state.backupInfo = data
  146. },
  147. SetHotspot(state, data) {
  148. state.hotspot = data
  149. this.commit("BackupHotSpot", browser.CloneObject(data))
  150. },
  151. BackupHotSpot(state, data) {
  152. state.backupHotSpot = data
  153. },
  154. setUploadStatusListImage(state, data) {
  155. state.uploadStatusListImage = data
  156. },
  157. setUploadStatusListAudio(state, data) {
  158. state.uploadStatusListAudio = data
  159. },
  160. setUploadStatusListVideo(state, data) {
  161. state.uploadStatusListVideo = data
  162. },
  163. setEditorNavDragType(state, data) {
  164. if (!['topologyGroupLevel1', 'topologyGroupLevel2', 'scene'].includes(data)) {
  165. throw("拖拽类型必须是'topologyGroupLevel1', 'topologyGroupLevel2', 'scene'之一!")
  166. }
  167. state.editorNavDragInfo.type = data
  168. },
  169. setEditorNavDragNode(state, data) {
  170. state.editorNavDragInfo.node = data
  171. },
  172. clearEditorNavDragInfo(state) {
  173. state.editorNavDragInfo.type = ''
  174. state.editorNavDragInfo.node = {}
  175. }
  176. },
  177. actions: {
  178. refreshUserInfo(context) {
  179. try {
  180. const userInfo = JSON.parse(localStorage.getItem('info'))
  181. context.commit('SetUserAvatar', userInfo.head)
  182. context.commit('SetUserNickName', userInfo.nickName)
  183. } catch (error) {
  184. console.log('从storage没有读取到有意义的info,store里用户信息置空。')
  185. context.commit('SetUserAvatar', '')
  186. context.commit('SetUserNickName', '')
  187. }
  188. },
  189. },
  190. modules: {
  191. }
  192. })
  193. export default store