index.vue 10.3 KB
<template>
  <div class="menu">
    <el-tree 
      class="menu-tree"
      :data="menuList"
      node-key="id"
      default-expand-all
      :props="defaultProps">
      <span :class="['custom-tree-node',{'menu-active': (!curModuleInfo.thirdMenuId && node.level ==2 && curModuleInfo.secondaryMenuId == data.id) || (curModuleInfo.thirdMenuId== data.id && node.level ==3)}]" slot-scope="{ node, data }"  @click="selectedSubmenu('tree' ,node)" >
        <span>{{ node.label }}</span>
        <span class="son-operate-wrap">
          <img class="operate-btn" @click.stop="showOperateDialog(node, data)" :src="require('@/assets/draggable.png')" />
          <div class="son-operate-dialog" ref="menuOpRef" v-show="boxShow && menuForm.id == data.id">
            <ul>
              <li class="operate-item" v-for="oItem in node.level ==1 ? primaryLevelOpList : node.level ==2 ?  secondLevelOpList : secondLevelOpList.slice(1, 4)" :key="oItem.name" @click.stop="addMenu(oItem)">{{ oItem.name }}</li>
            </ul>
          </div>
        </span>
      </span>
    </el-tree>
    <GLDialog :title="title" mode="small" :visible.sync="menuVisible" @onOk="commitMenu" :before-close="closeMenuDialog" @onCancel="closeMenuDialog">
      <div class="menu-dialog-body">
        <el-form ref="menuRef" label-width="154px" :rules="menuRules" :model="menuForm">
          <el-form-item label="模块名称:" prop="name" class="m-t-20">
            <el-input style="width: 284px;" :size="size" type="text" v-model="menuForm.name" placeholder="请输入" :maxlength="menuForm.level==1 && menuForm.type=='rename' ? 6 : 8" show-word-limit></el-input>
          </el-form-item>
        </el-form>
      </div>
      </GLDialog>
  </div>
</template>
  
<script>
  export default {
    name: 'Menu',
    props: {
      name: {
        type: String,
        default: ''
      }
    },
    computed: {
      title() {
        return this.menuForm.type == 'rename' ? '重命名' : this.menuForm.position == '1' ? '新增子级' : '新建菜单'
      }
    },
    data() {
      return {
        menuVisible: false,
        defaultProps: {
          children: 'subList',
          label: 'name'
        },
        menuForm: {
          id: '',
          name: '',
          position: '',
          sceneCode: '',
          type: '',
          level: ''
        },
        menuRules: {
          name: [
            {
              required: true,
              validator: (rule, value, callback) => {
                if (!value || (value && !value.trim())) {
                  return callback(new Error('请输入菜单名称'))
                } else {
                  return callback()
                }
              },
              trigger: 'change'
            }
          ]
        },
        primaryLevelOpList: [
          {
            name: '新增子级',
            type: 'add',
            position: 1
          },
          {
            name: '重命名',
            type: 'rename'
          }
        ],
        secondLevelOpList:[
          {
            name: '新增子级',
            type: 'add',
            position: 1
          },
          {
            name: '下方新增同级',
            type: 'add',
            position: 3
          },
          {
            name: '上方新增同级',
            type: 'add',
            position: 2
          },
          {
            name: '重命名',
            type: 'rename'
          }
        ],
        menuList: [],
        curModuleInfo: {
          menuName: '',
          menuId: '',
          secondaryMenuName: '',
          secondaryMenuId: '',
          thirdMenuName: '',
          thirdMenuId: '',
        },
        boxShow: false
      }
    },
    created(){
      const menuInfo = window.localStorage.getItem('menuInfo') //判断是否是新增产品进来,不切换二级目录
      if(menuInfo){
        this.getMenuList('rename', JSON.parse(menuInfo))
      }else{
        this.getMenuList()
      }
    },
    mounted() {
      document.addEventListener("click", this.eventListener);
    },
    beforeDestroy(){
      document.removeEventListener("click", this.eventListener);
    },
    methods: {
      eventListener(e){
        if (!this.$refs?.menuOpRef?.[0]?.contains(e.target)) this.boxShow = false;
      },
      allowDrop(draggingNode, dropNode, type) {
        if (draggingNode.level === dropNode.level) {
            return type === 'prev' || type === 'next'
        } else if (draggingNode.level == 2 && dropNode.level==1) {
            return type === 'inner'
        } else {
            return false
        }
      },
      getMenuList(type, menuInfo) {
        this.$apis.discover.getMenuListApi({}).then(res => {
          if(res?.length){
            this.menuList = res

            let menuItem = res[0] || {}
            let secondaryMenuItem = menuItem?.subList[0] || {}
            let thirdMenuItem = secondaryMenuItem?.subList[0] || {}
            if(type=='rename') {
              let curItem = menuInfo ? menuInfo : this.curModuleInfo
              const {menuId, secondaryMenuId, thirdMenuId} = curItem
              menuItem = res.find(item => item.id == menuId)
              secondaryMenuItem = menuItem?.subList?.find(item => item.id == secondaryMenuId)
              thirdMenuItem = secondaryMenuItem?.subList?.find(item => item.id == thirdMenuId)
              window.localStorage.removeItem('menuInfo')
            }
            // else{
            //   res.forEach(item => {
            //     item.subList.forEach(ele => {
            //       if(ele.subList?.length){
            //         menuItem = item
            //         secondaryMenuItem = ele
            //         thirdMenuItem = ele.subList[0]
            //       }
            //     })
            //   })
            // }
            this.selectedSubmenu('', menuItem, secondaryMenuItem, thirdMenuItem)
          }
        })
      },
      selectedSubmenu(type, menuItem={}, secondaryMenuItem={}, thirdMenuItem={}){
        this.boxShow = false
        let firstMenuItem = menuItem
        if(type=='tree') {
          if(menuItem.level==1) return
          if(menuItem.level==2) {
            if(menuItem.data.subList?.length) return;
            firstMenuItem = menuItem?.parent?.data || {}
            secondaryMenuItem = menuItem.data
          }else{
            firstMenuItem = menuItem?.parent?.parent?.data || {}
            secondaryMenuItem = menuItem?.parent?.data || {}
            thirdMenuItem = menuItem.data
          }
        }
        this.curModuleInfo = {
          menuName: firstMenuItem.name,
          menuId: firstMenuItem.id,
          secondaryMenuName: secondaryMenuItem.name,
          secondaryMenuId: secondaryMenuItem.id,
          thirdMenuName: thirdMenuItem.name || '',
          thirdMenuId: thirdMenuItem.id || '',
        }
        this.$parent.handleSearch();
      },
      addMenu(item){
        const { position, type } = item 
        this.menuForm = {...this.menuForm, position, type}
        if(type=='add') this.menuForm.name = ''
        this.menuVisible = true
        this.$nextTick(()=>{
          this.$refs?.menuRef?.clearValidate();
        })
      },
      commitMenu(){
        this.$refs.menuRef.validate(valid => {
          if(valid){
            const { id,  name, position , sceneCode , type } = this.menuForm
            let params = {}
            if(type=='add'){
              params = { name, position, sceneCode }
              if(position==1) params.parentMenuId = id
              if(position==2 || position==3) params.menuId = id
            }else{
              params = { id, name }
            }
            const api = params.id ? 'updateMenuApi' : 'addMenuApi'
            this.$apis.discover[api](params).then(res => {
              if(res){
                this.menuVisible = false
                this.$message.success(`${params.id ? '修改成功' : '新增成功'}`)
                this.getMenuList('rename')
              }
            })
          }
        })
      },
      showOperateDialog(node, item){
        this.boxShow = true
        const {id, sceneCode, name} = item
        this.menuForm = {...this.menuForm, id, sceneCode, name, level: node.level}
      },
      closeMenuDialog(){
        this.menuVisible = false
      }
    }
  }
