Skip to content

Commit fae2d09

Browse files
committed
前端 Token、账号、密码等信息,统一使用 LocalStorage 替代 Cookie 存储
1 parent 067419a commit fae2d09

File tree

4 files changed

+157
-55
lines changed

4 files changed

+157
-55
lines changed

src/utils/auth.js

Lines changed: 81 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,96 @@
1-
import Cookies from 'js-cookie'
1+
import {decrypt, encrypt} from "@/utils/jsencrypt";
22

33
const AccessTokenKey = 'ACCESS_TOKEN'
44
const RefreshTokenKey = 'REFRESH_TOKEN'
55

6+
// ========== Token 相关 ==========
7+
68
export function getAccessToken() {
7-
return Cookies.get(AccessTokenKey)
9+
return localStorage.getItem(AccessTokenKey)
810
}
911

1012
export function getRefreshToken() {
11-
return Cookies.get(RefreshTokenKey)
13+
return localStorage.getItem(RefreshTokenKey)
1214
}
1315

1416
export function setToken(token) {
15-
Cookies.set(AccessTokenKey, token.accessToken)
16-
Cookies.set(RefreshTokenKey, token.refreshToken)
17+
localStorage.setItem(AccessTokenKey, token.accessToken)
18+
localStorage.setItem(RefreshTokenKey, token.refreshToken)
1719
}
1820

1921
export function removeToken() {
20-
Cookies.remove(AccessTokenKey)
21-
Cookies.remove(RefreshTokenKey)
22+
localStorage.removeItem(AccessTokenKey)
23+
localStorage.removeItem(RefreshTokenKey)
24+
}
25+
26+
// ========== 账号相关 ==========
27+
28+
const UsernameKey = 'USERNAME'
29+
const PasswordKey = 'PASSWORD'
30+
const RememberMeKey = 'REMEMBER_ME'
31+
32+
export function getUsername() {
33+
return localStorage.getItem(UsernameKey)
34+
}
35+
36+
export function setUsername(username) {
37+
localStorage.setItem(UsernameKey, username)
38+
}
39+
40+
export function removeUsername() {
41+
localStorage.removeItem(UsernameKey)
42+
}
43+
44+
export function getPassword() {
45+
const password = localStorage.getItem(PasswordKey)
46+
return password ? decrypt(password) : undefined
47+
}
48+
49+
export function setPassword(password) {
50+
localStorage.setItem(PasswordKey, encrypt(password))
51+
}
52+
53+
export function removePassword() {
54+
localStorage.removeItem(PasswordKey)
55+
}
56+
57+
export function getRememberMe() {
58+
return localStorage.getItem(RememberMeKey) === 'true'
59+
}
60+
61+
export function setRememberMe(rememberMe) {
62+
localStorage.setItem(RememberMeKey, rememberMe)
63+
}
64+
65+
export function removeRememberMe() {
66+
localStorage.removeItem(RememberMeKey)
67+
}
68+
69+
// ========== 租户相关 ==========
70+
71+
const TenantIdKey = 'TENANT_ID'
72+
const TenantNameKey = 'TENANT_NAME'
73+
74+
export function getTenantName() {
75+
return localStorage.getItem(TenantNameKey)
76+
}
77+
78+
export function setTenantName(username) {
79+
localStorage.setItem(TenantNameKey, username)
80+
}
81+
82+
export function removeTenantName() {
83+
localStorage.removeItem(TenantNameKey)
84+
}
85+
86+
export function getTenantId() {
87+
return localStorage.getItem(TenantIdKey)
88+
}
89+
90+
export function setTenantId(username) {
91+
localStorage.setItem(TenantIdKey, username)
92+
}
93+
94+
export function removeTenantId() {
95+
localStorage.removeItem(TenantIdKey)
2296
}

src/utils/request.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import axios from 'axios'
22
import {Message, MessageBox, Notification} from 'element-ui'
33
import store from '@/store'
4-
import {getAccessToken, getRefreshToken, setToken} from '@/utils/auth'
4+
import {getAccessToken, getRefreshToken, getTenantId, setToken} from '@/utils/auth'
55
import errorCode from '@/utils/errorCode'
6-
import Cookies from "js-cookie";
76
import {getPath, getTenantEnable} from "@/utils/ruoyi";
87
import {refreshToken} from "@/api/login";
98

@@ -21,14 +20,15 @@ let requestList = []
2120
// 是否正在刷新中
2221
let isRefreshToken = false
2322

