app.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import { defineStore } from 'pinia'
  2. import { store } from '../index'
  3. import { useCache } from '@/hooks/web/useCache'
  4. import { appModules } from '@/config/app'
  5. import type { AppState, LayoutType } from '@/config/app'
  6. import { setCssVar, humpToUnderline } from '@/utils'
  7. import { ElMessage } from 'element-plus'
  8. const { wsCache } = useCache()
  9. export const useAppStore = defineStore({
  10. id: 'app',
  11. state: (): AppState => appModules,
  12. getters: {
  13. getBreadcrumb(): boolean {
  14. return this.breadcrumb
  15. },
  16. getBreadcrumbIcon(): boolean {
  17. return this.breadcrumbIcon
  18. },
  19. getCollapse(): boolean {
  20. return this.collapse
  21. },
  22. getHamburger(): boolean {
  23. return this.hamburger
  24. },
  25. getScreenfull(): boolean {
  26. return this.screenfull
  27. },
  28. getSize(): boolean {
  29. return this.size
  30. },
  31. getLocale(): boolean {
  32. return this.locale
  33. },
  34. getTagsView(): boolean {
  35. return this.tagsView
  36. },
  37. getLogo(): boolean {
  38. return this.logo
  39. },
  40. getFixedHeader(): boolean {
  41. return this.fixedHeader
  42. },
  43. getFixedMenu(): boolean {
  44. return this.fixedMenu
  45. },
  46. getGreyMode(): boolean {
  47. return this.greyMode
  48. },
  49. getLayout(): LayoutType {
  50. return this.layout
  51. },
  52. getTitle(): string {
  53. return this.title
  54. },
  55. getUserInfo(): string {
  56. return this.userInfo
  57. },
  58. getIsDark(): boolean {
  59. return this.isDark
  60. },
  61. getCurrentSize(): ElememtPlusSzie {
  62. return this.currentSize
  63. },
  64. getSizeMap(): ElememtPlusSzie[] {
  65. return this.sizeMap
  66. },
  67. getMobile(): boolean {
  68. return this.mobile
  69. },
  70. getTheme(): Recordable {
  71. return this.theme
  72. }
  73. },
  74. actions: {
  75. setBreadcrumb(breadcrumb: boolean) {
  76. this.breadcrumb = breadcrumb
  77. },
  78. setBreadcrumbIcon(breadcrumbIcon: boolean) {
  79. this.breadcrumbIcon = breadcrumbIcon
  80. },
  81. setCollapse(collapse: boolean) {
  82. this.collapse = collapse
  83. },
  84. setHamburger(hamburger: boolean) {
  85. this.hamburger = hamburger
  86. },
  87. setScreenfull(screenfull: boolean) {
  88. this.screenfull = screenfull
  89. },
  90. setSize(size: boolean) {
  91. this.size = size
  92. },
  93. setLocale(locale: boolean) {
  94. this.locale = locale
  95. },
  96. setTagsView(tagsView: boolean) {
  97. this.tagsView = tagsView
  98. },
  99. setLogo(logo: boolean) {
  100. this.logo = logo
  101. },
  102. setFixedHeader(fixedHeader: boolean) {
  103. this.fixedHeader = fixedHeader
  104. },
  105. setFixedMenu(fixedMenu: boolean) {
  106. this.fixedMenu = fixedMenu
  107. },
  108. setGreyMode(greyMode: boolean) {
  109. this.greyMode = greyMode
  110. },
  111. setLayout(layout: LayoutType) {
  112. if (this.mobile && layout !== 'classic') {
  113. ElMessage.warning('移动端模式下不支持切换其他布局')
  114. return
  115. }
  116. this.layout = layout
  117. wsCache.set('layout', this.layout)
  118. },
  119. setTitle(title: string) {
  120. this.title = title
  121. },
  122. setIsDark(isDark: boolean) {
  123. this.isDark = isDark
  124. if (this.isDark) {
  125. document.documentElement.classList.add('dark')
  126. document.documentElement.classList.remove('light')
  127. } else {
  128. document.documentElement.classList.add('light')
  129. document.documentElement.classList.remove('dark')
  130. }
  131. wsCache.set('isDark', this.isDark)
  132. },
  133. setCurrentSize(currentSize: ElememtPlusSzie) {
  134. this.currentSize = currentSize
  135. wsCache.set('currentSize', this.currentSize)
  136. },
  137. setMobile(mobile: boolean) {
  138. this.mobile = mobile
  139. },
  140. setTheme(theme: Recordable) {
  141. this.theme = Object.assign(this.theme, theme)
  142. wsCache.set('theme', this.theme)
  143. },
  144. setCssVarTheme() {
  145. for (const key in this.theme) {
  146. setCssVar(`--${humpToUnderline(key)}`, this.theme[key])
  147. }
  148. }
  149. }
  150. })
  151. export const useAppStoreWithOut = () => {
  152. return useAppStore(store)
  153. }