inleft
2022-03-01 88f419df77ade235ea5e5e24be204842a24b7b33
commit | author | age
9bcb19 1 <template>
I 2   <a-modal
3     title="授权角色"
4     :width="800"
5     :visible="visible"
6     :confirmLoading="confirmLoading"
7     @ok="handleSubmit"
8     @cancel="handleCancel"
9   >
10
11     <a-card :bordered="false">
12
13       <div>
14         <a-table
15           size="middle"
16           :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
17           :columns="columns"
18           :dataSource="loadData"
19           :pagination="false"
20           :loading="loading"
21           :rowKey="(record) => record.id"
22         />
23       </div>
24
25     </a-card>
26
27   </a-modal>
28 </template>
29
30 <script>
31   import { getRolePage } from '@/api/modular/system/roleManage'
32   import { sysUserOwnRole, sysUserGrantRole } from '@/api/modular/system/userManage'
33
34   const columns = [
35     {
36       title: '角色名称',
37       dataIndex: 'name'
38     },
39     {
40       title: '唯一编码',
41       dataIndex: 'code'
42     }
43   ]
44
45   export default {
46     name: 'UserRoleIndex',
47
48     data () {
49       return {
50         columns,
51         loadData: [],
52         selectedRowKeys: [], // Check here to configure the default column
53         loading: true,
54         visible: false,
55         confirmLoading: false,
56         recordEntity: []
57       }
58     },
59     computed: {
60       hasSelected () {
61         return this.selectedRowKeys.length > 0
62       }
63     },
64     methods: {
65       // 初始化方法
66       userRole (record) {
67         this.recordEntity = record
68         this.visible = true
69         // 加载已有数据
70         this.sysUserOwnRole()
71         // 获取全部列表,无需分页
72         getRolePage().then((res) => {
73           this.loadData = res.data.rows
74         })
75       },
76
77       /**
78        * 获取用户已有角色
79        */
80       sysUserOwnRole () {
81         this.loading = true
82         sysUserOwnRole({ id: this.recordEntity.id }).then((res) => {
83           // 选中多选框
84           this.selectedRowKeys = res.data
85           this.loading = false
86         })
87       },
88
89       onSelectChange (selectedRowKeys) {
90         this.selectedRowKeys = selectedRowKeys
91       },
92
93       handleSubmit () {
94         // eslint-disable-next-line no-unused-expressions
95         this.confirmLoading = false
96         this.visible = false
97         sysUserGrantRole({ id: this.recordEntity.id, grantRoleIdList: this.selectedRowKeys }).then((res) => {
98                if (res.success) {
99                  this.$message.success('授权成功')
100                  this.confirmLoading = false
101                  this.$emit('ok', this.recordEntity)
102                  this.handleCancel()
103                } else {
104                  this.$message.error('授权失败:' + res.message)
105                }
106              }).finally((res) => {
107                this.confirmLoading = false
108              })
109       },
110       handleCancel () {
111         this.recordEntity = []
112         this.selectedRowKeys = []
113         this.visible = false
114       }
115     }
116   }
117 </script>