Menu.vue 18.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
<template>
  <!-- 目前需求显示两级,代码可实现三级 -->
  <div class="menu glsx-portal-menu">
    <div class="accordion">
      <div class="section" v-for="(item, index) in menuList" :key="item.origin.menuNo">
        <input type="radio" name="accordion-radio" :id="'section-'+index" :checked="item.showSubList" />
        <label @click="titleClick(item, index)">
          <img class="menu-icon" :src="item.menuIcon" :alt="item.origin.menuName">
          <span>{{item.origin.menuName}}</span>
          <!-- :class="{'rotate-deg': item.showSubList}" -->
          <i class="el-icon-arrow-down" :class="{'rotate-deg': item.showSubList}" v-if="item.children && item.children.length > 0"></i>
        </label>
        <template v-if="item.children && item.children.length > 0">
          <div class="accordion-content" v-show="item.showSubList">
            <ul class="accordion-ul">
              <li class="accordion-li" :class="{'is-actived':cItem.subItemActive}" v-for="cItem in item.children" :key="cItem.origin.menuNo" @click.stop="cItemClick(cItem.origin.frontRoute || '', cItem, item)">
                <span>{{cItem.origin.menuName}}</span>
              </li>
            </ul>
          </div>
        </template>
      </div>
      <!-- <div class="section">
                <input type="radio" name="accordion-1" id="section-2" value="toggle"/>
                <label for="section-2"><span>Usage</span></label>
                <div class="accordion-content">
                    <ul class="accordion-ul">
                        <li><i class="fa fa-cog"></i><span>System</span></li>
                        <li><i class="fa fa-group"></i><span>Users    </span></li>
                        <li><i class="fa fa-sitemap"></i><span>Visitation</span></li>
                    </ul>
                </div>
            </div> -->
    </div>
    <!-- <el-menu
            :uniqueOpened="true"
            background-color="#001529"
            text-color="hsla(0,0%,100%,.65)"
            active-text-color="#fff">
            <div v-for="(item, pIndex) in menuList" 
                :key="item.origin.menuNo">
                <el-submenu v-if="item.children && item.children.length > 0" :index="pIndex.toString()">
                    <template #title>
                        <i class="el-icon-tableware"></i>
                        <span>{{item.origin.menuName}}</span>
                    </template>
                    <el-menu-item
                        v-for="(cItem, cIndex) in item.children" 
                        :key="cItem.origin.menuNo"
                        :index="pIndex+'-'+cIndex"
                        @click="cItemClick(cItem.origin.frontRoute || '', cItem, item)">
                        {{cItem.origin.menuName}}
                    </el-menu-item>
                </el-submenu>
                <el-menu-item v-else :index="pIndex.toString()">
                    <template #title>
                        {{item.origin.menuName}}
                    </template>
                </el-menu-item>
            </div>
        </el-menu> -->
    <!-- 三级菜单 原生写法
        <ul class="ant-menu ant-menu-inline ant-menu-sub"
            v-for="thirdChildrenItem in cItem.children"
            :key="thirdChildrenItem.origin.menuNo">
            <li class="ant-menu-item" 
                :class="{'ant-menu-item-selected': thirdChildrenItem.subItemActive}"
                style="padding-left: 72px;"
                @click.stop="subMenuClicked(thirdChildrenItem, cItem, item)">{{thirdChildrenItem.origin.menuName}}</li>
        </ul> -->
  </div>
</template>

<script>
import { reactive, toRefs, onBeforeMount, onMounted, computed,watch } from "vue";
import { useStore } from "vuex";

import MenuService from "services/Menu";
import { menuTree as menuTreeLocalList } from "config/menuTree";
import { Common } from "@/utils/common/common";

