apis.vue 2.7 KB
Newer Older
1 2 3
<template>
  <div>
    <div class="clearflex">
P
piexlmax 已提交
4
      <el-button class="fl-right" size="mini" type="primary" @click="authApiEnter">确 定</el-button>
5 6
    </div>
    <el-tree
何秀钢 已提交
7
      ref="apiTree"
8 9 10 11 12
      :data="apiTreeData"
      :default-checked-keys="apiTreeIds"
      :props="apiDefaultProps"
      default-expand-all
      highlight-current
13
      node-key="onlyId"
14
      show-checkbox
何秀钢 已提交
15 16
      @check="nodeChange"
    />
17 18
  </div>
</template>
何秀钢 已提交
19

20 21
<script>
import { getAllApis } from '@/api/api'
R
rainyan 已提交
22
import { UpdateCasbin, getPolicyPathByAuthorityId } from '@/api/casbin'
23 24 25 26 27 28 29 30 31 32 33 34 35 36
export default {
  name: 'Apis',
  props: {
    row: {
      default: function() {
        return {}
      },
      type: Object
    }
  },
  data() {
    return {
      apiTreeData: [],
      apiTreeIds: [],
何秀钢 已提交
37
      needConfirm: false,
38 39 40 41 42 43
      apiDefaultProps: {
        children: 'children',
        label: 'description'
      }
    }
  },
何秀钢 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
  async created() {
    // 获取api并整理成树结构
    const res2 = await getAllApis()
    const apis = res2.data.apis

    this.apiTreeData = this.buildApiTree(apis)
    const res = await getPolicyPathByAuthorityId({
      authorityId: this.row.authorityId
    })
    this.activeUserId = this.row.authorityId
    this.apiTreeIds = []
    res.data.paths && res.data.paths.map(item => {
      this.apiTreeIds.push('p:' + item.path + 'm:' + item.method)
    })
  },
59
  methods: {
何秀钢 已提交
60
    nodeChange() {
61 62 63
      this.needConfirm = true
    },
    // 暴露给外层使用的切换拦截统一方法
何秀钢 已提交
64
    enterAndNext() {
65 66
      this.authApiEnter()
    },
67 68
    // 创建api树方法
    buildApiTree(apis) {
何秀钢 已提交
69
      const apiObj = {}
70 71
      apis &&
        apis.map(item => {
何秀钢 已提交
72 73
          item.onlyId = 'p:' + item.path + 'm:' + item.method
          if (Object.prototype.hasOwnProperty.call(apiObj, item.apiGroup)) {
74
            apiObj[item.apiGroup].push(item)
75
          } else {
76
            Object.assign(apiObj, { [item.apiGroup]: [item] })
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
          }
        })
      const apiTree = []
      for (const key in apiObj) {
        const treeNode = {
          ID: key,
          description: key + '',
          children: apiObj[key]
        }
        apiTree.push(treeNode)
      }
      return apiTree
    },
    // 关联关系确定
    async authApiEnter() {
92 93
      const checkArr = this.$refs.apiTree.getCheckedNodes(true)
      var casbinInfos = []
何秀钢 已提交
94
      checkArr && checkArr.map(item => {
95
        var casbinInfo = {
何秀钢 已提交
96 97
          path: item.path,
          method: item.method
98 99 100
        }
        casbinInfos.push(casbinInfo)
      })
R
rainyan 已提交
101
      const res = await UpdateCasbin({
102
        authorityId: this.activeUserId,
103
        casbinInfos
104
      })
何秀钢 已提交
105 106
      if (res.code === 0) {
        this.$message({ type: 'success', message: 'api设置成功' })
107 108 109 110 111
      }
    }
  }
}
</script>