@@ -2,27 +2,33 @@ import Cookies from 'js-cookie';
2
2
import { logout } from '@/api/auth' ;
3
3
import store from '@/store' ;
4
4
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 } ` ) ;
15
21
}
16
22
17
23
// MUSIC_U 只有在账户登录的情况下才有
18
24
export function isLoggedIn ( ) {
19
- return Cookies . get ( 'MUSIC_U' ) !== undefined ? true : false ;
25
+ return getCookie ( 'MUSIC_U' ) !== undefined ;
20
26
}
21
27
22
28
// 账号登录
23
29
export function isAccountLoggedIn ( ) {
24
30
return (
25
- Cookies . get ( 'MUSIC_U' ) !== undefined &&
31
+ getCookie ( 'MUSIC_U' ) !== undefined &&
26
32
store . state . data . loginMode === 'account'
27
33
) ;
28
34
}
@@ -37,25 +43,14 @@ export function isLooseLoggedIn() {
37
43
return isAccountLoggedIn ( ) || isUsernameLoggedIn ( ) ;
38
44
}
39
45
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 } ) ;
61
56
}
0 commit comments