Files
2025-04-21 17:49:17 +08:00

112 lines
4.4 KiB
JavaScript

import { apiUserInfoSave } from '../public/api/index.js';
import { fetchApi,onError,strSite} from '../public/api/fetch.js';
import { layerPopup } from '../public/commonFun-news.js';
import { getLoginStatus }from '../public/localstorage.js';
$(document).ready(function() {
// 登录状态
if (getLoginStatus()) {
//getUserInfo()
}else{
location.href="/login.html"
}
const PASSWORD_MIN_LENGTH = 6;
const PASSWORD_MAX_LENGTH = 20;
// 去空格
const getValueAndTrim = (selector) => $(selector).length > 0 ? $(selector).val().replace(/\s*/g, "") : "";
// 当前密码验证
const isValidCurrentPassword = (password) => {
if (password.length < PASSWORD_MIN_LENGTH ) {
$('.sc-current-password-error').text(`请输入密码`);
return false;
}
return true;
};
// 密码验证
const isValidPassword = (password, confirmPass = null) => {
if (password.length < PASSWORD_MIN_LENGTH || password.length > PASSWORD_MAX_LENGTH) {
$('.sc-password-error').text(`密码必须由${PASSWORD_MIN_LENGTH}-${PASSWORD_MAX_LENGTH}位数字或者字母组成`);
return false;
}
if (confirmPass !== null && password !== confirmPass) {
$('.sc-confirm-password-error').text(`两次密码不匹配,请确认后重新输入`);
return false;
}
return true;
};
// 输入框监听事件
const setupInputValidation = (selector, errorSelector, validationFunc,type) => {
$(selector).bind('input propertychange', function(e) {
const value = e.target.value.replace(/\s*/g, "");
const strPasswordVal = getValueAndTrim(".sc-password");
const strConfirmPasswordval = getValueAndTrim(".sc-confirm-password");
if (!validationFunc(value)) {
$(errorSelector).text(value.length === 0 ? `请输入密码` : $(errorSelector).text());
if(type == 1){
// 检查确认密码是否匹配
if (strConfirmPasswordval.length > 0 && strConfirmPasswordval !== strPasswordVal) {
$('.sc-confirm-password-error').text(`两次密码不匹配,请确认后重新输入`);
} else {
$('.sc-confirm-password-error').text(``);
}
}
} else {
$(errorSelector).text('');
if(type == 1){
// 检查确认密码是否匹配
if (strConfirmPasswordval.length > 0 && strConfirmPasswordval !== strPasswordVal) {
$('.sc-confirm-password-error').text(`两次密码不匹配,请确认后重新输入`);
} else {
$('.sc-confirm-password-error').text(``);
}
}
}
});
};
// 0代表用户名 1密码 2手机号 1确认密码 3验证码
setupInputValidation(".sc-current-password", ".sc-current-password-error", isValidCurrentPassword,0);
setupInputValidation(".sc-password", ".sc-password-error", (val) => isValidPassword(val),1);
setupInputValidation(".sc-confirm-password", ".sc-confirm-password-error", (val) => isValidPassword($('.sc-password').val(), val),1);
// 提交表单逻辑
const submitForm = async (isLogin) => {
const strCurrentPasswordVal = getValueAndTrim(".sc-current-password");
const strPasswordVal = getValueAndTrim(".sc-password");
const strConfirmPasswordval = getValueAndTrim(".sc-confirm-password");
if (isLogin ==1) {
//修改密码
if (isValidCurrentPassword(strCurrentPasswordVal) && isValidPassword(strPasswordVal, strConfirmPasswordval)) {
await handleSubmit(strCurrentPasswordVal, strPasswordVal,strConfirmPasswordval);
}
}
};
const handleSubmit = async (strCurrentPasswordVal, strPasswordVal) => {
let postData = {
'old_mu_pwd':strCurrentPasswordVal,
'mu_pwd':strPasswordVal,
'si_id':strSite,
}
fetchApi(apiUserInfoSave, postData,successFun, onError)
function successFun(res){
if(res.code == '000'){
layerPopup('修改成功', 3)
let time = setTimeout(()=>{
clearTimeout(time)
history.go(-1)
},1500)
// $(".sc-public-popup").show()
// $('.sc-trends-text').text(`修改成功`);
}
}
};
//修改密码
$('.sc-modify-btn').on('click', function() {
submitForm(1);
});
});