Navbar.vue 8.3 KB

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