authority.vue 8.3 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
        <el-form-item label="父级角色"  prop="parentId">
30 31 32 33 34 35 36 37
           <el-cascader
              :disabled="dialogType=='add'"
              v-model="form.parentId"
              :options="AuthorityOption"
              :show-all-levels="false"
              :props="{ checkStrictly: true,label:'authorityName',value:'authorityId',disabled:'disabled',emitPath:false}"
              filterable>
              </el-cascader>
38
        </el-form-item>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
39
        <el-form-item label="角色ID" prop="authorityId">
40
          <el-input autocomplete="off" :disabled="dialogType=='edit'" v-model="form.authorityId"></el-input>
41
        </el-form-item>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
42
        <el-form-item label="角色姓名" prop="authorityName">
43 44 45 46 47 48 49 50
          <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.奇淼( 已提交
51

Mr.奇淼('s avatar
Mr.奇淼( 已提交
52
    <el-drawer :visible.sync="drawer" :with-header="false" size="40%" title="角色配置" v-if="drawer">
53 54 55 56 57 58 59
      <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.奇淼( 已提交
60 61
        <el-tab-pane label="资源权限">
          <Datas :authority="tableData" :row="activeRow" />
Mr.奇淼('s avatar
Mr.奇淼( 已提交
62
        </el-tab-pane>
63 64
      </el-tabs>
    </el-drawer>
65 66 67 68
  </div>
</template>

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

Mr.奇淼('s avatar
Mr.奇淼( 已提交
71 72 73
import {
  getAuthorityList,
  deleteAuthority,
74 75
  createAuthority,
  updateAuthority 
Mr.奇淼('s avatar
Mr.奇淼( 已提交
76
} from '@/api/authority'
77 78 79

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

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

Mr.奇淼('s avatar
Mr.奇淼( 已提交
169
    async enterDialog() {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
170
      if (this.form.authorityId == '0') {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
171
        this.$message({
Mr.奇淼('s avatar
Mr.奇淼( 已提交
172 173
          type: 'error',
          message: '角色id不能为0'
Mr.奇淼('s avatar
Mr.奇淼( 已提交
174
        })
Mr.奇淼('s avatar
Mr.奇淼( 已提交
175
        return false
Mr.奇淼('s avatar
Mr.奇淼( 已提交
176
      }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
177 178
      this.$refs.authorityForm.validate(async valid => {
        if (valid) {
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
          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.奇淼( 已提交
208
          }
209
          
Mr.奇淼('s avatar
Mr.奇淼( 已提交
210 211 212 213
          this.initForm()
          this.dialogFormVisible = false
        }
      })
214
    },
215 216 217 218 219 220
    setOptions(){
       this.AuthorityOption = [{
          authorityId:"0",
          authorityName:"根角色"
        }]
      this.setAuthorityOptions(this.tableData,this.AuthorityOption,false)
221
    },
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
    setAuthorityOptions(AuthorityData,optionsData,disabled){
      AuthorityData&&AuthorityData.map(item=>{
        if(item.children.length){
          const option = {
            authorityId:item.authorityId,
            authorityName:item.authorityName,
            disabled:disabled||item.authorityId == this.form.authorityId,
            children:[]
        }
          this.setAuthorityOptions(item.children,option.children,disabled||item.authorityId == this.form.authorityId)
          optionsData.push(option)
        }else{
          const option = {
              authorityId:item.authorityId,
              authorityName:item.authorityName,
              disabled:disabled||item.authorityId == this.form.authorityId,
          }
          optionsData.push(option)
240 241 242
        }
      })
    },
Mr.奇淼('s avatar
Mr.奇淼( 已提交
243
    // 增加角色
244
    addAuthority(parentId) {
245
      this.dialogTitle = "新增角色"
246
      this.dialogType = "add"
247
      this.form.parentId = parentId
248
      this.setOptions()
Mr.奇淼('s avatar
Mr.奇淼( 已提交
249
      this.dialogFormVisible = true
250
      
251
    },
252
    // 编辑角色
253
    editAuthority(row) {
254 255
      this.setOptions()
      this.dialogTitle = "编辑角色"
256 257 258 259
      this.dialogType = "edit"
      for(let key in this.form){
        this.form[key] = row[key]
      }
260
      this.setOptions()
261
      this.dialogFormVisible = true
262
    }
263
  },
264
  async created() {
265
    this.pageSize = 999
266
    await this.getTableData()
267 268 269
  }
}
</script>
270 271 272 273 274 275 276
<style lang="scss">
.authority {
  .button-box {
    padding: 10px 20px;
    .el-button {
      float: right;
    }
277 278
  }
}
279
.role-box {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
280 281 282
  .el-tabs__content {
    height: calc(100vh - 150px);
    overflow: auto;
283
  }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
284
}
285
</style>