app.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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, ThemeTypes } 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. persist: {
  13. enabled: true
  14. },
  15. getters: {
  16. getBreadcrumb(): boolean {
  17. return this.breadcrumb
  18. },
  19. getBreadcrumbIcon(): boolean {
  20. return this.breadcrumbIcon
  21. },
  22. getCollapse(): boolean {
  23. return this.collapse
  24. },
  25. getUniqueOpened(): boolean {
  26. return this.uniqueOpened
  27. },
  28. getHamburger(): boolean {
  29. return this.hamburger
  30. },
  31. getScreenfull(): boolean {
  32. return this.screenfull
  33. },
  34. getSize(): boolean {
  35. return this.size
  36. },
  37. getLocale(): boolean {
  38. return this.locale
  39. },
  40. getTagsView(): boolean {
  41. return this.tagsView
  42. },
  43. getTagsViewIcon(): boolean {
  44. return this.tagsViewIcon
  45. },
  46. getLogo(): boolean {
  47. return this.logo
  48. },
  49. getFixedHeader(): boolean {
  50. return this.fixedHeader
  51. },
  52. getGreyMode(): boolean {
  53. return this.greyMode
  54. },
  55. getPageLoading(): boolean {
  56. return this.pageLoading
  57. },
  58. getLayout(): LayoutType {
  59. return this.layout
  60. },
  61. getTitle(): string {
  62. return this.title
  63. },
  64. getUserInfo(): string {
  65. return this.userInfo
  66. },
  67. getIsDark(): boolean {
  68. return this.isDark
  69. },
  70. getCurrentSize(): ElememtPlusSzie {
  71. return this.currentSize
  72. },
  73. getSizeMap(): ElememtPlusSzie[] {
  74. return this.sizeMap
  75. },
  76. getMobile(): boolean {
  77. return this.mobile
  78. },
  79. getTheme(): ThemeTypes {
  80. return this.theme
  81. },
  82. getFooter(): boolean {
  83. return this.footer
  84. }
  85. },
  86. actions: {
  87. setBreadcrumb(breadcrumb: boolean) {
  88. this.breadcrumb = breadcrumb
  89. },
  90. setBreadcrumbIcon(breadcrumbIcon: boolean) {
  91. this.breadcrumbIcon = breadcrumbIcon
  92. },
  93. setCollapse(collapse: boolean) {
  94. this.collapse = collapse
  95. },
  96. setUniqueOpened(uniqueOpened: boolean) {
  97. this.uniqueOpened = uniqueOpened
  98. },
  99. setHamburger(hamburger: boolean) {
  100. this.hamburger = hamburger
  101. },
  102. setScreenfull(screenfull: boolean) {
  103. this.screenfull = screenfull
  104. },
  105. setSize(size: boolean) {
  106. this.size = size
  107. },
  108. setLocale(locale: boolean) {
  109. this.locale = locale
  110. },
  111. setTagsView(tagsView: boolean) {
  112. this.tagsView = tagsView
  113. },
  114. setTagsViewIcon(tagsViewIcon: boolean) {
  115. this.tagsViewIcon = tagsViewIcon
  116. },
  117. setLogo(logo: boolean) {
  118. this.logo = logo
  119. },
  120. setFixedHeader(fixedHeader: boolean) {
  121. this.fixedHeader = fixedHeader
  122. },
  123. setGreyMode(greyMode: boolean) {
  124. this.greyMode = greyMode
  125. },
  126. setPageLoading(pageLoading: boolean) {
  127. this.pageLoading = pageLoading
  128. },
  129. setLayout(layout: LayoutType) {
  130. if (this.mobile && layout !== 'classic') {
  131. ElMessage.warning('移动端模式下不支持切换其他布局')
  132. return
  133. }
  134. this.layout = layout
  135. wsCache.set('layout', this.layout)
  136. },
  137. setTitle(title: string) {
  138. this.title = title
  139. },
  140. setIsDark(isDark: boolean) {
  141. this.isDark = isDark
  142. if (this.isDark) {
  143. document.documentElement.classList.add('dark')
  144. document.documentElement.classList.remove('light')
  145. } else {
  146. document.documentElement.classList.add('light')
  147. document.documentElement.classList.remove('dark')
  148. }
  149. wsCache.set('isDark', this.isDark)
  150. },
  151. setCurrentSize(currentSize: ElememtPlusSzie) {
  152. this.currentSize = currentSize
  153. wsCache.set('currentSize', this.currentSize)
  154. },
  155. setMobile(mobile: boolean) {
  156. this.mobile = mobile
  157. },
  158. setTheme(theme: ThemeTypes) {
  159. this.theme = Object.assign(this.theme, theme)
  160. wsCache.set('theme', this.theme)
  161. },
  162. setCssVarTheme() {
  163. for (const key in this.theme) {
  164. setCssVar(`--${humpToUnderline(key)}`, this.theme[key])
  165. }
  166. },
  167. setFooter(footer: boolean) {
  168. this.footer = footer
  169. }
  170. }
  171. })
  172. export const useAppStoreWithOut = () => {
  173. return useAppStore(store)
  174. }