-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
accountManager.ts
106 lines (96 loc) · 2.8 KB
/
accountManager.ts
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
import { window } from "vscode";
import { PlaylistItem } from "../constant/type";
import {
apiDailySignin,
apiLikelist,
apiLoginRefresh,
apiLoginStatus,
apiLogout,
apiUserPlaylist,
} from "../util/api";
import { loggedIn } from "../state/login";
// eslint-disable-next-line @typescript-eslint/naming-convention
const { login, login_cellphone } = require("NeteaseCloudMusicApi");
const { cookieToJson } = require("NeteaseCloudMusicApi/util/index");
export class AccountManager {
static cookie = {};
static uid = 0;
static nickname = "";
static likelist: Set<number> = new Set<number>();
static async dailySignin(): Promise<void> {
if (loggedIn.get()) {
const code = await apiDailySignin();
if (code === 200) {
window.showInformationMessage("Daily check success");
}
} else {
window.showErrorMessage("Please sign in");
}
}
static async login(
phone: boolean,
account: string,
// eslint-disable-next-line @typescript-eslint/naming-convention
md5_password: string
): Promise<boolean> {
if (loggedIn.get()) {
window.showInformationMessage("Already sign in");
return true;
}
try {
const { status, body } = phone
? await login_cellphone({
phone: account,
// eslint-disable-next-line @typescript-eslint/naming-convention
md5_password,
})
: await login({
username: account,
// eslint-disable-next-line @typescript-eslint/naming-convention
md5_password,
});
if (status === 200) {
const { cookie, profile } = body;
const { userId, nickname } = profile;
loggedIn.set(true);
this.cookie = cookieToJson(cookie);
this.uid = userId;
this.nickname = nickname;
const ids = await apiLikelist();
ids.forEach((id) => this.likelist.add(id));
return true;
}
return false;
} catch {
return false;
}
}
static async loginRefresh(): Promise<void> {
await apiLoginRefresh();
}
static async loginStatus(): Promise<void> {
await apiLoginStatus();
}
static async logout(): Promise<boolean> {
if (await apiLogout()) {
loggedIn.set(false);
this.cookie = {};
this.uid = 0;
this.nickname = "";
this.likelist.clear();
return true;
}
return false;
}
static async playlist(): Promise<PlaylistItem[]> {
return await apiUserPlaylist();
}
static async userPlaylist(): Promise<PlaylistItem[]> {
const lists = await apiUserPlaylist();
return lists.filter((list) => list.userId === AccountManager.uid);
}
static async favoritePlaylist(): Promise<PlaylistItem[]> {
const lists = await apiUserPlaylist();
return lists.filter((list) => list.userId !== AccountManager.uid);
}
}