apis.vue 2.7 KB
Newer Older
1 2 3 4 5 6 7 8 9
<template>
  <div>
    <div class="clearflex">
      <el-button @click="authApiEnter" class="fl-right" size="small" type="primary">确 定</el-button>
    </div>
    <el-tree
      :data="apiTreeData"
      :default-checked-keys="apiTreeIds"
      :props="apiDefaultProps"
10
      @check="nodeChange"
11 12
      default-expand-all
      highlight-current
13
      node-key="onlyId"
14 15 16 17 18 19 20
      ref="apiTree"
      show-checkbox
    ></el-tree>
  </div>
</template>
<script>
import { getAllApis } from '@/api/api'
R
rainyan 已提交
21
import { UpdateCasbin, getPolicyPathByAuthorityId } from '@/api/casbin'
22 23 24 25 26 27 28 29 30 31 32 33 34 35
export default {
  name: 'Apis',
  props: {
    row: {
      default: function() {
        return {}
      },
      type: Object
    }
  },
  data() {
    return {
      apiTreeData: [],
      apiTreeIds: [],
36
      needConfirm:false,
37 38 39 40 41 42 43
      apiDefaultProps: {
        children: 'children',
        label: 'description'
      }
    }
  },
  methods: {
44 45 46 47 48 49 50
    nodeChange(){
      this.needConfirm = true
    },
    // 暴露给外层使用的切换拦截统一方法
    enterAndNext(){
      this.authApiEnter()
    },
51 52 53 54 55
    // 创建api树方法
    buildApiTree(apis) {
      const apiObj = new Object()
      apis &&
        apis.map(item => {
56
        item.onlyId = "p:"+item.path+"m:"+item.method
Mr.奇淼('s avatar
Mr.奇淼( 已提交
57
          if (Object.prototype.hasOwnProperty.call(apiObj,item.apiGroup)) {
58
            apiObj[item.apiGroup].push(item)
59
          } else {
60
            Object.assign(apiObj, { [item.apiGroup]: [item] })
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
          }
        })
      const apiTree = []
      for (const key in apiObj) {
        const treeNode = {
          ID: key,
          description: key + '',
          children: apiObj[key]
        }
        apiTree.push(treeNode)
      }
      return apiTree
    },
    // 关联关系确定
    async authApiEnter() {
76 77 78 79 80 81 82 83 84
      const checkArr = this.$refs.apiTree.getCheckedNodes(true)
      var casbinInfos = []
      checkArr&&checkArr.map(item=>{
        var casbinInfo = {
          path:item.path,
          method:item.method
        }
        casbinInfos.push(casbinInfo)
      })
R
rainyan 已提交
85
      const res = await UpdateCasbin({
86
        authorityId: this.activeUserId,
87
        casbinInfos
88
      })
89
      if (res.code == 0) {
90
        this.$message({ type: 'success', message: "api设置成功" })
91 92 93 94 95 96 97
      }
    }
  },
  async created() {
    // 获取api并整理成树结构
    const res2 = await getAllApis()
    const apis = res2.data.apis
98
   
99 100 101 102 103
    this.apiTreeData = this.buildApiTree(apis)
    const res = await getPolicyPathByAuthorityId({
      authorityId: this.row.authorityId
    })
    this.activeUserId = this.row.authorityId
104 105 106 107
    this.apiTreeIds = []
    res.data.paths&&res.data.paths.map(item=>{
      this.apiTreeIds.push("p:"+item.path+"m:"+item.method)
    })
108 109 110 111 112
  }
}
</script>
<style lang="scss">
</style>