123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- <template>
- <div class="authority">
- <div class="button-box clearflex">
- <el-button @click="addAuthority('0')" type="primary">新增角色</el-button>
- </div>
- <el-table
- :data="tableData"
- :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
- border
- row-key="authorityId"
- 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>
- <el-button @click="copyAuthority(scope.row)" size="small" type="text">拷贝角色</el-button>
- <el-button @click="editAuthority(scope.row)" size="small" type="text">编辑角色</el-button>
- <el-button @click="deleteAuth(scope.row)" size="small" type="text">删除角色</el-button>
- </template>
- </el-table-column>
- </el-table>
- <!-- 新增角色弹窗 -->
- <el-dialog :visible.sync="dialogFormVisible" :title="dialogTitle">
- <el-form :model="form" :rules="rules" ref="authorityForm">
- <el-form-item label="父级角色" prop="parentId">
- <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>
- </el-form-item>
- <el-form-item label="角色ID" prop="authorityId">
- <el-input autocomplete="off" :disabled="dialogType=='edit'" v-model="form.authorityId"></el-input>
- </el-form-item>
- <el-form-item label="角色姓名" prop="authorityName">
- <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>
- <el-drawer :visible.sync="drawer" :with-header="false" size="40%" title="角色配置" v-if="drawer">
- <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>
- <el-tab-pane label="资源权限">
- <Datas :authority="tableData" :row="activeRow" />
- </el-tab-pane>
- </el-tabs>
- </el-drawer>
- </div>
- </template>
- <script>
- // 获取列表内容封装在mixins内部 getTableData方法 初始化已封装完成
- import {
- getAuthorityList,
- deleteAuthority,
- createAuthority,
- updateAuthority,
- copyAuthority
- } from '@/api/authority'
- import Menus from '@/view/superAdmin/authority/components/menus'
- import Apis from '@/view/superAdmin/authority/components/apis'
- import Datas from '@/view/superAdmin/authority/components/datas'
- import infoList from '@/components/mixins/infoList'
- export default {
- name: 'Authority',
- mixins: [infoList],
- data() {
- return {
- AuthorityOption:[{
- authorityId:"0",
- authorityName:"根角色"
- }],
- listApi: getAuthorityList,
- drawer: false,
- dialogType:"add",
- activeRow: {},
- activeUserId: 0,
- dialogTitle:"新增角色",
- dialogFormVisible: false,
- apiDialogFlag: false,
- copyForm: {},
- form: {
- authorityId: '',
- authorityName: '',
- parentId: '0'
- },
- rules: {
- authorityId: [
- { required: true, message: '请输入角色ID', trigger: 'blur' }
- ],
- authorityName: [
- { required: true, message: '请输入角色名', trigger: 'blur' }
- ],
- parentId: [
- { required: true, message: '请选择请求方式', trigger: 'blur' }
- ]
- }
- }
- },
- components: {
- Menus,
- Apis,
- Datas
- },
- methods: {
- // 拷贝角色
- copyAuthority(row) {
- this.setOptions()
- this.dialogTitle = "拷贝角色"
- this.dialogType = "copy"
- for(let k in this.form) {
- this.form[k] = row[k]
- }
- this.copyForm = row
- this.dialogFormVisible = true;
- },
- opdendrawer(row) {
- this.drawer = true
- this.activeRow = row
- },
- // 删除角色
- deleteAuth(row) {
- this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
- confirmButtonText: '确定',
- cancelButtonText: '取消',
- type: 'warning'
- })
- .then(async () => {
- const res = await deleteAuthority({ authorityId: row.authorityId })
- if (res.code == 0) {
- this.$message({
- type: 'success',
- message: '删除成功!'
- })
- this.getTableData()
- }
- })
- .catch(() => {
- this.$message({
- type: 'info',
- message: '已取消删除'
- })
- })
- },
- // 初始化表单
- initForm() {
- this.$refs.authorityForm.resetFields()
- this.form = {
- authorityId: '',
- authorityName: '',
- parentId: '0'
- }
- },
- // 关闭窗口
- closeDialog() {
- this.initForm()
- this.dialogFormVisible = false
- this.apiDialogFlag = false
- },
- // 确定弹窗
- async enterDialog() {
- if (this.form.authorityId == '0') {
- this.$message({
- type: 'error',
- message: '角色id不能为0'
- })
- return false
- }
- this.$refs.authorityForm.validate(async valid => {
- if (valid) {
- 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;
- 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()
- }
- }
- }
-
- this.initForm()
- this.dialogFormVisible = false
- }
- })
- },
- setOptions(){
- this.AuthorityOption = [{
- authorityId:"0",
- authorityName:"根角色"
- }]
- this.setAuthorityOptions(this.tableData,this.AuthorityOption,false)
- },
- 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)
- }
- })
- },
- // 增加角色
- addAuthority(parentId) {
- this.dialogTitle = "新增角色"
- this.dialogType = "add"
- this.form.parentId = parentId
- this.setOptions()
- this.dialogFormVisible = true
-
- },
- // 编辑角色
- editAuthority(row) {
- this.setOptions()
- this.dialogTitle = "编辑角色"
- this.dialogType = "edit"
- for(let key in this.form){
- this.form[key] = row[key]
- }
- this.setOptions()
- this.dialogFormVisible = true
- }
- },
- async created() {
- this.pageSize = 999
- await this.getTableData()
- }
- }
- </script>
- <style lang="scss">
- .authority {
- .button-box {
- padding: 10px 20px;
- .el-button {
- float: right;
- }
- }
- }
- .role-box {
- .el-tabs__content {
- height: calc(100vh - 150px);
- overflow: auto;
- }
- }
- </style>
|