简单的登录

我会带着你远行 2022-05-29 22:43 241阅读 0赞
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>无标题文档</title>
  6. <script src="jquery-2.1.1.js"></script>
  7. <script src="apis.js"></script>
  8. <script src="logins.js"></script>
  9. </head>
  10. <body>
  11. <p>name:<input type="text" id="card_num"/></p>
  12. <p>pwd:<input type="text" id="Password"/></p>
  13. <button type="button" class="login_btn">登录</button>
  14. </body>
  15. </html>
  16. apis.js
  17. var root = "http://211.95.60.40:16868";
  18. var api={
  19. loginApi :root + "/health/carduser",
  20. }
  21. //两个参数,一个是cookie的名子,一个是值 function SetCookie(name, value) {
  22. var Days = 30; //此 cookie 将被保存 30 天 var exp = new Date(); //new Date("December 31, 9998"); exp.setTime(exp.getTime() + Days * 60 * 1000);
  23. document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
  24. }
  25. //取cookies函数 function getCookie(name) {
  26. var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
  27. if (arr != null) return unescape(arr[2]);
  28. return null;
  29. }
  30. //删除cookie function delCookie(name) {
  31. var exp = new Date();
  32. exp.setTime(exp.getTime() - 1);
  33. var cval = getCookie(name);
  34. if (cval != null) document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
  35. }
  36. // 日期去横杠 function toStr(str) {
  37. return str.split('-').join('');
  38. }
  39. function changeDate2(val) {
  40. var date = new Date().getTime() - (val * 24 * 60 * 60 * 1000);
  41. var ndate = new Date(date);
  42. var Y = ndate.getFullYear() + '-';
  43. var M = (ndate.getMonth() + 1 < 10 ? '0' + (ndate.getMonth() + 1) : (ndate.getMonth() + 1)) + "-";
  44. var D = (ndate.getDate() < 10 ? '0' + (ndate.getDate()) : ndate.getDate() + "");
  45. ndate = Y + M + D;
  46. return ndate;
  47. }
  48. //获取当前时间 function changeDate() {
  49. var ndate = new Date();
  50. var Y = ndate.getFullYear() + '-';
  51. var M = (ndate.getMonth() + 1 < 10 ? '0' + (ndate.getMonth() + 1) : (ndate.getMonth() + 1)) + '-';
  52. var D = (ndate.getDate() < 10 ? '0' + (ndate.getDate()) : ndate.getDate());
  53. ndate = Y + M + D;
  54. return ndate;
  55. }
  56. // 本地存储 localS function setLocVal(key, value) {
  57. window.localStorage[key] = value;
  58. }
  59. function getLocVal(key) {
  60. if (window.localStorage[key])
  61. return window.localStorage[key];
  62. else return "";
  63. }
  64. function delLocVal(key) {
  65. if (window.localStorage[key])
  66. return "" }
  67. // setLocVal("A",arr) // getLocVal("A") // 验证cookie是否过期 function loginAgain() {
  68. var cookie = getCookie("cardid");
  69. if (cookie == null) {
  70. window.location.href = "nocookie.html";
  71. }
  72. }
  73. // js数字转化成金额格式 function toMoney(num) {
  74. num = num.toFixed(2);
  75. num = parseFloat(num)
  76. num = num.toLocaleString();
  77. return num; //返回的是字符串23,245.12保留2位小数 }

logins.js

  1. $(function () {
  2. //定义一个全局变量 var func={
  3. //cardid card_id:"",
  4. //password pwd:"",
  5. //登录开始 loginEve:function () {
  6. //开始进行登录 $(".login_btn").click(function () {
  7. func.card_id=$("#card_num").val();
  8. func.pwd=$("#Password").val();
  9. //非空验证 if(func.card_id=="")
  10. {
  11. alert("卡号不能为空!");
  12. return;
  13. }else if(func.pwd=="")
  14. {
  15. alert("密码错误!!!");
  16. return;
  17. }else {
  18. func.httpRequest(func.card_id,func.pwd);
  19. }
  20. })
  21. },
  22. //接口请求 httpRequest:function(id,pwd)
  23. {
  24. $.ajax({
  25. url:api.loginApi,
  26. type:"post",
  27. dataType:"json",
  28. data:{
  29. cardid: id,
  30. password: pwd
  31. },
  32. success:function (data) {
  33. if(data)
  34. {
  35. if(data.code==101)
  36. {
  37. alert("卡号错误");
  38. return;
  39. }else if (data.code==102)
  40. {
  41. alert("密码错误!!!");
  42. return;
  43. }else if(data.code==200)
  44. {
  45. // 存储到本地的 LocalStorage中 var arrs = []; // 创建空数组 if (getLocVal("arrs")) { // 如果本地存储中已经有数组存在 arrs = []; // 将数组取出 } setLocVal("arrs", JSON.stringify(arrs)); // 再次存到localStorage 中 // 用户 id 获取 // var planCookie = getCookie("cardid"); var planCookie = 2;
  46. // 存储cookie // SetCookie("cardid", planCookie); window.location.href = "operapro.html";
  47. }
  48. }
  49. },
  50. error:function (xhr) {
  51. console.log(data) ;
  52. }
  53. })
  54. }
  55. }
  56. //执行事件 func.loginEve();
  57. })

发表评论

表情:
评论列表 (有 0 条评论,241人围观)

还没有评论,来说两句吧...

相关阅读