-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
rkeyManager.js
55 lines (49 loc) · 1.18 KB
/
rkeyManager.js
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
//摘自LLOneBot
class RkeyManager {
serverUrl = "";
rkeyData = {
group_rkey: "",
private_rkey: "",
expired_time: 0,
};
constructor(serverUrl) {
this.serverUrl = serverUrl;
}
async getRkey() {
if (this.isExpired()) {
try {
await this.refreshRkey();
} catch (e) {
console.log("获取rkey失败", e);
}
}
return this.rkeyData;
}
isExpired() {
const now = new Date().getTime() / 1000;
// console.log(`now: ${now}, expired_time: ${this.rkeyData.expired_time}`)
return now > this.rkeyData.expired_time;
}
async refreshRkey() {
//刷新rkey
this.rkeyData = await this.fetchServerRkey();
}
async fetchServerRkey() {
return new Promise((resolve, reject) => {
fetch(this.serverUrl)
.then((response) => {
if (!response.ok) {
return reject(response.statusText); // 请求失败,返回错误信息
}
return response.json(); // 解析 JSON 格式的响应体
})
.then((data) => {
resolve(data);
})
.catch((error) => {
reject(error);
});
});
}
}
module.exports.RkeyManager = RkeyManager;