App.vue 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <template>
  2. <HcAppConfig>
  3. <router-view/>
  4. </HcAppConfig>
  5. </template>
  6. <script setup>
  7. import {nextTick, ref, watch} from "vue";
  8. import {useAppStore} from "~src/store";
  9. import {useOsTheme} from '~src/plugins/useOsTheme';
  10. import {setElementMainColor, ulog, getObjValue} from "js-fast-way"
  11. import {getStoreValue, setStoreValue} from "~uti/storage";
  12. import {getVersionJson} from "~api/other";
  13. import config from '~src/config/index';
  14. //初始变量
  15. const appStore = useAppStore()
  16. const UserTheme = ref(appStore.getTheme)
  17. //监听
  18. watch(() => [
  19. appStore.getTheme,
  20. appStore.getThemeVal,
  21. appStore.getColor
  22. ], ([Theme, ThemeVal, ColorVal]) => {
  23. UserTheme.value = Theme
  24. setUserTheme(ThemeVal, ColorVal)
  25. })
  26. nextTick(() => {
  27. setUserTheme(appStore.getThemeVal, appStore.getColor)
  28. //检测新版本
  29. setTimeout(() => {
  30. getVersionJsonApi()
  31. }, 1000)
  32. })
  33. //获取版本更新信息
  34. const getVersionJsonApi = async () => {
  35. const cache_version = getStoreValue('version')
  36. const {res} = await getVersionJson()
  37. const version = getObjValue(res)?.value
  38. setStoreValue('version', version)
  39. if (cache_version && cache_version !== version) {
  40. window?.$messageBox?.alert('检测到有新版本更新,请点击更新,或手动刷新网页更新,如果不更新,将无法使用相关功能', '更新提醒', {
  41. showCancelButton: true,
  42. confirmButtonText: '立即更新',
  43. cancelButtonText: '暂不更新',
  44. type: 'warning',
  45. callback: (action) => {
  46. if (action === 'confirm') {
  47. //刷新页面
  48. window.location.reload()
  49. }
  50. }
  51. })
  52. }
  53. }
  54. //设置主题 #0081ff
  55. const setUserTheme = (theme, appColor) => {
  56. const colorVal = getObjValue(appColor)
  57. //设置主色调
  58. setElementMainColor(colorVal?.color)
  59. const colorName = colorVal?.name || 'blue'
  60. //设置主题
  61. let val = UserTheme.value
  62. if (val === 'auto') {
  63. theme = useOsTheme().value;
  64. }
  65. if (theme === '') {
  66. theme = val;
  67. }
  68. //挂载相关样式
  69. document.documentElement.setAttribute('class', `${theme} color-${colorName}`)
  70. }
  71. </script>