long
2021-06-08 4602affd72cb8f5312bb737ca8cbd09214a846d0
提交 | 用户 | age
2a61f6 1 <template>
L 2   <div class="app-container">
a12059 3     <el-page-header class="mb20" @back="jumpBack" />
36b711 4     <el-form ref="roleAddForm" label-position="left" :model="mData" label-width="150px" :rules="rules" size="small">
2a61f6 5       <el-form-item label="角色名称:" prop="name">
36b711 6         <el-input v-model="mData.name" placeholder="请输入名称" maxlength="50" class="com-edit-input" />
2a61f6 7       </el-form-item>
L 8       <el-form-item label="是否启用:">
9         <el-switch v-model="mData.isUse" :active-value="1" :inactive-value="0" />
10       </el-form-item>
11       <el-form-item label="权限选择:">
12         <div v-for="(item, index) in authorityGroups" :key="index" class="flex">
13           <div style="min-width: 140px">
14             <el-checkbox v-model="item.checked" @change="chooseAll(index)">{{ item.name }}:</el-checkbox>
15           </div>
16           <div class="flex flex-ver flex-wrap role-list">
17             <div v-for="(auth, a_idx) in item.authoritys" :key="a_idx" class="item flex" :class="{active: auth.active}" @click="chooseAuth(index, a_idx)">
18               <div class="name">{{ auth.name }}</div>
19               <i class="el-icon-check" />
20             </div>
21           </div>
22         </div>
23       </el-form-item>
24     </el-form>
25
26     <div class="com-edit-bottom-btns flex flex-align-center">
27       <el-button size="mini" @click="jumpBack">取消</el-button>
28       <el-button type="primary" size="mini" @click="submit">提交</el-button>
29     </div>
30
31   </div>
32 </template>
33
34 <script>
35
36 export default {
37   name: 'RoleEdit',
38   data() {
39     return {
40       id: '',
41       mData: {
42         isUse: 1, // 是否启用
43         name: '' // 角色名称
44       },
45       authorityGroups: [], // 权限选项
46       // 表单校验
47       rules: {
48         name: [
49           { required: true, message: '角色名称不能为空', trigger: 'change' }
50         ]
51       }
52     }
53   },
54   computed: {
55     // 已选中的权限id
56     authIds() {
57       const arr = []
58       this.authorityGroups.forEach(item => {
59         item.authoritys.forEach(auth => {
60           if (auth.active) {
61             arr.push(auth.id)
62           }
63         })
64       })
65       return arr
66     }
67   },
68   mounted() {
69     this.init()
70   },
71   methods: {
72     init() {
73       this.id = this.$route.query.id || ''
74       this.getRoleData()
75     },
76
77     jumpBack() {
78       this.$router.go(-1)
79     },
80
81     // 获取角色数据
82     getRoleData() {
83       this.postFN({
84         url: 'admin/role/see',
85         params: {
86           id: this.id
87         },
88         mockData: {
89           code: 100, msg: '',
90           data: {
91             role: {
92               id: '2d23c696e84f11eab06bb8599f4cafbe',
93               isDel: 0,
94               isUse: 1,
95               name: '超级管理员'
96             },
97             'authorityGroups': [
98               {
99                 'name': '分类1',
100                 'authoritys': [
101                   {
102                     'name': '权限1',
103                     'id': '1'
104                   }
105                 ]
106               }, {
107                 'name': '分类2',
108                 'authoritys': [
109                   {
110                     'name': '权限21',
111                     'id': '21'
112                   }, {
113                     'name': '权限22',
114                     'id': '22'
115                   }
116                 ]
117               }
118             ],
119             roleAuthoritys: [{
120               sysAdAuId: '1'
121             }, {
122               sysAdAuId: '21'
123             }, {
124               sysAdAuId: '22'
125             }]
126           }
127         }
128       }, (inf) => {
129         console.log(inf)
130         // 处理权限数组
131         const arr = inf.roleAuthoritys.map((_roleAuth) => _roleAuth.sysAdAuId)
132         inf.authorityGroups.forEach(item => {
133           let flag = true
134           item.authoritys.forEach(auth => {
135             auth.active = arr.indexOf(auth.id) > -1
136             if (!auth.active) {
137               flag = false
138             }
139           })
140           item.checked = flag // 全选
141         })
142         this.authorityGroups = inf.authorityGroups
143         this.mData = inf.role
144       })
145     },
146
147     // 获取权限数组
148     getAuthData() {
149       this.postFN({
150         url: 'admin/role/add/page',
151         mockData: {
152           code: 100, msg: '',
153           data: {
154             'authorityGroups': [
155               {
156                 'name': '分类1',
157                 'authoritys': [
158                   {
159                     'name': '权限1',
160                     'id': '1'
161                   }
162                 ]
163               }, {
164                 'name': '分类2',
165                 'authoritys': [
166                   {
167                     'name': '权限21',
168                     'id': '21'
169                   }, {
170                     'name': '权限22',
171                     'id': '22'
172                   }
173                 ]
174               }
175             ]
176           }
177         }
178       }, (inf) => {
179         this.authorityGroups = inf.authorityGroups
180       })
181     },
182
183     // 选中权限
184     // @param idx1 一级
185     // @param idx2 二级
186     chooseAuth(idx1, idx2) {
187       const value = this.authorityGroups[idx1].authoritys[idx2].active
188       this.$set(this.authorityGroups[idx1].authoritys[idx2], 'active', !value)
189       // 处理全选
190       this.$set(this.authorityGroups[idx1], 'checked', this.checkAllChecked(this.authorityGroups[idx1].authoritys))
191     },
192
193     // 检查数组是否全部已选
194     checkAllChecked(arr) {
195       let flag = true
196       arr.find(item => {
197         if (!item.active) {
198           flag = false
199           return true
200         }
201       })
202       return flag
203     },
204
205     // 点击全选
206     chooseAll(index) {
207       var value = this.authorityGroups[index].checked
208       this.authorityGroups[index].authoritys.forEach((item, idx2) => {
209         this.$set(this.authorityGroups[index].authoritys[idx2], 'active', value)
210       })
211     },
212
213     submit() {
214       this.$refs['roleAddForm'].validate(valid => {
215         if (valid) {
216           var { mData, authIds } = this
217           var params = {
218             id: this.id,
219             name: mData.name,
220             isUse: mData.isUse,
221             authIds: JSON.stringify(authIds)
222           }
223
224           this.postFN({
225             url: 'admin/role/update',
226             params: params,
227             mockData: {
228               code: 100,
229               msg: '',
230               data: {}
231             }
232           }, () => {
233             this.$messageSuc('保存成功')
234             this.jumpBack()
235           })
236         }
237       })
238     }
239   }
240 }
241 </script>
242
243 <style lang="scss" scoped>
244 .role-list{
245   .item{
246     cursor: pointer;
247     margin: 6px 15px 6px 0;
248     .name{
249       color: #fff;
250       background: #ccc;
251       text-align: center;
252       height: 26px;
253       line-height: 26px;
254       padding: 0 12px;
255       font-size: 14px;
256     }
257     .el-icon-check{
258       color: #fff;
259       font-size: 20px;
260       border: 1px solid #ccc;
261       text-align: center;
262       display: block;
263       line-height: 24px;
264       height: 24px;
265       width: 24px;
266       box-sizing: content-box;
267     }
268
482658 269     // &:hover{
L 270     //   .name {
271     //     background: #999;
272     //   }
273     //   .el-icon-check{
274     //     color: #999;
275     //   }
276     // }
2a61f6 277   }
L 278   .active, .active:hover{
279     .name{
482658 280       background: #409EFF;
L 281       // background: #999;
2a61f6 282     }
L 283     .el-icon-check{
3fccf2 284       color: #409EFF;
L 285       // color: #999;
2a61f6 286     }
L 287   }
288 }
289 </style>