Navbar.vue 8.2 KB

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