layout-store.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import frameworkApi from '@/api/framework'
  2. import storage from '@/framework/utils/storage'
  3. import axios from 'axios'
  4. import authutils from '../../utils/authutils'
  5. const KEY_MENU_SELECTED = 'menu_selected'
  6. const KEY_PROJECT_SELECTED = 'global_project_selected'
  7. const KEY_PAGE_BRANDCRUMB = 'page_brandcrumb'
  8. export default {
  9. namespaced: true,
  10. state: {
  11. sidebarClosed: false,
  12. sidebarSelected: '', // sidebar选中的选项
  13. userInfo: null, //{ username: 'admin' },
  14. permissions: { "system:role:delete": true, "system:role:create": true, "system:role:query": true, "system:role:setOpts": true },
  15. projectId: 'Pj4201050001',
  16. projects: [{ name: '风雅园', id: 'Pj1101010001' }, { name: '亚心医院', id: 'Pj4201050001' }],
  17. breadcrumb: [],
  18. secret: "saga123456", //密码
  19. userId: "test", //用户id
  20. },
  21. getters: {
  22. sidebarClosed: state => state.sidebarClosed,
  23. secret: state => state.secret,
  24. userId: state => state.userId,
  25. sidebarSelected: state => {
  26. // if (!state.pageSidebarSelected) {
  27. // let menu = storage.get(KEY_MENU_SELECTED)
  28. // if (menu) {
  29. // state.pageSidebarSelected = menu
  30. // }
  31. // }
  32. // return state.pageSidebarSelected
  33. return state.sidebarSelected
  34. },
  35. userInfo: state => state.userInfo,
  36. permissions: state => state.permissions,
  37. projects: state => state.projects,
  38. projectId: state => {
  39. if (!state.projectId) {
  40. let pid = storage.get(KEY_PROJECT_SELECTED)
  41. if (pid) {
  42. state.projectId = pid
  43. }
  44. }
  45. return state.projectId
  46. },
  47. breadcrumb: state => {
  48. if (!state.breadcrumb) {
  49. let arr = storage.get(KEY_PAGE_BRANDCRUMB)
  50. if (arr) {
  51. state.breadcrumb = arr
  52. }
  53. }
  54. return state.breadcrumb
  55. }
  56. },
  57. mutations: {
  58. setSidebarClosed: (state, val) => (state.sidebarClosed = val),
  59. setSidebarSelected: (state, val) => {
  60. state.sidebarSelected = val
  61. storage.set(KEY_MENU_SELECTED, val)
  62. },
  63. setprojectId: (state, val) => {
  64. state.projectId = val
  65. localStorage.setItem('projectId', val)
  66. storage.set(KEY_PROJECT_SELECTED, val)
  67. }
  68. },
  69. actions: {
  70. loadUserInfo({ state }) {
  71. console.log(state)
  72. return new Promise((resolve, reject) => {
  73. frameworkApi.loadUserInfo().then(resp => {
  74. console.log(resp)
  75. if (resp.result == 'success') {
  76. state.userInfo = { username: resp.username }
  77. state.permissions = {}
  78. if (resp.permissions) {
  79. resp.permissions.forEach(p => (state.permissions[p] = true))
  80. }
  81. state.projects = []
  82. if (resp.projects) {
  83. resp.projects.forEach(proj =>
  84. state.projects.push({
  85. id: proj.projId,
  86. name: proj.projLocalName
  87. })
  88. )
  89. }
  90. } else {
  91. state.userInfo = null
  92. }
  93. resolve(resp)
  94. })
  95. })
  96. },
  97. setBreadcrumb: {
  98. root: true,
  99. handler({ state }, val) {
  100. state.breadcrumb = []
  101. state.breadcrumb = val
  102. storage.set(KEY_PAGE_BRANDCRUMB, val)
  103. }
  104. }
  105. }
  106. }