import { layerPopup } from '../public/commonFun-news.js'; import { fetchApi,onError,strSite} from '../public/api/fetch.js'; import { storageObj } from '../public/localstorage.js'; // import { checkoutHuiyuanTip } from '/assets/js/html.js?'; import { apiGetUserInfo,apiLoginByPhone ,apiLoginByEmail,apiReGist} from '../public/api/index.js'; $(document).ready(function() { const PHONE_LENGTH = 11; const PASSWORD_MIN_LENGTH = 6; const PASSWORD_MAX_LENGTH = 20; let intChangIndex = 0; const $getCodeButton = $('.get-v-code'); // 去空格 const getValueAndTrim = (selector) => $(selector).length > 0 ? $(selector).val().replace(/\s*/g, "") : ""; // 用户名验证 const isValidUsername = (username) => { if (username.length < PASSWORD_MIN_LENGTH || username.length > PASSWORD_MAX_LENGTH) { $('.sc-username-error').text(`必须由${PASSWORD_MIN_LENGTH}-${PASSWORD_MAX_LENGTH}位数字或者字母组成`); return false; } return true; }; const validateEmail = (strAccount) => { let regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/; if (strAccount.length < PASSWORD_MIN_LENGTH ) { $('.sc-username-error').text(`请输入大于六位数的正确邮箱!`); return false; }else{ if (regex.test(strAccount) || !strAccount) { return true; } else { $('.sc-username-error').text(`账号格式输入不正确,请重新输入`); return false; } } } // 手机号验证 const isValidPhone = (phone) => { if (phone.length < PHONE_LENGTH ) { $('.sc-phone-error').text(`请填写正确的手机号码`); return false; } return true; }; //验证码验证 const isValidCode = (code) => { if (code.length !== 6 ) { $('.sc-code-error').text(`短信验证码必须是6位数字`); 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 strPasswordVal2 = getValueAndTrim(".sc-password"); const strConfirmPasswordval2 = getValueAndTrim(".sc-confirm-password"); if (!validationFunc(value)) { if(type == 0){ $(errorSelector).text(value.length === 0 ? `请输入邮箱` : $(errorSelector).text()); }else if (type == 2){ $(errorSelector).text(value.length === 0 ? `请填写正确的手机号码` : $(errorSelector).text()); }else if (type == 1){ $(errorSelector).text(value.length === 0 ? `请输入密码` : $(errorSelector).text()); }else{ $(errorSelector).text(value.length === 0 ? `短信验证码必须是6位数字` : $(errorSelector).text()); } if(type == 1){ // 检查确认密码是否匹配 if (strConfirmPasswordval2.length > 0 && strConfirmPasswordval2 !== strPasswordVal2) { $('.sc-confirm-password-error').text(`两次密码不匹配,请确认后重新输入`); } else { $('.sc-confirm-password-error').text(``); } } } else { $(errorSelector).text(''); if(type == 1){ // 检查确认密码是否匹配 if (strConfirmPasswordval2.length > 0 && strConfirmPasswordval2 !== strPasswordVal2) { $('.sc-confirm-password-error').text(`两次密码不匹配,请确认后重新输入`); } else { $('.sc-confirm-password-error').text(``); } } } }); }; // 0代表用户名 1密码 2手机号 1确认密码 3验证码 setupInputValidation(".sc-username", ".sc-username-error", isValidUsername,0); setupInputValidation(".sc-code", ".sc-code-error", isValidCode,3); setupInputValidation(".sc-phone", ".sc-phone-error", isValidPhone,2); 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 strUsernameVal = getValueAndTrim(".sc-username"); const strPhoneVal = getValueAndTrim(".sc-phone"); const strCodeVal = getValueAndTrim(".sc-code"); const strPasswordVal = getValueAndTrim(".sc-password"); const strConfirmPasswordval = getValueAndTrim(".sc-confirm-password"); if (isLogin ==1) { //登录 if (intChangIndex === 0) { if (isValidUsername(strUsernameVal) && isValidPassword(strPasswordVal)) { await handleSubmit(strUsernameVal, strPasswordVal,isLogin); } } else { if (isValidPhone(strPhoneVal) && isValidPassword(strPasswordVal)) { await handleSubmit(strPhoneVal, strPasswordVal,isLogin); } } } else if (isLogin ==2) { //注册 if (validateEmail(strUsernameVal) && isValidPassword(strPasswordVal, strConfirmPasswordval)) { await handleSubmit(strUsernameVal, strPasswordVal,isLogin,strConfirmPasswordval); } }else if (isLogin ==3){ //忘记密码 if (isValidPhone(strPhoneVal) && isValidCode(strCodeVal) && isValidPassword(strPasswordVal, strConfirmPasswordval)) { await handleSubmit(strPhoneVal,strPasswordVal,isLogin,strConfirmPasswordval,strCodeVal); } } }; const handleSubmit = async (username, password,isLogin,confirmpassword,code,) => { if(isLogin == 1){ if(intChangIndex == 0){ userLogin(username, password) }else{ phoneLogin(username, password) } }else if(isLogin == 2){ userRegister(username, password) }else if(isLogin == 3){ forgetPassWord(username, password,code) } }; /* mu_email string Y 用户邮箱 mu_pwd string Y 用户密码 si_id int Y 站点 ID */ //用户名登录 function userLogin(username, password) { let postData = { 'mu_email':username, 'mu_pwd':password, 'si_id':strSite, } fetchApi(apiLoginByEmail, postData,successFun, onError) function successFun(res){ if (res.code == '000') { layerPopup(res.message, 3) localStorage.setItem(storageObj.tokenKey, res.data.token) localStorage.setItem(storageObj.loginStatus, true) localStorage.setItem(storageObj.isMember, res.data.mu_is_vip) localStorage.setItem(storageObj.userInfo, res.data) history.go(-1) // getInfo() } else if (res.code == '014') { // 需要重置密码 strSetPwdKey = res.data.key localStorage.setItem(storageObj.loginStatus, true) layerPopup(res.message, 3) } else { layerPopup('出错啦!请重试', 3) } } } //手机登录 function phoneLogin(username, password) { let postData = { 'phone':username, 'password':password, 'si_id':strSite, } fetchApi(apiLoginByPhone, postData,successFun, onError) function successFun(res){ if (res.code == '000') { layerPopup(res.message, 3) localStorage.setItem(storageObj.tokenKey, res.data.token) localStorage.setItem(storageObj.loginStatus, true) localStorage.setItem(storageObj.isMember, res.data.mu_is_vip) localStorage.setItem(storageObj.userInfo, res.data) // history.go(-1) // getInfo() }else if (res.code == '014') { // 需要重置密码 strSetPwdKey = res.data.key localStorage.setItem(storageObj.loginStatus, true) layerPopup(res.message, 3) } else { layerPopup('出错啦!请重试', 3) } } } //用户注册 function userRegister(username, password) { let postData = { 'mu_email':username, 'mu_pwd':password, 'si_id':strSite, mu_channel_code: localStorage.getItem(EncAndDec.encryptData('dlfx_channelcode')) || '', mu_channel_domain: localStorage.getItem(EncAndDec.encryptData('dlfx_sourceDomain')) || '', mu_referrer_id: localStorage.getItem(EncAndDec.encryptData('dlfx_muReferrerId')) || '' } fetchApi(apiReGist, postData,successFun, onError) function successFun(res){ if (res.code == '000') { //$(".sc-public-popup").show() layerPopup('注册成功!' ,3) let time = setTimeout(()=>{ clearTimeout(time) history.go(-1) },1500) }else{ layerPopup('请求超时,请稍后再试', 3) } } } //忘记密码 function forgetPassWord(username, password,code) { let postData = { 'phone':username, 'password':password, 'code':code, } fetchApi(apiLoginByPhone, postData,successFun, onError) function successFun(res){ console.log(res.message) layerPopup(res.message, 3) console.log(res) } } //用户详情 function getInfo() { fetchApi(apiGetUserInfo, null,successFn, onError) function successFn(res){ if (res.code == '000') { var data = res.data localStorage.setItem(storageObj.userInfo, JSON.stringify(data)) let timestamp = Date.parse(new Date()); if (data.mu_vip_expired != null) { localStorage.setItem(storageObj.isMember, true) let vip_timesStamp = new Date(data.mu_vip_expired).getTime() let seconds = 2 * 24 * 60 * 60 * 1000 if (vip_timesStamp - timestamp > seconds) { console.log('大于2天') history.go(-1) } else { console.log('小于2天') if (vip_timesStamp < timestamp) { localStorage.setItem(storageObj.isMember, false) $('.charge-dialog').show() } else { $('.vip-dialog').show() $('.tip-date').text('你的会员即将过期,请及时续费,续费会员,纵想激情') } } } else { // $(".sc-vip_modal").show() // $('.yh-expire').show() localStorage.setItem(storageObj.isMember, false) } } else { layerPopup(res.msg, 3) } } } //登录 $('.sc-login-btn').on('click', function() { submitForm(1); }); //注册 $('.sc-register-btn').on('click', function() { submitForm(2); }); //忘记密码 $('.sc-forget-btn').on('click', function() { submitForm(3); }); // 登录页 用户登录/手机登录切换 var a = $(".grid .login_item"); var c = $(".grid .input-pu-lic") for (var i = 0; i < a.length; i++) { a[i].index = i; a[i].setAttribute("index", i); a[i].onclick = function () { for (var i = 0; i < a.length; i++) { a[i].className = 'undefined point login_item' c[i].className = 'align_center gap10 input-pu-lic ee58ce4adf'; } this.className = '_2a4b899e3f point login_item'; var index = this.getAttribute("index"); intChangIndex = index c[index].className = 'fl align_center gap10 _53fc7f89df _17d18e62bf input-pu-lic'; $(".sc-username").val('') $(".sc-password").val('') $(".sc-phone").val(''); $('.sc-username-error').text(`请输入邮箱`); $('.sc-password-error').text(`请输入密码`); $('.sc-phone-error').text(`请填写正确的手机号码`); } } /** * 发送验证码 */ const sendMsg = () => { $(".yzm-btn").on('click', function () { const intPhone = getValueAndTrim(".sc-phone"); if (isValidPhone(intPhone)) { getSmsCode(intPhone); }else{ // $(".sc-public-popup").show() // $(".sc-public-popup .sc-trends-text").text(`请填写正确的手机号码`); } }); function getSmsCode(strPhone) { let postData = { 'phone':strPhone, 'type':'reg', } let timerCount = 60; let timerCctvQiDon = setInterval(() => { if (timerCount <= 0) { $getCodeButton.text('获取验证码'); clearInterval(timerCctvQiDon); } else { timerCount--; $getCodeButton.text(`${timerCount} s`); } }, 1000); } } $(".sc-junp-vip-buy").on('click', function () { window.open('/taochang'); }); $(function () { sendMsg() }); });