app.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. import { defineStore } from 'pinia'
  2. import { store } from '../index'
  3. import { setCssVar, humpToUnderline } from '@/utils'
  4. import { ElMessage } from 'element-plus'
  5. import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
  6. import { ElementPlusSize } from '@/types/elementPlus'
  7. import { LayoutType } from '@/types/layout'
  8. import { ThemeTypes } from '@/types/theme'
  9. const { wsCache } = useCache()
  10. interface AppState {
  11. breadcrumb : boolean
  12. breadcrumbIcon : boolean
  13. collapse : boolean
  14. uniqueOpened : boolean
  15. hamburger : boolean
  16. screenfull : boolean
  17. search : boolean
  18. size : boolean
  19. locale : boolean
  20. message : boolean
  21. tagsView : boolean
  22. tagsViewIcon : boolean
  23. logo : boolean
  24. fixedHeader : boolean
  25. greyMode : boolean
  26. pageLoading : boolean
  27. layout : LayoutType
  28. title : string
  29. userInfo : string
  30. isDark : boolean
  31. currentSize : ElementPlusSize
  32. sizeMap : ElementPlusSize[]
  33. mobile : boolean
  34. footer : boolean
  35. theme : ThemeTypes
  36. fixedMenu : boolean
  37. }
  38. export const useAppStore = defineStore('app', {
  39. state: () : AppState => {
  40. return {
  41. userInfo: 'userInfo', // 登录信息存储字段-建议每个项目换一个字段,避免与其他项目冲突
  42. sizeMap: ['default', 'large', 'small'],
  43. mobile: false, // 是否是移动端
  44. title: import.meta.env.VITE_APP_TITLE, // 标题
  45. pageLoading: false, // 路由跳转loading
  46. breadcrumb: true, // 面包屑
  47. breadcrumbIcon: true, // 面包屑图标
  48. collapse: false, // 折叠菜单
  49. uniqueOpened: true, // 是否只保持一个子菜单的展开
  50. hamburger: true, // 折叠图标
  51. screenfull: true, // 全屏图标
  52. search: true, // 搜索图标
  53. size: true, // 尺寸图标
  54. locale: true, // 多语言图标
  55. message: true, // 消息图标
  56. tagsView: false, // 标签页
  57. tagsViewIcon: true, // 是否显示标签图标
  58. logo: true, // logo
  59. fixedHeader: true, // 固定toolheader
  60. footer: true, // 显示页脚
  61. greyMode: false, // 是否开始灰色模式,用于特殊悼念日
  62. fixedMenu: wsCache.get('fixedMenu') || false, // 是否固定菜单
  63. layout: wsCache.get(CACHE_KEY.LAYOUT) || 'classic', // layout布局
  64. isDark: wsCache.get(CACHE_KEY.IS_DARK) || false, // 是否是暗黑模式
  65. currentSize: wsCache.get('default') || 'default', // 组件尺寸
  66. theme: wsCache.get(CACHE_KEY.THEME) || {
  67. // 主题色
  68. elColorPrimary: '#409eff',
  69. // 左侧菜单边框颜色
  70. leftMenuBorderColor: 'inherit',
  71. // 左侧菜单背景颜色
  72. leftMenuBgColor: '#001529',
  73. // 左侧菜单浅色背景颜色
  74. leftMenuBgLightColor: '#0f2438',
  75. // 左侧菜单选中背景颜色
  76. leftMenuBgActiveColor: 'var(--el-color-primary)',
  77. // 左侧菜单收起选中背景颜色
  78. leftMenuCollapseBgActiveColor: 'var(--el-color-primary)',
  79. // 左侧菜单字体颜色
  80. leftMenuTextColor: '#bfcbd9',
  81. // 左侧菜单选中字体颜色
  82. leftMenuTextActiveColor: '#fff',
  83. // logo字体颜色
  84. logoTitleTextColor: '#fff',
  85. // logo边框颜色
  86. logoBorderColor: 'inherit',
  87. // 头部背景颜色
  88. topHeaderBgColor: '#fff',
  89. // 头部字体颜色
  90. topHeaderTextColor: 'inherit',
  91. // 头部悬停颜色
  92. topHeaderHoverColor: '#f6f6f6',
  93. // 头部边框颜色
  94. topToolBorderColor: '#eee'
  95. }
  96. }
  97. },
  98. getters: {
  99. getBreadcrumb() : boolean {
  100. return this.breadcrumb
  101. },
  102. getBreadcrumbIcon() : boolean {
  103. return this.breadcrumbIcon
  104. },
  105. getCollapse() : boolean {
  106. return this.collapse
  107. },
  108. getUniqueOpened() : boolean {
  109. return this.uniqueOpened
  110. },
  111. getHamburger() : boolean {
  112. return this.hamburger
  113. },
  114. getScreenfull() : boolean {
  115. return this.screenfull
  116. },
  117. getSize() : boolean {
  118. return this.size
  119. },
  120. getLocale() : boolean {
  121. return this.locale
  122. },
  123. getMessage() : boolean {
  124. return this.message
  125. },
  126. getTagsView() : boolean {
  127. return this.tagsView
  128. },
  129. getTagsViewIcon() : boolean {
  130. return this.tagsViewIcon
  131. },
  132. getLogo() : boolean {
  133. return this.logo
  134. },
  135. getFixedHeader() : boolean {
  136. return this.fixedHeader
  137. },
  138. getGreyMode() : boolean {
  139. return this.greyMode
  140. },
  141. getFixedMenu() : boolean {
  142. return this.fixedMenu
  143. },
  144. getPageLoading() : boolean {
  145. return this.pageLoading
  146. },
  147. getLayout() : LayoutType {
  148. return this.layout
  149. },
  150. getTitle() : string {
  151. return this.title
  152. },
  153. getUserInfo() : string {
  154. return this.userInfo
  155. },
  156. getIsDark() : boolean {
  157. return this.isDark
  158. },
  159. getCurrentSize() : ElementPlusSize {
  160. return this.currentSize
  161. },
  162. getSizeMap() : ElementPlusSize[] {
  163. return this.sizeMap
  164. },
  165. getMobile() : boolean {
  166. return this.mobile
  167. },
  168. getTheme() : ThemeTypes {
  169. return this.theme
  170. },
  171. getFooter() : boolean {
  172. return this.footer
  173. }
  174. },
  175. actions: {
  176. setBreadcrumb(breadcrumb : boolean) {
  177. this.breadcrumb = breadcrumb
  178. },
  179. setBreadcrumbIcon(breadcrumbIcon : boolean) {
  180. this.breadcrumbIcon = breadcrumbIcon
  181. },
  182. setCollapse(collapse : boolean) {
  183. this.collapse = collapse
  184. },
  185. setUniqueOpened(uniqueOpened : boolean) {
  186. this.uniqueOpened = uniqueOpened
  187. },
  188. setHamburger(hamburger : boolean) {
  189. this.hamburger = hamburger
  190. },
  191. setScreenfull(screenfull : boolean) {
  192. this.screenfull = screenfull
  193. },
  194. setSize(size : boolean) {
  195. this.size = size
  196. },
  197. setLocale(locale : boolean) {
  198. this.locale = locale
  199. },
  200. setMessage(message : boolean) {
  201. this.message = message
  202. },
  203. setTagsView(tagsView : boolean) {
  204. this.tagsView = tagsView
  205. },
  206. setTagsViewIcon(tagsViewIcon : boolean) {
  207. this.tagsViewIcon = tagsViewIcon
  208. },
  209. setLogo(logo : boolean) {
  210. this.logo = logo
  211. },
  212. setFixedHeader(fixedHeader : boolean) {
  213. this.fixedHeader = fixedHeader
  214. },
  215. setGreyMode(greyMode : boolean) {
  216. this.greyMode = greyMode
  217. },
  218. setFixedMenu(fixedMenu : boolean) {
  219. wsCache.set('fixedMenu', fixedMenu)
  220. this.fixedMenu = fixedMenu
  221. },
  222. setPageLoading(pageLoading : boolean) {
  223. this.pageLoading = pageLoading
  224. },
  225. setLayout(layout : LayoutType) {
  226. if (this.mobile && layout !== 'classic') {
  227. ElMessage.warning('移动端模式下不支持切换其他布局')
  228. return
  229. }
  230. this.layout = layout
  231. wsCache.set(CACHE_KEY.LAYOUT, this.layout)
  232. },
  233. setTitle(title : string) {
  234. this.title = title
  235. },
  236. setIsDark(isDark : boolean) {
  237. this.isDark = isDark
  238. if (this.isDark) {
  239. document.documentElement.classList.add('dark')
  240. document.documentElement.classList.remove('light')
  241. } else {
  242. document.documentElement.classList.add('light')
  243. document.documentElement.classList.remove('dark')
  244. }
  245. wsCache.set(CACHE_KEY.IS_DARK, this.isDark)
  246. },
  247. setCurrentSize(currentSize : ElementPlusSize) {
  248. this.currentSize = currentSize
  249. wsCache.set('currentSize', this.currentSize)
  250. },
  251. setMobile(mobile : boolean) {
  252. this.mobile = mobile
  253. },
  254. setTheme(theme : ThemeTypes) {
  255. this.theme = Object.assign(this.theme, theme)
  256. wsCache.set(CACHE_KEY.THEME, this.theme)
  257. },
  258. setCssVarTheme() {
  259. for (const key in this.theme) {
  260. setCssVar(`--${humpToUnderline(key)}`, this.theme[key])
  261. }
  262. },
  263. setFooter(footer : boolean) {
  264. this.footer = footer
  265. }
  266. }
  267. })
  268. export const useAppStoreWithOut = () => {
  269. return useAppStore(store)
  270. }