index.vue 8.45 KB
<template>
  <div class="menu">
    <el-tree 
      class="menu-tree"
      :data="menuList"
      node-key="id"
      default-expand-all
      :props="defaultProps"
      :expand-on-click-node="false">
      <span :class="['custom-tree-node',{'menu-active': curModuleInfo.subMenuId == data.id}]" slot-scope="{ node, data }"  @click="slectedSubmenu(node, data, 'tree')" >
        <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 : secondLevelOpList" :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: 'childList',
          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: 3
          },
          {
            name: '上方新增同级',
            type: 'add',
            position: 2
          },
          {
            name: '重命名',
            type: 'rename'
          }
        ],
        menuList: [],
        curModuleInfo: {
          menuName: '',
          menuId: '',
          subMenuName: '',
          subMenuId: ''
        },
        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;
      },
      getMenuList(type, menuInfo) {
        this.$apis.discover.getMenuListApi({}).then(res => {
          if(res?.length){
            this.menuList = res
            let menuItem = res[0]
            let subMenuItem = res[0]?.childList[0] || {}
            if(type=='rename') {
              let menuId = menuInfo ? menuInfo.menuId : this.curModuleInfo.menuId
              let subMenuId =  menuInfo ? menuInfo.subMenuId : this.curModuleInfo.subMenuId
              menuItem = res.find(item => item.id == menuId)
              subMenuItem = menuItem?.childList?.find(item => item.id == subMenuId)
              window.localStorage.removeItem('menuInfo')
            }
            this.slectedSubmenu(menuItem, subMenuItem)
          }
        })
      },
      slectedSubmenu(menuItem, subMenuItem = {},type){
        console.log(menuItem, subMenuItem)
        if(type=='tree') {
          if(menuItem.level==1) return
          menuItem = menuItem?.parent?.data || {}
        }
        this.curModuleInfo = {
          menuName: menuItem.name,
          menuId: menuItem.id,
          subMenuName: subMenuItem.name,
          subMenuId: subMenuItem.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;
      .el-tree-node__content{
        padding-left: 0!important;
        .custom-tree-node{
          margin-left: 30px;
          padding-left: 10px;
          &:hover{
            background-color: #F5F7FA;
          }
        }
      }
      .is-current{
        background-color: #fff!important;
      }
      .el-tree-node__expand-icon {
        display: none;
      }
    }
    /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;
    }
  }
}


</style>