-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
685 lines (611 loc) · 18.6 KB
/
index.js
File metadata and controls
685 lines (611 loc) · 18.6 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
#!/usr/bin/env node
/*
* Copyright (C) 2025-2025 SunWuyuan <https://github.com/sunwuyuan>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { writeFileSync } from "fs";
import { resolve } from "path";
import axios from "axios";
import ProgressBar from "progress";
import readline from "readline";
import qrcode from "qrcode-terminal";
import ExcelJS from "exceljs";
import UserAgent from "user-agents";
import { existsSync, readFileSync } from "fs";
const CACHE_FILE = "cache.json";
// Cache management functions
function saveCache(data) {
try {
writeFileSync(CACHE_FILE, JSON.stringify(data, null, 2));
return true;
} catch (error) {
console.error("Failed to save cache:", error);
return false;
}
}
function loadCache() {
try {
if (existsSync(CACHE_FILE)) {
const cache = JSON.parse(readFileSync(CACHE_FILE, "utf8"));
return cache;
}
} catch (error) {
console.error("Failed to load cache:", error);
}
return null;
}
function isTokenValid(cache) {
if (!cache || !cache.jwttoken || !cache.timestamp) {
return false;
}
// Check if token is less than 24 hours old
const tokenAge = Date.now() - cache.timestamp;
return tokenAge < 24 * 60 * 60 * 1000; // 24 hours in milliseconds
}
let imprint = process.env.IMPRINT || "";
let jwttoken = process.env.JWTTOKEN || "";
let versionInfo = null;
let requestConfig = null;
function generateBrowserInfo() {
const userAgent = new UserAgent();
const ua = userAgent.data;
return {
userAgent: ua.userAgent,
secChUa: `"${ua.browserName}";v="${ua.browserVersion}", "Not A(Brand";v="99"`,
secChUaPlatform: `"${ua.platform}"`,
secChUaMobile: ua.deviceCategory === "mobile" ? "?1" : "?0",
};
}
const browserInfo = generateBrowserInfo();
console.log(`使用随机浏览器信息:${browserInfo.userAgent}`);
// Headers will be generated for each request
function getHeaders() {
return {
accept: "*/*",
"accept-language": "zh-CN,zh;q=0.9,en;q=0.8",
"app-info": versionInfo?.appInfo || "1/3.3.7/814",
"cache-control": "no-cache",
"content-type": "application/json; charset=utf-8",
"sec-ch-ua": browserInfo.secChUa,
"sec-ch-ua-mobile": browserInfo.secChUaMobile,
"sec-ch-ua-platform": browserInfo.secChUaPlatform,
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "cross-site",
"user-agent": browserInfo.userAgent,
referer: "https://service.banjixiaoguanjia.com/",
"Referrer-Policy": "strict-origin-when-cross-origin",
};
}
async function fetchVersionInfo() {
try {
const response = await axios.get(
"https://service.banjixiaoguanjia.com/appweb/version.json"
);
const { version, build_number } = response.data;
versionInfo = {
appInfo: `1/${version}/${build_number}`,
appVcode: build_number,
appVname: version,
};
console.log(`请求版本信息成功:${versionInfo.appInfo}`);
return versionInfo;
} catch (error) {
console.error("Failed to fetch version info:", error);
// Fallback to default values if API fails
versionInfo = {
appInfo: "1/3.3.7/814",
appVcode: "814",
appVname: "3.3.7",
};
console.log(`请求版本信息失败,使用默认值:${versionInfo.appInfo}`);
return versionInfo;
}
}
function initRequestConfig() {
requestConfig = {
channel: "app_web",
platform: "app",
app_info: {
app_vcode: versionInfo.appVcode,
app_vname: versionInfo.appVname,
},
device_info: {
os_name: null,
os_version: null,
brand: null,
model: null,
screen_height: null,
screen_width: null,
device_id: null,
network_type: "WiFi",
},
};
}
async function getQrCode() {
const response = await axios.post(
"https://b.welife001.com/app/auth/getQrCodeImg",
{
...requestConfig,
},
{ headers: getHeaders() }
);
return response.data;
}
async function checkLoginStatus(random) {
const response = await axios.post(
"https://b.welife001.com/app/auth/checkLoginStatusWithToken",
{
random,
...requestConfig,
},
{ headers: getHeaders() }
);
return response.data;
}
async function login() {
const qrCodeData = await getQrCode();
console.log("请扫描以下二维码登录:");
console.log("或者在浏览器中打开以下链接:");
console.log(
`https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=${qrCodeData.data.ticket}`
);
qrcode.generate(
`https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=${qrCodeData.data.ticket}`,
{ small: true }
);
let loginStatus = { code: 0, msg: "未扫码" };
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
while (loginStatus.code !== 1) {
await new Promise((resolve) => {
rl.question("按回车键尝试登录...", async () => {
loginStatus = await checkLoginStatus(qrCodeData.data.random);
if (loginStatus.code === 1) {
console.log("登录成功!");
jwttoken = loginStatus.data;
// Save cache after successful login
saveCache({
jwttoken,
imprint,
timestamp: Date.now(),
});
resolve();
} else if (loginStatus.msg !== "未扫码") {
console.error("登录失败:", loginStatus.msg);
process.exit(1);
} else {
console.log("等待扫码...");
resolve();
}
});
});
}
rl.close();
}
async function getUserChildInfo() {
const response = await axios.post(
"https://b.welife001.com/applet/getUser",
{},
{
headers: { ...getHeaders(), authorization: jwttoken },
}
);
//console.log(response.data.data.currentUser.child_class_list);
return response.data.data.currentUser.child_class_list;
}
async function getParentInfo(members) {
let allData = [];
let page = 0;
let hasMoreData = true;
while (hasMoreData) {
const response = await axios.get(
`https://b.welife001.com/info/getParent?members=${members.join(
"%3A"
)}&type=-1&date=-1&page=${page}&size=20&isRecent=false`,
{
headers: { ...getHeaders(), authorization: jwttoken },
}
);
const pageData = response.data.data;
if (!pageData || pageData.length === 0) {
hasMoreData = false;
} else {
allData = allData.concat(pageData);
page++;
}
}
return allData;
}
// 处理不同类型数据的函数
async function processDataByType(data, classMap) {
//console.log(classMap);
if (data.type === 0) {
// 班级信息类型
const membersData = await getClassMembers(data.cls);
return processClassMemberData(membersData, classMap, data.cls);
}
switch (data.type) {
case 4:
return processScoreData(data, classMap);
case 15:
return processStudentInfoData(data, classMap);
default:
console.log(`暂不支持处理类型 ${data.type} 的数据`);
return null;
}
}
// 处理成绩数据
async function processScoreData(data, classMap) {
const studentNames = await getClassById(data.cls);
const results = [];
const progressBar = new ProgressBar("[:bar] :percent", {
complete: "=",
incomplete: " ",
width: 20,
total: studentNames.length,
});
for (const name of studentNames) {
progressBar.tick();
const scoreDetail = await getStudentScore(name, data.score);
if (scoreDetail) {
results.push({ name, scoreDetail });
console.log(
`\r ${progressBar.curr}号 ${name} - ${scoreDetail.length}科目成绩获取成功`
);
} else {
results.push({ name, scoreDetail: [{ subject: "错误", score: "错误" }] });
console.log(`\r ${progressBar.curr}号 ${name} - 获取成绩失败`);
}
}
const subjects = [
...new Set(
results.flatMap((result) =>
result.scoreDetail.map((detail) => detail.subject)
)
),
];
return {
type: "score",
data: results,
columns: ["姓名", ...subjects],
fileName: `${data.title}_${classMap[data.cls]}_${data.creator_wx_name}_${
results.length
}人`,
formatRow: (result) => {
const row = { 姓名: result.name };
result.scoreDetail.forEach((detail) => {
row[detail.subject] = detail.score;
});
return row;
},
};
}
// 处理学生信息数据
async function processStudentInfoData(data, classMap) {
if (!data.attach || !data.attach.headers || !data.attach.detail) {
console.log("数据格式不正确,缺少必要的信息");
return null;
}
// 从attach对象中获取表头信息
const headers = ["姓名", ...data.attach.headers];
// 处理每个学生的数据
const studentData = data.attach.detail.map((student) => {
const row = {
姓名: student.name,
};
// 将infos数组的数据按顺序填入对应的列
student.infos.forEach((info, index) => {
row[data.attach.headers[index]] = info.latest || "";
});
return row;
});
return {
type: "studentInfo",
data: studentData,
columns: headers,
fileName: `${data.title}_${classMap[data.cls]}_${data.creator_wx_name}_${
studentData.length
}人`,
formatRow: (row) => row,
};
}
async function processClassMemberData(data, classMap, currentCls) {
// 提取所有成员信息
const members = data.data.members;
// 定义表头
const headers = ["姓名", "电话", "微信昵称", "微信头像", "身份"];
// 处理每个成员的数据
const memberData = [];
// 首先处理老师信息
const teachers = members.filter((member) => member.teach_role !== -1);
const students = members.filter((member) => member.teach_role === -1);
// 处理老师数据
teachers.forEach((teacher) => {
// 添加老师本人信息
const teacherRow = {
姓名: teacher.name || "",
电话: teacher.phone || "",
微信昵称: teacher.wx_name || "",
微信头像: teacher.wx_avatar || "",
身份: teacher.teach_role_str ? `${teacher.teach_role_str}老师` : "老师",
};
memberData.push(teacherRow);
// 处理老师的家庭成员信息
teacher.family.forEach((familyMember) => {
const familyRow = {
姓名: "",
电话: familyMember.phone || "",
微信昵称: familyMember.wx_name || "",
微信头像: familyMember.wx_avatar || "",
身份: `${teacherRow.姓名}的家长`,
};
memberData.push(familyRow);
});
});
// 处理学生数据
students.forEach((student) => {
// 添加学生本人信息
const studentRow = {
姓名: student.name || "",
电话: student.phone || "",
微信昵称: student.wx_name || "",
微信头像: student.wx_avatar || "",
身份: "学生",
};
memberData.push(studentRow);
// 处理学生的家庭成员信息
student.family.forEach((familyMember) => {
const familyRow = {
姓名: "",
电话: familyMember.phone || "",
微信昵称: familyMember.wx_name || "",
微信头像: familyMember.wx_avatar || "",
身份: `${studentRow.姓名}的家长`,
};
memberData.push(familyRow);
});
});
return {
type: "classMember",
data: memberData,
columns: headers,
fileName: `班级成员信息_${classMap[currentCls]}_${members.length}人`,
formatRow: (row) => row,
};
}
async function getClassMembers(cid) {
const response = await axios.post(
"https://b.welife001.com/applet/getMembersByCid",
{
cid,
updateclass: 1,
all_status: true,
},
{
headers: { ...getHeaders(), authorization: jwttoken },
}
);
return response.data;
}
// 生成Excel文件
async function generateExcel(processedData) {
const workbook = new ExcelJS.Workbook();
const worksheet = workbook.addWorksheet("数据");
// 设置列
worksheet.columns = processedData.columns.map((header) => ({
header,
key: header,
width: 20,
}));
// 添加数据行
if (processedData.type === "score") {
processedData.data.forEach((result) => {
worksheet.addRow(processedData.formatRow(result));
});
} else if (processedData.type === "studentInfo") {
// 添加说明行
worksheet.addRow(
processedData.columns.reduce((obj, header) => {
obj[header] = header === "姓名" ? "说明" : "";
return obj;
}, {})
);
worksheet.addRow(
processedData.columns.reduce((obj, header) => {
obj[header] =
header === "身份证件号"
? "请大家核对学籍信息是否有误 ① 名字是否有误 ②性别是否有误 ③ 身份证号码是否有误,如有误请及时与班主任联系,谢谢!"
: "";
return obj;
}, {})
);
// 添加空行
worksheet.addRow({});
// 添加学生数据
processedData.data.forEach((row) => {
worksheet.addRow(row);
});
} else if (processedData.type === "classMember") {
// 添加说明行
worksheet.addRow(
processedData.columns.reduce((obj, header) => {
obj[header] = header === "姓名" ? "说明" : "";
return obj;
}, {})
);
worksheet.addRow(
processedData.columns.reduce((obj, header) => {
obj[header] =
header === "身份证件号"
? "请大家核对学籍信息是否有误 ① 名字是否有误 ②性别是否有误 ③ 身份证号码是否有误,如有误请及时与班主任联系,谢谢!"
: "";
return obj;
}, {})
);
// 添加空行
worksheet.addRow({});
// 添加成员数据
processedData.data.forEach((row) => {
worksheet.addRow(row);
});
}
const now = Date.now();
const fileNameWithTime = `${processedData.fileName}_${now}.xlsx`;
const outputPath = resolve(process.cwd(), fileNameWithTime);
await workbook.xlsx.writeFile(outputPath);
console.log(`\n\n数据处理完毕,已保存到${outputPath}`);
}
async function getClassByMemberId(memberIds) {
const response = await axios.post(
"https://b.welife001.com/applet/getClassByMemberId",
{
member_ids: memberIds,
},
{
headers: { ...getHeaders(), authorization: jwttoken },
}
);
return response.data.data;
}
async function getClassById(cid) {
const response = await axios.post(
"https://b.welife001.com/applet/getClassById",
{
cid,
},
{
headers: { ...getHeaders(), authorization: jwttoken },
}
);
return response.data.data.class.rosters.map((roster) => roster.name);
}
async function getStudentScore(name, id) {
try {
const response = await axios.post(
"https://b.welife001.com/getStudentScoreById",
{ id, name },
{
headers: { ...getHeaders(), imprint },
}
);
return response.data.data.studentScore.score_detail;
} catch (error) {
console.error(`Error fetching score for ${name}:`, error);
return null;
}
}
async function main() {
console.log(`
========================================
班级小管家工具
孙悟元 https://wuyuan.dev
项目使用AGPL-3.0-only协议开源,
开源地址:https://github.com/Sunwuyuan/XGJPplus
Copyright (C) 2025-${new Date().getFullYear()} SunWuyuan <https://github.com/Sunwuyuan>.
========================================
`);
/*
项目使用AGPL-3.0-only协议开源,因此,
如需署名请添加新行,请勿删除或修改上述内容。
请勿删除或修改这里的注释。
*/
// Fetch version info before anything else
await fetchVersionInfo();
// Initialize request config
initRequestConfig();
// Update headers with fetched version info
// The headers are now generated dynamically in getHeaders
// Load cache and check if we have a valid token
const cache = loadCache();
if (cache && isTokenValid(cache)) {
console.log("从缓存中获取到可用令牌");
jwttoken = cache.jwttoken;
imprint = cache.imprint;
try {
// Test if the cached token is still valid
await getUserChildInfo();
console.log("缓存中的令牌可用(如需退出请删除cache.json)");
} catch (error) {
console.log("缓存中的令牌已失效,重新登录...");
jwttoken = "";
imprint = "";
}
}
if (!jwttoken) {
await login();
}
const childList = await getUserChildInfo();
const members = childList.map((child) => child.member_id);
const parentInfo = await getParentInfo(members);
const classInfo = await getClassByMemberId(members);
const classMap = classInfo.reduce((map, cls) => {
map[cls._id] = cls.class_name;
return map;
}, {});
console.log("请选择要处理的数据:");
console.log("0. 处理所有班级成员信息");
parentInfo.forEach((info, index) => {
const className = classMap[info.cls] || "未知班级";
const typeDesc =
info.type === 4
? "成绩单"
: info.type === 15
? "学生信息"
: `类型${info.type} 暂不支持`;
console.log(
`${index + 1}. ${info.title} (${className}) - ${
info.creator_wx_name
} [${typeDesc}]`
);
});
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const selectedIndex = await new Promise((resolve) => {
rl.question("请输入选择的编号:", (answer) => {
rl.close();
resolve(parseInt(answer));
});
});
if (selectedIndex === 0) {
// 处理所有班级信息
console.log("开始处理所有班级信息...");
for (const [cls, className] of Object.entries(classMap)) {
console.log(`正在处理班级: ${className}`);
const membersData = await getClassMembers(cls);
const processedData = await processClassMemberData(
membersData,
classMap,
cls
);
if (processedData) {
await generateExcel(processedData);
}
}
console.log("所有班级处理完成!");
} else {
const selectedInfo = parentInfo[selectedIndex - 1];
imprint = selectedInfo.creator_wx_openid;
const processedData = await processDataByType(selectedInfo, classMap);
if (processedData) {
await generateExcel(processedData);
}
}
}
main();