forked from ux34/daka
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
111 lines (101 loc) · 3.31 KB
/
Copy pathindex.js
File metadata and controls
111 lines (101 loc) · 3.31 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const sendMail = require('./mail'); // 邮箱发送
const { login, getUserInfo, submit } = require('./request'); // 需要使用到的请求
const getForm = require('./form'); //返回需要提交的表单
// 同步倒计时/秒
const syncTimeout = second => new Promise((resolve) => {
setTimeout(() => resolve(), 1000 * second)
});
// 本地test
/*
process.env["INFO"] = `
{
"类型": 0,
"学号": "1xxxxxxxxx",
"密码": "xxxxxxxx",
"位置": "福建省xxxxxxxxx"
}
`;
process.env["MAIL"] = 'ux34@qq.com';
*/
// 开始执行
(async () => {
// 获取通知邮箱
let mail = process.env["MAIL"];
const mailReg = new RegExp("^[a-z0-9A-Z]+[- | a-z0-9A-Z . _]+@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-z]{2,}$");
if (!mailReg.test(mail)) {
console.log('不是正确的邮箱,将取消发送邮箱,请修改')
mail = null
}
try {
// 解析环境变量中的JSON字符串
let info = JSON.parse(process.env["INFO"]);
// 默认为0:离校不在厦门
// 开学将默认更新为2:在校
if (info['类型'] === undefined) {
info['类型'] = 0
}
if (
!(
[0,1,2,3].includes(info['类型'])
&& info['学号']
&& info['密码']
&& info['位置']
)) {
throw new Error('GitHub Secrets 信息不完整,[类型, 学号, 密码, 位置]为必填项,请按文档说明填写');
}
// 1:家在厦门 || 3:厦门租房 需要多填一个社区
if ([1,3].includes(info['类型'])) {
if (!info['社区']) {
throw new Error('GitHub Secrets 信息不完整,家在厦门需要多填一个社区,请按文档说明填写');
}
}
// 登录获取cookie
let cookie = await login(info).catch(err => {
console.log('登录失败:' + err.message)
mail && sendMail(mail, '登录失败,将在5分钟后重新尝试', err.message)
return null
})
if (!cookie) {
// 登录失败5分钟后再次尝试登录
// 同步倒计时5分钟
await syncTimeout(5*60)
// 再次失败就不管了
// 不知道什么原因经常请求失败
cookie = await login(info)
}
// 直接通过cookie获取用户信息,减少 Secrets 的配置项
let userInfo = await getUserInfo(cookie);
if (!userInfo) {
throw new Error('获取用户信息失败');
}
info['姓名'] = userInfo['username'];
info['path'] = userInfo['path'];
info['组织'] = userInfo['organization'];
info['性别'] = userInfo['gender'];
info['电话'] = userInfo['cellphone'];
if (
!(
info['姓名']
&& info['path']
&& info['组织']
&& info['性别']
&& info['电话']
)
) {
throw new Error('用户信息不完整,请检查手机微哨【个人资料】的完成度');
}
let form = getForm(info);
// 提交表单
let result = await submit(cookie, JSON.stringify(form)).catch(err => {
throw new Error('提交表单失败:' + err.message)
})
console.log('打卡成功:' + result);
// 发过结果到通知邮件
mail && sendMail(mail, '打卡成功', result);
} catch (error) {
// 错误处理
console.log('打卡失败:' + error.message);
// 发过失败的结果到通知邮件
mail && sendMail(mail, '打卡失败', error.message);
}
})();