<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <script src="jquery-2.1.1.js"></script> <script src="apis.js"></script> <script src="logins.js"></script> </head> <body> <p>name:<input type="text" id="card_num"/></p> <p>pwd:<input type="text" id="Password"/></p> <button type="button" class="login_btn">登录</button> </body> </html>
apis.js
var root = "http://211.95.60.40:16868"; var api={ loginApi :root + "/health/carduser", } //两个参数,一个是cookie的名子,一个是值 function SetCookie(name, value) { var Days = 30; //此 cookie 将被保存 30 天 var exp = new Date(); //new Date("December 31, 9998"); exp.setTime(exp.getTime() + Days * 60 * 1000); document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString(); } //取cookies函数 function getCookie(name) { var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)")); if (arr != null) return unescape(arr[2]); return null; } //删除cookie function delCookie(name) { var exp = new Date(); exp.setTime(exp.getTime() - 1); var cval = getCookie(name); if (cval != null) document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString(); } // 日期去横杠 function toStr(str) { return str.split('-').join(''); } function changeDate2(val) { var date = new Date().getTime() - (val * 24 * 60 * 60 * 1000); var ndate = new Date(date); var Y = ndate.getFullYear() + '-'; var M = (ndate.getMonth() + 1 < 10 ? '0' + (ndate.getMonth() + 1) : (ndate.getMonth() + 1)) + "-"; var D = (ndate.getDate() < 10 ? '0' + (ndate.getDate()) : ndate.getDate() + ""); ndate = Y + M + D; return ndate; } //获取当前时间 function changeDate() { var ndate = new Date(); var Y = ndate.getFullYear() + '-'; var M = (ndate.getMonth() + 1 < 10 ? '0' + (ndate.getMonth() + 1) : (ndate.getMonth() + 1)) + '-'; var D = (ndate.getDate() < 10 ? '0' + (ndate.getDate()) : ndate.getDate()); ndate = Y + M + D; return ndate; } // 本地存储 localS function setLocVal(key, value) { window.localStorage[key] = value; } function getLocVal(key) { if (window.localStorage[key]) return window.localStorage[key]; else return ""; } function delLocVal(key) { if (window.localStorage[key]) return "" } // setLocVal("A",arr) // getLocVal("A") // 验证cookie是否过期 function loginAgain() { var cookie = getCookie("cardid"); if (cookie == null) { window.location.href = "nocookie.html"; } } // js数字转化成金额格式 function toMoney(num) { num = num.toFixed(2); num = parseFloat(num) num = num.toLocaleString(); return num; //返回的是字符串23,245.12保留2位小数 }
logins.js
$(function () { //定义一个全局变量 var func={ //cardid card_id:"", //password pwd:"", //登录开始 loginEve:function () { //开始进行登录 $(".login_btn").click(function () { func.card_id=$("#card_num").val(); func.pwd=$("#Password").val(); //非空验证 if(func.card_id=="") { alert("卡号不能为空!"); return; }else if(func.pwd=="") { alert("密码错误!!!"); return; }else { func.httpRequest(func.card_id,func.pwd); } }) }, //接口请求 httpRequest:function(id,pwd) { $.ajax({ url:api.loginApi, type:"post", dataType:"json", data:{ cardid: id, password: pwd }, success:function (data) { if(data) { if(data.code==101) { alert("卡号错误"); return; }else if (data.code==102) { alert("密码错误!!!"); return; }else if(data.code==200) { // 存储到本地的 LocalStorage中 var arrs = []; // 创建空数组 if (getLocVal("arrs")) { // 如果本地存储中已经有数组存在 arrs = []; // 将数组取出 } setLocVal("arrs", JSON.stringify(arrs)); // 再次存到localStorage 中 // 用户 id 获取 // var planCookie = getCookie("cardid"); var planCookie = 2; // 存储cookie // SetCookie("cardid", planCookie); window.location.href = "operapro.html"; } } }, error:function (xhr) { console.log(data) ; } }) } } //执行事件 func.loginEve(); })