• Cookie保存用户名和账户密码


    cookiesRemember.js

    //得到最后登录的用户
    function GetLastUser() {
        var id = "49BAC005-7D5B-4231-8CEA-16939BEACD67";//GUID标识符
        var name = GetCookie(id);//取Cookie的值
        if (name != null) {
            document.getElementById('user_name').value = name;
        } else {
            document.getElementById('user_name').value = "";
        }
        GetPwdAndChk();//查看是否有对应的密码存在cookie
    }
    //用户名失去焦点时调用该方法,查看是否有对应的密码存在cookie
    function GetPwdAndChk() {
    var name = $("#user_name").val();
    var password = GetCookie(name);//取Cookie的值
        if (password != null) {
        document.getElementById('checkboxLogin').checked = true;
            document.getElementById('user_password').value = password;
        } else {
        document.getElementById('checkboxLogin').checked = false;
            document.getElementById('user_password').value = "";
        }
    }
    //取Cookie的值
    function GetCookie(name) {
        var arg = name + "=";
        var alen = arg.length;
        var clen = document.cookie.length;
        var i = 0;
        while (i < clen) {
            var j = i + alen;
           /*  alert(j); */
            if (document.cookie.substring(i, j) == arg) 
            return getCookieVal(j);//取到cookie的值
            i = document.cookie.indexOf(" ", i) + 1;
            if (i == 0) 
            break;
        }
        return null;
    }
    //取到cookie的值
    function getCookieVal(offset) {
    var endstr = document.cookie.indexOf(";", offset);
    if (endstr == -1)
    endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
    }
    //保存最后登录的用户的用户名
    function SetLastUser(name) {
        var id = "49BAC005-7D5B-4231-8CEA-16939BEACD67";
        var expdate = new Date();
        //当前时间加上两周的时间
        expdate.setTime(expdate.getTime() + 14 * (24 * 60 * 60 * 1000));
        SetCookie(id, name, expdate);//写入到Cookie 
    }
    //写入到Cookie 
    function SetCookie(name, value, expires) {
      var argv = SetCookie.arguments;
      //本例中length = 3
      var argc = SetCookie.arguments.length;
      var expires = (argc > 2) ? argv[2] : null;
      var path = (argc > 3) ? argv[3] : null;
      var domain = (argc > 4) ? argv[4] : null;
      var secure = (argc > 5) ? argv[5] : false;
      document.cookie = name + "=" + escape(value) + ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + ((path == null) ? "" : ("; path=" + path)) + ((domain == null) ? "" : ("; domain=" + domain)) + ((secure == true) ? "; secure" : "");
    }
    //如果没有选中记住密码,而当前用户又是cookie里面存在的,也可以清除掉cookie
    function ResetCookie() {
    alert("没有选中记住密码");
    var name = $("#user_name").val();
    var expdate = new Date();
    SetCookie(name, null, expdate);//写入到Cookie 
    }

    user-login.jsp

    <script type=text/javascript>
    
    $(function(){
    GetLastUser();
    });
    
    //登录
    function login(){
    var name = $("#user_name").val();
    var password = $("#user_password").val();
    var newUrl = "${url}";
    if(name == "" || password == ""){
    window.wxc.xcConfirm('用户名和密码不能为空', window.wxc.xcConfirm.typeEnum.info);
    return;
    }
    
    var params = {
        "name" : name,
        "password" : password
    };
    //将最后一个用户信息写入到Cookie
        SetLastUser(name);
    if(!$("#checkboxLogin").attr("checked")==false){
            alert("你选择了记住密码,我们将记住你的密码:"+password);
            var expdate = new Date();
            expdate.setTime(expdate.getTime() + 14 * (24 * 60 * 60 * 1000));
            //将用户名和密码写入到Cookie
            SetCookie(name,password, expdate);
    }
    else {
            //如果没有选中记住密码,则立即过期
            ResetCookie();
        }
    
    $.ajax({ 
    type: "POST",
    timeout: 20000,
    url: '<%=request.getContextPath()%>/user/passport/login',
    data: params,
    dataType:"json",
    success: function (data) {
    if(data.returnCode == 0){
    if(newUrl != null && newUrl != ""){ 
    window.location.href = "<%=request.getContextPath()%>"+"/"+newUrl;
    } else {
    window.location.href = "<%=request.getContextPath()%>/user/zonglan"; 
    }
    }else{
    window.wxc.xcConfirm(data.returnMsg, window.wxc.xcConfirm.typeEnum.error);
    }
          },
          error: function (XMLHttpRequest, textStatus, errorThrown) {
              if(textStatus == 'timeout'){
             window.wxc.xcConfirm('请求超时', window.wxc.xcConfirm.typeEnum.error);
              }else{
             window.wxc.xcConfirm('连接异常', window.wxc.xcConfirm.typeEnum.error);
                }
          },
          complete: function(xhr, ts){
              
            }
    });
    }
    
    </script>
    
    <input type="text" placeholder="请输入手机号/邮箱" class="login_int" id="user_name" onblur="GetPwdAndChk()" >
    
    <input type="password" placeholder="请输入密码" class="login_int" id="user_password">
    
    <a href="javascript:void(0);" onclick="login();">立即登录</a>
    

      

  • 相关阅读:
    3月4日毕设进度
    3月3日毕设进度
    3月2日毕设进度
    3月1日毕设进度
    2月29日毕设进度
    2月28日毕设进度
    2月27日毕设进度
    进度报告七 (重大技术需求调研)
    进度报告六-(重大技术需求调研)
    进度报告五 (重大技术需求调研)
  • 原文地址:https://www.cnblogs.com/nbkyzms/p/5040579.html
Copyright © 2020-2023  润新知