-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqm.js
148 lines (138 loc) · 4.91 KB
/
qm.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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
let fs = require('fs');
let path = require('path');
let fetch = require('node-fetch');
// // //////////////////////////////////////////////////////////////////////////////
function dispel(ids, cookie) {
const body = JSON.stringify({ user_ids: ids });
fetch('https://apiv3.shanbay.com/team/group/manage/members', {
headers: {
accept: '*/*',
'accept-language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7',
'cache-control': 'no-cache',
'content-type': 'application/json',
pragma: 'no-cache',
'sec-ch-ua': '"Google Chrome";v="89", "Chromium";v="89", ";Not A Brand";v="99"',
'sec-ch-ua-mobile': '?0',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-site',
'x-csrftoken': '34cdb66556d51897e64d63d69fdc77f2',
'x-referrer-app': 'client/team',
cookie,
},
referrer: 'https://web.shanbay.com/',
referrerPolicy: 'strict-origin-when-cross-origin',
body,
method: 'DELETE',
mode: 'cors',
})
.then(r => r.json())
.then(r => {
console.log(r);
})
.catch(e => {
fs.writeFile(path.join(__dirname, 'log.txt'), `${Date()}\n${e.toString()}\n`, { flag: 'a' });
});
}
// 检查结果,过滤出应踢出的userid ==> { ids, record: { date, dispel: [{userInfo}] } }
function filterUserIds(r) {
console.log(r);
let date = Date();
const ids = [];
const record = { date, dispel: [] };
r.objects.forEach(i => {
i.checkin_rate *= 100;
if (i.checkin_rate >= 97) {
// 打卡率大于97则表示当前页都合格
} else if (i.checkin_today && i.checkin_yesterday) {
} else if (i.age >= 30 && i.checkin_yesterday) {
} else if (i.checkin_rate >= 96 && i.checkin_today) {
} else {
// 若打卡率低于97%,且组龄小与30天
ids.push(i.user_id); // 将id计入ids
let allInfo = {};
allInfo.avatar = i.user_info.avatar_url; // 用户头像
allInfo.dataid = i.user_id;
allInfo.name = i.user_info.nickname; // 用户昵称
allInfo.link = `https://web.shanbay.com/web/users/${i.user_id}/zone`; // 用户首页
allInfo.points = i.points; // 用户积分
allInfo.days = i.age; // 用户组龄
allInfo.rate = i.checkin_rate.toFixed(2); // 用户打卡率
allInfo.check0 = i.checkin_yesterday ? '已打卡' : '-'; // 前一天是否打卡
allInfo.check1 = i.checkin_today ? '已打卡' : '-'; // 当日是否打卡
record.dispel.push(allInfo);
}
});
return { ids, record };
}
// 写入记录 / 执行回调函数(users为展示当前不合格的组员,crontab则为真正执行踢人函数)
function wtFileAndCallback(result, recordFile, callbackFunction, cookie) {
fs.writeFile(
path.join(__dirname, `${recordFile}-log.txt`),
`\n${JSON.stringify(result)}\n`,
{ flag: 'a' },
function (err2) {
if (err2) {
console.log('fs writeFile err: ', err2);
}
}
);
fs.writeFile(path.join(__dirname, `${recordFile}.json`), JSON.stringify(result), function (err2) {
if (err2) {
console.log('fs writeFile err: ', err2);
}
callbackFunction && callbackFunction(result, cookie); // 执行回掉函数
});
console.log('end: check');
}
function check(recordFile, callbackFunction) {
console.log('OY-check-start:');
// 每次都需要取最新的cookie
const { cookie } = JSON.parse(fs.readFileSync(path.join(__dirname, 'config.json'), 'utf-8'));
fetch('https://apiv3.shanbay.com/team/group/manage/members?ipp=50&page=1&rank_type=CHECKIN_RATE&order=POSITIVE', {
headers: {
accept: '*/*',
'accept-language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7',
'cache-control': 'no-cache',
'content-type': 'application/json',
pragma: 'no-cache',
'sec-ch-ua': '"Google Chrome";v="89", "Chromium";v="89", ";Not A Brand";v="99"',
'sec-ch-ua-mobile': '?0',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-site',
'x-csrftoken': '34cdb66556d51897e64d63d69fdc77f2',
'x-referrer-app': 'client/team',
cookie,
},
referrer: 'https://web.shanbay.com/',
referrerPolicy: 'strict-origin-when-cross-origin',
body: null,
method: 'GET',
mode: 'cors',
})
.then(r => r.json())
.then(r => {
if (!r.objects && r.msg) {
throw new Error(r.msg);
}
const result = filterUserIds(r);
wtFileAndCallback(result, recordFile, callbackFunction, cookie);
})
.catch(e => {
console.error('OY-check-error:', e.toString());
let date = new Date();
fs.writeFileSync(path.join(__dirname, 'log.txt'), `${date.toString()} \n ERR: ${e.toString()}\n`, {
flag: 'a',
});
// 若是crt,则表示是查询当前不合格用户,即/users路由
if (recordFile === 'crt') {
callbackFunction({ msg: e.toString(), record: { date, dispel: [] } });
}
});
}
const qm = {
disple: dispel,
check,
};
module.exports = qm;