index.vue 6.02 KB
Newer Older
耿迪迪's avatar
耿迪迪 committed
1 2 3 4 5 6 7
<template>
  <el-menu
    :default-active="activeMenu"
    mode="horizontal"
    @select="handleSelect"
  >
    <template v-for="(item, index) in topMenus">
yaqizhang's avatar
yaqizhang committed
8
      <!-- <el-menu-item :style="{'--theme': theme}" :index="item.path" :key="index" v-if="index < visibleNumber" -->
9 10 11 12 13 14 15 16 17 18 19
      <el-menu-item
        :style=""
        :index="item.path"
        :key="index"
        v-if="index < visibleNumber"
      >
        <!-- 导航栏左侧图标 -->
        <!-- <svg-icon :icon-class="item.meta.icon" /> -->
        {{ item.meta.title }}
      </el-menu-item>
      <!-- <div class="menu-item-div"></div> -->
耿迪迪's avatar
耿迪迪 committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
    </template>

    <!-- 顶部菜单超出数量折叠 -->
    <el-submenu index="more" v-if="topMenus.length > visibleNumber">
      <template slot="title">更多菜单</template>
      <template v-for="(item, index) in topMenus">
        <el-menu-item
          :index="item.path"
          :key="index"
          v-if="index >= visibleNumber"
          ><svg-icon :icon-class="item.meta.icon" />
          {{ item.meta.title }}</el-menu-item
        >
      </template>
    </el-submenu>
  </el-menu>
</template>

<script>
import { constantRoutes } from "@/router";
40
import "../../common/font/font.css";
耿迪迪's avatar
耿迪迪 committed
41 42 43 44 45 46 47 48
export default {
  data() {
    return {
      // 顶部栏初始数
      visibleNumber: 5,
      // 是否为首次加载
      isFrist: false,
      // 当前激活菜单的 index
49
      currentIndex: undefined,
耿迪迪's avatar
耿迪迪 committed
50 51 52 53 54 55 56 57 58 59 60 61 62
    };
  },
  computed: {
    theme() {
      return this.$store.state.settings.theme;
    },
    // 顶部显示菜单
    topMenus() {
      let topMenus = [];
      this.routers.map((menu) => {
        if (menu.hidden !== true) {
          // 兼容顶部栏一级菜单内部跳转
          if (menu.path === "/") {
63
            topMenus.push(menu.children[0]);
耿迪迪's avatar
耿迪迪 committed
64
          } else {
65
            topMenus.push(menu);
耿迪迪's avatar
耿迪迪 committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
          }
        }
      });
      return topMenus;
    },
    // 所有的路由信息
    routers() {
      return this.$store.state.permission.topbarRouters;
    },
    // 设置子路由
    childrenMenus() {
      var childrenMenus = [];
      this.routers.map((router) => {
        for (var item in router.children) {
          if (router.children[item].parentPath === undefined) {
81 82 83
            if (router.path === "/") {
              router.children[item].path =
                "/redirect/" + router.children[item].path;
耿迪迪's avatar
耿迪迪 committed
84
            } else {
85 86 87 88
              if (!this.ishttp(router.children[item].path)) {
                router.children[item].path =
                  router.path + "/" + router.children[item].path;
              }
耿迪迪's avatar
耿迪迪 committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
            }
            router.children[item].parentPath = router.path;
          }
          childrenMenus.push(router.children[item]);
        }
      });
      return constantRoutes.concat(childrenMenus);
    },
    // 默认激活的菜单
    activeMenu() {
      const path = this.$route.path;
      let activePath = this.routers[0].path;
      if (path.lastIndexOf("/") > 0) {
        const tmpPath = path.substring(1, path.length);
        activePath = "/" + tmpPath.substring(0, tmpPath.indexOf("/"));
      } else if ("/index" == path || "" == path) {
        if (!this.isFrist) {
          this.isFrist = true;
        } else {
          activePath = "index";
        }
      }
      var routes = this.activeRoutes(activePath);
      if (routes.length === 0) {
113
        activePath = this.currentIndex || this.routers[0].path;
耿迪迪's avatar
耿迪迪 committed
114 115 116 117 118 119
        this.activeRoutes(activePath);
      }
      return activePath;
    },
  },
  beforeMount() {
120
    window.addEventListener("resize", this.setVisibleNumber);
耿迪迪's avatar
耿迪迪 committed
121 122
  },
  beforeDestroy() {
123
    window.removeEventListener("resize", this.setVisibleNumber);
耿迪迪's avatar
耿迪迪 committed
124 125 126 127 128 129 130 131 132 133 134 135
  },
  mounted() {
    this.setVisibleNumber();
  },
  methods: {
    // 根据宽度计算设置显示栏数
    setVisibleNumber() {
      const width = document.body.getBoundingClientRect().width / 3;
      this.visibleNumber = parseInt(width / 85);
    },
    // 菜单选择事件
    handleSelect(key, keyPath) {
136
      console.log(key)
耿迪迪's avatar
耿迪迪 committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
      this.currentIndex = key;
      if (this.ishttp(key)) {
        // http(s):// 路径新窗口打开
        window.open(key, "_blank");
      } else if (key.indexOf("/redirect") !== -1) {
        // /redirect 路径内部打开
        this.$router.push({ path: key.replace("/redirect", "") });
      } else {
        // 显示左侧联动菜单
        this.activeRoutes(key);
      }
    },
    // 当前激活的路由
    activeRoutes(key) {
      var routes = [];
      if (this.childrenMenus && this.childrenMenus.length > 0) {
        this.childrenMenus.map((item) => {
          if (key == item.parentPath || (key == "index" && "" == item.path)) {
            routes.push(item);
          }
        });
      }
159
      if (routes.length > 0) {
耿迪迪's avatar
耿迪迪 committed
160 161 162 163
        this.$store.commit("SET_SIDEBAR_ROUTERS", routes);
      }
      return routes;
    },
164 165 166
    ishttp(url) {
      return url.indexOf("http://") !== -1 || url.indexOf("https://") !== -1;
    },
耿迪迪's avatar
耿迪迪 committed
167 168 169 170 171 172 173
  },
};
</script>

