App.vue 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. import { useRoute } from 'vue-router'
  14. //初始变量
  15. const appStore = useAppStore()
  16. const UserTheme = ref(appStore.getTheme)
  17. const useRoutes = useRoute()
  18. //监听
  19. watch(() => [
  20. appStore.getTheme,
  21. appStore.getThemeVal,
  22. appStore.getColor,
  23. ], ([Theme, ThemeVal, ColorVal]) => {
  24. UserTheme.value = Theme
  25. setUserTheme(ThemeVal, ColorVal)
  26. })
  27. nextTick(() => {
  28. detectionBrowser()
  29. window['$split'] = split
  30. setUserTheme(appStore.getThemeVal, appStore.getColor)
  31. addDocumentsJs()
  32. //生产环境下,检测更新
  33. if (import.meta.env.PROD) {
  34. getAppVersion()
  35. }
  36. })
  37. //设置主题
  38. const setUserTheme = (theme, appColor) => {
  39. const projectInfo = appStore.projectInfo
  40. const id = '1891312830718746625'
  41. let isYunNan = useRoutes?.path !== '/config/theme' && projectInfo?.id === id
  42. if (isYunNan) {
  43. setElementMainColor('#409eff')
  44. let colorName = 'blue'
  45. let theme = 'light'
  46. document.documentElement.setAttribute('class', `${theme} color-${colorName}`)
  47. return
  48. }
  49. const colorVal = getObjValue(appColor)
  50. //设置主色调
  51. setElementMainColor(colorVal?.color)
  52. const colorName = colorVal?.name || 'green'
  53. //设置主题
  54. let val = UserTheme.value
  55. if (val === 'auto') {
  56. theme = useOsTheme()
  57. }
  58. if (theme === '') {
  59. theme = val
  60. }
  61. //挂载相关样式
  62. document.documentElement.setAttribute('class', `${theme} color-${colorName}`)
  63. }
  64. </script>