Navbar.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. proxy?.$tab.refreshPage();
  120. }
  121. };
  122. const dynamicClearEvent = async () => {
  123. await dynamicClear();
  124. dynamic.value = false;
  125. proxy?.$tab.closeAllPage();
  126. proxy?.$router.push('/');
  127. proxy?.$tab.refreshPage();
  128. };
  129. /** 租户列表 */
  130. const initTenantList = async () => {
  131. const { data } = await getTenantList();
  132. tenantEnabled.value = data.tenantEnabled === undefined ? true : data.tenantEnabled;
  133. if (tenantEnabled.value) {
  134. tenantList.value = data.voList;
  135. }
  136. };
  137. defineExpose({
  138. initTenantList
  139. });
  140. const toggleSideBar = () => {
  141. appStore.toggleSideBar(false);
  142. };
  143. const logout = async () => {
  144. await ElMessageBox.confirm('确定注销并退出系统吗?', '提示', {
  145. confirmButtonText: '确定',
  146. cancelButtonText: '取消',
  147. type: 'warning'
  148. });
  149. await userStore.logout();
  150. location.href = import.meta.env.VITE_APP_CONTEXT_PATH + 'index';
  151. };
  152. const emits = defineEmits(['setLayout']);
  153. const setLayout = () => {
  154. emits('setLayout');
  155. };
  156. // 定义Command方法对象 通过key直接调用方法
  157. const commandMap: { [key: string]: any } = {
  158. setLayout,
  159. logout
  160. };
  161. const handleCommand = (command: string) => {
  162. // 判断是否存在该方法
  163. if (commandMap[command]) {
  164. commandMap[command]();
  165. }
  166. };
  167. //用深度监听 消息
  168. watch(
  169. () => noticeStore.state.value.notices,
  170. (newVal) => {
  171. newNotice.value = newVal.filter((item: any) => !item.read).length;
  172. },
  173. { deep: true }
  174. );
  175. </script>
  176. <style lang="scss" scoped>
  177. :deep(.el-select .el-input__wrapper) {
  178. height: 30px;
  179. }
  180. :deep(.el-badge__content.is-fixed) {
  181. top: 12px;
  182. }
  183. .flex {
  184. display: flex;
  185. }
  186. .align-center {
  187. align-items: center;
  188. }
  189. .navbar {
  190. height: 50px;
  191. overflow: hidden;
  192. position: relative;
  193. //background: #fff;
  194. box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  195. .hamburger-container {
  196. line-height: 46px;
  197. height: 100%;
  198. float: left;
  199. cursor: pointer;
  200. transition: background 0.3s;
  201. -webkit-tap-highlight-color: transparent;
  202. &:hover {
  203. background: rgba(0, 0, 0, 0.025);
  204. }
  205. }
  206. .breadcrumb-container {
  207. float: left;
  208. }
  209. .topmenu-container {
  210. position: absolute;
  211. left: 50px;
  212. }
  213. .errLog-container {
  214. display: inline-block;
  215. vertical-align: top;
  216. }
  217. .right-menu {
  218. float: right;
  219. height: 100%;
  220. line-height: 50px;
  221. display: flex;
  222. &:focus {
  223. outline: none;
  224. }
  225. .right-menu-item {
  226. display: inline-block;
  227. padding: 0 8px;
  228. height: 100%;
  229. font-size: 18px;
  230. color: #5a5e66;
  231. vertical-align: text-bottom;
  232. &.hover-effect {
  233. cursor: pointer;
  234. transition: background 0.3s;
  235. &:hover {
  236. background: rgba(0, 0, 0, 0.025);
  237. }
  238. }
  239. }
  240. .avatar-container {
  241. margin-right: 40px;
  242. .avatar-wrapper {
  243. margin-top: 5px;
  244. position: relative;
  245. .user-avatar {
  246. cursor: pointer;
  247. width: 40px;
  248. height: 40px;
  249. border-radius: 10px;
  250. margin-top: 10px;
  251. }
  252. i {
  253. cursor: pointer;
  254. position: absolute;
  255. right: -20px;
  256. top: 25px;
  257. font-size: 12px;
  258. }
  259. }
  260. }
  261. }
  262. }
  263. </style>