服务器之家

服务器之家 > 正文

基于localStorge开发登录模块的记住密码与自动登录实例

时间:2020-05-14 12:37     来源/作者:jingxian

关于这个模块功能模块的由来,这是鸟大大的处女秀,为什么这么说呢?一天在群里,一个哥们说有私活,开发一个****模块,我那天手痒痒就和他聊了两句,然后,就决定给她做这个模块了,和他谈了谈交付时间,他说最迟两天,然后谈了谈加个,最后达成,500¥!!!这个模块其实第一天晚上我就开发出来了,那时我给他微信说,功能模块开发ok了,要不要远程查看一下,没问题的话就交了,一会他回我,好了就发过来,然后微信就转过来500¥,当时很诧异,毕竟是处女秀,然后就把项目交给他了,并且是完美交付,在客户那里,也没有出现问题!到如今想想,还激动啊!记录那个时刻--2016-3。

摘要:传动的记住密码与自动登录模块,都是基于cookie,但是cookie上做的话,有一些弊端,鸟看了就是cookie文件大小受限,所以本问叙述的是基于H5上的storge,本地持久化存储来做的自动登录和记住密码的,所以如果你不懂storge的话,建议先去充电!

充电:了解localstorge

备注:这是一个仿网页知乎的登录模块,如果想要完整源码,可以联系鸟哦

效果图:

基于localStorge开发登录模块的记住密码与自动登录实例

核心源码分享:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
 
  <title>登录 - 仿知乎 - Thousands Find</title>
  <link rel="stylesheet" type="text/css" href="style/register-login.css" rel="external nofollow" >
  <script type="text/javascript" src="js/jquery.min.js"></script>
  <script>
    $(document).ready(function () {
      //读取 localStage 本地存储,填充用户名密码,如果自动登录有值直接跳转; 
      //相反,跳转到本页面,等待登陆处理 
      var storage = window.localStorage;
      var getEmail = storage["email"];
      var getPwd = storage["password"];
      var getisstroepwd = storage["isstorePwd"];
      var getisautologin = storage["isautologin"];
      if ("yes" == getisstroepwd) {
        if ("yes" == getisautologin) {
          if ((("" != getEmail) || (null != getEmail)) && (("" != getPwd) || (null != getPwd))) {
            //lacoste 已经保存 登陆信息 直接登陆 
             //alert('正在自动登录');
            $("#email").val(getEmail);
            $("#password").val(getPwd);
            // window.location="";  
            //加载时显示:正在自动登录
            $.ajax({
              url: 'LoginServlet.ashx',
              data: {
                email: getEmail,
                password: getPwd
              },
              
              dataType: 'json',
              success: function (data) {
                if (data.msg == "") {
                  alert("账号信息异常,请核实后重新登录");
                } else {
                  //alert(123);
                  //登录成功后保存session,如果选择了记住密码,再保存到本地
                  window.location.href ='Default2.aspx';
                }
              },
              error: function () {
                alert("系统错误");
              }
            });
          }
        }
        else {
          $("#email").val(getEmail);
          $("#password").val(getPwd);
          document.getElementById("isRemberPwdId").checked = true;
        }
      }
    });
    
    
    function login() {
      var userEmail = $("#email").val();
      var userPassWord = $("#password").val();
      if (userEmail != "" && userPassWord != "") {
 
 
        var storage = window.localStorage;
        //记住密码 
        if (document.getElementById("isRemberPwdId").checked) {
          //存储到loaclStage  
          //alert(134);
          storage["email"] = userEmail;
          storage["password"] = userPassWord;
          storage["isstorePwd"] = "yes";
        }
        else {
          storage["email"] = userEmail;
          storage["isstorePwd"] = "no";
        }
 
        //下次自动登录 
        if (document.getElementById("isAutoLoginId").checked) {
          //存储到loaclStage  
          storage["email"] = userEmail;
          storage["password"] = userPassWord;
          storage["isstorePwd"] = "yes";
          storage["isautologin"] = "yes";
        }
        else {
          storage["email"] = userEmail;
          storage["isautologin"] = "no";
        }
        $.ajax({
          url: 'LoginServlet.ashx',
          data: {
            "email": userEmail,
            "password": userPassWord
          },
          dataType: 'json',
          success: function (data) {
            if (data.msg == "") {
              alert("用户名或密码错误");
            } else {
              alert("登陆成功");
              //登录成功后保存session,如果选择了记住密码,再保存到本地
              window.location.href = 'Default.aspx';
            }
          },
          error: function () {
            alert("系统错误1");
          }
        });
        //alert("登录成功");
      }
      else {
        alert("用户名密码不能为空");
      }
    }
    
  </script>
