main.ts 2.5 KB

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