authority.vue 8.4 KB
Newer Older
1
<template>
2
  <div class="authority">
3
    <div class="button-box clearflex">
4
      <el-button @click="addAuthority('0')" type="primary">新增角色</el-button>
5
    </div>
K
klausY 已提交
6
    <el-table
Mr.奇淼('s avatar
Mr.奇淼( 已提交
7 8 9
      :data="tableData"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      border
Mr.奇淼('s avatar
Mr.奇淼( 已提交
10
      row-key="authorityId"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
11 12 13 14 15 16 17 18 19
      stripe
      style="width: 100%"
    >
      <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>
      <el-table-column fixed="right" label="操作" min-width="300">
        <template slot-scope="scope">
          <el-button @click="opdendrawer(scope.row)" size="small" type="text">设置权限</el-button>
          <el-button @click="addAuthority(scope.row.authorityId)" size="small" type="text">新增子角色</el-button>
20 21 22
          <el-button @click="editAuthority(scope.row)" size="small" type="text">编辑角色</el-button>
          <el-button @click="deleteAuth(scope.row)" size="small" type="text">删除角色</el-button>

Mr.奇淼('s avatar
Mr.奇淼( 已提交
23 24 25
        </template>
      </el-table-column>
    </el-table>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
26
    <!-- 新增角色弹窗 -->
27
    <el-dialog :visible.sync="dialogFormVisible" :title="dialogTitle">
Mr.奇淼('s avatar
Mr.奇淼( 已提交
28
      <el-form :model="form" :rules="rules" ref="authorityForm">
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
        <el-form-item label="父级角色"  prop="parentId">
           <el-select
           :disabled="dialogType=='add'"
            placeholder="请选择"
            v-model="form.parentId"
            filterable
          >
            <el-option
              :disabled="canSelect(item)"
              :key="item.authorityId"
              :label="item.authorityName"
              :value="item.authorityId"
              v-for="item in AuthorityOption"
            ></el-option>
          </el-select>
44
        </el-form-item>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
45
        <el-form-item label="角色ID" prop="authorityId">
46
          <el-input autocomplete="off" :disabled="dialogType=='edit'" v-model="form.authorityId"></el-input>
47
        </el-form-item>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
48
        <el-form-item label="角色姓名" prop="authorityName">
49 50 51 52 53 54 55 56
          <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.奇淼( 已提交
57

Mr.奇淼('s avatar
Mr.奇淼( 已提交
58
    <el-drawer :visible.sync="drawer" :with-header="false" size="40%" title="角色配置" v-if="drawer">
59 60 61 62 63 64 65
      <el-tabs class="role-box" type="border-card">
        <el-tab-pane label="角色菜单">
          <Menus :row="activeRow" />
        </el-tab-pane>
        <el-tab-pane label="角色api">
          <apis :row="activeRow" />
        </el-tab-pane>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
66 67
        <el-tab-pane label="资源权限">
          <Datas :authority="tableData" :row="activeRow" />
Mr.奇淼('s avatar
Mr.奇淼( 已提交
68
        </el-tab-pane>
69 70
      </el-tabs>
    </el-drawer>
71 72 73 74
  </div>
</template>

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

Mr.奇淼('s avatar
Mr.奇淼( 已提交
77 78 79
import {
  getAuthorityList,
  deleteAuthority,
80 81
  createAuthority,
  updateAuthority 
Mr.奇淼('s avatar
Mr.奇淼( 已提交
82
} from '@/api/authority'
83 84 85

import Menus from '@/view/superAdmin/authority/components/menus'
import Apis from '@/view/superAdmin/authority/components/apis'
Mr.奇淼('s avatar
Mr.奇淼( 已提交
86
import Datas from '@/view/superAdmin/authority/components/datas'
87

88
import infoList from '@/components/mixins/infoList'
89 90
export default {
  name: 'Authority',
91
  mixins: [infoList],
92 93
  data() {
    return {
94 95 96 97
      AuthorityOption:[{
          authorityId:"0",
          authorityName:"根角色"
        }],
98
      listApi: getAuthorityList,
99
      drawer: false,
100
      dialogType:"add",
101
      activeRow: {},
Mr.奇淼('s avatar
Mr.奇淼( 已提交
102
      activeUserId: 0,
103
      dialogTitle:"新增角色",
104
      dialogFormVisible: false,
Mr.奇淼('s avatar
Mr.奇淼( 已提交
105
      apiDialogFlag: false,
Mr.奇淼('s avatar
Mr.奇淼( 已提交
106 107
      form: {
        authorityId: '',
108
        authorityName: '',
Mr.奇淼('s avatar
Mr.奇淼( 已提交
109 110 111 112 113 114 115 116 117 118 119 120
        parentId: '0'
      },
      rules: {
        authorityId: [
          { required: true, message: '请输入角色ID', trigger: 'blur' }
        ],
        authorityName: [
          { required: true, message: '请输入角色名', trigger: 'blur' }
        ],
        parentId: [
          { required: true, message: '请选择请求方式', trigger: 'blur' }
        ]
121 122 123
      }
    }
  },
124 125
  components: {
    Menus,
Mr.奇淼('s avatar
Mr.奇淼( 已提交
126 127
    Apis,
    Datas
128
  },
129
  methods: {
130 131 132 133
    opdendrawer(row) {
      this.drawer = true
      this.activeRow = row
    },
Mr.奇淼('s avatar
Mr.奇淼( 已提交
134
    // 删除角色
135 136 137 138 139 140 141
    deleteAuth(row) {
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
142
          const res = await deleteAuthority({ authorityId: row.authorityId })
143
          if (res.code == 0) {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
144 145 146 147
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
148
            this.getTableData()
Mr.奇淼('s avatar
Mr.奇淼( 已提交
149
          }
150 151 152 153 154 155 156 157
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
Mr.奇淼('s avatar
Mr.奇淼( 已提交
158 159
    // 初始化表单
    initForm() {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
160
      this.$refs.authorityForm.resetFields()
161 162 163 164 165
      this.form =  {
        authorityId: '',
        authorityName: '',
        parentId: '0'
      }
166
    },
Mr.奇淼('s avatar
Mr.奇淼( 已提交
167 168 169 170
    // 关闭窗口
    closeDialog() {
      this.initForm()
      this.dialogFormVisible = false
Mr.奇淼('s avatar
Mr.奇淼( 已提交
171
      this.apiDialogFlag = false
172
    },
Mr.奇淼('s avatar
Mr.奇淼( 已提交
173
    // 确定弹窗
Mr.奇淼('s avatar
Mr.奇淼( 已提交
174

Mr.奇淼('s avatar
Mr.奇淼( 已提交
175
    async enterDialog() {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
176
      if (this.form.authorityId == '0') {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
177
        this.$message({
Mr.奇淼('s avatar
Mr.奇淼( 已提交
178 179
          type: 'error',
          message: '角色id不能为0'
Mr.奇淼('s avatar
Mr.奇淼( 已提交
180
        })
Mr.奇淼('s avatar
Mr.奇淼( 已提交
181
        return false
Mr.奇淼('s avatar
Mr.奇淼( 已提交
182
      }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
183 184
      this.$refs.authorityForm.validate(async valid => {
        if (valid) {
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
          switch (this.dialogType) {
            case 'add':
              {
                const res = await createAuthority(this.form)
                if (res.code == 0) {
                  this.$message({
                    type: 'success',
                    message: '添加成功!'
                  })
                  this.getTableData()
                  this.closeDialog()
                }
              }
              break;
            case 'edit':
              {
                const res = await updateAuthority(this.form)
                if (res.code == 0) {
                  this.$message({
                    type: 'success',
                    message: '添加成功!'
                  })
                  this.getTableData()
                  this.closeDialog()
                }
              }
              break;
            default:
              break;
Mr.奇淼('s avatar
Mr.奇淼( 已提交
214
          }
215
          
Mr.奇淼('s avatar
Mr.奇淼( 已提交
216 217 218 219
          this.initForm()
          this.dialogFormVisible = false
        }
      })
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
    getAuthorityList(AuthorityData){
      AuthorityData.map(item=>{
        this.AuthorityOption.push({
          authorityId:item.authorityId,
          authorityName:item.authorityName
        })
        if(item.children){
          this.getAuthorityList(item.children)
        }
      })
    },
    findAuthoritySelf(authority,authData,outData){
      authData.some(item=>{
        if(item.authorityId == authority.authorityId){
          outData.push(item)
          return true
        }
        this.findAuthoritySelf(authority,item.children,outData)
      })
    },
    findAllChild(authority,array){
      authority&&authority.map(item=>{
        array.push(item.authorityId)
        this.findAllChild(item.children,array)
      })
    },
    canSelect(authority){
      const array = []
      const arrayIds = []
      this.findAuthoritySelf({authorityId:this.form.authorityId},this.tableData,array)
      this.findAllChild(array,arrayIds)
      return arrayIds.indexOf(authority.authorityId)>-1
    },
Mr.奇淼('s avatar
Mr.奇淼( 已提交
254
    // 增加角色
255
    addAuthority(parentId) {
256 257
      this. dialogTitle = "新增角色"
      this.dialogType = "add"
258
      this.form.parentId = parentId
Mr.奇淼('s avatar
Mr.奇淼( 已提交
259
      this.dialogFormVisible = true
260 261 262 263 264 265 266 267 268
    },
    // 增加角色
    editAuthority(row) {
      this. dialogTitle = "编辑角色"
      this.dialogType = "edit"
      for(let key in this.form){
        this.form[key] = row[key]
      }
      this.dialogFormVisible = true
269
    }
270
  },
271
  async created() {
272
    this.pageSize = 999
273 274
    await this.getTableData()
    this.getAuthorityList(this.tableData)
275 276 277
  }
}
</script>
278 279 280 281 282 283 284
<style lang="scss">
.authority {
  .button-box {
    padding: 10px 20px;
    .el-button {
      float: right;
    }
285 286
  }
}
287
.role-box {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
288 289 290
  .el-tabs__content {
    height: calc(100vh - 150px);
    overflow: auto;
291
  }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
292
}
293
</style>