App.vue 1.4 KB

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