layui.use(['form', 'layer'], function () { window.form = layui.form window.layer = layui.layer; }); form.render() // 退出用户登录,清除用户信息 function loginOut(href) { window.localStorage.removeItem('token_ss') window.localStorage.removeItem('ss_username') window.localStorage.removeItem('ss_currentName') window.localStorage.removeItem('ss_currentMobile') if (href) { window.location.href = href } } // 获取地址栏参数 $.getUrlParam = function (name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); var r = window.location.search.substr(1).match(reg); if (r != null) return decodeURI(r[2]); return null; } //文章搜索 $(document).ready(function () { var searchName = $.getUrlParam('search') if (searchName) { $("#searchName").val(searchName); } }); var currentHref = window.location.href.split('?')[0] var searchValue $('.go-search').click(function (e) { e.preventDefault(); searchValue = $("#searchName").val(); window.location.href = currentHref + '?search=' + searchValue }); // 发送验证码倒计时 var yzmDjs = true var countdown = 60 function settime() { if (countdown == 0) { $('#getCode').text('获取验证码') $('#getCode').css('cursor', 'pointer') $('#getCode').attr('disabled', false) countdown = 60 yzmDjs = true } else { $('#getCode').css('cursor', 'not-allowed') $('#getCode').text(countdown + 's') $('#getCode').attr('disabled', true) countdown-- yzmDjs = false setTimeout(function () { settime() }, 1000) } } // 用户登录 form.on('submit(userLogin)', function (data) { var returnUrl = $.getUrlParam('returnUrl') || '/index.html' $('#userLogin').attr('disabled', true); $('#userLogin').css('cursor', 'not-allowed'); $.ajax({ type: "POST", url: "/login.html", data: data.field, dataType: "json", success: function (response) { if (response.retCode == 200) { window.localStorage.setItem('token_ss', String(response.retValue.token)) window.localStorage.setItem('ss_username', String(response.retValue.email)); window.localStorage.setItem('ss_currentName', String(response.retValue.name)); window.localStorage.setItem('ss_currentMobile', String(response.retValue.mobile)); layer.msg('登录成功!即将跳转到之前的页面...'); setTimeout(function () { window.location.href = returnUrl }, 1000); } else { layer.msg(response.retDesc) $('#userLogin').attr('disabled', false); $('#userLogin').css('cursor', 'pointer'); } } }); return false; }); // 发送验证码 $('#getCode').on('click',function(){ var myreg = /^([\.a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/; if($('.email').val() == ''){ layer.msg('邮箱不能为空',{ icon:5, time:3000 }) }else if(!myreg.test($('.email').val())){ layer.msg('邮箱格式有误',{ icon:5, time:3000 }) }else{ settime() $.ajax({ type: "POST", url: "/index.htmp/Login/code", data: { email: $('.email').val() }, dataType: "json", success: function (response) { layer.msg('验证码已发送,请查收!'); if (!response.retCode === 200) { layer.msg(response.retDesc); } } }); } return false; }) // 找回密码 form.on('submit(passwordReset)', function (data) { var returnUrl = $.getUrlParam('returnUrl') || '/index.html' if (data.field.pwd !== data.field.password) { layer.msg('两次输入的密码不一致,请重新输入'); } else { $.ajax({ type: "POST", url: "/index.htmp/Login/reset_password", data: data.field, dataType: "json", success: function (response) { if (response.retCode == 200) { layer.msg('修改密码成功!即将跳转到登录页面'); setTimeout(function () { window.location.href = 'login.html' }, 1000); } else { layer.msg(response.retDesc) } } }); } return false }) // 修改用户基本资料 // updateInfo.html form.on('submit(infoEdit)', function (data) { data.field.token = String(window.token_ss); $.ajax({ type: "POST", url: "/updateInfo.html", data: data.field, dataType: "json", success: function (response) { if (response.retCode == 200) { layer.msg('用户基本资料修改成功!'); setTimeout(function () { //loginOut('/login.html?returnUrl=updateInfo.html') location.reload(); }, 1000); } else { layer.msg(response.retDesc) } } }); return false }) //修改密码 //updatePassword.html form.on('submit(passwordEdit)', function (data) { data.field.token = String(window.token_ss); if (data.field.pwd !== data.field.repwd) { layer.msg('两次输入的密码不一致!') return false } $.ajax({ type: "POST", url: "/updatePassword.html", data: data.field, dataType: "json", success: function (response) { if (response.retCode == 200) { layer.msg('修改密码成功!'); setTimeout(function () { loginOut('/login.html?returnUrl=updateInfo.html') }, 1000); } else { layer.msg(response.retDesc) } } }); return false }) // 下载鉴权 var fileList = $('.file-list li a.pdf-preview') var windowWidth = $(window).width(); var token_ss = window.localStorage.getItem('token_ss'); fileList.on('click', function () { var This = $(this); if (!token_ss) { window.location.href = '/login.html?returnUrl=' + window.location.href + '' } else { if (windowWidth > 1024) { if (This.attr('data-end') == 'pdf') { layer.open({ type: 1, area: ['90%', '90%'], title: false, content: $('#pdfPreview'), success: function () { PDFObject.embed(This.parent().find('a').data('href'), "#pdfPreview"); }, }) } else { This.attr('href', This.attr('data-href')); This.click(); } } else { $.each(fileList, function (index, ele) { $(ele).attr('href', $(ele).data('href')); }); } } }) $('.files-list li a.pdf-preview').on('click', function () { var This = $(this); if (windowWidth > 1024) { if (This.attr('data-end') == 'pdf') { layer.open({ type: 1, area: ['90%', '90%'], title: false, content: $('#pdfPreview'), success: function () { PDFObject.embed(This.parent().find('a').data('href'), "#pdfPreview"); }, }) } else { This.attr('href', This.attr('data-href')); This.click(); } } else { $.each(fileList, function (index, ele) { $(ele).attr('href', $(ele).data('href')); }); } }) // 退出登录 $('.btn-loginout').click(function (e) { e.preventDefault(); loginOut('/index.html') });