inleft
2022-02-15 91dc6cdda0548ff7f6e473a81933fa206d912cef
commit | author | age
9bcb19 1 <template>
I 2   <a-modal
3     title="授权数据"
4     :width="600"
5     :visible="visible"
6     :confirmLoading="confirmLoading"
7     @ok="handleSubmit"
8     @cancel="handleCancel"
9   >
10     <a-spin :spinning="formLoading">
11       <a-form :form="form">
12         <a-form-item
13           label="选择机构"
14           :labelCol="labelCol"
15           :wrapperCol="wrapperCol"
16         >
17
18           <a-tree
19             v-model="checkedKeys"
20             checkable
21             :auto-expand-parent="autoExpandParent"
22             :expanded-keys="expandedKeys"
23             :tree-data="orgTreeData"
24             :selected-keys="selectedKeys"
25             :replaceFields="replaceFields"
26             @expand="onExpand"
27             @select="onSelect"
28           />
29         </a-form-item>
30
31       </a-form>
32     </a-spin>
33   </a-modal>
34 </template>
35
36 <script>
37   import { getOrgTree } from '@/api/modular/system/orgManage'
38   import { sysUserOwnData, sysUserGrantData } from '@/api/modular/system/userManage'
39
40   export default {
41     data () {
42       return {
43         labelCol: {
44           style: { 'padding-right': '20px' },
45           xs: { span: 24 },
46           sm: { span: 5 }
47         },
48         wrapperCol: {
49           xs: { span: 24 },
50           sm: { span: 15 }
51         },
52         orgTreeData: [],
53         expandedKeys: [],
54         checkedKeys: [],
55         visible: false,
56         confirmLoading: false,
57         formLoading: true,
58         autoExpandParent: true,
59         selectedKeys: [],
60         userEntity: [],
61         replaceFields: {
62           key: 'id'
63         },
64         form: this.$form.createForm(this)
65       }
66     },
67
68     methods: {
69       // 初始化方法
70       userOrg (record) {
71         this.userEntity = record
72         this.visible = true
73         // 获取机构树
74         this.getOrgTree()
75         // 已关联数据
76         this.sysUserOwnData(this.userEntity)
77       },
78
79       /**
80        * 获取机构树
81        */
82       getOrgTree () {
83         this.formLoading = true
84         getOrgTree().then((res) => {
85            if (res.success) {
86              this.orgTreeData = res.data
87              // 默认展开
88              this.orgTreeData.forEach(item => {
89                this.expandedKeys.push(item.id)
90              })
91            }
92         })
93       },
94
95       /**
96        * 此用户已有数据列表
97        */
98       sysUserOwnData (record) {
99         sysUserOwnData({ id: record.id }).then((res) => {
100           if (res.success) {
101             this.checkedKeys = res.data
102           }
103           this.formLoading = false
104         })
105       },
106
107       onExpand (expandedKeys) {
108         this.expandedKeys = expandedKeys
109         this.autoExpandParent = false
110       },
111       onCheck (checkedKeys) {
112         this.checkedKeys = checkedKeys
113       },
114       onSelect (selectedKeys, info) {
115         this.selectedKeys = selectedKeys
116       },
117
118       handleSubmit () {
119         const { form: { validateFields } } = this
120         this.confirmLoading = true
121         validateFields((errors, values) => {
122           if (!errors) {
123             sysUserGrantData({ id: this.userEntity.id, grantOrgIdList: this.checkedKeys }).then((res) => {
124               if (res.success) {
125                 this.$message.success('授权成功')
126                 this.confirmLoading = false
127                 this.$emit('ok', values)
128                 this.handleCancel()
129               } else {
130                 this.$message.error('授权失败:' + res.message)
131               }
132             }).finally((res) => {
133               this.confirmLoading = false
134             })
135           } else {
136             this.confirmLoading = false
137           }
138         })
139       },
140       handleCancel () {
141         this.form.resetFields()
142         // 清空已选择的
143         this.checkedKeys = []
144         // 清空已展开的
145         this.expandedKeys = []
146         this.visible = false
147       }
148     }
149   }
150 </script>