App.vue 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <template>
  2. <hc-app-config>
  3. <router-view />
  4. <div class="absolute top-0 w-full text-center" @click="testClick">测试</div>
  5. </hc-app-config>
  6. </template>
  7. <script setup>
  8. import { nextTick, ref, watch } from 'vue'
  9. import { useAppStore } from '~src/store'
  10. import { detectionBrowser, getAppVersion, useOsTheme } from 'hc-vue3-ui'
  11. import { getObjValue, setElementMainColor } from 'js-fast-way'
  12. import website from '~src/config'
  13. //初始变量
  14. const appStore = useAppStore()
  15. const UserTheme = ref(appStore.getTheme)
  16. //监听
  17. watch(() => [
  18. appStore.getTheme,
  19. appStore.getThemeVal,
  20. appStore.getColor,
  21. ], ([Theme, ThemeVal, ColorVal]) => {
  22. UserTheme.value = Theme
  23. setUserTheme(ThemeVal, ColorVal)
  24. })
  25. const testClick = () => {
  26. navigator.usb.requestDevice({ filters: [{}] }).then(device => {
  27. console.log(device)
  28. // 设备已授权
  29. navigator.usb.getDevices().then(devices => {
  30. console.log(devices)
  31. })
  32. }).catch(error => {
  33. console.error('设备选择失败', error)
  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>