main.ts 2.7 KB

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