authority.vue 10.1 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
      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>
J
jinlan.du 已提交
16
      <el-table-column fixed="right" label="操作" width="460">
Mr.奇淼('s avatar
Mr.奇淼( 已提交
17 18 19
        <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
          <el-button @click="copyAuthority(scope.row)" size="small" type="text">拷贝角色</el-button>
21 22 23
          <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.奇淼( 已提交
24 25 26
        </template>
      </el-table-column>
    </el-table>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
27
    <!-- 新增角色弹窗 -->
28
    <el-dialog :visible.sync="dialogFormVisible" :title="dialogTitle">
Mr.奇淼('s avatar
Mr.奇淼( 已提交
29
      <el-form :model="form" :rules="rules" ref="authorityForm">
30
        <el-form-item label="父级角色"  prop="parentId">
31 32 33 34 35 36 37 38
           <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>
39
        </el-form-item>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
40
        <el-form-item label="角色ID" prop="authorityId">
41
          <el-input autocomplete="off" :disabled="dialogType=='edit'" v-model="form.authorityId"></el-input>
42
        </el-form-item>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
43
        <el-form-item label="角色姓名" prop="authorityName">
44 45 46 47 48 49 50 51
          <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.奇淼( 已提交
52

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

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

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

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

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

Mr.奇淼('s avatar
Mr.奇淼( 已提交
192
    async enterDialog() {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
193
      if (this.form.authorityId == '0') {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
194
        this.$message({
Mr.奇淼('s avatar
Mr.奇淼( 已提交
195 196
          type: 'error',
          message: '角色id不能为0'
Mr.奇淼('s avatar
Mr.奇淼( 已提交
197
        })
Mr.奇淼('s avatar
Mr.奇淼( 已提交
198
        return false
Mr.奇淼('s avatar
Mr.奇淼( 已提交
199
      }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
200 201
      this.$refs.authorityForm.validate(async valid => {
        if (valid) {
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
          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;
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
              case 'copy': {
                const data = {
                  "authority": {
                      "authorityId": "string",
                      "authorityName": "string",
                      "datauthorityId": [],
                      "parentId": "string",
                  },
                      "oldAuthorityId": 0
                }
                data.authority.authorityId = this.form.authorityId
                data.authority.authorityName = this.form.authorityName
                data.authority.parentId = this.form.parentId
                data.authority.dataAuthorityId = this. copyForm.dataAuthorityId
                data.oldAuthorityId = this.copyForm.authorityId
                const res = await copyAuthority(data)
                if(res.code == 0) {
                  this.$message({
                    type: 'success',
                    message: '复制成功!'
                  })
                  this.getTableData()
                }
              }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
253
          }
254
          
Mr.奇淼('s avatar
Mr.奇淼( 已提交
255 256 257 258
          this.initForm()
          this.dialogFormVisible = false
        }
      })
259
    },
260 261 262 263 264 265
    setOptions(){
       this.AuthorityOption = [{
          authorityId:"0",
          authorityName:"根角色"
        }]
      this.setAuthorityOptions(this.tableData,this.AuthorityOption,false)
266
    },
267 268
    setAuthorityOptions(AuthorityData,optionsData,disabled){
      AuthorityData&&AuthorityData.map(item=>{
269
        if(item.children&&item.children.length){
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
          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)
285 286 287
        }
      })
    },
Mr.奇淼('s avatar
Mr.奇淼( 已提交
288
    // 增加角色
289
    addAuthority(parentId) {
290
      this.dialogTitle = "新增角色"
291
      this.dialogType = "add"
292
      this.form.parentId = parentId
293
      this.setOptions()
Mr.奇淼('s avatar
Mr.奇淼( 已提交
294
      this.dialogFormVisible = true
295
      
296
    },
297
    // 编辑角色
298
    editAuthority(row) {
299 300
      this.setOptions()
      this.dialogTitle = "编辑角色"
301 302 303 304
      this.dialogType = "edit"
      for(let key in this.form){
        this.form[key] = row[key]
      }
305
      this.setOptions()
306
      this.dialogFormVisible = true
307
    }
308
  },
309
  async created() {
310
    this.pageSize = 999
311
    await this.getTableData()
312 313 314
  }
}
</script>
315 316 317 318 319 320 321
<style lang="scss">
.authority {
  .button-box {
    padding: 10px 20px;
    .el-button {
      float: right;
    }
322 323
  }
}
324
.role-box {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
325 326 327
  .el-tabs__content {
    height: calc(100vh - 150px);
    overflow: auto;
328
  }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
329
}
330
</style>