export default {
  name: "Menu",
  setup(props, context) {
    const store = useStore();
    const state = reactive({
      menuList: menuTreeLocalList,
      defaultActive: "0-0",
      menuIcon: require("@/assets/images/icon/setting-icon.png"),
    });
    console.log(store.state)
    watch(() => store.state.menu.updateMenu, (n,o) => {
      console.log("监听菜单更新");
      console.log(n);
      if(n){
        //getMenuAuth()
      }
    },{
      immediate:true,
      deep:true
    });

    // 点击主菜单标题
    const titleClick = (item, idx) => {
      console.log(item);
      state.menuList.forEach((i, index) => {
        if (idx !== index) {
          i.showSubList = false;
        }
      });
      item.showSubList = !item.showSubList;
    };

    // 高亮被选中的子集标题
    const setActive = (arr) => {
      arr.forEach((cEle) => {
        cEle.subItemActive = false;
        if (cEle.children) {
          setActive(cEle.children);
        }
      });
    };

    const filterPath = (path) => {
      const { menuPathArr } = store.getters;
      let flag = false;
      menuPathArr.forEach((i) => {
        if (path && path.includes(i)) {
          flag = true;
        }
      });
      return path && flag ? path : "/noData";
    };

    // 点击三级菜单做存储
    const subMenuClicked = (thisItem, parentItem, item) => {
      let showEmpty = false;
      const pOrigin = parentItem.origin,
        thisOrigin = thisItem.origin,
        itemOrigin = item.origin;
      // 高亮被选中的标题
      state.menuList.forEach((ele) => {
        const children = ele.children;
        setActive(children);
      });
      thisItem.subItemActive = true;

      let path = filterPath(thisOrigin.frontRoute);
      if (path === "/noData") {
        showEmpty = true;
      }
      store.dispatch("app/setCommonData", { showEmpty });
      history.pushState(null, path, path);
    };

    // 点击二级菜单标题
    const cItemClick = (path, cItem, parentItem) => {
      console.log('menuList:',store.state.menu.menuList)
      console.log('parentItem:',parentItem)
      console.log('path:',path)
      console.log('cItem:',cItem)
      Common.setBreadCrumbs(path);
      
      const cOrigin = cItem.origin;
      const pOrigin = parentItem.origin;
      let showEmpty = false;
      // 高亮被选中的标题
      state.menuList.forEach((ele) => {
        const children = ele.children;
        setActive(children);
      });
      cItem.subItemActive = true;

      path = filterPath(path);
      if (path === "/noData") {
        showEmpty = true;
      }
      let breadcrumb = [
        {
          name: pOrigin.menuName,
          path: pOrigin.frontRoute,
        },
        {
          name: cOrigin.menuName,
          path: cOrigin.frontRoute,
        },
      ];
      getMenuAuth(cItem.id, cOrigin.menuName);
      store.dispatch("app/setCommonData", { showEmpty });
      store.dispatch("menu/setBreadCrumb", breadcrumb);
      history.pushState({ breadcrumb }, path, path);
    };

    // 获取权限菜单(假数据)
    // const res = {"code":200,"data":[{"children":[{"children":[],"id":20101,"origin":{"menuNo":20101,"menuName":"ODB管理","parentId":201,"type":2,"icon":null,"frontRoute":null,"orderNum":1,"checked":false},"order":1,"depth":null,"parentId":201,"label":"ODB管理"}],"id":201,"origin":{"menuNo":201,"menuName":"设备管理","parentId":0,"type":1,"icon":null,"frontRoute":null,"orderNum":1,"checked":false},"order":1,"depth":null,"parentId":0,"label":"设备管理"},{"children":[{"children":[],"id":30101,"origin":{"menuNo":30101,"menuName":"测试菜单","parentId":301,"type":2,"icon":null,"frontRoute":"bbb","orderNum":1,"checked":false},"order":1,"depth":null,"parentId":301,"label":"测试菜单"}],"id":301,"origin":{"menuNo":301,"menuName":"测试菜单","parentId":0,"type":1,"icon":null,"frontRoute":"bbb","orderNum":1,"checked":false},"order":1,"depth":null,"parentId":0,"label":"测试菜单"},{"children":[{"children":[],"id":10101,"origin":{"menuNo":10101,"menuName":"用户管理","parentId":101,"type":2,"icon":null,"frontRoute":null,"orderNum":1,"checked":false},"order":1,"depth":null,"parentId":101,"label":"用户管理"},{"children":[],"id":10102,"origin":{"menuNo":10102,"menuName":"用户组管理","parentId":101,"type":2,"icon":null,"frontRoute":null,"orderNum":2,"checked":false},"order":2,"depth":null,"parentId":101,"label":"用户组管理"},{"children":[],"id":10103,"origin":{"menuNo":10103,"menuName":"角色管理","parentId":101,"type":2,"icon":null,"frontRoute":null,"orderNum":3,"checked":false},"order":3,"depth":null,"parentId":101,"label":"角色管理"},{"children":[],"id":10104,"origin":{"menuNo":10104,"menuName":"菜单管理","parentId":101,"type":2,"icon":null,"frontRoute":null,"orderNum":4,"checked":false},"order":4,"depth":null,"parentId":101,"label":"菜单管理"},{"children":[],"id":10105,"origin":{"menuNo":10105,"menuName":"组织管理","parentId":101,"type":2,"icon":null,"frontRoute":null,"orderNum":5,"checked":false},"order":5,"depth":null,"parentId":101,"label":"组织管理"},{"children":[{"children":[],"id":1010607,"origin":{"menuNo":1010607,"menuName":"数据字典3","parentId":10106,"type":2,"icon":null,"frontRoute":"bbb","orderNum":1,"checked":false},"order":1,"depth":null,"parentId":10106,"label":"数据字典3"},{"children":[],"id":1010608,"origin":{"menuNo":1010608,"menuName":"数据字典4","parentId":10106,"type":2,"icon":null,"frontRoute":"bbb","orderNum":1,"checked":false},"order":1,"depth":null,"parentId":10106,"label":"数据字典4"},{"children":[],"id":1010609,"origin":{"menuNo":1010609,"menuName":"数据字典5","parentId":10106,"type":2,"icon":null,"frontRoute":"bbb","orderNum":1,"checked":false},"order":1,"depth":null,"parentId":10106,"label":"数据字典5"},{"children":[],"id":1010610,"origin":{"menuNo":1010610,"menuName":"数据字典6","parentId":10106,"type":2,"icon":null,"frontRoute":"bbb","orderNum":1,"checked":false},"order":1,"depth":null,"parentId":10106,"label":"数据字典6"}],"id":10106,"origin":{"menuNo":10106,"menuName":"字典管理","parentId":101,"type":2,"icon":null,"frontRoute":null,"orderNum":6,"checked":false},"order":6,"depth":null,"parentId":101,"label":"字典管理"},{"children":[],"id":10107,"origin":{"menuNo":10107,"menuName":"操作日志","parentId":101,"type":2,"icon":null,"frontRoute":null,"orderNum":7,"checked":false},"order":7,"depth":null,"parentId":101,"label":"操作日志"}],"id":101,"origin":{"menuNo":101,"menuName":"系统管理","parentId":0,"type":1,"icon":null,"frontRoute":null,"orderNum":2,"checked":false},"order":2,"depth":null,"parentId":0,"label":"系统管理"}],"message":"success"};
    // const getNewTree = getMenuTree(res.data, menuTreeLocalList);

    const getMenuAuth = (parentId, menuName) => {
      const menuService = new MenuService();
      menuService.getMenuAuth({ parentId }).then(
        (res) => {
          const { code, data } = res;
          if (code == 200) {
            store.dispatch("menu/setMenuAuth", { menuName, data });
          }
        },
        (err) => {}
      );
    };

    const setMenuIcon = (resData) => {
      const menuObj = {
        系统管理: "setting-icon",
        待办管理: "todo-icon",
        试驾线路管理: "route-icon",
        试驾车辆管理: "car-icon",
        上牌资料管理: "license-icon",
        报表管理: "report-icon",
        警情中心: "alarm-icon",
        特约店管理: "form-icon",
      };
      resData.forEach((item) => {
        item.menuIcon = require(`../../../assets/images/icon/${
          menuObj[item.origin.menuName] || "setting-icon"
        }.png`);
      });
    };

    const sortMenu = (param) => {
      if (param instanceof Array) {
        const arr1 = param.sort((a, b) => a.order - b.order);
        arr1.forEach((item) => {
          if (item.children && item.children.length > 1) {
            sortMenu(item);
          } else {
            item.children.sort((a, b) => a.order - b.order);
          }
        });
      } else if (param instanceof Object) {
        param.children.sort((a, b) => a.order - b.order);
      }
      return param;
    };

    // 获取菜单树接口
    const getServerTree = async (isInit) => {
      const menuService = new MenuService();
      menuService.getMenuTree().then(
        (res) => {
          console.log(res);
          if (res.code === 200) {
            const resData = res.data;
            let curBreadcrumb = localStorage.getItem("breadcrumb");
            // const getNewTree = getMenuTree(res.data, menuTreeLocalList)
            state.menuList = sortMenu(resData);
            console.log(state.menuList)

            // 数据存入state
            store.commit('menu/SET_MENU_LIST',state.menuList)

            // 初始化操作
            let showEmpty = false;

            setMenuIcon(resData);

            // 同步激活样式
            if (curBreadcrumb) {
              curBreadcrumb = JSON.parse(curBreadcrumb);
              resData.forEach((item) => {
                const { menuName: pMenuName } = item.origin;
                item.showSubList = pMenuName === curBreadcrumb[0].name;
                item.children.forEach((cItem, cIndex) => {
                  cItem.subItemActive = false;
                  if (cItem.origin.menuName === curBreadcrumb[1].name) {
                    cItem.subItemActive = true;
                    curBreadcrumb[1].id = cItem.id;
                  }
                });
              });
            }

            if (isInit && curBreadcrumb) {
              let thisBread = curBreadcrumb[curBreadcrumb.length - 1];
              let path = thisBread.path || "/noData";
              let name = thisBread.name || "";
              let id = thisBread.id || "";
              if (!path || path == "/noData") {
                showEmpty = true;
              }
              name && getMenuAuth(id, name);
              history.pushState({ breadcrumb: curBreadcrumb }, path, path);
              store.dispatch("app/setCommonData", { showEmpty });
            } else {
              let firstList = state.menuList[0];
              let pOrigin = firstList.origin;
              let cOrigin = firstList.children[0].origin;
              const breadcrumb = [
                {
                  name: pOrigin.menuName,
                  path: pOrigin.frontRoute,
                },
                {
                  name: cOrigin.menuName,
                  path: cOrigin.frontRoute,
                },
              ];
              if (!cOrigin.frontRoute || cOrigin.frontRoute == "/noData") {
                showEmpty = true;
              }
              firstList.showSubList = true;
              firstList.children[0].subItemActive = true;
              cOrigin.menuName &&
                getMenuAuth(firstList.children[0].id, cOrigin.menuName);
              history.pushState(
                { breadcrumb },
                cOrigin.frontRoute,
                cOrigin.frontRoute
              );
              store.dispatch("menu/setBreadCrumb", breadcrumb);
              store.dispatch("app/setCommonData", { showEmpty });
            }
          }
        },
        (err) => {}
      );
    };

    onBeforeMount(() => {
      getServerTree(true);
    });

    onMounted(() => {
      window.addEventListener(
        "popstate",
        function (e) {
          const { state } = e;
          if (state) {
            setTimeout(() => {
              const { breadcrumb, pushState } = state;
              if (breadcrumb && pushState) {
                store.dispatch("menu/setBreadCrumb", breadcrumb);
              }
            }, 200);
          }
        },
        false
      );
    });

    const updateMenu = computed(() => {
      const updateNow = !!store.getters.updateMenu;
      if (updateNow) {
        getServerTree(false);
      }
      return updateNow;
    });

    return {
      ...toRefs(state),
      titleClick,
      cItemClick,
      subMenuClicked,
      updateMenu,
    };
  },
};
</script>

