Navbar.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <template>
  2. <div class="navbar">
  3. <hamburger id="hamburger-container" :is-active="appStore.sidebar.opened" class="hamburger-container" @toggleClick="toggleSideBar" />
  4. <breadcrumb id="breadcrumb-container" class="breadcrumb-container" v-if="!settingsStore.topNav" />
  5. <top-nav id="topmenu-container" class="topmenu-container" v-if="settingsStore.topNav" />
  6. <div class="right-menu flex align-center">
  7. <template v-if="appStore.device !== 'mobile'">
  8. <el-select
  9. v-model="companyName"
  10. clearable
  11. filterable
  12. reserve-keyword
  13. :placeholder="$t('navbar.selectTenant')"
  14. v-if="userId === 1 && tenantEnabled"
  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 @command="handleCommand" class="right-menu-item hover-effect" trigger="click">
  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 to="/user/profile" v-if="!dynamic">
  68. <el-dropdown-item>{{ $t('navbar.personalCenter') }}</el-dropdown-item>
  69. </router-link>
  70. <el-dropdown-item command="setLayout" v-if="settingsStore.showSettings">
  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 { ComponentInternalInstance } from "vue";
  91. import { TenantVO } from "@/api/types";
  92. import notice from './notice/index.vue';
  93. import useNoticeStore from '@/store/modules/notice';
  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(() => noticeStore.state.value.notices, (newVal, oldVal) => {
  167. newNotice.value = newVal.filter((item: any) => !item.read).length;
  168. }, { deep: true });
  169. </script>
  170. <style lang="scss" scoped>
  171. :deep(.el-select .el-input__wrapper) {
  172. height:30px;
  173. }
  174. :deep(.el-badge__content.is-fixed){
  175. top: 12px;
  176. }
  177. .flex {
  178. display: flex;
  179. }
  180. .align-center {
  181. align-items: center;
  182. }
  183. .navbar {
  184. height: 50px;
  185. overflow: hidden;
  186. position: relative;
  187. //background: #fff;
  188. box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  189. .hamburger-container {
  190. line-height: 46px;
  191. height: 100%;
  192. float: left;
  193. cursor: pointer;
  194. transition: background 0.3s;
  195. -webkit-tap-highlight-color: transparent;
  196. &:hover {
  197. background: rgba(0, 0, 0, 0.025);
  198. }
  199. }
  200. .breadcrumb-container {
  201. float: left;
  202. }
  203. .topmenu-container {
  204. position: absolute;
  205. left: 50px;
  206. }
  207. .errLog-container {
  208. display: inline-block;
  209. vertical-align: top;
  210. }
  211. .right-menu {
  212. float: right;
  213. height: 100%;
  214. line-height: 50px;
  215. display: flex;
  216. &:focus {
  217. outline: none;
  218. }
  219. .right-menu-item {
  220. display: inline-block;
  221. padding: 0 8px;
  222. height: 100%;
  223. font-size: 18px;
  224. color: #5a5e66;
  225. vertical-align: text-bottom;
  226. &.hover-effect {
  227. cursor: pointer;
  228. transition: background 0.3s;
  229. &:hover {
  230. background: rgba(0, 0, 0, 0.025);
  231. }
  232. }
  233. }
  234. .avatar-container {
  235. margin-right: 40px;
  236. .avatar-wrapper {
  237. margin-top: 5px;
  238. position: relative;
  239. .user-avatar {
  240. cursor: pointer;
  241. width: 40px;
  242. height: 40px;
  243. border-radius: 10px;
  244. margin-top: 10px;
  245. }
  246. i {
  247. cursor: pointer;
  248. position: absolute;
  249. right: -20px;
  250. top: 25px;
  251. font-size: 12px;
  252. }
  253. }
  254. }
  255. }
  256. }
  257. </style>