vue項目實現記住密碼到cookie功能

vue項目實現記住密碼到cookie功能

一、實現功能:

1.記住密碼勾選,點登陸時,將賬號和密碼保存到cookie,下次登陸自動顯示到表單內
2.不勾選,點登陸時候則清空之前保存到cookie的值,下次登陸需要手動輸入
在這裏插入圖片描述

二、思路:

大體思路就是通過存/取/刪cookie實現的;每次進入登錄頁,先去讀取cookie,如果瀏覽器的cookie中有賬號信息,就自動填充到登錄框中,存cookie是在登錄成功之後,判斷當前用戶是否勾選了記住密碼,如果勾選了,則把賬號信息存到cookie當中,效果圖如上:

三、具體實現:

html部分

<div class="ms-login">
        <div class="login-form">
                        <el-form ref="form" :model="form" :rules="rules">
                            <el-form-item prop="name">
                                <el-input v-model="form.name" placeholder="用戶名" prefix-icon="el-icon-user">
                                    <!--<i slot="prefix" class="el-input__icon el-icon-user"></i>-->
                                </el-input>
                            </el-form-item>
                            <el-form-item prop="password">
                                <el-input type="password" v-model="form.password" @keyup.enter.native="onSubmit('form')" placeholder="密   碼" prefix-icon="el-icon-lock"></el-input>
                            </el-form-item>
                            <el-form-item prop="rem">
                                <el-checkbox v-model="form.rem">記住密碼,下次直接登錄</el-checkbox>
                            </el-form-item>
                            <el-form-item>
                                <el-button @click="onSubmit('form')" v-loading="submitState">登 錄</el-button>
                                <!--<el-button @click="testHttp">Test</el-button>-->
                            </el-form-item>
                        </el-form>
                    </div>
    </div>

js部分:


  data() {
            return {
                submitState: false,
                form: {
                    name: '',
                    password: '',
                    rem: false
                }
            }
        },
    mounted() {
   		//頁面加載調用獲取cookie值
        this.getCookie();
    },
    methods: {
        /**
             * 提交
             * @param formName
             */
            onSubmit(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.submitState = true;
                        const self = this;
                        //判斷複選框是否被勾選 勾選則調用配置cookie方法
                        if (self.form.rem == true) {
                            console.log("checked == true");
                            //傳入賬號名,密碼,和保存天數3個參數
                            self.setCookie(self.form.name, self.form.password,self.form.rem, 30);
                        }else {
                            console.log("清空Cookie");
                            //清空Cookie
                            self.clearCookie();
                        }

						//與後端請求代碼,本功能不需要與後臺交互所以省略
                        console.log("登陸成功");

                        this.$axios.post('/public/user/login',{pwd:this.form.password,username:this.form.name}).then(res => {
                            if (res.data.code === this.$webConfig.httpSuccessStatus) {
                                localStorage.accountName = res.data.data.real_name;
                                this.$ls.set(this.$webConfig.authTokenName,res.data.data);
                            }else {
                                this.$message(res.data.message);
                            }
                            this.submitState = false;
                        }).catch(()=>{
                            this.submitState = false;
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
              //設置cookie
            setCookie(c_name, c_pwd,c_status, exdays) {
                var exdate = new Date(); //獲取時間
                exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays); //保存的天數
                //字符串拼接cookie
                window.document.cookie = "userName" + "=" + c_name + ";path=/;expires=" + exdate.toGMTString();
                window.document.cookie = "userPwd" + "=" + c_pwd + ";path=/;expires=" + exdate.toGMTString();
                window.document.cookie = "userPwdStatus" + "=" + c_status + ";path=/;expires=" + exdate.toGMTString();
                console.log(window.document.cookie);
            },
            //讀取cookie
            getCookie: function() {
                if (document.cookie.length > 0) {
                    var arr = document.cookie.split('; '); //這裏顯示的格式需要切割一下自己可輸出看下
                    for (var i = 0; i < arr.length; i++) {
                        var arr2 = arr[i].split('='); //再次切割
                        //判斷查找相對應的值
                        console.log(arr,arr2);
                        if (arr2[0] == 'userName') {
                            this.form.name = arr2[1]; //保存到保存數據的地方
                        } else if (arr2[0] == 'userPwd') {
                            this.form.password = arr2[1];
                        } else if (arr2[0] == 'userPwdStatus') {
                            if(arr2[1] == 'true'){
                                this.form.rem = true;
                            }else {
                                this.form.rem = false;
                            }
                        }
                    }
                }
            },
            //清除cookie
            clearCookie: function() {
                this.setCookie("", "", -1); //修改2值都爲空,天數爲負1天就好了
            }
    

瀏覽器中的cookie信息如下圖,注意這裏的cookie的expire/Max-Age過期時間,這個時間是格林尼治標準時間GMT,世界統一的時間,GMT+8小時就是北京時間。(這裏不做加密功能)
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章