#
Junjie
2 天以前 ab6dd7555377d0d5fe3a78149c0cdb98e2f15275
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
(function () {
    function handleForbidden(res) {
        if (res && Number(res.code) === 403) {
            top.location.href = baseUrl + "/";
            return true;
        }
        return false;
    }
 
    new Vue({
        el: "#app",
        data: function () {
            return {
                loading: false,
                dialogSubmitting: false,
                resetSubmitting: false,
                searchForm: {
                    username: "",
                    mobile: ""
                },
                tableData: [],
                selection: [],
                roles: [],
                page: {
                    curr: 1,
                    limit: 15,
                    total: 0
                },
                dialog: {
                    visible: false,
                    mode: "create"
                },
                dialogForm: {
                    id: null,
                    username: "",
                    mobile: "",
                    password: "",
                    roleId: null,
                    status: 1,
                    mfaAllow: 0
                },
                resetDialog: {
                    visible: false,
                    userId: null,
                    username: "",
                    password: ""
                },
                dialogRules: {
                    username: [
                        { required: true, message: "请输入登录账户", trigger: "blur" }
                    ],
                    mobile: [
                        { required: true, message: "请输入账号", trigger: "blur" }
                    ],
                    roleId: [
                        { required: true, message: "请选择角色", trigger: "change" }
                    ],
                    password: [
                        {
                            validator: function (rule, value, callback) {
                                if (this.dialog.mode !== "create") {
                                    callback();
                                    return;
                                }
                                if (!value) {
                                    callback(new Error("请输入初始密码"));
                                    return;
                                }
                                if (String(value).length < 4) {
                                    callback(new Error("初始密码不能少于4位"));
                                    return;
                                }
                                callback();
                            }.bind(this),
                            trigger: "blur"
                        }
                    ]
                },
                resetRules: {
                    password: [
                        { required: true, message: "请输入新密码", trigger: "blur" },
                        {
                            validator: function (rule, value, callback) {
                                if (String(value || "").length < 4) {
                                    callback(new Error("新密码不能少于4位"));
                                    return;
                                }
                                callback();
                            },
                            trigger: "blur"
                        }
                    ]
                }
            };
        },
        created: function () {
            this.loadRoles();
            this.loadTable();
        },
        methods: {
            authHeaders: function () {
                return { token: localStorage.getItem("token") };
            },
            loadRoles: function () {
                var vm = this;
                $.ajax({
                    url: baseUrl + "/role/list/auth",
                    method: "GET",
                    headers: vm.authHeaders(),
                    data: {
                        curr: 1,
                        limit: 500
                    },
                    success: function (res) {
                        if (handleForbidden(res)) {
                            return;
                        }
                        if (Number(res.code) !== 200) {
                            vm.$message.error(res.msg || "角色加载失败");
                            return;
                        }
                        var payload = res.data || {};
                        vm.roles = Array.isArray(payload.records) ? payload.records : [];
                    },
                    error: function () {
                        vm.$message.error("角色加载失败");
                    }
                });
            },
            loadTable: function () {
                var vm = this;
                vm.loading = true;
                $.ajax({
                    url: baseUrl + "/user/list/auth",
                    method: "GET",
                    headers: vm.authHeaders(),
                    data: {
                        curr: vm.page.curr,
                        limit: vm.page.limit,
                        username: vm.searchForm.username,
                        mobile: vm.searchForm.mobile
                    },
                    success: function (res) {
                        if (handleForbidden(res)) {
                            return;
                        }
                        if (Number(res.code) !== 200) {
                            vm.$message.error(res.msg || "加载失败");
                            return;
                        }
                        var payload = res.data || {};
                        vm.tableData = Array.isArray(payload.records) ? payload.records : [];
                        vm.page.total = Number(payload.total || 0);
                    },
                    error: function () {
                        vm.$message.error("加载失败");
                    },
                    complete: function () {
                        vm.loading = false;
                    }
                });
            },
            handleSearch: function () {
                this.page.curr = 1;
                this.loadTable();
            },
            handleResetSearch: function () {
                this.searchForm = {
                    username: "",
                    mobile: ""
                };
                this.page.curr = 1;
                this.loadTable();
            },
            handleSelectionChange: function (rows) {
                this.selection = rows || [];
            },
            handleCurrentChange: function (curr) {
                this.page.curr = curr;
                this.loadTable();
            },
            handleSizeChange: function (limit) {
                this.page.limit = limit;
                this.page.curr = 1;
                this.loadTable();
            },
            openCreateDialog: function () {
                this.dialog.mode = "create";
                this.dialog.visible = true;
                this.dialogForm = {
                    id: null,
                    username: "",
                    mobile: "",
                    password: "",
                    roleId: null,
                    status: 1,
                    mfaAllow: 0
                };
                this.clearDialogValidate();
            },
            openEditDialog: function (row) {
                this.dialog.mode = "edit";
                this.dialog.visible = true;
                this.dialogForm = {
                    id: row.id,
                    username: row.username || "",
                    mobile: row.mobile || "",
                    password: "",
                    roleId: row.roleId == null ? null : Number(row.roleId),
                    status: row.status == null ? 1 : Number(row.status),
                    mfaAllow: row.mfaAllow == null ? 0 : Number(row.mfaAllow)
                };
                this.clearDialogValidate();
            },
            clearDialogValidate: function () {
                var vm = this;
                vm.$nextTick(function () {
                    if (vm.$refs.dialogForm) {
                        vm.$refs.dialogForm.clearValidate();
                    }
                });
            },
            closeDialog: function () {
                this.dialog.visible = false;
            },
            submitDialog: function () {
                var vm = this;
                if (!vm.$refs.dialogForm) {
                    return;
                }
                vm.$refs.dialogForm.validate(function (valid) {
                    if (!valid) {
                        return false;
                    }
                    vm.dialogSubmitting = true;
                    $.ajax({
                        url: baseUrl + "/user/" + (vm.dialog.mode === "create" ? "add" : "update") + "/auth",
                        method: "POST",
                        headers: vm.authHeaders(),
                        data: vm.buildDialogPayload(),
                        success: function (res) {
                            if (handleForbidden(res)) {
                                return;
                            }
                            if (Number(res.code) !== 200) {
                                vm.$message.error(res.msg || "保存失败");
                                return;
                            }
                            vm.$message.success(res.msg || "保存成功");
                            vm.dialog.visible = false;
                            vm.loadTable();
                        },
                        error: function () {
                            vm.$message.error("保存失败");
                        },
                        complete: function () {
                            vm.dialogSubmitting = false;
                        }
                    });
                    return true;
                });
            },
            buildDialogPayload: function () {
                var payload = {
                    username: this.dialogForm.username,
                    mobile: this.dialogForm.mobile,
                    roleId: this.dialogForm.roleId,
                    status: this.dialogForm.status,
                    mfaAllow: this.dialogForm.mfaAllow
                };
                if (this.dialog.mode === "edit") {
                    payload.id = this.dialogForm.id;
                } else {
                    payload.password = hex_md5(this.dialogForm.password);
                }
                return payload;
            },
            openResetPasswordDialog: function (row) {
                this.resetDialog = {
                    visible: true,
                    userId: row.id,
                    username: row.username || "",
                    password: ""
                };
                var vm = this;
                vm.$nextTick(function () {
                    if (vm.$refs.resetForm) {
                        vm.$refs.resetForm.clearValidate();
                    }
                });
            },
            closeResetDialog: function () {
                this.resetDialog.visible = false;
            },
            submitResetPassword: function () {
                var vm = this;
                if (!vm.$refs.resetForm) {
                    return;
                }
                vm.$refs.resetForm.validate(function (valid) {
                    if (!valid) {
                        return false;
                    }
                    vm.resetSubmitting = true;
                    $.ajax({
                        url: baseUrl + "/user/resetPassword/auth",
                        method: "POST",
                        headers: vm.authHeaders(),
                        data: {
                            id: vm.resetDialog.userId,
                            password: hex_md5(vm.resetDialog.password)
                        },
                        success: function (res) {
                            if (handleForbidden(res)) {
                                return;
                            }
                            if (Number(res.code) !== 200) {
                                vm.$message.error(res.msg || "重置密码失败");
                                return;
                            }
                            vm.$message.success("重置密码成功");
                            vm.resetDialog.visible = false;
                        },
                        error: function () {
                            vm.$message.error("重置密码失败");
                        },
                        complete: function () {
                            vm.resetSubmitting = false;
                        }
                    });
                    return true;
                });
            },
            toggleStatus: function (row) {
                var vm = this;
                var currentStatus = Number(row.status) === 1 ? 1 : 0;
                $.ajax({
                    url: baseUrl + "/user/edit/auth",
                    method: "POST",
                    headers: vm.authHeaders(),
                    data: {
                        id: row.id,
                        status: currentStatus
                    },
                    success: function (res) {
                        if (handleForbidden(res)) {
                            return;
                        }
                        if (Number(res.code) !== 200) {
                            row.status = currentStatus === 1 ? 0 : 1;
                            vm.$message.error(res.msg || "状态更新失败");
                            return;
                        }
                        row.status$ = currentStatus === 1 ? "正常" : "禁用";
                        vm.$message.success("状态已更新");
                    },
                    error: function () {
                        row.status = currentStatus === 1 ? 0 : 1;
                        vm.$message.error("状态更新失败");
                    }
                });
            },
            removeSelection: function () {
                var ids = this.selection.map(function (item) {
                    return item.id;
                });
                this.removeRows(ids);
            },
            removeRows: function (ids) {
                var vm = this;
                if (!ids || ids.length === 0) {
                    vm.$message.warning("请选择要删除的数据");
                    return;
                }
                vm.$confirm("确定删除选中的记录吗?", "提示", {
                    type: "warning",
                    confirmButtonText: "确定",
                    cancelButtonText: "取消"
                }).then(function () {
                    $.ajax({
                        url: baseUrl + "/user/delete/auth",
                        method: "POST",
                        headers: vm.authHeaders(),
                        traditional: true,
                        data: { "ids[]": ids },
                        success: function (res) {
                            if (handleForbidden(res)) {
                                return;
                            }
                            if (Number(res.code) !== 200) {
                                vm.$message.error(res.msg || "删除失败");
                                return;
                            }
                            vm.$message.success("删除成功");
                            vm.loadTable();
                        },
                        error: function () {
                            vm.$message.error("删除失败");
                        }
                    });
                }).catch(function () {
                });
            }
        }
    });
})();