navbar.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <template>
  2. <a-layout-header class="header-mobile" v-if="isMobile" :style="{backgroundColor: headerBgColor}">
  3. <a-row>
  4. <a-col :span="6" >
  5. <search />
  6. </a-col>
  7. <a-col :span="6" class="df-center">
  8. <a-button type="text" @click="changeTheme" > <IconTsx :name="iconName" /></a-button>
  9. </a-col>
  10. <a-col :span="6" class="df-center" >
  11. <user />
  12. </a-col>
  13. </a-row>
  14. </a-layout-header>
  15. <a-layout-header class="header-pc" v-else :style="{backgroundColor: headerBgColor, height: '54px'}">
  16. <a-row style="width: 100%;height: 54px;" justify="start" >
  17. <a-col :span="15" style="height: 54px;" >
  18. <a-menu
  19. mode="horizontal"
  20. :style="{ lineHeight: '54px', border: 'none' , marginLeft: '-20px'}"
  21. :selectedKeys="selectedKeys"
  22. >
  23. <a-menu-item
  24. v-for="route in appRouter.$state.router.navbar.route"
  25. :key="route.path"
  26. @click="changeRouter(route)"
  27. >
  28. {{route.name}}
  29. </a-menu-item>
  30. </a-menu>
  31. </a-col>
  32. <a-col :span="9" style="height: 54px;" >
  33. <a-row :gutter="[8, 8]" justify="end" style="height: 54px;" >
  34. <a-col class="df-center" style="height: 54px;" >
  35. <search />
  36. </a-col>
  37. <a-col class="df-center" style="height: 54px;">
  38. <a-tooltip placement="bottom">
  39. <template #title>
  40. <span>添加网页到桌面</span>
  41. </template>
  42. <a-button type="text" @click="addDeskToApp" > <DesktopOutlined style="font-size: 22px;" /></a-button>
  43. </a-tooltip>
  44. </a-col>
  45. <a-col class="df-center" style="height: 54px;">
  46. <a-button type="text" @click="changeTheme" > <IconTsx :name="iconName" /></a-button>
  47. </a-col>
  48. <a-col class="df-center" style="height: 54px;">
  49. <user v-if="!AppConfig.userInfoHidden" />
  50. </a-col>
  51. </a-row>
  52. </a-col>
  53. </a-row>
  54. </a-layout-header>
  55. </template>
  56. <script lang="ts" setup >
  57. import { computed, ref } from 'vue'
  58. import { useAppRouter } from '@/store/router'
  59. import user from './user.vue'
  60. import { useRoute, useRouter } from 'vue-router'
  61. import { IconTsx } from '@/components/MicroComponents/index'
  62. import { useDesignStore } from '@/store'
  63. import { useDeviceType } from '@/hooks'
  64. import search from './components/search/index.vue'
  65. import { DesktopOutlined } from '@ant-design/icons-vue'
  66. import { message } from 'ant-design-vue'
  67. import AppConfig from 'AppConfig'
  68. import { useIsMicro } from '@/hooks/effect'
  69. const route = useRoute()
  70. const router = useRouter()
  71. const appRouter = useAppRouter()
  72. const selectedKeys = ref<string[]>([route.matched[useIsMicro() ? 1 : 0].path])
  73. const designStore = useDesignStore()
  74. const iconName = computed(() => designStore.theme ? 'sun' : 'moon')
  75. const headerBgColor = computed(() => designStore.theme ? '#141414' : '#fff')
  76. const isMobile = useDeviceType()
  77. const changeTheme = () => designStore.changeModeltheme()
  78. const changeRouter = (route: ROUTER.RoutesProps) => {
  79. selectedKeys.value = [route.path]
  80. appRouter.changeNavbarRoute(route)
  81. }
  82. const addDeskToApp = () => {
  83. if ('beforeinstallprompt' in window) {
  84. // 在支持 PWA 安装的浏览器中,手动触发安装提示
  85. window.beforeinstallprompt.prompt()
  86. window.beforeinstallprompt.userChoice.then((choiceResult) => {
  87. if (choiceResult.outcome === 'accepted') {
  88. console.log('用户已接受安装提示')
  89. } else {
  90. console.log('用户已拒绝安装提示')
  91. }
  92. })
  93. } else {
  94. message.error('该浏览器暂不支持此操作')
  95. }
  96. }
  97. </script>
  98. <style lang="less" scoped >
  99. .header-mobile {
  100. padding: 0 24px;
  101. }
  102. .header-pc {
  103. width: 100%;
  104. position: relative;
  105. height: 54px;
  106. z-index: 20;
  107. // box-shadow: 0 1px 4px rgba(0,21,41,.12);
  108. overflow: hidden;
  109. padding: 0;
  110. padding-right: 32px;
  111. }
  112. .df-center {
  113. display: flex;
  114. justify-content: center;
  115. align-items: center;
  116. }
  117. </style>