“dzb”
2022-10-21 b9b2d3d63335fa9c72078e64f1ac1b365cf2d8dd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<template>
    <div class="updateUser">
        <main>
            <div class="mainContent">
                <el-form ref="user" label-width="140px" autoComplete="on" :model="user" :rules="createUserRules"
                    label-position="right">
                    <!-- 选择角色 -->
                    <el-form-item class="optionItem" label="选择角色:" prop="role" v-if="flag.role">
                        <el-select v-model="user.role" placeholder="请选择所属角色" multiple collapse-tags>
                            <el-option v-for="item in roleList" :key="item.id" :label="item.name" :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <!-- 所属部门 -->
                    <el-form-item class="optionItem" label="所属部门:" prop="departmentId" v-else>
                        <el-select v-model="user.departName" placeholder="请选择所属部门" :disabled="!flag.depart">
                            <el-option :value="mylabel">
                                <el-tree ref="tree" :check-strictly="true" :data="departList" :props="defaultProps"
                                    show-checkbox @check-change="handleCheck"
                                    :default-checked-keys="[selectOrg.orgsid[0],]" default-expand-all node-key="id">
                                </el-tree>
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item>
                        <div class="optionBtn">
                            <el-button type="primary" class="btn submit" @click.native.prevent="handleUser">提交
                            </el-button>
                        </div>
                    </el-form-item>
                </el-form>
 
            </div>
        </main>
    </div>
