index.vue 8.68 KB
<template>
    <div class="menu">
        <div v-for="(item,index) in menuList" :key="index">
          <!-- {'menu-active': menuForm.id == item.id} -->
            <div :class="['primary-level']">
              <span>
                <i class="el-icon-caret-bottom icon-btn" v-if="item.expansion"></i>
                <i class="el-icon-caret-right icon-btn" v-else></i>
                {{item.name}}
              </span>
              <div class="son-operate-wrap" ref="menuOpRef" v-show="boxShow && menuForm.id == item.id">
                <ul>
                  <li class="operate-item" v-for="oItem in primaryLevelOpList" :key="oItem.name" @click.stop="addMenu(oItem)">{{ oItem.name }}</li>
                </ul>
              </div>
              <span>
                <img class="operate-btn" @click.stop="showOperateDialog(1, item)" :src="require('@/assets/draggable.png')" />
                <!-- <i class="el-icon-edit-outline operate-btn"></i> -->
              </span>
            </div>
            <div class="second-level">
                <div :class="['level', {'menu-active': curModuleInfo.subMenuId == ele.id}]" @click="slectedSubmenu(item, ele)"  v-for="(ele,idx) in item.childList" :key="idx">
                  <span>{{ele.name}}</span>
                  <span>
                    <img class="operate-btn" @click.stop="showOperateDialog(2, ele)" :src="require('@/assets/draggable.png')" />
                  </span>
                  <div class="son-operate-wrap" ref="subMenuOpRef" v-show="boxShow && menuForm.id == ele.id">
                      <ul>
                        <li class="operate-item" v-for="oItem in secondLevelOpList" :key="oItem.name" @click.stop="addMenu(oItem)">{{ oItem.name }}</li>
                      </ul>
                  </div>
                </div>
            </div>
        </div>
        <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,
        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 = {}){
        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(level, item){
        this.boxShow = true
        const {id, sceneCode, name} = item
        this.menuForm = {...this.menuForm, id, sceneCode, name, level}
      },
      closeMenuDialog(){
        this.menuVisible = false
      }
    }
  }
</script>
<style lang="scss" scoped>
.menu{
  padding: 18px 4px 18px 16px;
  width: 260px;
  box-sizing: border-box;
  .menu-dialog-body{
    height: 150px;
    display: flex;
    align-items: center;
  }
  .son-operate-wrap{
    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: 2;
    .operate-item{
      height: 36px;
      line-height: 36px;
      padding: 0 20px;
      font-size: 14px;
      color: #666666;
      list-style: none;
      &:hover{
        background: #F2F1FF;
      }
    }
  }
  
  .operate-btn{
    height: 16px;
    width: 16px;
    vertical-align: middle;
    color: #5850FF;
    cursor: pointer;
    &:first-child{
      margin-right: 13px;
    }
  }
  .menu-active{
    background: #F2F1FF;
  }
  .primary-level{
    height: 28px;
    line-height: 28px;
    color: #333333;
    text-align: left;
    display: flex;
    justify-content: space-between;
    font-size: 14px;
    font-family: PingFangSC-Regular;
    cursor: default;
    position: relative;
    &:hover{
      background: #F2F1FF;
    }
    .son-operate-wrap{
      right: 4px;
    }
    .icon-btn{
      cursor: pointer;
    }
  }
  .second-level{
    margin-left: 32px;
    .level{
      padding-left: 8px;
      height: 32px;
      line-height: 32px;
      font-size: 14px;
      color: #333333;
      cursor: default;
      position: relative;
      display: flex;
      justify-content: space-between;
      &:hover{
        background: #F2F1FF;
      }
    }
  }
}
</style>