App.vue 2.0 KB

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