main.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import './assets/main.css'
  2. import './styles/index.scss'
  3. import './styles/icons/iconfont.js'
  4. import VXETable from 'vxe-table'
  5. import 'vxe-table/lib/style.css'
  6. import enUS from 'vxe-table/lib/locale/lang/en-US'
  7. import VxeUI from 'vxe-pc-ui'
  8. import 'element-plus/dist/index.css'
  9. import 'vxe-pc-ui/lib/style.css'
  10. import Antd from 'ant-design-vue'
  11. import 'ant-design-vue/dist/reset.css'
  12. import * as ElementPlusIconsVue from '@element-plus/icons-vue'
  13. import VXETablePluginExportXLSX from 'vxe-table-plugin-export-xlsx'
  14. import ExcelJS from 'exceljs'
  15. import { VLoading } from './directive/VLoading'
  16. import { createApp } from 'vue'
  17. import { createPinia } from 'pinia'
  18. import App from './App.vue'
  19. import router from './router'
  20. import { useThemeStore } from '@/stores/modules/theme'
  21. const app = createApp(App)
  22. for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  23. app.component(key, component)
  24. }
  25. // 让VxeTable支持导出Excel
  26. VXETable.use(VXETablePluginExportXLSX, {
  27. ExcelJS
  28. })
  29. VXETable.setI18n('en-US', enUS)
  30. VXETable.setLanguage('en-US')
  31. app.use(createPinia())
  32. app.use(VXETable)
  33. app.use(VxeUI)
  34. app.use(router)
  35. app.use(Antd)
  36. // 注册全局指令
  37. app.directive('vloading', VLoading)
  38. const themeStore = useThemeStore()
  39. // 动态加载暗黑主题
  40. async function loadDarkTheme() {
  41. await import('element-plus/theme-chalk/dark/css-vars.css') // 动态导入暗黑主题
  42. }
  43. // 动态移除暗黑主题
  44. function unloadDarkTheme() {
  45. const darkThemeStylesheet = document.getElementById('dark-theme-style')
  46. if (darkThemeStylesheet) {
  47. darkThemeStylesheet.remove()
  48. }
  49. }
  50. // 用户没有手动切换主题时,根据系统设置自动切换
  51. if (!themeStore.isManualChange) {
  52. // 根据用户偏好或系统设置决定是否添加暗黑模式类名
  53. if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
  54. themeStore.toggleTheme('dark')
  55. loadDarkTheme()
  56. } else {
  57. unloadDarkTheme()
  58. themeStore.toggleTheme('light')
  59. }
  60. } else {
  61. // 用户手动切换主题时,根据用户设置切换
  62. if (themeStore.theme === 'dark') {
  63. loadDarkTheme()
  64. themeStore.toggleTheme('dark')
  65. }
  66. }
  67. // 提供一个全局方法来切换主题
  68. app.config.globalProperties.$toggleDarkMode = () => {
  69. const html = document.documentElement
  70. if (html.classList.contains('dark')) {
  71. unloadDarkTheme()
  72. html.classList.remove('dark')
  73. localStorage.setItem('theme', 'light')
  74. } else {
  75. loadDarkTheme()
  76. html.classList.add('dark')
  77. localStorage.setItem('theme', 'dark')
  78. }
  79. }
  80. app.mount('#app')