<style lang="scss">
.el-menu--horizontal > .el-menu-item {
  float: left;
yaqizhang's avatar
yaqizhang committed
174 175 176
  height: 80px;
  text-align: center;
  line-height: 80px;
yaqizhang's avatar
yaqizhang committed
177
  margin: 0 20px;
yaqizhang's avatar
yaqizhang committed
178
  color: #fff;
yaqizhang's avatar
yaqizhang committed
179 180 181 182
  font-size: 22px;
  font-family: "pingfang";
  font-weight: 600;
  text-shadow: 0px 2px 3px rgba(0, 0, 0, 0.2);
耿迪迪's avatar
耿迪迪 committed
183 184 185 186
  padding: 0 5px;
}

.el-menu--horizontal > .el-menu-item.is-active {
187
  border-bottom: 3px solid #{"var(--theme)"};
yaqizhang's avatar
yaqizhang committed
188
  /* box-shadow: inset 2px 2px 20px #5CD9D4; */
189
  color: #5cd9d4;
耿迪迪's avatar
耿迪迪 committed
190 191 192 193
}

/* submenu item */
.el-menu--horizontal > .el-submenu .el-submenu__title {
194 195
  height: 50px !important;
  line-height: 50px !important;
耿迪迪's avatar
耿迪迪 committed
196
}
197 198
.el-menu--horizontal > .el-menu-item:not(.is-disabled):hover,
.el-menu--horizontal > .el-menu-item:not(.is-disabled):focus {
yaqizhang's avatar
yaqizhang committed
199 200
  background-color: rgba(0, 0, 0, 0);
  /* opacity: 0; */
yaqizhang's avatar
yaqizhang committed
201 202 203
  /* box-shadow: inset 2px 2px 20px #5CD9D4; */
  color: #fff;
}
204
.menu-item-div {
yaqizhang's avatar
yaqizhang committed
205 206
  width: 2px;
  height: 30px;
207
  margin-top: 25px;
yaqizhang's avatar
yaqizhang committed
208 209
  background-color: rgb(190, 189, 189);
  float: left;
210 211 212 213 214 215
  background: linear-gradient(
    130deg,
    rgba(0, 0, 0, 0) 0%,
    rgba(255, 255, 255, 0.7) 50%,
    rgba(0, 0, 0, 0) 100%
  );
yaqizhang's avatar
yaqizhang committed
216

217 218 219 220
  &:last-child {
    width: 0px;
    height: 0px;
  }
yaqizhang's avatar
yaqizhang committed
221
}
耿迪迪's avatar
耿迪迪 committed
222
</style>