header-bar.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <template>
  2. <div id="main" class="header-bar" @click="isChildMenu = false">
  3. <header>
  4. <ul class="nav">
  5. <div class="logo" style="display: flex; align-items: center; justify-content: center">
  6. <img :src="'/img/img_logo.png'" style="height: 46px" alt="" />
  7. </div>
  8. <li class="header-bar-hover-warp nav-li" :class="isChildMenu ? 'active' : ''">
  9. <div @click.stop="isChildMenu = !isChildMenu" class="menu-modal">
  10. <i class="iconfont icon-icomx_gongndh" style="margin: 0 5px 0 0"></i>
  11. {{ $t("header.functionGuide") }}
  12. <i class="iconfont icon-iconm_xialan1" style="margin: 0 0 0 3px"></i>
  13. </div>
  14. </li>
  15. <div class="auto-list">
  16. <li class="nav-li" @click="commonsBannerToRouter(i)" v-for="(i, index) in commonsRouterList" v-show="index < autoListChidrenNum - 1" :key="i.id">
  17. <span>{{ i.menuName }}</span>
  18. </li>
  19. <li class="nav-li more-box" v-show="commonsRouterList.length > autoListChidrenNum - 1">
  20. {{ $t("header.more") }}
  21. <i class="iconfont icon-iconm_xialan1"></i>
  22. <ul class="more-list">
  23. <li v-for="(i, index) in commonsRouterList" v-show="index >= autoListChidrenNum - 1" :key="i.id">
  24. <span @click="commonsBannerToRouter(i)">{{ i.menuName }}</span>
  25. </li>
  26. </ul>
  27. </li>
  28. </div>
  29. <div class="header-bar-warp" v-if="isChildMenu" @click.stop="isChildMenu = false">
  30. <div class="header-bar-hover" @click.stop>
  31. <div class="header-bar-hover-content">
  32. <div class="left-banner">
  33. <div class="first-order" @click="leftBanerType = 2">
  34. <i class="iconfont icon-iconm_changycd" style="position: relative; top: -1px"></i>
  35. {{ $t("header.commonFunctions") }}
  36. </div>
  37. <div class="first-order">
  38. <i class="iconfont icon-iconm_gongncd" style="position: relative; top: -1px"></i>
  39. {{ $t("header.functionMenu") }}
  40. </div>
  41. <ul>
  42. <li
  43. :class="menuName == i.menuName ? 'active' : ''"
  44. @click="openLeftBaner(i, index)"
  45. v-for="(i, index) in sidebarRoutersCopy"
  46. :key="i.name"
  47. v-show="i.type == 1 && i.status == '0'">
  48. {{ i.menuName }}
  49. </li>
  50. </ul>
  51. </div>
  52. <div class="menu-warp" v-show="leftBanerType == 1">
  53. <div class="first-order-title">{{ menuName }}</div>
  54. <el-row>
  55. <el-col :span="3" v-for="(item, index) in activeLeftData" :key="index">
  56. <li class="menu-title">
  57. <a v-if="item.icon && item.icon !== '#'" :class="'iconfont icon-' + item.icon" style="margin-right: 4px"></a>
  58. <span>{{ item.menuName }}</span>
  59. </li>
  60. <div v-for="(itemChild, key) in item.children" :key="key">
  61. <li class="menu-ul" @click="commonsBannerToRouter(itemChild)" style="cursor: pointer">
  62. <span>{{ itemChild.menuName }}</span>
  63. </li>
  64. </div>
  65. </el-col>
  66. </el-row>
  67. </div>
  68. <div class="menu-warp" v-show="leftBanerType == 2">
  69. <div class="first-order-title">{{ $t("header.commonFunctions") }}</div>
  70. <div class="commons-warp">
  71. <div>
  72. <div class="header-button-box cp" v-for="(i, index) in commonsRouterList" :key="i.name" @click="commonsBannerToRouter(i)">
  73. <span>{{ i.menuName }}</span>
  74. <div class="right-icon" @click.stop="deleteCommonsRouter(i, index)">
  75. <el-icon color="#BBBBBB" v-show="isEidtType">
  76. <Close />
  77. </el-icon>
  78. </div>
  79. </div>
  80. <div class="header-add-button-box" v-if="!isEidtType" @click="isEidtType = true">
  81. <el-icon color="#46A6FF">
  82. <CirclePlus />
  83. </el-icon>
  84. {{ $t("header.edit") }}
  85. </div>
  86. </div>
  87. </div>
  88. <div class="first-order-title" v-if="isEidtType">{{ $t("header.toAdd") }}</div>
  89. <div class="all-menu-warp" v-if="isEidtType">
  90. <div v-for="(i, index) in sidebarRoutersCopy" :key="i.name" v-show="i.type === 1">
  91. <div class="all-menu-title" v-for="(j, jindex) in i.children" :key="j.name">
  92. <div class="title">
  93. {{ j.menuName }}
  94. </div>
  95. <div class="all-menu-lists">
  96. <div class="header-button-box" v-for="(n, nindex) in j.children" v-show="n.visible == '0'" :key="n.name">
  97. <span>{{ n.menuName }}</span>
  98. <div class="right-icon" @click="addCommonsRouter(index, jindex, nindex, n)">
  99. <el-icon color="#46A6FF">
  100. <CirclePlus />
  101. </el-icon>
  102. </div>
  103. </div>
  104. </div>
  105. </div>
  106. </div>
  107. </div>
  108. <div class="btn-warp" v-if="isEidtType">
  109. <el-button type="" @click="isEidtType = false">取消</el-button>
  110. <el-button type="primary" @click="userMenuEdit">保存</el-button>
  111. </div>
  112. </div>
  113. </div>
  114. </div>
  115. </div>
  116. </ul>
  117. <div class="fr">
  118. <el-dropdown @command="handleCommand" class="right-menu-item hover-effect" trigger="click">
  119. <div class="dropdown-box">
  120. {{ userStore.user.nickName }}
  121. </div>
  122. <template #dropdown>
  123. <el-dropdown-menu>
  124. <router-link to="/user/profile">
  125. <el-dropdown-item>{{ $t("header.personalCenter") }}</el-dropdown-item>
  126. </router-link>
  127. <el-dropdown-item command="setLayout">
  128. <span>{{ $t("header.layoutSettings") }}</span>
  129. </el-dropdown-item>
  130. <el-dropdown-item divided command="logout">
  131. <span>{{ $t("header.logout") }}</span>
  132. </el-dropdown-item>
  133. </el-dropdown-menu>
  134. </template>
  135. </el-dropdown>
  136. </div>
  137. </header>
  138. </div>
  139. </template>
  140. <script setup>
  141. import { ElMessageBox, ElMessage } from "element-plus";
  142. import useUserStore from "/src/store/modules/user";
  143. import "@/components/headerBar/header.scss";
  144. import { useRouter } from "vue-router";
  145. const router = useRouter();
  146. const userStore = useUserStore();
  147. const { proxy } = getCurrentInstance();
  148. const sidebarRoutersCopy = ref([]);
  149. const isChildMenu = ref(false);
  150. const autoListChidrenNum = ref(0);
  151. const isEidtType = ref(false);
  152. const leftBanerType = ref(2);
  153. const commonsRouterList = ref([]);
  154. const activeLeftData = ref({});
  155. const openLeftBaner = (i) => {
  156. leftBanerType.value = 1;
  157. menuName.value = i.menuName;
  158. activeLeftData.value = i.children;
  159. };
  160. let menuName = ref("");
  161. const userMenuEdit = () => {
  162. proxy
  163. .post("/sysUserMenu/edit", {
  164. type: 1,
  165. menuIdList: commonsRouterList.value.map((item) => item.menuId),
  166. })
  167. .then((res) => {
  168. ElMessage({
  169. message: "保存成功",
  170. type: "success",
  171. });
  172. isEidtType.value = false;
  173. });
  174. };
  175. const deleteCommonsRouter = (i, index) => {
  176. commonsRouterList.value.splice(index, 1);
  177. };
  178. const addCommonsRouter = (index, jindex, nindex, n) => {
  179. for (let i = 0; i < commonsRouterList.value.length; i++) {
  180. const element = commonsRouterList.value[i];
  181. if (element.menuId === n.menuId) {
  182. ElMessage({
  183. message: proxy.t("header.thisDirectoryHasBeenAdded"),
  184. type: "error",
  185. });
  186. return;
  187. }
  188. }
  189. if (n.hidden) {
  190. ElMessage({
  191. message: proxy.t("header.thisDirectoryRequiresAdditionalParameters"),
  192. type: "error",
  193. });
  194. return;
  195. }
  196. commonsRouterList.value.push(n);
  197. sidebarRoutersCopy.value[index].children[jindex].children[nindex].isCommonsBanner = true;
  198. };
  199. function handleCommand(command) {
  200. switch (command) {
  201. case "setLayout":
  202. setLayout();
  203. break;
  204. case "logout":
  205. logout();
  206. break;
  207. default:
  208. break;
  209. }
  210. }
  211. function logout() {
  212. ElMessageBox.confirm(proxy.t("header.areYouSureYouWantToLogOutAndExitTheSystem"), proxy.t("common.prompt"), {
  213. confirmButtonText: proxy.t("common.confirm"),
  214. cancelButtonText: proxy.t("common.cancel"),
  215. type: "warning",
  216. })
  217. .then(() => {
  218. userStore.logOut().then(() => {
  219. location.href = "/index";
  220. sessionStorage.removeItem("tags-view_sd");
  221. });
  222. })
  223. .catch(() => {});
  224. }
  225. //使用id计算拼接url
  226. const commonsBannerToRouter = (i) => {
  227. sidebarRoutersCopy.value.map((item) => {
  228. if (item.children) {
  229. item.children.map((j) => {
  230. if (j.children) {
  231. j.children.map((n) => {
  232. if (n.menuId === i.menuId) {
  233. router.push("/" + item.path + "/" + j.path + "/" + n.path);
  234. isChildMenu.value = false;
  235. }
  236. });
  237. }
  238. });
  239. }
  240. });
  241. };
  242. //获取浏览器宽度
  243. function getBrowserWidth() {
  244. return document.documentElement.clientWidth;
  245. }
  246. //计算auto-list 能放下几个
  247. function calcAutoListChidren() {
  248. autoListChidrenNum.value = Math.floor((getBrowserWidth() - 616) / 120);
  249. }
  250. //一维数组转tree
  251. onMounted(() => {
  252. calcAutoListChidren();
  253. window.addEventListener("resize", calcAutoListChidren);
  254. proxy.get("/system/menu/list").then((res) => {
  255. res.data = res.data.map((item) => {
  256. return {
  257. ...item,
  258. isCommonsBanner: false,
  259. };
  260. });
  261. sidebarRoutersCopy.value = proxy.handleTree(res.data, "menuId");
  262. //循环删除 i.status != 0 || i.visible != 0 的元素
  263. sidebarRoutersCopy.value.map((item) => {
  264. if (item.children) {
  265. item.children.map((j) => {
  266. if (j.children) {
  267. j.children.map((n) => {
  268. if (n.status != 0 || n.visible != 0 || n.type != 1) {
  269. j.children.splice(j.children.indexOf(n), 1);
  270. }
  271. });
  272. }
  273. });
  274. }
  275. });
  276. });
  277. proxy.post("/sysUserMenu/list", { type: 1 }).then((res) => {
  278. commonsRouterList.value = res;
  279. });
  280. });
  281. </script>
  282. <style lang="scss"></style>