-
Notifications
You must be signed in to change notification settings - Fork 186
Expand file tree
/
Copy pathmain.js
More file actions
93 lines (65 loc) · 2 KB
/
main.js
File metadata and controls
93 lines (65 loc) · 2 KB
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
import { appendFileSync } from "fs";
const host = process.env.HOST || "ikuuu.win";
const checkInUrl = `https://${host}/user/checkin`;
// 签到
async function checkIn(account) {
const response = await fetch(checkInUrl, {
method: "POST",
headers: {
Cookie: account.cookie,
},
});
if (!response.ok) {
throw new Error(`网络请求出错 - ${response.status}`);
}
const data = await response.json();
console.log(`${account.name}: ${data.msg}`);
return data.msg;
}
// 处理
async function processSingleAccount(account) {
const checkInResult = await checkIn(account);
return checkInResult;
}
function setGitHubOutput(name, value) {
appendFileSync(process.env.GITHUB_OUTPUT, `${name}<<EOF\n${value}\nEOF\n`);
}
// 入口
async function main() {
let accounts;
try {
if (!process.env.ACCOUNTS) {
throw new Error("❌ 未配置账户信息。");
}
accounts = JSON.parse(process.env.ACCOUNTS);
} catch (error) {
const message = `❌ ${error.message.includes("JSON") ? "账户信息配置格式错误。" : error.message
}`;
console.error(message);
setGitHubOutput("result", message);
process.exit(1);
}
const allPromises = accounts.map((account) => processSingleAccount(account));
const results = await Promise.allSettled(allPromises);
const msgHeader = "\n======== 签到结果 ========\n\n";
console.log(msgHeader);
let hasError = false;
const resultLines = results.map((result, index) => {
const accountName = accounts[index].name;
const isSuccess = result.status === "fulfilled";
if (!isSuccess) {
hasError = true;
}
const icon = isSuccess ? "✅" : "❌";
const message = isSuccess ? result.value : result.reason.message;
const line = `${accountName}: ${icon} ${message}`;
isSuccess ? console.log(line) : console.error(line);
return line;
});
const resultMsg = resultLines.join("\n");
setGitHubOutput("result", resultMsg);
if (hasError) {
process.exit(1);
}
}
main();