</head>
<body>
  <div id="box"></div>
  <div class="cent-box">
    <div class="cent-box-header">
      <h1 class="main-title hide">仿知乎</h1>
      <h2 class="sub-title">生活热爱分享 - Thousands Find</h2>
    </div>
 
    <div class="cont-main clearfix">
      <div class="index-tab">
        <div class="index-slide-nav">
          <a href="login.html" rel="external nofollow" class="active">登录</a>
          <a href="register.html" rel="external nofollow" >注册</a>
          <div class="slide-bar"></div>
        </div>
      </div>
      <form id="loginform" name="loginform" autocomplete="on" method="post">
        <div class="login form">
          <div class="group">
            <div class="group-ipt email">
              <input type="email" name="email" id="email" class="ipt" placeholder="邮箱地址" required/>
            </div>
            <div class="group-ipt password">
              <input type="password" name="password" id="password" class="ipt" placeholder="输入您的登录密码" required/>
            </div>
 
          </div>
        </div>
 
        <div class="button">
          <button type="button" class="login-btn register-btn" id="button" onclick="login()">登录</button>
        </div>
 
        <div class="remember clearfix">
 
          <label for="loginkeeping" class="remember-me">
            <input type="checkbox" name="isRemberPwdId" id="isRemberPwdId" class="remember-mecheck" checked />
            记住密码 
          </label>
 
          <label for="autologin" class="forgot-password">
            <input type="checkbox" name="isAutoLoginId" id="isAutoLoginId" class="remember-mecheck" checked />
            自动登录 
          </label>
 
        </div>
      </form>
    </div>
  </div>
 
  <div class="footer">
    <p>仿知乎 - Thousands Find</p>
    <p>copy@*.* 2016</p>
  </div>
 
  <script src='js/particles.js' type="text/javascript"></script>
  <script src='js/background.js' type="text/javascript"></script>
  <script src='js/jquery.min.js' type="text/javascript"></script>
  
  <script src='js/layer/layer.js' type="text/javascript"></script>
  <script src='js/index.js' type="text/javascript"></script>
 
</body>
</html>

最后总结一下:

这个模块是通用的,我们要做的是:

1.当用户点击登录的时候,首先拿到表单里的数据
2.做出判断,判断用户是否勾选记住密码 或者 自动登录

3.都没勾选,对数据进行加密,发到服务器端做登录校验,之后返回

4.勾选了记住密码,就将用户名密码保存到storge,核心代码赞一下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
var storage = window.localStorage;
        //记住密码 
        if (document.getElementById("isRemberPwdId").checked) {
          //存储到loaclStage  
          //alert(134);
          storage["email"] = userEmail;
          storage["password"] = userPassWord;
          storage["isstorePwd"] = "yes";
        }
        else {
          storage["email"] = userEmail;
          storage["isstorePwd"] = "no";
        }

记住,这时你已经勾选了记住密码,下次登录时,该如何操作?

在$(function (){})里,也就是浏览器渲染标签时,做出判断,看一下storge['isstorePwd']是否为yes,核心代码赞一赞

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$(document).ready(function () {
      //读取 localStage 本地存储,填充用户名密码,如果自动登录有值直接跳转; 
      //相反,跳转到本页面,等待登陆处理 
      var storage = window.localStorage;
      var getEmail = storage["email"];
      var getPwd = storage["password"];
      var getisstroepwd = storage["isstorePwd"];
      var getisautologin = storage["isautologin"];
      if ("yes" == getisstroepwd) {
        if ("yes" == getisautologin) {
          ....
          }
        }
        else {
          $("#email").val(getEmail);
          $("#password").val(getPwd);
          document.getElementById("isRemberPwdId").checked = true;
        }
      }
    });

ok 如果记住密码就搞定了!

5.自动登录:这个功能还用我说吗?和记住密码类似!

?
1
2
3
4
5
6
7
8
9
10
11
12
//下次自动登录 
        if (document.getElementById("isAutoLoginId").checked) {
          //存储到loaclStage  
          storage["email"] = userEmail;
          storage["password"] = userPassWord;//密码存到storage里
          storage["isstorePwd"] = "yes";
          storage["isautologin"] = "yes";
        }
        else {
          storage["email"] = userEmail;
          storage["isautologin"] = "no";
        }

当用户再次登录的时候,还是在一加载的时候,做出判断,是否勾选自动登录,勾选的话,从storage里拿到数据,直接发生异步

请求,就不用用户做出点击登录事件了!

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
if ("yes" == getisautologin) {
          if ((("" != getEmail) || (null != getEmail)) && (("" != getPwd) || (null != getPwd))) {
            //lacoste 已经保存 登陆信息 直接登陆 
             //alert('正在自动登录');
            $("#email").val(getEmail);
            $("#password").val(getPwd);
            // window.location="";  
            //加载时显示:正在自动登录
            $.ajax({
              url: 'LoginServlet.ashx',
              data: {
                email: getEmail,
                password: getPwd
              },
              
              dataType: 'json',
              success: function (data) {
                if (data.msg == "") {
                  alert("账号信息异常,请核实后重新登录");
                } else {
                  //alert(123);
                  //登录成功后保存session,如果选择了记住密码,再保存到本地
                  window.location.href ='Default2.aspx';
                }
              },
              error: function () {
                alert("系统错误");
              }
            });

以上这篇基于localStorge开发登录模块的记住密码与自动登录实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:http://www.cnblogs.com/gdsblog/archive/2017/08/24/7425917.html

相关文章

热门资讯

歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意
沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意 2019-07-07
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分 2019-06-21
男生常说24816是什么意思?女生说13579是什么意思?
男生常说24816是什么意思?女生说13579是什么意思? 2019-09-17
超A是什么意思 你好a表达的是什么
超A是什么意思 你好a表达的是什么 2019-06-06
返回顶部