authority.vue 7.9 KB
Newer Older
1 2 3 4 5 6
<template>
  <div>
    <div class="button-box clearflex">
      <el-button @click="addAuthority" type="primary">新增角色</el-button>
    </div>
    <el-table :data="tableData" border stripe>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
7
      <el-table-column label="id" min-width="180" prop="ID"></el-table-column>      
8 9
      <el-table-column label="角色id" min-width="180" prop="authorityId"></el-table-column>
      <el-table-column label="角色名称" min-width="180" prop="authorityName"></el-table-column>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
10
      <el-table-column fixed="right" label="操作" width="500">
11
        <template slot-scope="scope">
Mr.奇淼('s avatar
Mr.奇淼( 已提交
12 13
          <el-button @click="addAuthMenu(scope.row)" size="small" type="text">变更角色菜单</el-button>
          <el-button @click="addAuthApi(scope.row)" size="small" type="text">变更角色Api</el-button>
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
          <el-button @click="deleteAuth(scope.row)" size="small" type="text">删除角色</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="page"
      :page-size="pageSize"
      :page-sizes="[10, 30, 50, 100]"
      :style="{float:'right',padding:'20px'}"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
      hide-on-single-page
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
29
    <!-- 新增角色弹窗 -->
30 31 32 33 34 35 36 37 38 39 40 41 42 43
    <el-dialog :visible.sync="dialogFormVisible" title="新增角色">
      <el-form :model="form">
        <el-form-item label="角色ID">
          <el-input autocomplete="off" v-model="form.authorityId"></el-input>
        </el-form-item>
        <el-form-item label="角色姓名">
          <el-input autocomplete="off" v-model="form.authorityName"></el-input>
        </el-form-item>
      </el-form>
      <div class="dialog-footer" slot="footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button @click="enterDialog" type="primary">确 定</el-button>
      </div>
    </el-dialog>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
44 45 46 47

    <!-- 关联menu弹窗 -->
    <el-dialog :visible.sync="menuDialogFlag" title="关联菜单">
      <el-tree
Mr.奇淼('s avatar
Mr.奇淼( 已提交
48 49 50 51
        v-if="menuDialogFlag"
        :data="menuTreeData"
        :default-checked-keys="menuTreeIds"
        :props="menuDefaultProps"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
52 53 54
        default-expand-all
        highlight-current
        node-key="ID"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
55
        ref="menuTree"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
56 57 58 59 60 61 62
        show-checkbox
      ></el-tree>
      <div class="dialog-footer" slot="footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button @click="relation" type="primary">确 定</el-button>
      </div>
    </el-dialog>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83


    
    <!-- 关联api弹窗 -->
    <el-dialog :visible.sync="apiDialogFlag" title="关联api">
      <el-tree
        v-if="apiDialogFlag"
        :data="apiTreeData"
        :default-checked-keys="apiTreeIds"
        :props="apiDefaultProps"
        default-expand-all
        highlight-current
        node-key="ID"
        ref="apiTree"
        show-checkbox
      ></el-tree>
      <div class="dialog-footer" slot="footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button @click="authApiEnter" type="primary">确 定</el-button>
      </div>
    </el-dialog>
84 85 86 87
  </div>
</template>

<script>
Mr.奇淼('s avatar
注释  
Mr.奇淼( 已提交
88 89
// 获取列表内容封装在mixins内部  getTableData方法 初始化已封装完成

Mr.奇淼('s avatar
Mr.奇淼( 已提交
90 91 92 93 94 95
import {
  getAuthorityList,
  deleteAuthority,
  createAuthority
} from '@/api/authority'
import { getBaseMenuTree, addMenuAuthority, getMenuAuthority } from '@/api/menu'
Mr.奇淼('s avatar
Mr.奇淼( 已提交
96 97 98 99 100
import {
  getAllApis,
  getAuthAndApi,
  setAuthAndApi
} from '@/api/api'
101
import infoList from '@/view/superAdmin/mixins/infoList'
102 103
export default {
  name: 'Authority',
104
  mixins:[infoList],
105 106
  data() {
    return {
107 108
      listApi: getAuthorityList,
      listKey:'list',
Mr.奇淼('s avatar
Mr.奇淼( 已提交
109
      activeUserId: 0,
Mr.奇淼('s avatar
Mr.奇淼( 已提交
110 111 112
      menuTreeData: [],
      menuTreeIds: [],
      menuDefaultProps: {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
113 114 115
        children: 'children',
        label: 'nickName'
      },
Mr.奇淼('s avatar
Mr.奇淼( 已提交
116 117 118 119 120 121 122

      apiTreeData: [],
      apiTreeIds: [],
      apiDefaultProps: {
        children: 'children',
        label: 'description'
      },
123
      dialogFormVisible: false,
Mr.奇淼('s avatar
Mr.奇淼( 已提交
124
      menuDialogFlag: false,
Mr.奇淼('s avatar
Mr.奇淼( 已提交
125
      apiDialogFlag: false,
Mr.奇淼('s avatar
Mr.奇淼( 已提交
126 127 128
      form: {
        authorityId: '',
        authorityName: ''
129 130 131 132
      }
    }
  },
  methods: {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
133
    // 删除角色
134 135 136 137 138 139 140
    deleteAuth(row) {
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
141
          const res = await deleteAuthority({ authorityId: row.authorityId })
Mr.奇淼('s avatar
Mr.奇淼( 已提交
142 143 144 145 146
          if (res.success) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
147
            this.getTableData()
Mr.奇淼('s avatar
Mr.奇淼( 已提交
148
          }
149 150 151 152 153 154 155 156
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
Mr.奇淼('s avatar
Mr.奇淼( 已提交
157 158 159 160 161
    // 初始化表单
    initForm() {
      for (const key in this.form) {
        this.form[key] = ''
      }
162
    },
Mr.奇淼('s avatar
Mr.奇淼( 已提交
163 164 165 166 167
    // 关闭窗口
    closeDialog() {
      this.initForm()
      this.dialogFormVisible = false
      this.menuDialogFlag = false
Mr.奇淼('s avatar
Mr.奇淼( 已提交
168
      this.apiDialogFlag = false
169
    },
Mr.奇淼('s avatar
Mr.奇淼( 已提交
170
    // 确定弹窗
Mr.奇淼('s avatar
Mr.奇淼( 已提交
171

Mr.奇淼('s avatar
Mr.奇淼( 已提交
172 173 174 175 176 177 178
    async enterDialog() {
      const res = await createAuthority(this.form)
      if (res.success) {
        this.$message({
          type: 'success',
          message: '添加成功!'
        })
179
        this.getTableData()
Mr.奇淼('s avatar
Mr.奇淼( 已提交
180 181 182 183
        this.closeDialog()
      }
      this.initForm()
      this.dialogFormVisible = false
184
    },
Mr.奇淼('s avatar
Mr.奇淼( 已提交
185
    // 增加角色
186
    addAuthority() {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
187
      this.dialogFormVisible = true
188
    },
189
    
Mr.奇淼('s avatar
Mr.奇淼( 已提交
190
    // 关联用户列表关系
Mr.奇淼('s avatar
Mr.奇淼( 已提交
191
    async addAuthMenu(row) {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
192
      const res1 = await getMenuAuthority({ authorityId: row.authorityId })
Mr.奇淼('s avatar
Mr.奇淼( 已提交
193 194
      const menus = res1.data.menus
      const arr = []
Mr.奇淼('s avatar
Mr.奇淼( 已提交
195
      menus.map(item => {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
196 197 198 199
        // 防止直接选中父级造成全选
        if(!menus.some(same=>same.parentId === item.menuId)){
          arr.push(Number(item.menuId))
        }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
200
      })
Mr.奇淼('s avatar
Mr.奇淼( 已提交
201
      this.menuTreeIds = arr
Mr.奇淼('s avatar
Mr.奇淼( 已提交
202 203 204
      this.activeUserId = row.authorityId
      this.menuDialogFlag = true
    },
Mr.奇淼('s avatar
Mr.奇淼( 已提交
205
    // 关联树 确认方法
Mr.奇淼('s avatar
Mr.奇淼( 已提交
206
    async relation() {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
207 208
      const checkArr = this.$refs.menuTree
        .getCheckedNodes(false,true)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
209 210 211 212 213 214 215 216 217 218 219
      const res = await addMenuAuthority({
        menus: checkArr,
        authorityId: this.activeUserId
      })
      if (res.success) {
        this.$message({
          type: 'success',
          message: '添加成功!'
        })
      }
      this.closeDialog()
Mr.奇淼('s avatar
Mr.奇淼( 已提交
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
    },
    // 创建api树方法
    buildApiTree(apis){
      const apiObj = new Object
      apis&&apis.map(item=>{
        if(apiObj.hasOwnProperty(item.group)){
          apiObj[item.group].push(item)
        }else{
          Object.assign(apiObj,{[item.group]:[item]})
        }
      })
      const apiTree = []
      for(const key in apiObj){
        const treeNode = {
          ID:key,
          description:key+"",
          children:apiObj[key]
        }
        apiTree.push(treeNode)
      }
      return apiTree
    },
    // 关联用户api关系
    async addAuthApi(row){
      const res = await getAuthAndApi({authorityId:row.authorityId})
      this.activeUserId = row.authorityId
      this.apiTreeIds = res.data.apis||[]
      this.apiDialogFlag = true
    },
    async authApiEnter(){
      const checkArr = this.$refs.apiTree.getCheckedKeys(true)
       const res = await setAuthAndApi({authorityId:this.activeUserId,apiIds:checkArr})
      if (res.success) {
        this.$message({
          type: 'success',
          message: '添加成功!'
        })
      }
      this.closeDialog()
259 260
    }
  },
Mr.奇淼('s avatar
Mr.奇淼( 已提交
261
  async created() {
262
    this.getTableData()
Mr.奇淼('s avatar
Mr.奇淼( 已提交
263 264 265 266 267 268 269
    // 获取所有菜单树
    const res = await getBaseMenuTree()
    this.menuTreeData = res.data.menus
    // 获取api并整理成树结构
    const res2 = await getAllApis()
    const apis = res2.data.apis
    this.apiTreeData = this.buildApiTree(apis)
270 271 272
  }
}
</script>
273
<style scoped lang="scss">
274 275 276 277 278 279 280
.button-box {
  padding: 10px 20px;
  .el-button {
    float: right;
  }
}
</style>