</script>
<style lang="scss" scoped>
.menu{
  padding: 18px 4px 18px 16px;
  width: 260px;
  box-sizing: border-box;
  .menu-tree{
    color: #333333;
    font-size: 14px;
    /deep/ .el-tree-node__content{
      height: 28px;
      line-height: 28px;
      .el-tree-node__expand-icon {
        color: #333333;
      }
      .custom-tree-node{
        width: 100%;
        display: flex;
        justify-content: space-between;
        .son-operate-wrap{
          position: relative;
          .operate-btn{
            height: 16px;
            width: 16px;
            vertical-align: middle;
            color: #5850FF;
            cursor: pointer;
          }
          .son-operate-dialog{
            position: absolute;
            background-color: #fff;
            top: 80%;
            right: 4px;
            box-shadow: 0 3px 6px -4px #0000001f, 0 6px 16px 0 #00000014, 0 9px 28px 8px #0000000d;
            z-index: 999;
            .operate-item{
              height: 36px;
              line-height: 36px;
              padding: 0 20px;
              font-size: 14px;
              color: #666666;
              list-style: none;
              &:hover{
                background: #F2F1FF;
              }
            }
          }
        }
      }
      .menu-active{
        background: #F2F1FF;
      }
    }
    /deep/ .el-tree-node__children{
      overflow: unset;
      .is-leaf::before {
        // 隐藏或加透明都可以
        // display: none;
        opacity: 0;
      }
      .el-tree-node__content{
        // padding-left: 0!important;
        .custom-tree-node{
          &:hover{
            background-color: #F5F7FA;
          }
        }
      }
      .is-current{
        background-color: #fff!important;
      }
    }
    /deep/ .el-tree-node.is-current > .el-tree-node__content {
      background-color: #fff !important;
    }
    /deep/ .el-tree-node__content:hover, .el-upload-list__item:hover{
      background-color: #fff !important;
    }
  }
}

/deep/ .menu-tree>.el-tree-node>.el-tree-node__children>.el-tree-node>.el-tree-node__children>.el-tree-node{
  // .el-tree-node__expand-icon {
  //   display: none;
  // }
  .custom-tree-node{
    &:hover{
      background-color: #F5F7FA;
    }
  }
}


</style>