Initial commit
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
/**
|
||||
* 修改密码:POST /api/auth/profile { oldPassword, newPassword }
|
||||
*/
|
||||
(function () {
|
||||
var form = document.querySelector("[data-edit-password-form]");
|
||||
if (!form) return;
|
||||
|
||||
var currentInput = form.querySelector("[data-edit-password-current]");
|
||||
var newInput = form.querySelector("[data-edit-password-new]");
|
||||
var confirmInput = form.querySelector("[data-edit-password-confirm]");
|
||||
var submitBtn = form.querySelector("[data-edit-password-submit]");
|
||||
|
||||
document.querySelectorAll("[data-password-toggle]").forEach(function (btn) {
|
||||
btn.addEventListener("click", function () {
|
||||
var id = btn.getAttribute("data-password-toggle");
|
||||
var input = id ? document.getElementById(id) : null;
|
||||
if (!input) return;
|
||||
var show = input.type === "password";
|
||||
input.type = show ? "text" : "password";
|
||||
btn.textContent = show ? "隐藏" : "显示";
|
||||
});
|
||||
});
|
||||
|
||||
function getField(name) {
|
||||
return form.querySelector('[data-edit-password-field="' + name + '"]');
|
||||
}
|
||||
|
||||
function setFieldError(name, message) {
|
||||
var field = getField(name);
|
||||
if (!field) return;
|
||||
var error = field.querySelector("[data-edit-password-error]");
|
||||
field.classList.toggle("is-error", !!message);
|
||||
if (error) {
|
||||
if (message) {
|
||||
error.hidden = false;
|
||||
error.textContent = message;
|
||||
} else {
|
||||
error.hidden = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function clearErrors() {
|
||||
setFieldError("current", "");
|
||||
setFieldError("new", "");
|
||||
setFieldError("confirm", "");
|
||||
}
|
||||
|
||||
function isValidLength(value) {
|
||||
return value.length >= 4 && value.length <= 16;
|
||||
}
|
||||
|
||||
function toast(message, type) {
|
||||
if (window.AuthAPI) AuthAPI.toast(message, type);
|
||||
else if (typeof showToast === "function") showToast(message, type);
|
||||
}
|
||||
|
||||
function validate() {
|
||||
clearErrors();
|
||||
var current = (currentInput && currentInput.value) || "";
|
||||
var next = (newInput && newInput.value) || "";
|
||||
var confirm = (confirmInput && confirmInput.value) || "";
|
||||
var ok = true;
|
||||
|
||||
if (!current) {
|
||||
setFieldError("current", "请输入当前密码");
|
||||
ok = false;
|
||||
} else if (!isValidLength(current)) {
|
||||
setFieldError("current", "当前密码需为 4–16 个字符");
|
||||
ok = false;
|
||||
}
|
||||
|
||||
if (!next) {
|
||||
setFieldError("new", "请输入新密码");
|
||||
ok = false;
|
||||
} else if (!isValidLength(next)) {
|
||||
setFieldError("new", "新密码需为 4–16 个字符");
|
||||
ok = false;
|
||||
} else if (next === current) {
|
||||
setFieldError("new", "新密码不能与当前密码相同");
|
||||
ok = false;
|
||||
}
|
||||
|
||||
if (!confirm) {
|
||||
setFieldError("confirm", "请再次输入新密码");
|
||||
ok = false;
|
||||
} else if (confirm !== next) {
|
||||
setFieldError("confirm", "两次输入的新密码不一致");
|
||||
ok = false;
|
||||
}
|
||||
|
||||
if (!ok) return null;
|
||||
return { oldPassword: current, newPassword: next };
|
||||
}
|
||||
|
||||
[currentInput, newInput, confirmInput].forEach(function (input) {
|
||||
if (!input) return;
|
||||
input.addEventListener("input", function () {
|
||||
var field = input.closest("[data-edit-password-field]");
|
||||
if (!field) return;
|
||||
var name = field.getAttribute("data-edit-password-field");
|
||||
setFieldError(name, "");
|
||||
});
|
||||
});
|
||||
|
||||
form.addEventListener("submit", function (event) {
|
||||
event.preventDefault();
|
||||
var payload = validate();
|
||||
if (!payload) return;
|
||||
if (!window.AuthAPI || !window.HttpRequest) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (submitBtn) submitBtn.disabled = true;
|
||||
|
||||
AuthAPI.updateProfile(payload)
|
||||
.then(function () {
|
||||
toast("密码修改成功", "success");
|
||||
setTimeout(function () {
|
||||
window.location.href = "/setting/";
|
||||
}, 400);
|
||||
})
|
||||
.catch(function (err) {
|
||||
var tip = (err && err.message) || "修改失败,请稍后重试";
|
||||
if (/旧|当前|原密码|old/i.test(tip)) {
|
||||
setFieldError("current", tip);
|
||||
return;
|
||||
}
|
||||
if (/新密码|new/i.test(tip)) {
|
||||
setFieldError("new", tip);
|
||||
return;
|
||||
}
|
||||
toast(tip, "error");
|
||||
})
|
||||
.finally(function () {
|
||||
if (submitBtn) submitBtn.disabled = false;
|
||||
});
|
||||
});
|
||||
})();
|
||||
Reference in New Issue
Block a user