# 前端记住密码功能

本文作者:程序员飞云

本站地址:https://www.flycode.icu (opens new window)

java1234 (opens new window)

使用cookie来记住密码

# 1. 安装js-cookie依赖

"js-cookie": "^3.0.5"
1

# 2. 安装jsencrypt

存储用户密码,为了安全需要加密,获取密码需要解密

"jsencrypt": "^3.3.2"
1

# 3. 创建脚本

密钥对生成http://web.chacuo.net/netrsakeypair

import { JSEncrypt } from "jsencrypt";

// 密钥对生成http://web.chacuo.net/netrsakeypair
const publicKey =
  "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqjtN3wBrRUwGGijirOpW\n" +
  "sISg9Q6BlzagJGAYh9+0cgahL28NUFL6wHmagpURrsWsHZP8dt/9DOfFrqS+/R7f\n" +
  "VFyjclXH3XDDr4cP2+Wxgwn+PgJ3+TqkhHgdRzBQ4giCEGBJdsByUR8RN7iV+DQY\n" +
  "vLQrFrmZm5Jc/FcCqLUeNJW+kSvb75qtQV5hQq4O+BQN1TGoQPDGrN+02mxmNV0O\n" +
  "HI6U/iqTvPhDpHB6bM1jZUI7Jkeo8ySJblLmv/RtglPaV2TSDYiMWpFYy9VWmJcW\n" +
  "b0gAfUOK340negeFGUXK5Xt+jnJZ7VrVM5wIKP7ls7WPPW3yfwrx7L0Sc1w1stge\n" +
  "2QIDAQAB";

const privateKey =
  "MIIEwAIBADANBgkqhkiG9w0BAQEFAASCBKowggSmAgEAAoIBAQCqO03fAGtFTAYa\n" +
  "KOKs6lawhKD1DoGXNqAkYBiH37RyBqEvbw1QUvrAeZqClRGuxawdk/x23/0M58Wu\n" +
  "pL79Ht9UXKNyVcfdcMOvhw/b5bGDCf4+Anf5OqSEeB1HMFDiCIIQYEl2wHJRHxE3\n" +
  "uJX4NBi8tCsWuZmbklz8VwKotR40lb6RK9vvmq1BXmFCrg74FA3VMahA8Mas37Ta\n" +
  "bGY1XQ4cjpT+KpO8+EOkcHpszWNlQjsmR6jzJIluUua/9G2CU9pXZNINiIxakVjL\n" +
  "1VaYl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tRiCWgsRhM55NbfzcKgTKe6FuyT6KrwxZA+s5I/7XuFn/\n" +
  "n6NSLlDUJvDRvedW1c5ISCKEc8ViwL9zEvIQiwcoQPo2jiif51Lh99O6CoYEYmVa\n" +
  "JopEcMXLvBpYXe9xa7CZH1/SDCDC/qXVc7qScNS9F4ECgYEAiCEJYYLb5fA8Avx8\n" +
  "1SIKF9/Yspc0twi+wvNVtQnLiA/xsnyQMgaMUl9PyV4IkyOiR7C82iy3q2iy7X3S\n" +
  "wv2b+vgiX0KogJb2uyTeuGj+rNo53zF96iDlLhM5Hwr7xjXLvUhbhQxQJSWS4+Fz\n" +
  "lUyPBMbw1c1PVs3WS7efadCNY5k=";

// 加密
export function encrypt(txt) {
  const jsEncrypt = new JSEncrypt();
  jsEncrypt.setPublicKey(publicKey);
  return jsEncrypt.encrypt(txt);
}

// 解密
export function decrypt(text) {
  const jsEncrypt = new JSEncrypt();
  jsEncrypt.setPrivateKey(privateKey);
  return jsEncrypt.decrypt(text);
}
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

# 4. 前端界面

<template>
  <div class="login">
    <el-form ref="loginRef" :model="loginForm" :rules="loginRules" class="login-form">
      <h3 class="title">飞云定制旅游后台系统</h3>
      <el-form-item prop="username">
        <el-input
            v-model="loginForm.username"
            type="text"
            size="large"
            auto-complete="off"
            placeholder="账号"
            @keyup.enter="handleLogin"
        >
          <template #prefix>
            <svg-icon-index icon="user"/>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
            v-model="loginForm.password"
            type="password"
            size="large"
            auto-complete="off"
            placeholder="密码"
            @keyup.enter="handleLogin"
        >
          <template #prefix>
            <svg-icon-index icon="password"/>
          </template>
        </el-input>
      </el-form-item>

      <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
      <el-form-item style="width:100%;">
        <el-button
            size="large"
            type="primary"
            style="width:100%;"
            @click="handleLogin"
        >
          <span>登 录</span>

        </el-button>
        <!--            @keyup.enter="handleLogin"-->
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-login-footer">
      <!--      <span></span>-->
    </div>
  </div>
</template>

<script setup>

import SvgIconIndex from "@/components/SvgIcon/SvgIconIndex.vue";
import {ref} from 'vue'
import requestUtil from '@/utils/request'
import store from "@/store";
import {ElMessage} from "element-plus";
import qs from "qs";
import router from "@/router";
import {decrypt, encrypt} from "@/utils/jsencrypt";
import Cookies from "js-cookie";

const loginRef = ref(null);
const loginForm = ref({
  username: "",
  password: "",
  rememberMe: false
})

const loginRules = {
  username: [{required: true, trigger: "blur", message: "请输入你的账号"}],
  password: [{required: true, trigger: "blur", message: "请输入你的密码"}],
};


const handleLogin = () => {
  loginRef.value.validate(async (valid) => {
      // 记住密码,30天
      if (loginForm.value.rememberMe) {
        Cookies.set("username", loginForm.value.username, { expires: 30 });
        Cookies.set("password", encrypt(loginForm.value.password), {
          expires: 30,
        });
        Cookies.set("rememberMe", loginForm.value.rememberMe, {
          expires: 30,
        });
      } else {
        Cookies.remove("username");
        Cookies.remove("password");
        Cookies.remove("rememberMe");
      }
    if (valid) {
      let res = await requestUtil.post(
          "login?" +
          qs.stringify(loginForm.value)
      );

      if (res.data.code === 0) {
        store.commit('SET_TOKEN', res.data.data)
        console.log(res.data.data)
        ElMessage.success("登录成功")
        await router.replace("/")
      } else {
        ElMessage.error(res.data)
      }
    } else {
      console.log("error");
    }
  });
};

 // 获取cookie
function getCookie() {
  const username = Cookies.get("username");
  const password = Cookies.get("password");
  const rememberMe = Cookies.get("rememberMe");
  loginForm.value = {
    username: username === undefined ? loginForm.value.username : username,
    password:
        password === undefined ? loginForm.value.password : decrypt(password),
    rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
  };
}

getCookie();
</script>

<style lang="scss" scoped>
a {
  color: white
}

.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background: url("https://gw.alipayobjects.com/zos/rmsportal/FfdJeJRQWjEeGTpqgBKj.png") 0% 0% / 100% 100%;
  background-size: cover;
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;

  .el-input {
    height: 40px;


    input {
      display: inline-block;
      height: 40px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 0px;
  }

}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  height: 40px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial, cursive;
  font-size: 12px;
  letter-spacing: 1px;
}

.login-code-img {
  height: 40px;
  padding-left: 12px;
}
</style>
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
最近更新: 12/30/2024, 12:04:33 AM
飞云编程   |