menus.vue 2.0 KB
Newer Older
1 2 3 4 5 6 7 8 9
<template>
  <div>
    <div class="clearflex">
      <el-button @click="relation" class="fl-right" size="small" type="primary">确 定</el-button>
    </div>
    <el-tree
      :data="menuTreeData"
      :default-checked-keys="menuTreeIds"
      :props="menuDefaultProps"
10
      @check="nodeChange"
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
      default-expand-all
      highlight-current
      node-key="ID"
      ref="menuTree"
      show-checkbox
    ></el-tree>
  </div>
</template>
<script>
import { getBaseMenuTree, getMenuAuthority, addMenuAuthority } from '@/api/menu'

export default {
  name: 'Menus',
  props: {
    row: {
      default: function() {
        return {}
      },
      type: Object
    }
  },
  data() {
    return {
      menuTreeData: [],
      menuTreeIds: [],
36
      needConfirm:false,
37 38
      menuDefaultProps: {
        children: 'children',
39 40 41
        label: function(data){
          return data.meta.title
        }
42 43 44 45
      }
    }
  },
  methods: {
46 47 48 49 50 51 52
    nodeChange(){
      this.needConfirm = true
    },
    // 暴露给外层使用的切换拦截统一方法
    enterAndNext(){
      this.relation()
    },
53 54 55 56 57 58 59
    // 关联树 确认方法
    async relation() {
      const checkArr = this.$refs.menuTree.getCheckedNodes(false, true)
      const res = await addMenuAuthority({
        menus: checkArr,
        authorityId: this.row.authorityId
      })
60
      if (res.code == 0) {
61 62
        this.$message({
          type: 'success',
63
          message: '菜单设置成功!'
64 65 66 67 68 69 70 71 72 73 74 75 76 77
        })
      }
    }
  },
  async created() {
    // 获取所有菜单树
    const res = await getBaseMenuTree()
    this.menuTreeData = res.data.menus

    const res1 = await getMenuAuthority({ authorityId: this.row.authorityId })
    const menus = res1.data.menus
    const arr = []
    menus.map(item => {
      // 防止直接选中父级造成全选
Mr.奇淼('s avatar
Mr.奇淼( 已提交
78 79
      if (!menus.some(same => same.parentId === item.menuId)) {
        arr.push(Number(item.menuId))
80 81 82 83 84 85 86 87
      }
    })
    this.menuTreeIds = arr
  }
}
</script>
<style lang="scss">
</style>