App.vue 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 split from 'split.js'
  12. //初始变量
  13. const appStore = useAppStore()
  14. const UserTheme = ref(appStore.getTheme)
  15. //监听
  16. watch(() => [
  17. appStore.getTheme,
  18. appStore.getThemeVal,
  19. appStore.getColor,
  20. ], ([Theme, ThemeVal, ColorVal]) => {
  21. UserTheme.value = Theme
  22. setUserTheme(ThemeVal, ColorVal)
  23. })
  24. nextTick(() => {
  25. detectionBrowser()
  26. setUserTheme(appStore.getThemeVal, appStore.getColor)
  27. //生产环境下,检测更新
  28. if (import.meta.env.PROD) {
  29. getAppVersion()
  30. }
  31. window['$split'] = split
  32. })
  33. //设置主题
  34. const setUserTheme = (theme, appColor) => {
  35. const colorVal = getObjValue(appColor)
  36. //设置主色调
  37. setElementMainColor(colorVal?.color)
  38. const colorName = colorVal?.name || 'green'
  39. //设置主题
  40. let val = UserTheme.value
  41. if (val === 'auto') {
  42. theme = useOsTheme()
  43. }
  44. if (theme === '') {
  45. theme = val
  46. }
  47. //挂载相关样式
  48. document.documentElement.setAttribute('class', `${theme} color-${colorName}`)
  49. }
  50. </script>