24-
2523
axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
2624
// 创建axios实例
2725
const service = axios.create({
2826
// axios中请求配置有baseURL选项,表示请求URL公共部分
2927
baseURL: process.env.VUE_APP_BASE_API + '/admin-api/', // 此处的 /admin-api/ 地址,原因是后端的基础路径为 /admin-api/
3028
// 超时
31-
timeout: 30000
29+
timeout: 30000,
30+
// 禁用 Cookie 等信息
31+
withCredentials: false,
3232
})
3333
// request拦截器
3434
service.interceptors.request.use(config => {
@@ -39,7 +39,7 @@ service.interceptors.request.use(config => {
3939
}
4040
// 设置租户
4141
if (getTenantEnable()) {
42-
const tenantId = Cookies.get('tenantId');
42+
const tenantId = getTenantId();
4343
if (tenantId) {
4444
config.headers['tenant-id'] = tenantId;
4545
}
@@ -79,7 +79,6 @@ service.interceptors.response.use(async res => {
7979
// 获取错误信息
8080
const msg = res.data.msg || errorCode[code] || errorCode['default']
8181
if (ignoreMsgs.indexOf(msg) !== -1) { // 如果是忽略的错误码,直接返回 msg 异常
82-
console.log('132312311');
8382
return Promise.reject(msg)
8483
} else if (code === 401) {
8584
// 如果未认证,并且未进行刷新令牌,说明可能是访问令牌过期了
@@ -166,7 +165,7 @@ service.interceptors.response.use(async res => {
166165
export function getBaseHeader() {
167166
return {
168167
'Authorization': "Bearer " + getAccessToken(),
169-
'tenant-id': Cookies.get('tenantId'),
168+
'tenant-id': getTenantId(),
170169
}
171170
}
172171

src/views/login.vue

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,17 @@
107107
import {getCodeImg, sendSmsCode, socialAuthRedirect} from "@/api/login";
108108
import {getTenantIdByName} from "@/api/system/tenant";
109109
import Cookies from "js-cookie";
110-
import {decrypt, encrypt} from '@/utils/jsencrypt'
111110
import {SystemUserSocialTypeEnum} from "@/utils/constants";
112111
import {getTenantEnable} from "@/utils/ruoyi";
112+
import {
113+
getPassword,
114+
getRememberMe, getTenantName,
115+
getUsername,
116+
removePassword, removeRememberMe, removeTenantName,
117+
removeUsername,
118+
setPassword, setRememberMe, setTenantId, setTenantName,
119+
setUsername
120+
} from "@/utils/auth";
113121
114122
export default {
115123
name: "Login",
@@ -161,7 +169,7 @@ export default {
161169
const tenantId = res.data;
162170
if (tenantId && tenantId >= 0) {
163171
// 设置租户
164-
Cookies.set("tenantId", tenantId);
172+
setTenantId(tenantId)
165173
callback();
166174
} else {
167175
callback('租户不存在');
@@ -172,8 +180,6 @@ export default {
172180
}
173181
]
174182
},
175-
176-
177183
loading: false,
178184
redirect: undefined,
179185
// 枚举
@@ -213,21 +219,16 @@ export default {
213219
});
214220
},
215221
getCookie() {
216-
const username = Cookies.get("username");
217-
const password = Cookies.get("password");
218-
const rememberMe = Cookies.get('rememberMe')
219-
const tenantName = Cookies.get('tenantName');
220-
const mobile = Cookies.get('mobile');
221-
const mobileCode = Cookies.get('mobileCode');
222-
const loginType = Cookies.get('loginType');
222+
const username = getUsername();
223+
const password = getPassword();
224+
const rememberMe = getRememberMe();
225+
const tenantName = getTenantName();
223226
this.loginForm = {
224-
username: username === undefined ? this.loginForm.username : username,
225-
password: password === undefined ? this.loginForm.password : decrypt(password),
226-
rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
227-
tenantName: tenantName === undefined ? this.loginForm.tenantName : tenantName,
228-
mobile: mobile === undefined ? this.loginForm.mobile : mobile,
229-
mobileCode: mobileCode === undefined ? this.loginForm.mobileCode : mobileCode,
230-
loginType: loginType === undefined ? this.loginForm.loginType : loginType,
227+
...this.loginForm,
228+
username: username ? username : this.loginForm.username,
229+
password: password ? password : this.loginForm.password,
230+
rememberMe: rememberMe ? getRememberMe() : false,
231+
tenantName: tenantName ? tenantName : this.loginForm.tenantName,
231232
};
232233
},
233234
handleLogin() {
@@ -236,18 +237,18 @@ export default {
236237
this.loading = true;
237238
// 设置 Cookie
238239
if (this.loginForm.rememberMe) {
239-
Cookies.set("username", this.loginForm.username, {expires: 30});
240-
Cookies.set("password", encrypt(this.loginForm.password), {expires: 30});
241-
Cookies.set('rememberMe', this.loginForm.rememberMe, {expires: 30});
242-
Cookies.set('tenantName', this.loginForm.tenantName, {expires: 30});
240+
setUsername(this.loginForm.username)
241+
setPassword(this.loginForm.password)
242+
setRememberMe(this.loginForm.rememberMe)
243+
setTenantName(this.loginForm.tenantName)
243244
} else {
244-
Cookies.remove("username");
245-
Cookies.remove("password");
246-
Cookies.remove('rememberMe');
247-
Cookies.remove('tenantName');
245+
removeUsername()
246+
removePassword()
247+
removeRememberMe()
248+
removeTenantName()
248249
}
249250
// 发起登陆
250-
console.log("发起登录", this.loginForm);
251+
// console.log("发起登录", this.loginForm);
251252
this.$store.dispatch(this.loginForm.loginType === "sms" ? "SmsLogin" : "Login", this.loginForm).then(() => {
252253
this.$router.push({path: this.redirect || "/"}).catch(() => {
253254
});

src/views/socialLogin.vue

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
</el-tab-pane>
2020
</el-tabs>
2121
<div>
22-
<el-form ref="loginForm" :model="loginForm" :rules="LoginRules" class="login-form">
22+
<el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
2323
<!-- 账号密码登录 -->
2424
<el-form-item prop="username">
2525
<el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号">
@@ -65,16 +65,28 @@
6565
<script>
6666
import Cookies from "js-cookie";
6767
import { encrypt, decrypt } from '@/utils/jsencrypt'
68+
import {
69+
getPassword, getRememberMe,
70+
getUsername,
71+
removePassword,
72+
removeUsername,
73+
setPassword,
74+
setRememberMe,
75+
setUsername
76+
} from "@/utils/auth";
77+
import {getCodeImg} from "@/api/login";
6878
6979
export default {
7080
name: "ThirdLogin",
7181
data() {
7282
return {
83+
codeUrl: "",
84+
captchaEnable: true,
7385
loginForm: {
7486
loginType: "uname",
7587
username: "admin",
7688
password: "admin123",
77-
rememberMe: false, // TODO 芋艿:后面看情况,去掉这块
89+
rememberMe: false,
7890
},
7991
loginRules: {
8092
username: [
@@ -104,6 +116,7 @@ export default {
104116
this.getCookie();
105117
// 重定向地址
106118
this.redirect = this.$route.query.redirect;
119+
this.getCode();
107120
// 社交登录相关
108121
this.type = this.$route.query.type;
109122
this.code = this.$route.query.code;
@@ -119,28 +132,43 @@ export default {
119132
});
120133
},
121134
methods: {
135+
getCode() {
136+
// 只有开启的状态,才加载验证码。默认开启
137+
if (!this.captchaEnable) {
138+
return;
139+
}
140+
// 请求远程,获得验证码
141+
getCodeImg().then(res => {
142+
res = res.data;
143+
this.captchaEnable = res.enable;
144+
if (this.captchaEnable) {
145+
this.codeUrl = "data:image/gif;base64," + res.img;
146+
this.loginForm.uuid = res.uuid;
147+
}
148+
});
149+
},
122150
getCookie() {
123-
const username = Cookies.get("username");
124-
const password = Cookies.get("password");
125-
const rememberMe = Cookies.get('rememberMe')
126-
const loginType = Cookies.get('loginType');
151+
const username = getUsername();
152+
const password = getPassword();
153+
const rememberMe = getRememberMe();
127154
this.loginForm = {
128-
username: username === undefined ? this.loginForm.username : username,
129-
password: password === undefined ? this.loginForm.password : decrypt(password),
130-
rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
131-
loginType: loginType === undefined ? this.loginForm.loginType : loginType,
155+
username: username ? username : this.loginForm.username,
156+
password: password ? password : this.loginForm.password,
157+
rememberMe: rememberMe ? getRememberMe() : false,
158+
loginType: this.loginForm.loginType,
132159
};
133160
},
134161
handleLogin() {
135162
this.$refs.loginForm.validate(valid => {
136163
if (valid) {
137164
this.loading = true;
138165
if (this.loginForm.rememberMe) {
139-
Cookies.set("username", this.loginForm.username, { expires: 30 });
140-
Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
166+
setUsername(this.loginForm.username)
167+
setPassword(this.loginForm.password)
168+
setRememberMe(this.loginForm.rememberMe)
141169
} else {
142-
Cookies.remove("username");
143-
Cookies.remove("password");
170+
removeUsername()
171+
removePassword()
144172
}
145173
this.$store.dispatch("SocialLogin2", {
146174
code: this.code,

0 commit comments

Comments
 (0)