</template>
<script>
export default {
    data() {
        return {
            user: {
            },
            createUserRules: {
            },
            roleList: [
 
            ],
            departList: [],
            mylabel: "",
            defaultProps: {
                children: "children",
                label: "departName",
            },
            selectOrg: {
                orgsid: [],
            },
            tempName: '',
            tempRole: []
        }
    },
    created() {
        this.user = JSON.parse(JSON.stringify(this.userInfo));
        this.tempName = this.user.departName;
        this.selectOrg.orgsid.push(this.user.departmentId);
        // 获取所有角色列表
        this.getRoleList();
        // 获取当前用户角色列表
        this.getUserRole(this.user.id)
        // 获取全部部门列表
        this.$axios.get('/sccg/depart/tree').then(res => {
            this.departList = res.data;
        })
    },
    methods: {
        // 修改部门
        changeDepart(data) {
            this.departList.forEach(item => {
                if (item.id === data) {
                    this.user.departName = item.departName;
                }
            })
        },
        // 修改用户信息
        handleUser() {
            const { flag, user, tempName, selectOrg, tempRole, isArraySame } = this;
            this.$refs.user.validate((valid) => {
                if (valid) {
                    user.isDy = `${user.isDy}`;
                    if (flag.depart) {
                        if (tempName === user.departName) {
                            this.$message({
                                type: 'warning',
                                message: '您的部门还未更改'
                            })
                        } else {
                            if (user.departName) {
                                this.$axios({
                                    method: 'post',
                                    url: `sccg/admin/update/${user.id}`,
                                    data: {
                                        "id": user.id,
                                        "username": user.username,
                                        "password": user.password,
                                        "icon": user.icon,
                                        "email": user.email,
                                        "nickName": user.nickName,
                                        "note": user.note,
                                        "createTime": user.createTime,
                                        "loginTime": user.loginTime,
                                        "status": user.status ? 1 : 0,
                                        "macAddress": user.macAddress,
                                        "ipAddress": user.ipAddress,
                                        "isDy": `${user.isDy}`,
                                        "sex": `${user.sex}`,
                                        "jobTitle": user.jobTitle,
                                        "departmentId": selectOrg.orgsid[0],
                                        "departName": user.departName,
                                        "userType": user.userType,
                                        "zj": user.zj,
                                        "mobile": user.mobile,
                                    }
                                })
                                    .then(res => {
                                        this.$message({
                                            type: res.code === 200 ? 'success' : 'warning',
                                            message: res.message,
                                        })
                                        this.getUserList();
                                        this.$emit('closeDialog', { flag: false });
                                    })
                            } else {
                                this.$message({
                                    type: 'warning',
                                    message: '用户部门不能为空'
                                })
                            }
                        }
 
                    } else {
                        if (user.role.length === 0) {
                            this.$message({
                                type: 'warning',
                                message: '用户角色不能为空',
                            })
                        }
                        else if (isArraySame(tempRole, user.role) && tempRole.length === user.role.length) {
                            this.$message({
                                type: 'warning',
                                message: '您还未更改角色',
                            })
                        }
                        else {
                            this.$axios({
                                method: 'post',
                                url: 'sccg/admin/role/update?adminId=' + user.id + '&roleIds=' + user.role,
                            })
                                .then(res => {
                                    this.$message({
                                        type: res.code === 200 ? 'success' : 'warning',
                                        message: res.message,
                                    })
                                    this.getUserList();
                                    this.$emit('closeDialog', { flag: false });
                                })
                        }
                    }
                } else {
                    return false;
                }
            })
        },
        // 获取角色列表
        getRoleList() {
            this.$axios({
                method: 'get',
                url: 'sccg/role/listAll',
            })
                .then(res => {
                    this.roleList = res.data;
                })
        },
        // 获取用户的角色列表
        getUserRole(id) {
            this.$axios({
                method: 'get',
                url: `sccg/admin/role/${id}`
            })
                .then(res => {
                    const arr = [];
                    res.data.forEach(item => {
                        arr.push(item.id);
                    })
                    this.user.role = arr;
                    this.tempRole = arr;
                })
        },
        // 部门修改
        handleCheck(data, checked) {
            this.user.departName = data.departName;
            // 获取当前选择的id在数组中的索引
            const indexs = this.selectOrg.orgsid.indexOf(data.id);
            // 如果不存在数组中,并且数组中已经有一个id并且checked为true的时候,代表不能再次选择。
            if (indexs < 0 && this.selectOrg.orgsid.length === 1 && checked) {
                this.$message({
                    message: "只能选择一个部门!",
                    type: "warning",
                    showClose: true,
                });
                // 设置已选择的节点为false 很重要
                this.$refs.tree.setChecked(data, false);
            } else if (this.selectOrg.orgsid.length === 0 && checked) {
                // 发现数组为空 并且是已选择
                // 防止数组有值,首先清空,再push
                this.selectOrg.orgsid = [];
                this.selectOrg.orgsid.push(data.id);
            } else if (
                indexs >= 0 &&
                this.selectOrg.orgsid.length === 1 &&
                !checked
            ) {
                // 再次直接进行赋值为空操作
                this.selectOrg.orgsid = [];
                this.user.departName = ''
            }
        },
        // 判断数组是否相等
        isArraySame(arr1, arr2) {
            let flag = true
            arr1.sort((a, b) => a - b)
            arr2.sort((a, b) => a - b)
            arr1.forEach(item => {
                arr2.forEach(child => {
                    if (item !== child) {
                        flag = false
                    }
                })
            })
            return flag
        }
    },
    props: ['userInfo', 'flag', 'closeDialog', 'getUserList']
}
</script>
<style lang="scss" scoped>
.updateUser {
    border-radius: 1px;
    background-color: #09152f;
 
    :deep(.el-tag) {
        background-color: #09152f;
    }
 
    .el-select-dropdown.is-multiple .el-select-dropdown__item.selected {
        background-color: #09152f;
        color: #4b9bb7;
    }
 
    main {
        text-align: left;
        padding: 0 55px;
        background-color: #09152f;
        padding-bottom: 50px;
 
        .mainContent {
            display: flex;
            justify-content: center;
            padding-top: 50px;
 
            .el-form-item__content {
                width: 400px;
 
                .el-select {
                    width: 100%;
                }
            }
 
            .optionHandleSp {
                display: flex;
 
                .areaNumber,
                .moreNumber {
                    flex: 1;
                }
 
                .telNumber {
                    flex: 2;
                }
            }
 
            .optionBtn {
                display: flex;
                margin-top: 20px;
 
                .btn {
                    padding: 12px 50px;
                }
            }
 
        }
    }
}
 
.updateUser::v-deep .el-form-item__label {
    color: #4b9bb7;
}
 
.updateUser::v-deep .el-input__inner {
    background-color: #09152f;
    border: 1px solid #17324c;
}
</style>