App.vue 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <template>
  2. <hc-app-config>
  3. <router-view />
  4. </hc-app-config>
  5. </template>
  6. <script setup>
  7. import { nextTick, onMounted, 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 website from '~src/config'
  12. import { useRoute } from 'vue-router'
  13. //初始变量
  14. const appStore = useAppStore()
  15. const UserTheme = ref(appStore.getTheme)
  16. const useRoutes = useRoute()
  17. //监听
  18. watch(() => [appStore.getTheme, appStore.getThemeVal, appStore.getColor], ([Theme, ThemeVal, ColorVal]) => {
  19. UserTheme.value = Theme
  20. setUserTheme(ThemeVal, ColorVal)
  21. })
  22. onMounted(() => {
  23. // 监听设备连接
  24. navigator?.usb?.addEventListener('connect', ({ device }) => {
  25. console.log('USB 设备已连接:', device)
  26. if (device.manufacturerName === 'CFIST') {
  27. appStore.setIsUKey(true)
  28. }
  29. })
  30. // 监听设备断开
  31. navigator?.usb?.addEventListener('disconnect', ({ device }) => {
  32. console.log('USB 设备已断开:', device)
  33. if (device.manufacturerName === 'CFIST') {
  34. appStore.setIsUKey(false)
  35. }
  36. })
  37. })
  38. nextTick(() => {
  39. window.$localModel = website.localModel
  40. setUserTheme(appStore.getThemeVal, appStore.getColor)
  41. //生产环境下,检测更新
  42. if (import.meta.env.PROD && appStore.isSource !== 'app') {
  43. detectionBrowser()
  44. getAppVersion()
  45. }
  46. //获取屏幕宽高
  47. const dom = document.body
  48. console.log('高:' + dom.clientHeight, '宽:' + dom.clientWidth)
  49. })
  50. //设置主题
  51. const setUserTheme = (theme, appColor) => {
  52. const projectInfo = appStore.projectInfo
  53. const id = '1891312830718746625'
  54. let isYunNan = useRoutes?.path !== '/home/config' && projectInfo?.id === id
  55. if (isYunNan) {
  56. setElementMainColor('#409eff')
  57. let colorName = 'blue'
  58. let theme = 'light'
  59. document.documentElement.setAttribute('class', `${theme} color-${colorName}`)
  60. return
  61. }
  62. const colorVal = getObjValue(appColor)
  63. //设置主色调
  64. setElementMainColor(colorVal?.color)
  65. const colorName = colorVal?.name || 'green'
  66. //设置主题
  67. let val = UserTheme.value
  68. if (val === 'auto') {
  69. theme = useOsTheme()
  70. }
  71. if (theme === '') {
  72. theme = val
  73. }
  74. //挂载相关样式
  75. document.documentElement.setAttribute('class', `${theme} color-${colorName}`)
  76. }
  77. </script>