App.vue 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <template>
  2. <hc-app-config>
  3. <router-view />
  4. </hc-app-config>
  5. </template>
  6. <script setup>
  7. import { nextTick, ref, watch } from 'vue'
  8. import { useAppStore } from '~src/store'
  9. import { detectionBrowser, getAppVersion, useOsTheme } from 'hc-vue3-ui'
  10. import { getObjValue, setElementMainColor } from 'js-fast-way'
  11. import { addDocumentsJs } from '~uti/tools'
  12. import split from 'split.js'
  13. //初始变量
  14. const appStore = useAppStore()
  15. const UserTheme = ref(appStore.getTheme)
  16. //监听
  17. watch(() => [
  18. appStore.getTheme,
  19. appStore.getThemeVal,
  20. appStore.getColor,
  21. ], ([Theme, ThemeVal, ColorVal]) => {
  22. UserTheme.value = Theme
  23. setUserTheme(ThemeVal, ColorVal)
  24. })
  25. nextTick(() => {
  26. detectionBrowser()
  27. window['$split'] = split
  28. setUserTheme(appStore.getThemeVal, appStore.getColor)
  29. addDocumentsJs()
  30. //生产环境下,检测更新
  31. if (import.meta.env.PROD) {
  32. getAppVersion()
  33. }
  34. })
  35. //设置主题
  36. const setUserTheme = (theme, appColor) => {
  37. const colorVal = getObjValue(appColor)
  38. //设置主色调
  39. setElementMainColor(colorVal?.color)
  40. const colorName = colorVal?.name || 'green'
  41. //设置主题
  42. let val = UserTheme.value
  43. if (val === 'auto') {
  44. theme = useOsTheme()
  45. }
  46. if (theme === '') {
  47. theme = val
  48. }
  49. //挂载相关样式
  50. document.documentElement.setAttribute('class', `${theme} color-${colorName}`)
  51. }
  52. </script>