global.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { defineStore } from 'pinia'
  2. import { changeColor } from '@/utils/themeUtil'
  3. import config from '@/config'
  4. import { message } from 'ant-design-vue'
  5. import tool from '@/utils/tool'
  6. const toolDataGet = (key) => {
  7. return tool.data.get(key)
  8. }
  9. // 获取缓存中的,如果取不到那就用配置的
  10. const getCacheConfig = (value) => {
  11. const data = toolDataGet(value)
  12. if (data === null) {
  13. return config[value]
  14. }
  15. return data
  16. }
  17. /**
  18. * deprecated 请使用 useGlobalStore
  19. */
  20. export const globalStore = defineStore({
  21. id: 'global',
  22. state: () => ({
  23. // 移动端布局
  24. isMobile: false,
  25. // 布局
  26. layout: getCacheConfig('SNOWY_LAYOUT'),
  27. // 菜单是否折叠 toggle
  28. menuIsCollapse: getCacheConfig('SNOWY_MENU_COLLAPSE'),
  29. // 侧边菜单是否排他展开
  30. sideUniqueOpen: getCacheConfig('SNOWY_SIDE_UNIQUE_OPEN'),
  31. // 多标签栏
  32. layoutTagsOpen: getCacheConfig('SNOWY_LAYOUT_TAGS_OPEN'),
  33. // 是否展示面包屑
  34. breadcrumbOpen: getCacheConfig('SNOWY_BREADCRUMD_OPEN'),
  35. // 顶栏是否应用主题色
  36. topHeaderThemeColorOpen: getCacheConfig('SNOWY_TOP_HEADER_THEME_COLOR_OPEN'),
  37. // 顶栏主题色通栏
  38. topHeaderThemeColorSpread: getCacheConfig('SNOWY_TOP_HEADER_THEME_COLOR_SPREAD'),
  39. // 模块坞
  40. moduleUnfoldOpen: getCacheConfig('SNOWY_MODULE_UNFOLD_OPEN'),
  41. // 主题
  42. theme: getCacheConfig('SNOWY_THEME'),
  43. // 主题颜色
  44. themeColor: toolDataGet('SNOWY_THEME_COLOR') || config.COLOR,
  45. // 整体表单风格
  46. formStyle: getCacheConfig('SNOWY_FORM_STYLE'),
  47. // 用户信息
  48. userInfo: toolDataGet('USER_INFO') || {},
  49. // 系统配置
  50. sysBaseConfig: toolDataGet('SNOWY_SYS_BASE_CONFIG') || config.SYS_BASE_CONFIG,
  51. // 默认应用
  52. module: getCacheConfig('SNOWY_MENU_MODULE_ID')
  53. }),
  54. getters: {},
  55. actions: {
  56. setIsMobile(key) {
  57. this.isMobile = key
  58. },
  59. setLayout(key) {
  60. this.layout = key
  61. },
  62. setTheme(key) {
  63. this.theme = key
  64. const closeMessage = message.loading(`加载中...`)
  65. changeColor(this.themeColor, key).then(closeMessage)
  66. },
  67. setThemeColor(key) {
  68. this.themeColor = key
  69. const closeMessage = message.loading(`加载中...`)
  70. changeColor(key, this.theme).then(closeMessage)
  71. },
  72. initTheme() {
  73. const closeMessage = message.loading(`加载中...`)
  74. changeColor(this.themeColor, this.theme).then(closeMessage)
  75. },
  76. toggleConfig(key) {
  77. this[key] = !this[key]
  78. },
  79. setFormStyle(key) {
  80. this.formStyle = key
  81. },
  82. setUserInfo(key) {
  83. this.userInfo = key
  84. },
  85. setSysBaseConfig(key) {
  86. this.sysBaseConfig = key
  87. },
  88. setModule(key) {
  89. this.module = key
  90. }
  91. }
  92. })
  93. export const useGlobalStore = globalStore