Navbar.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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="proxy.$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="proxy.$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="proxy.$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="proxy.$t('navbar.full')" effect="dark" placement="bottom">
  51. <screenfull id="screenfull" class="right-menu-item hover-effect" />
  52. </el-tooltip>
  53. <el-tooltip :content="proxy.$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="proxy.$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>{{ proxy.$t('navbar.personalCenter') }}</el-dropdown-item>
  70. </router-link>
  71. <el-dropdown-item v-if="settingsStore.showSettings" command="setLayout">
  72. <span>{{ proxy.$t('navbar.layoutSetting') }}</span>
  73. </el-dropdown-item>
  74. <el-dropdown-item divided command="logout">
  75. <span>{{ proxy.$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. import router from '@/router';
  95. import {ElMessageBoxOptions} from "element-plus/es/components/message-box/src/message-box.type";
  96. const appStore = useAppStore();
  97. const userStore = useUserStore();
  98. const settingsStore = useSettingsStore();
  99. const noticeStore = storeToRefs(useNoticeStore());
  100. const newNotice = ref(<number>0);
  101. const { proxy } = getCurrentInstance() as ComponentInternalInstance;
  102. const userId = ref(userStore.userId);
  103. const companyName = ref(undefined);
  104. const tenantList = ref<TenantVO[]>([]);
  105. // 是否切换了租户
  106. const dynamic = ref(false);
  107. // 租户开关
  108. const tenantEnabled = ref(true);
  109. // 搜索菜单
  110. const searchMenuRef = ref<InstanceType<typeof SearchMenu>>();
  111. const openSearchMenu = () => {
  112. searchMenuRef.value?.openSearch();
  113. };
  114. // 动态切换
  115. const dynamicTenantEvent = async (tenantId: string) => {
  116. if (companyName.value != null && companyName.value !== '') {
  117. await dynamicTenant(tenantId);
  118. dynamic.value = true;
  119. await proxy?.$router.push('/');
  120. await proxy?.$tab.closeAllPage();
  121. await proxy?.$tab.refreshPage();
  122. }
  123. };
  124. const dynamicClearEvent = async () => {
  125. await dynamicClear();
  126. dynamic.value = false;
  127. await proxy?.$router.push('/');
  128. await proxy?.$tab.closeAllPage();
  129. await proxy?.$tab.refreshPage();
  130. };
  131. /** 租户列表 */
  132. const initTenantList = async () => {
  133. const { data } = await getTenantList(true);
  134. tenantEnabled.value = data.tenantEnabled === undefined ? true : data.tenantEnabled;
  135. if (tenantEnabled.value) {
  136. tenantList.value = data.voList;
  137. }
  138. };
  139. defineExpose({
  140. initTenantList
  141. });
  142. const toggleSideBar = () => {
  143. appStore.toggleSideBar(false);
  144. };
  145. const logout = async () => {
  146. await ElMessageBox.confirm('确定注销并退出系统吗?', '提示', {
  147. confirmButtonText: '确定',
  148. cancelButtonText: '取消',
  149. type: 'warning'
  150. } as ElMessageBoxOptions);
  151. userStore.logout().then(() => {
  152. router.replace({
  153. path: '/login',
  154. query: {
  155. redirect: encodeURIComponent(router.currentRoute.value.fullPath || '/')
  156. }
  157. });
  158. });
  159. };
  160. const emits = defineEmits(['setLayout']);
  161. const setLayout = () => {
  162. emits('setLayout');
  163. };
  164. // 定义Command方法对象 通过key直接调用方法
  165. const commandMap: { [key: string]: any } = {
  166. setLayout,
  167. logout
  168. };
  169. const handleCommand = (command: string) => {
  170. // 判断是否存在该方法
  171. if (commandMap[command]) {
  172. commandMap[command]();
  173. }
  174. };
  175. //用深度监听 消息
  176. watch(
  177. () => noticeStore.state.value.notices,
  178. (newVal) => {
  179. newNotice.value = newVal.filter((item: any) => !item.read).length;
  180. },
  181. { deep: true }
  182. );
  183. </script>
  184. <style lang="scss" scoped>
  185. :deep(.el-select .el-input__wrapper) {
  186. height: 30px;
  187. }
  188. :deep(.el-badge__content.is-fixed) {
  189. top: 12px;
  190. }
  191. .flex {
  192. display: flex;
  193. }
  194. .align-center {
  195. align-items: center;
  196. }
  197. .navbar {
  198. height: 50px;
  199. overflow: hidden;
  200. position: relative;
  201. //background: #fff;
  202. box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  203. .hamburger-container {
  204. line-height: 46px;
  205. height: 100%;
  206. float: left;
  207. cursor: pointer;
  208. transition: background 0.3s;
  209. -webkit-tap-highlight-color: transparent;
  210. &:hover {
  211. background: rgba(0, 0, 0, 0.025);
  212. }
  213. }
  214. .breadcrumb-container {
  215. float: left;
  216. }
  217. .topmenu-container {
  218. position: absolute;
  219. left: 50px;
  220. }
  221. .errLog-container {
  222. display: inline-block;
  223. vertical-align: top;
  224. }
  225. .right-menu {
  226. float: right;
  227. height: 100%;
  228. line-height: 50px;
  229. display: flex;
  230. &:focus {
  231. outline: none;
  232. }
  233. .right-menu-item {
  234. display: inline-block;
  235. padding: 0 8px;
  236. height: 100%;
  237. font-size: 18px;
  238. color: #5a5e66;
  239. vertical-align: text-bottom;
  240. &.hover-effect {
  241. cursor: pointer;
  242. transition: background 0.3s;
  243. &:hover {
  244. background: rgba(0, 0, 0, 0.025);
  245. }
  246. }
  247. }
  248. .avatar-container {
  249. margin-right: 40px;
  250. .avatar-wrapper {
  251. margin-top: 5px;
  252. position: relative;
  253. .user-avatar {
  254. cursor: pointer;
  255. width: 40px;
  256. height: 40px;
  257. border-radius: 10px;
  258. margin-top: 10px;
  259. }
  260. i {
  261. cursor: pointer;
  262. position: absolute;
  263. right: -20px;
  264. top: 25px;
  265. font-size: 12px;
  266. }
  267. }
  268. }
  269. }
  270. }
  271. </style>