Skip to content

Commit eb94aae

Browse files
committed
fix: 扫码登录问题
1 parent cc914d4 commit eb94aae

File tree

3 files changed

+32
-37
lines changed

3 files changed

+32
-37
lines changed

src/utils/auth.js

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,33 @@ import Cookies from 'js-cookie';
22
import { logout } from '@/api/auth';
33
import store from '@/store';
44

5-
export function doLogout() {
6-
logout();
7-
// 网易云的接口会自动移除该 cookies
8-
Cookies.remove('MUSIC_U');
9-
// 更新状态仓库中的用户信息
10-
store.commit('updateData', { key: 'user', value: {} });
11-
// 更新状态仓库中的登录状态
12-
store.commit('updateData', { key: 'loginMode', value: null });
13-
// 更新状态仓库中的喜欢列表
14-
store.commit('updateData', { key: 'likedSongPlaylistID', value: undefined });
5+
export function setCookies(string) {
6+
const cookies = string.split(';;');
7+
cookies.map(cookie => {
8+
document.cookie = cookie;
9+
const cookieKeyValue = cookie.split(';')[0].split('=');
10+
localStorage.setItem(`cookie-${cookieKeyValue[0]}`, cookieKeyValue[1]);
11+
});
12+
}
13+
14+
export function getCookie(key) {
15+
return Cookies.get(key) ?? localStorage.getItem(`cookie-${key}`);
16+
}
17+
18+
export function removeCookie(key) {
19+
Cookies.remove(key);
20+
localStorage.removeItem(`cookie-${key}`);
1521
}
1622

1723
// MUSIC_U 只有在账户登录的情况下才有
1824
export function isLoggedIn() {
19-
return Cookies.get('MUSIC_U') !== undefined ? true : false;
25+
return getCookie('MUSIC_U') !== undefined;
2026
}
2127

2228
// 账号登录
2329
export function isAccountLoggedIn() {
2430
return (
25-
Cookies.get('MUSIC_U') !== undefined &&
31+
getCookie('MUSIC_U') !== undefined &&
2632
store.state.data.loginMode === 'account'
2733
);
2834
}
@@ -37,25 +43,14 @@ export function isLooseLoggedIn() {
3743
return isAccountLoggedIn() || isUsernameLoggedIn();
3844
}
3945

40-
export function getMusicU(string) {
41-
const temp = string.split(';');
42-
if (!temp.length) {
43-
return undefined;
44-
}
45-
const MUSIC_U = temp.find(item => item.includes('MUSIC_U'));
46-
if (MUSIC_U) {
47-
return MUSIC_U.split('=')[1];
48-
}
49-
return '';
50-
}
51-
52-
export function setMusicU(key, value) {
53-
return Cookies.set(key, value);
54-
}
55-
56-
export function setCookies(string) {
57-
const cookies = string.split(';;');
58-
cookies.map(cookie => {
59-
document.cookie = cookie;
60-
});
46+
export function doLogout() {
47+
logout();
48+
removeCookie('MUSIC_U');
49+
removeCookie('__csrf');
50+
// 更新状态仓库中的用户信息
51+
store.commit('updateData', { key: 'user', value: {} });
52+
// 更新状态仓库中的登录状态
53+
store.commit('updateData', { key: 'loginMode', value: null });
54+
// 更新状态仓库中的喜欢列表
55+
store.commit('updateData', { key: 'likedSongPlaylistID', value: undefined });
6156
}

src/utils/request.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import axios from 'axios';
2-
import Cookies from 'js-cookie';
2+
import { getCookie } from '@/utils/auth';
33

44
let baseURL = '';
55
// Web 和 Electron 跑在不同端口避免同时启动时冲突
@@ -22,10 +22,10 @@ const service = axios.create({
2222
service.interceptors.request.use(function (config) {
2323
if (!config.params) config.params = {};
2424
if (baseURL[0] !== '/' && !process.env.IS_ELECTRON) {
25-
config.params.cookie = `MUSIC_U=${Cookies.get('MUSIC_U')};`;
25+
config.params.cookie = `MUSIC_U=${getCookie('MUSIC_U')};`;
2626
}
2727

28-
if (!process.env.IS_ELECTRON) {
28+
if (!process.env.IS_ELECTRON && !config.url.includes('/login')) {
2929
config.params.realIP = '211.161.244.70';
3030
}
3131

src/views/loginAccount.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ export default {
271271
clearInterval(this.qrCodeCheckInterval);
272272
this.qrCodeInformation = '登录成功,请稍等...';
273273
result.code = 200;
274-
result.cookie = result.cookie.replace('HTTPOnly;', '');
274+
result.cookie = result.cookie.replace('HTTPOnly', '');
275275
this.handleLoginResponse(result);
276276
}
277277
});

0 commit comments

Comments
 (0)