Navbar.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <template>
  2. <div class="navbar">
  3. <hamburger id="hamburger-container" :is-active="appStore.sidebar.opened" class="hamburger-container" @toggle-click="toggleSideBar" />
  4. <breadcrumb v-if="!settingsStore.topNav" id="breadcrumb-container" class="breadcrumb-container" />
  5. <top-nav v-if="settingsStore.topNav" id="topmenu-container" class="topmenu-container" />
  6. <div class="right-menu flex align-center">
  7. <template v-if="appStore.device !== 'mobile'">
  8. <el-select
  9. v-if="userId === 1 && tenantEnabled"
  10. v-model="companyName"
  11. class="min-w-244px"
  12. clearable
  13. filterable
  14. reserve-keyword
  15. :placeholder="$t('navbar.selectTenant')"
  16. @change="dynamicTenantEvent"
  17. @clear="dynamicClearEvent"
  18. >
  19. <el-option v-for="item in tenantList" :key="item.tenantId" :label="item.companyName" :value="item.tenantId"> </el-option>
  20. <template #prefix><svg-icon icon-class="company" class="el-input__icon input-icon" /></template>
  21. </el-select>
  22. <!-- <header-search id="header-search" class="right-menu-item" /> -->
  23. <search-menu ref="searchMenuRef" />
  24. <el-tooltip content="搜索" effect="dark" placement="bottom">
  25. <div class="right-menu-item hover-effect" @click="openSearchMenu">
  26. <svg-icon class-name="search-icon" icon-class="search" />
  27. </div>
  28. </el-tooltip>
  29. <!-- 消息 -->
  30. <el-tooltip :content="$t('navbar.message')" effect="dark" placement="bottom">
  31. <div>
  32. <el-popover placement="bottom" trigger="click" transition="el-zoom-in-top" :width="300" :persistent="false">
  33. <template #reference>
  34. <el-badge :value="newNotice > 0 ? newNotice : ''" :max="99">
  35. <svg-icon icon-class="message" />
  36. </el-badge>
  37. </template>
  38. <template #default>
  39. <notice></notice>
  40. </template>
  41. </el-popover>
  42. </div>
  43. </el-tooltip>
  44. <el-tooltip content="Github" effect="dark" placement="bottom">
  45. <ruo-yi-git id="ruoyi-git" class="right-menu-item hover-effect" />
  46. </el-tooltip>
  47. <el-tooltip :content="$t('navbar.document')" effect="dark" placement="bottom">
  48. <ruo-yi-doc id="ruoyi-doc" class="right-menu-item hover-effect" />
  49. </el-tooltip>
  50. <el-tooltip :content="$t('navbar.full')" effect="dark" placement="bottom">
  51. <screenfull id="screenfull" class="right-menu-item hover-effect" />
  52. </el-tooltip>
  53. <el-tooltip :content="$t('navbar.language')" effect="dark" placement="bottom">
  54. <lang-select id="lang-select" class="right-menu-item hover-effect" />
  55. </el-tooltip>
  56. <el-tooltip :content="$t('navbar.layoutSize')" effect="dark" placement="bottom">
  57. <size-select id="size-select" class="right-menu-item hover-effect" />
  58. </el-tooltip>
  59. </template>
  60. <div class="avatar-container">
  61. <el-dropdown class="right-menu-item hover-effect" trigger="click" @command="handleCommand">
  62. <div class="avatar-wrapper">
  63. <img :src="userStore.avatar" class="user-avatar" />
  64. <el-icon><caret-bottom /></el-icon>
  65. </div>
  66. <template #dropdown>
  67. <el-dropdown-menu>
  68. <router-link v-if="!dynamic" to="/user/profile">
  69. <el-dropdown-item>{{ $t('navbar.personalCenter') }}</el-dropdown-item>
  70. </router-link>
  71. <el-dropdown-item v-if="settingsStore.showSettings" command="setLayout">
  72. <span>{{ $t('navbar.layoutSetting') }}</span>
  73. </el-dropdown-item>
  74. <el-dropdown-item divided command="logout">
  75. <span>{{ $t('navbar.logout') }}</span>
  76. </el-dropdown-item>
  77. </el-dropdown-menu>
  78. </template>
  79. </el-dropdown>
  80. </div>
  81. </div>
  82. </div>
  83. </template>
  84. <script setup lang="ts">
  85. import SearchMenu from './TopBar/search.vue';
  86. import useAppStore from '@/store/modules/app';
  87. import useUserStore from '@/store/modules/user';
  88. import useSettingsStore from '@/store/modules/settings';
  89. import useNoticeStore from '@/store/modules/notice';
  90. import { getTenantList } from '@/api/login';
  91. import { dynamicClear, dynamicTenant } from '@/api/system/tenant';
  92. import { TenantVO } from '@/api/types';
  93. import notice from './notice/index.vue';
  94. const appStore = useAppStore();
  95. const userStore = useUserStore();
  96. const settingsStore = useSettingsStore();
  97. const noticeStore = storeToRefs(useNoticeStore());
  98. const newNotice = ref(<number>0);
  99. const { proxy } = getCurrentInstance() as ComponentInternalInstance;
  100. const userId = ref(userStore.userId);
  101. const companyName = ref(undefined);
  102. const tenantList = ref<TenantVO[]>([]);
  103. // 是否切换了租户
  104. const dynamic = ref(false);
  105. // 租户开关
  106. const tenantEnabled = ref(true);
  107. // 搜索菜单
  108. const searchMenuRef = ref<InstanceType<typeof SearchMenu>>();
  109. const openSearchMenu = () => {
  110. searchMenuRef.value?.openSearch();
  111. };
  112. // 动态切换
  113. const dynamicTenantEvent = async (tenantId: string) => {
  114. if (companyName.value != null && companyName.value !== '') {
  115. await dynamicTenant(tenantId);
  116. dynamic.value = true;
  117. proxy?.$tab.closeAllPage();
  118. proxy?.$router.push('/');
  119. }
  120. };
  121. const dynamicClearEvent = async () => {
  122. await dynamicClear();
  123. dynamic.value = false;
  124. proxy?.$tab.closeAllPage();
  125. proxy?.$router.push('/');
  126. };
  127. /** 租户列表 */
  128. const initTenantList = async () => {
  129. const { data } = await getTenantList();
  130. tenantEnabled.value = data.tenantEnabled === undefined ? true : data.tenantEnabled;
  131. if (tenantEnabled.value) {
  132. tenantList.value = data.voList;
  133. }
  134. };
  135. defineExpose({
  136. initTenantList
  137. });
  138. const toggleSideBar = () => {
  139. appStore.toggleSideBar(false);
  140. };
  141. const logout = async () => {
  142. await ElMessageBox.confirm('确定注销并退出系统吗?', '提示', {
  143. confirmButtonText: '确定',
  144. cancelButtonText: '取消',
  145. type: 'warning'
  146. });
  147. await userStore.logout();
  148. location.href = import.meta.env.VITE_APP_CONTEXT_PATH + 'index';
  149. };
  150. const emits = defineEmits(['setLayout']);
  151. const setLayout = () => {
  152. emits('setLayout');
  153. };
  154. // 定义Command方法对象 通过key直接调用方法
  155. const commandMap: { [key: string]: any } = {
  156. setLayout,
  157. logout
  158. };
  159. const handleCommand = (command: string) => {
  160. // 判断是否存在该方法
  161. if (commandMap[command]) {
  162. commandMap[command]();
  163. }
  164. };
  165. //用深度监听 消息
  166. watch(
  167. () => noticeStore.state.value.notices,
  168. (newVal) => {
  169. newNotice.value = newVal.filter((item: any) => !item.read).length;
  170. },
  171. { deep: true }
  172. );
  173. </script>
  174. <style lang="scss" scoped>
  175. :deep(.el-select .el-input__wrapper) {
  176. height: 30px;
  177. }
  178. :deep(.el-badge__content.is-fixed) {
  179. top: 12px;
  180. }
  181. .flex {
  182. display: flex;
  183. }
  184. .align-center {
  185. align-items: center;
  186. }
  187. .navbar {
  188. height: 50px;
  189. overflow: hidden;
  190. position: relative;
  191. //background: #fff;
  192. box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  193. .hamburger-container {
  194. line-height: 46px;
  195. height: 100%;
  196. float: left;
  197. cursor: pointer;
  198. transition: background 0.3s;
  199. -webkit-tap-highlight-color: transparent;
  200. &:hover {
  201. background: rgba(0, 0, 0, 0.025);
  202. }
  203. }
  204. .breadcrumb-container {
  205. float: left;
  206. }
  207. .topmenu-container {
  208. position: absolute;
  209. left: 50px;
  210. }
  211. .errLog-container {
  212. display: inline-block;
  213. vertical-align: top;
  214. }
  215. .right-menu {
  216. float: right;
  217. height: 100%;
  218. line-height: 50px;
  219. display: flex;
  220. &:focus {
  221. outline: none;
  222. }
  223. .right-menu-item {
  224. display: inline-block;
  225. padding: 0 8px;
  226. height: 100%;
  227. font-size: 18px;
  228. color: #5a5e66;
  229. vertical-align: text-bottom;
  230. &.hover-effect {
  231. cursor: pointer;
  232. transition: background 0.3s;
  233. &:hover {
  234. background: rgba(0, 0, 0, 0.025);
  235. }
  236. }
  237. }
  238. .avatar-container {
  239. margin-right: 40px;
  240. .avatar-wrapper {
  241. margin-top: 5px;
  242. position: relative;
  243. .user-avatar {
  244. cursor: pointer;
  245. width: 40px;
  246. height: 40px;
  247. border-radius: 10px;
  248. margin-top: 10px;
  249. }
  250. i {
  251. cursor: pointer;
  252. position: absolute;
  253. right: -20px;
  254. top: 25px;
  255. font-size: 12px;
  256. }
  257. }
  258. }
  259. }
  260. }
  261. </style>