<style scoped>
.menu >>> .el-submenu.is-opened .el-submenu__title {
  color: #fff !important;
}
</style>
<style lang="less" scoped>
@import "styles/variables.less";

.el-menu-item.is-active {
  background-color: @defaultTextColor !important;
}

.glsx-portal-menu {
  background-color: @defaultThemeColor;
  .accordion {
    color: hsla(0, 0%, 100%, 0.65);
    width: 100%;
    font-size: 14px;
    .menu-icon {
      vertical-align: middle;
    }
    .section {
      width: 100%;
      .accordion-content {
        // padding: 0 15px;
        // max-height: 0;
        // -webkit-transition: all 0.4s;
        // -moz-transition: all 0.4s;
        // -ms-transition: all 0.4s;
        // transition: all 0.4s;
        // opacity: 0;
        // position: relative;
        // overflow-y: hidden;

        &::-webkit-scrollbar {
          display: none;
        }
      }
      input[type="radio"] {
        display: none;

        &:checked + label {
          // background: #363636;
          color: #fff;
        }

        &:checked + label:before {
        }

        &:checked ~ .accordion-content {
          padding: 15px 0;
          //max-height: 300px;
          opacity: 1;
          z-index: 10;
          overflow-y: auto;
        }
      }
      label {
        position: relative;
        cursor: pointer;
        padding: 15px;
        display: table;
        box-sizing: border-box;
        width: 100%;
        -webkit-transition: background 0.3s ease-in-out;
        -moz-transition: background 0.3s ease-in-out;
        -ms-transition: background 0.3s ease-in-out;
        transition: background 0.3s ease-in-out;
        -webkit-user-select: none;
        -moz-user-select: none;
        -ms-user-select: none;
        -o-user-select: none;
        user-select: none;

        &:before {
          content: " ";
          width: 100%;
          position: absolute;
          left: 0;
          top: 0;
          height: 1px;
        }

        &:hover {
          color: #fff;
          // background: @defaultTextColor;
        }
        span {
          display: table-cell;
          vertical-align: middle;
        }
      }
      .accordion-ul {
        width: 100%;
        padding: 0;
        margin: 0;
        list-style: none;

        li {
          padding: 12px 15px 12px 40px;

          &:hover {
            cursor: pointer;
            color: #fff;
          }
          &:hover i {
            color: #fff;
          }
          &.is-actived {
            color: #fff;
            background-color: @defaultTextColor;
          }
        }

        li i {
          font-size: 13px;
          width: 15px;
          margin-right: 15px;
        }
      }
      .el-icon-arrow-down {
        float: right;
        transition: transform 0.3s;

        &.rotate-deg {
          transform: rotate(180deg);
        }
      }
    }
  }
}
</style>