-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsign.ts
85 lines (79 loc) · 2.54 KB
/
sign.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
import { activeSign, ISignInQuery, signIn } from './requests';
import { config } from './consts';
import { QRSign } from './QRSign';
import { sendNotificaition, sleep } from './utils';
import { WechatDevtools } from './cdp';
export interface IContext {
openId: string;
studentName: string;
devtools?: WechatDevtools;
lastSignId: number;
qrSign?: QRSign;
signedIdSet: Set<number>;
openIdSet: Set<string>;
}
export const signOnce = async (ctx: IContext) => {
return await activeSign(ctx.openId)
.then(async (data) => {
if (!data.length) {
ctx.qrSign?.destory();
throw 'No sign-in available';
}
const queue = [
...data.filter((sign) => !sign.isQR),
...data.filter((sign) => sign.isQR),
];
for (const sign of queue) {
const { signId, courseId, isGPS, isQR, name } = sign;
console.log('current sign-in:', sign.name);
if (ctx.signedIdSet.has(signId)) {
throw `${name} already signed in`;
}
sendNotificaition(`INFO: ${name} sign-in is going on!`);
if (isQR) {
if (signId === ctx.lastSignId) {
return;
}
ctx.lastSignId = signId;
sendNotificaition(`WARNING: ${name} QR sign-in is going on!`);
ctx.qrSign?.destory();
ctx.qrSign = new QRSign(ctx, { courseId, signId });
ctx.qrSign.start().then((result) => {
ctx.signedIdSet.add(signId);
console.log(`QRSign:: result`, result);
if (!config.devtools) {
const prompt =
'Signed in successfully. However, you need to submit new openid!';
sendNotificaition(prompt);
console.warn(prompt);
ctx.openId = '';
}
ctx.qrSign?.destory();
});
} else {
let signInQuery: ISignInQuery = { courseId, signId };
if (isGPS) {
const { lat, lon } = config;
signInQuery = { ...signInQuery, lat, lon };
}
await sleep(config.wait);
await signIn(ctx.openId, signInQuery)
.then((data) => {
if (!data.errorCode || data.errorCode === 305) {
ctx.signedIdSet.add(signId);
}
console.log(data);
})
.catch((e) => {
console.log(e);
sendNotificaition(
`Error: failed to ${name} sign in. See output plz.`
);
});
}
}
})
.catch((e) => {
console.log(e);
});
};