(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 () {
|
});
|
}
|
}
|
});
|
})();
|