layout-store.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. import {
  2. getAllProject
  3. } from "@/api/scan/request";
  4. import {
  5. getUserInfo,getUserInfoByUserId
  6. } from "@/api/system/persagy-sso-server";
  7. import tools from "@/utils/scan/tools"
  8. import storage from '@/framework/utils/storage'
  9. import lStorage from '@/utils/localStorage'
  10. import store from '@/store'
  11. import project from "./project";
  12. const KEY_MENU_SELECTED = 'menu_selected'
  13. const KEY_PROJECT_SELECTED = 'global_project_selected'
  14. const KEY_PAGE_BRANDCRUMB = 'page_brandcrumb'
  15. export default {
  16. namespaced: true,
  17. state: {
  18. sidebarClosed: false,
  19. sidebarSelected: '', // sidebar选中的选项
  20. // userInfo: null, //{ username: 'admin' },
  21. userInfo: {
  22. userName: 'yanruolan',
  23. userId: '06328a53c69a41bb8f5bb1a552c6e8d6'
  24. },
  25. permissions: {
  26. "system:role:delete": true,
  27. "system:role:create": true,
  28. "system:role:query": true,
  29. "system:role:setOpts": true
  30. },
  31. menus: [], //系统菜单列表
  32. projectId: '',
  33. projectName: '',
  34. group_code: '',
  35. // projects: project["14"],
  36. projects: [],
  37. breadcrumb: [],
  38. uploaderList: [], //当前上传文件列表
  39. secret: "", //项目密码
  40. userId: "", //用户id
  41. // userId: "06328a53c69a41bb8f5bb1a552c6e8d6", //用户id
  42. rowEdit: false, //表格数据变化
  43. errorReport: [], //扫楼错误报告
  44. },
  45. getters: {
  46. sidebarClosed: state => state.sidebarClosed,
  47. secret: state => state.secret,
  48. userId: state => state.userId,
  49. sidebarSelected: state => {
  50. // if (!state.pageSidebarSelected) {
  51. // let menu = storage.get(KEY_MENU_SELECTED)
  52. // if (menu) {
  53. // state.pageSidebarSelected = menu
  54. // }
  55. // }
  56. // return state.pageSidebarSelected
  57. return state.sidebarSelected
  58. },
  59. userInfo: state => state.userInfo,
  60. permissions: state => state.permissions,
  61. menus: state => state.menus,
  62. projects: state => state.projects,
  63. uploaderList: state => state.uploaderList,
  64. projectId: state => {
  65. if (!state.projectId) {
  66. let pid = storage.get(KEY_PROJECT_SELECTED)
  67. if (pid) {
  68. state.projectId = pid
  69. } else if (state.projects && state.projects.length) {
  70. state.projectId = state.projects[0].id
  71. }
  72. }
  73. return state.projectId
  74. },
  75. projectName: state => state.projectName,
  76. group_code: state => state.group_code,
  77. breadcrumb: state => {
  78. if (!state.breadcrumb) {
  79. let arr = storage.get(KEY_PAGE_BRANDCRUMB)
  80. if (arr) {
  81. state.breadcrumb = arr
  82. }
  83. }
  84. return state.breadcrumb
  85. }
  86. },
  87. mutations: {
  88. setRowEdit: (state, val) => (state.rowEdit = val),
  89. setErrorReport: (state, val) => (state.errorReport = val),
  90. setSidebarClosed: (state, val) => (state.sidebarClosed = val),
  91. setUserInfo: (state, val) => (state.userInfo = val),
  92. setGroupCode: (state, val) => (state.group_code = val),
  93. setUserId: (state, val) => (state.userId = val),
  94. setSidebarSelected: (state, val) => {
  95. state.sidebarSelected = val
  96. storage.set(KEY_MENU_SELECTED, val)
  97. lStorage.set('screen_data', {
  98. path: val,
  99. data: {}
  100. })
  101. },
  102. setProjects: (state, val) => (state.projects = val),
  103. setprojectId: (state, val) => {
  104. lStorage.remove('cacheInfo') //待删除(删除用户浏览器无用缓存)
  105. let cacheInfo = lStorage.get('historyInfo') ? lStorage.get('historyInfo') : {}
  106. state.projectId = val
  107. lStorage.set('projectId', val)
  108. if (cacheInfo[state.userInfo.userName]) {
  109. // cacheInfo[state.userInfo.userName].projectId = val
  110. cacheInfo[state.userInfo.userName] = [...new Set([val, ...cacheInfo[state.userInfo.userName]])].slice(0, 3)
  111. lStorage.set('historyInfo', cacheInfo)
  112. } else {
  113. cacheInfo[state.userInfo.userName] = [val]
  114. lStorage.set('historyInfo', cacheInfo)
  115. }
  116. storage.set(KEY_PROJECT_SELECTED, val)
  117. state.projects.map((item) => {
  118. if (item.id == val) {
  119. state.secret = item.pwd
  120. state.projectName = item.name
  121. state.group_code = item.group_code
  122. lStorage.set('group_code', item.group_code)
  123. lStorage.set('secret', item.pwd)
  124. }
  125. })
  126. },
  127. setMenus: (state, val) => (state.menus = val),
  128. setUploaderList: (state, val) => {
  129. state.uploaderList = val ? val : []
  130. },
  131. },
  132. actions: {
  133. setRowEdit(contentx, value) {
  134. contentx.commit('setRowEdit', value)
  135. },
  136. setErrorReport(contentx, value) {
  137. contentx.commit('setErrorReport', value)
  138. },
  139. /**
  140. * @info 获取全部项目
  141. */
  142. getAllProject({
  143. state
  144. }) {
  145. return new Promise((resolve, reject) => {
  146. getAllProject({
  147. pageNumber: 1,
  148. pageSize: 1000
  149. }, res => {
  150. if (res.result === "success" && res.content && res.content.length) {
  151. state.projects = []
  152. // if (res.content[0] && res.content[0].id) {
  153. // state.projectId = res.content[0].id
  154. // state.projectName = res.content[0].localName || res.content[0].name || ""
  155. // state.secret = res.content[0].secret || ""
  156. // }
  157. res.content.forEach(proj =>
  158. state.projects.push({
  159. id: proj.id,
  160. name: proj.localName || proj.name || "",
  161. pwd: proj.secret || "",
  162. group_code: proj.groupCode || ""
  163. })
  164. )
  165. resolve(res)
  166. } else {
  167. reject()
  168. }
  169. })
  170. })
  171. },
  172. /**
  173. * 根据登录返回code获取用户信息和项目信息
  174. */
  175. loadUserInfo({
  176. state
  177. }) {
  178. return new Promise(async (resolve, reject) => {
  179. /**
  180. * 改为调用运维平台的根据用户ID获取用户信息的接口
  181. * nh 2021.10.25
  182. */
  183. let userId = store.state.ssoToken ?
  184. store.state.ssoToken :
  185. lStorage.get("ssoToken");
  186. let pd = lStorage.get("ssoPd");
  187. var infoData = await getUserInfoByUserId({ userId: userId, pd: pd });
  188. if (infoData.result === 'success') {
  189. const data = (infoData.content || [])[0] || {};
  190. state.userInfo = {
  191. userName: data.userName,
  192. userId: data.userId
  193. };
  194. state.userId = data.userId;
  195. state.projects = []; //初始化项目列表
  196. storage.set('user_name', data.userName);
  197. storage.set('user_id', data.userId);
  198. // 获取系统菜单成功
  199. if (data.authorizations && data.authorizations.length) {
  200. var menuArr=tools.formatMenu(data.authorizations);
  201. state.menus = menuArr;
  202. } else {
  203. state.menus = [];
  204. }
  205. // 获取项目信息成功
  206. if (data.projects && data.projects.length) {
  207. // 设置项目列表
  208. data.projects.forEach(proj =>
  209. state.projects.push({
  210. id: proj.projectId,
  211. name: proj.projectLocalName,
  212. pwd: proj.secret ? proj.secret : "",
  213. group_code: proj.groupCode
  214. })
  215. )
  216. }
  217. resolve(infoData);
  218. } else {
  219. state.userInfo = {
  220. userName: 'yanruolan',
  221. userId: '06328a53c69a41bb8f5bb1a552c6e8d6'
  222. };
  223. resolve(infoData);
  224. }
  225. return;
  226. var infoData = await getUserInfo({});
  227. if (infoData.code === 'Success') {
  228. const data = infoData.data;
  229. state.userInfo = {
  230. userName: data.name,
  231. userId: data.id
  232. };
  233. state.userId = data.id;
  234. state.projects = []; //初始化项目列表
  235. storage.set('user_name', data.username);
  236. storage.set('user_id', data.id);
  237. // 获取系统菜单成功
  238. if (data.menuList && data.menuList.length) {
  239. state.menus = tools.formatMenu(data.menuList);
  240. } else {
  241. state.menus = [];
  242. }
  243. // 获取项目信息成功
  244. if (data.projectList && data.projectList.length) {
  245. // 设置项目列表
  246. data.projectList.forEach(proj =>
  247. state.projects.push({
  248. id: proj.id,
  249. name: proj.localName,
  250. pwd: proj.secret ? proj.secret : "",
  251. group_code: proj.groupCode
  252. })
  253. )
  254. }
  255. resolve(infoData);
  256. } else {
  257. state.userInfo = {
  258. userName: 'yanruolan',
  259. userId: '06328a53c69a41bb8f5bb1a552c6e8d6'
  260. };
  261. resolve(infoData);
  262. }
  263. })
  264. },
  265. setBreadcrumb: {
  266. root: true,
  267. handler({
  268. state,
  269. commit
  270. }, val) {
  271. let label = val[0].label;
  272. if (label === "消息中心") {
  273. commit("setSidebarSelected", "message"); // 当进入消息中心页面的时候不选中导航栏
  274. }
  275. state.breadcrumb = []
  276. state.breadcrumb = val
  277. storage.set(KEY_PAGE_BRANDCRUMB, val)
  278. }
  279. }
  280. }
  281. }