-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcommand_qt.go
More file actions
1221 lines (1120 loc) · 38.8 KB
/
Copy pathcommand_qt.go
File metadata and controls
1221 lines (1120 loc) · 38.8 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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"encoding/json"
"fmt"
"math"
"regexp"
"strings"
)
var qtGradeRankPercent = map[string][2]float64{
"A1": {0, 1},
"A2": {1, 3},
"A3": {3, 6},
"A4": {6, 10},
"A5": {10, 15},
"B1": {15, 21},
"B2": {21, 28},
"B3": {28, 36},
"B4": {36, 43},
"B5": {43, 50},
"C1": {50, 56},
"C2": {56, 64},
"C3": {64, 71},
"C4": {71, 78},
"C5": {78, 84},
"D1": {84, 89},
"D2": {89, 93},
"D3": {93, 96},
"D4": {96, 98},
"D5": {98, 99},
"E": {99, 100},
}
var qtSubjectShortIDPattern = regexp.MustCompile(`^\d{1,3}$`)
func qtMapSlice(items []any) []map[string]any {
result := make([]map[string]any, 0, len(items))
for _, item := range items {
if mapped := asMap(item); len(mapped) > 0 {
result = append(result, mapped)
}
}
return result
}
func buildUniqueNumericPrefixes(digits map[string]string, minLen int) map[string]string {
lengths := make(map[string]int, len(digits))
for key := range digits {
lengths[key] = minLen
}
for {
buckets := make(map[string][]string, len(digits))
for key, digitString := range digits {
length := lengths[key]
if length > len(digitString) {
length = len(digitString)
}
buckets[digitString[:length]] = append(buckets[digitString[:length]], key)
}
hasConflict := false
for _, keys := range buckets {
if len(keys) <= 1 {
continue
}
hasConflict = true
for _, key := range keys {
lengths[key]++
}
}
if !hasConflict {
break
}
}
result := make(map[string]string, len(digits))
for key, digitString := range digits {
length := lengths[key]
if length > len(digitString) {
length = len(digitString)
}
result[key] = digitString[:length]
}
return result
}
func buildQTExamShortIDs(exams []map[string]any) map[string]string {
digits := make(map[string]string, len(exams))
for _, exam := range exams {
examGuid := asString(exam["examGuid"])
if examGuid == "" {
continue
}
digits[examGuid] = qtHashDigits(examGuid)
}
return buildUniqueNumericPrefixes(digits, 6)
}
func qtClaimedExamList(data any) []map[string]any {
return qtMapSlice(asSlice(asMap(data)["list"]))
}
func qtFlattenUnclaimedExams(data any) []map[string]any {
groups := qtMapSlice(asSlice(data))
items := make([]map[string]any, 0)
for _, group := range groups {
items = append(items, qtMapSlice(asSlice(group["list"]))...)
}
return items
}
func qtEstimateRankRange(grade string, total int) (int, int, bool) {
ratio, ok := qtGradeRankPercent[strings.ToUpper(strings.TrimSpace(grade))]
if !ok || total <= 0 {
return 0, 0, false
}
start := 1
if ratio[0] > 0 {
start = int(math.Ceil(float64(total)*ratio[0]/100)) + 1
}
end := int(math.Ceil(float64(total) * ratio[1] / 100))
if end < start {
end = start
}
if end > total {
end = total
}
if start > total {
start = total
}
return start, end, true
}
func qtVisibleSubjects(subjectsData map[string]any) []map[string]any {
subjects := qtMapSlice(asSlice(subjectsData["subjects"]))
result := make([]map[string]any, 0, len(subjects))
for _, subject := range subjects {
if asString(subject["km"]) == "总分" {
continue
}
if asString(asMap(subject["question"])["asiresponse"]) == "" {
continue
}
result = append(result, subject)
}
return result
}
func qtSubjectCount(subjectsData map[string]any) int {
if count := asInt(subjectsData["subjectCount"]); count > 0 {
return count
}
return len(qtVisibleSubjects(subjectsData))
}
func qtTotalSubject(subjectsData map[string]any) map[string]any {
for _, subject := range qtMapSlice(asSlice(subjectsData["subjects"])) {
if asString(subject["km"]) == "总分" {
return subject
}
}
return map[string]any{}
}
func qtBuildSubjectContext(exam map[string]any, subjectsData map[string]any) map[string]any {
items := map[string]any{}
aliases := map[string]any{}
visible := qtVisibleSubjects(subjectsData)
for index, subject := range visible {
shortID := fmt.Sprintf("%03d", index+1)
entry := map[string]any{
"shortID": shortID,
"subject": asString(subject["km"]),
"asiResponse": asString(asMap(subject["question"])["asiresponse"]),
"score": subject["myScore"],
"fullScore": subject["fullScore"],
}
items[shortID] = entry
aliases[asString(subject["km"])] = shortID
}
return map[string]any{
"__provider": "qt",
"__exam": exam,
"__items": items,
"__aliases": aliases,
"__subjectCount": qtSubjectCount(subjectsData),
}
}
func qtRenderExamOverview(exam map[string]any, shortID string, subjectsData, totalGradeRes map[string]any) string {
report := asMap(asMap(totalGradeRes["data"])["report"])
totalSubject := qtTotalSubject(subjectsData)
score := defaultString(asString(report["myScore"]), defaultString(asString(totalSubject["myScore"]), asString(exam["score"])))
fullScore := defaultString(asString(report["fullScore"]), asString(totalSubject["fullScore"]))
totalStudents := asInt(report["total"])
grade := strings.ToUpper(asString(report["grade"]))
text := asString(exam["examName"])
text += fmt.Sprintf("\n > 考试ID %s | 时间 %s", shortID, stringOrNA(exam["time"]))
if !(score == "0" && fullScore == "0") {
text += fmt.Sprintf("\n > 总分 %s/%s", stringOrNA(score), stringOrNA(fullScore))
}
if totalStudents > 0 && grade != "" {
text += fmt.Sprintf("\n > 年级人数 %d | 等第 %s", totalStudents, grade)
if start, end, ok := qtEstimateRankRange(grade, totalStudents); ok {
if start == end {
text += fmt.Sprintf("\n > 预估排名(可能有错误) %d", start)
} else {
text += fmt.Sprintf("\n > 预估排名(可能有错误) %d-%d", start, end)
}
}
} else if totalStudents > 0 {
text += fmt.Sprintf("\n > 年级人数 %d", totalStudents)
} else if grade != "" {
text += fmt.Sprintf("\n > 等第 %s", grade)
}
text += "\n > 科目短号 | 科目 | 分数/满分\n"
for index, subject := range qtVisibleSubjects(subjectsData) {
text += fmt.Sprintf(" > %03d | %s | %s/%s\n", index+1, stringOrNA(subject["km"]), stringOrNA(subject["myScore"]), stringOrNA(subject["fullScore"]))
}
text = strings.TrimRight(text, "\n")
text += "\n回复:/答题详情 [科目短号] 以获取详细信息。"
return text
}
func qtResolveExamSelector(selector string, exams []map[string]any) (map[string]any, string) {
selector = strings.TrimSpace(selector)
if selector == "" {
return nil, "* 错误: 指令格式有误!\n格式:/考试详情 [考试ID]\n示例:/考试详情 123456"
}
if strings.Contains(selector, "-") {
for _, exam := range exams {
if asString(exam["examGuid"]) == selector {
return exam, ""
}
}
return nil, "* 错误: 未找到对应考试,请先使用 /查询 确认考试是否存在。"
}
shortIDs := buildQTExamShortIDs(exams)
for _, exam := range exams {
if shortIDs[asString(exam["examGuid"])] == selector {
return exam, ""
}
}
return nil, "* 错误: 未找到对应考试,请先使用 /查询 确认考试ID是否正确。"
}
func qtSubjectSelector(content, command string) (string, string) {
selector := strings.TrimSpace(strings.TrimPrefix(content, command))
if selector == "" {
return "", fmt.Sprintf("* 错误: 指令格式有误!\n格式:%s [科目短号]\n示例:%s 001", command, command)
}
if qtSubjectShortIDPattern.MatchString(selector) {
return fmt.Sprintf("%03d", asInt(selector)), ""
}
return selector, ""
}
func qtLoadStoredSubjectContext(ctx *MessageContext, selector string) (map[string]any, map[string]any, map[string]any, string) {
examContext := opViewExamContext(ctx.UserID)
if examContext["Return"] != true {
return nil, nil, nil, "* 错误: 未找到前置考试信息,请先使用 /考试详情 查询一次考试。"
}
subjectMap := asMap(examContext["subject_map"])
if asString(subjectMap["__provider"]) != "qt" {
return nil, nil, nil, "* 错误: 当前前置考试信息不属于七天网络,请重新使用 /考试详情 查询一次七天考试。"
}
items := asMap(subjectMap["__items"])
aliases := asMap(subjectMap["__aliases"])
if alias := asString(aliases[selector]); alias != "" {
selector = alias
}
subject := asMap(items[selector])
if len(subject) == 0 {
return nil, nil, nil, "* 错误: 未查询到科目信息,请确认已获取对应考试详情。"
}
exam := asMap(subjectMap["__exam"])
if len(exam) == 0 || asString(exam["examGuid"]) == "" {
return nil, nil, nil, "* 错误: 前置考试上下文不完整,请重新使用 /考试详情 获取一次考试详情。"
}
return exam, subject, subjectMap, ""
}
func qtStoredSubjectCount(subjectMap map[string]any) int {
if count := asInt(subjectMap["__subjectCount"]); count > 0 {
return count
}
return len(asMap(subjectMap["__items"]))
}
func qtRenderSubjectGradeDetail(subject map[string]any, gradeRes map[string]any) string {
report := asMap(asMap(gradeRes["data"])["report"])
subjectName := defaultString(asString(subject["subject"]), "该科目")
score := defaultString(asString(report["myScore"]), asString(subject["score"]))
fullScore := defaultString(asString(report["fullScore"]), asString(subject["fullScore"]))
totalStudents := asInt(report["total"])
grade := strings.ToUpper(asString(report["grade"]))
text := fmt.Sprintf(" > %s ", subjectName)
text += fmt.Sprintf("分数 %s/%s", stringOrNA(score), stringOrNA(fullScore))
if totalStudents > 0 && grade != "" {
text += fmt.Sprintf("\n > 年级人数 %d | 等第 %s", totalStudents, grade)
if start, end, ok := qtEstimateRankRange(grade, totalStudents); ok {
if start == end {
text += fmt.Sprintf("\n > 预估年级排名 %d", start)
} else {
text += fmt.Sprintf("\n > 预估年级排名 %d-%d", start, end)
}
}
} else if totalStudents > 0 {
text += fmt.Sprintf("\n > 年级人数 %d", totalStudents)
} else if grade != "" {
text += fmt.Sprintf("\n > 等第 %s", grade)
}
return text
}
func qtTeacherLookupEnabled(teadata map[string]any) bool {
return teadata["Return"] == true && asString(teadata["tofenxi"]) != "FAILED"
}
func (h *CommandHandler) qtTeacherMarkFailed(teadata map[string]any, scene, reason string) {
schoolKey := asString(teadata["school"])
if schoolKey == "" {
return
}
opWriteTeacher(schoolKey, map[string]any{"tofenxi": "FAILED"})
moonSendMsg(appConfig.MoonGroupID, fmt.Sprintf(
"【机器人服务推送】\n七天教师端已标记FAILED\n学校:%s\n教师账号:%s\n触发场景:%s\n失败原因:%s",
schoolKey,
asString(teadata["account"]),
scene,
defaultString(reason, "未知错误"),
))
}
type qtTeacherLoginAttemptResult struct {
Updated map[string]any
Success bool
ForcedLogout bool
CredentialInvalid bool
Reason string
}
func (h *CommandHandler) qtTeacherLogin(ctx *MessageContext, teadata map[string]any) (map[string]any, bool) {
return h.qtTeacherLoginWithMode(ctx, teadata, qtTeacherLoginModeFromAny(teadata["login_mode"]))
}
func (h *CommandHandler) qtTeacherLoginWithMode(ctx *MessageContext, teadata map[string]any, preferredMode qtTeacherLoginMode) (map[string]any, bool) {
result := h.qtTeacherLoginSingleMode(ctx, teadata, preferredMode)
switch {
case result.Success:
return result.Updated, true
case result.CredentialInvalid:
h.qtTeacherMarkFailed(teadata, "登录失败", result.Reason)
return nil, false
case result.ForcedLogout && preferredMode == qtTeacherLoginModeWeb:
fallbackResult := h.qtTeacherLoginSingleMode(ctx, teadata, qtTeacherLoginModeApp)
switch {
case fallbackResult.Success:
return fallbackResult.Updated, true
case fallbackResult.CredentialInvalid:
h.qtTeacherMarkFailed(teadata, "登录失败", fallbackResult.Reason)
case fallbackResult.ForcedLogout:
h.qtTeacherMarkFailed(teadata, "被顶号", fallbackResult.Reason)
}
return nil, false
case result.ForcedLogout:
h.qtTeacherMarkFailed(teadata, "被顶号", result.Reason)
return nil, false
default:
return nil, false
}
}
func (h *CommandHandler) qtTeacherLoginSingleMode(ctx *MessageContext, teadata map[string]any, mode qtTeacherLoginMode) qtTeacherLoginAttemptResult {
schoolKey := asString(teadata["school"])
username := asString(teadata["account"])
password := asString(teadata["password"])
if schoolKey == "" || username == "" || password == "" {
return qtTeacherLoginAttemptResult{}
}
lastReason := ""
for attempt := 0; attempt < 2; attempt++ {
client := newQTTeacherClient("")
loginRes := client.loginWithModeWithContext(ctx, username, password, mode)
switch {
case qtTeacherIsCredentialInvalid(loginRes):
return qtTeacherLoginAttemptResult{
CredentialInvalid: true,
Reason: asString(loginRes["msg"]),
}
case qtTeacherIsForcedLogout(loginRes):
return qtTeacherLoginAttemptResult{
ForcedLogout: true,
Reason: asString(loginRes["msg"]),
}
case qtTeacherShouldRelogin(loginRes):
lastReason = asString(loginRes["msg"])
continue
case loginRes["getSuccess"] != true:
return qtTeacherLoginAttemptResult{Reason: asString(loginRes["msg"])}
}
type tenantListResult struct {
role int
res map[string]any
}
tenantListCh := make(chan tenantListResult, 2)
for _, role := range []int{1, 2} {
go func(role int) {
tenantListCh <- tenantListResult{
role: role,
res: client.tenantListWithContext(ctx, role),
}
}(role)
}
var roleOneRes map[string]any
var roleTwoRes map[string]any
for i := 0; i < 2; i++ {
result := <-tenantListCh
if result.role == 1 {
roleOneRes = result.res
} else {
roleTwoRes = result.res
}
}
switch {
case qtTeacherIsForcedLogout(roleOneRes):
return qtTeacherLoginAttemptResult{
ForcedLogout: true,
Reason: asString(roleOneRes["msg"]),
}
case qtTeacherShouldRelogin(roleOneRes):
lastReason = asString(roleOneRes["msg"])
continue
case roleOneRes["getSuccess"] != true:
return qtTeacherLoginAttemptResult{Reason: asString(roleOneRes["msg"])}
}
roleOneTenants := asSlice(asMap(roleOneRes["data"])["list"])
switch {
case qtTeacherIsForcedLogout(roleTwoRes):
return qtTeacherLoginAttemptResult{
ForcedLogout: true,
Reason: asString(roleTwoRes["msg"]),
}
case qtTeacherShouldRelogin(roleTwoRes):
lastReason = asString(roleTwoRes["msg"])
continue
case roleTwoRes["getSuccess"] != true:
return qtTeacherLoginAttemptResult{Reason: asString(roleTwoRes["msg"])}
}
roleTwoTenants := asSlice(asMap(roleTwoRes["data"])["list"])
globalRole := 0
tenantCode := qtTeacherSelectTenantCode(schoolKey, 1, roleOneTenants)
if tenantCode != "" {
globalRole = 1
} else {
tenantCode = qtTeacherSelectTenantCode(schoolKey, 2, roleTwoTenants)
if tenantCode != "" {
globalRole = 2
}
}
if globalRole == 0 {
h.qtTeacherMarkFailed(
teadata,
"学校角色匹配失败",
fmt.Sprintf(
"未找到学校名完全一致的租户,目标学校:%s,roleType=1 可选学校:%s,roleType=2 可选学校:%s",
qtTeacherTargetSchoolName(schoolKey),
strings.Join(qtTeacherTenantNames(roleOneTenants), "、"),
strings.Join(qtTeacherTenantNames(roleTwoTenants), "、"),
),
)
return qtTeacherLoginAttemptResult{}
}
if tenantCode == "" {
return qtTeacherLoginAttemptResult{}
}
entryRes := client.entryTenantWithContext(ctx, tenantCode, globalRole)
switch {
case qtTeacherIsForcedLogout(entryRes):
return qtTeacherLoginAttemptResult{
ForcedLogout: true,
Reason: asString(entryRes["msg"]),
}
case qtTeacherShouldRelogin(entryRes):
lastReason = asString(entryRes["msg"])
continue
case entryRes["getSuccess"] != true:
return qtTeacherLoginAttemptResult{Reason: asString(entryRes["msg"])}
}
opWriteTeacher(schoolKey, map[string]any{
"cookie": client.token,
"cookie_fx": tenantCode,
"login_mode": string(mode),
})
return qtTeacherLoginAttemptResult{
Updated: opViewTeacher(schoolKey),
Success: true,
}
}
return qtTeacherLoginAttemptResult{Reason: lastReason}
}
func (h *CommandHandler) qtTeacherRecoverFromAuthError(ctx *MessageContext, teadata, response map[string]any, reloginUsed, fallbackUsed *bool) (map[string]any, bool) {
currentMode := qtTeacherLoginModeFromAny(teadata["login_mode"])
switch {
case qtTeacherShouldRelogin(response):
if *reloginUsed {
return nil, false
}
updated, ok := h.qtTeacherLoginWithMode(ctx, teadata, currentMode)
if !ok {
return nil, false
}
*reloginUsed = true
return updated, true
case qtTeacherIsForcedLogout(response):
if currentMode == qtTeacherLoginModeWeb && !*fallbackUsed {
updated, ok := h.qtTeacherLoginWithMode(ctx, teadata, qtTeacherLoginModeApp)
if !ok {
return nil, false
}
*fallbackUsed = true
return updated, true
}
h.qtTeacherMarkFailed(teadata, "被顶号", asString(response["msg"]))
return nil, false
default:
return nil, false
}
}
func (h *CommandHandler) qtTeacherResolveRuleRef(ctx *MessageContext, teadata map[string]any, examGuid string, forceRefresh bool) (qtTeacherRuleRef, any, map[string]any) {
schoolKey := asString(teadata["school"])
if !forceRefresh {
if cachedRule := opViewQTTeacherRuleCache(schoolKey, examGuid); cachedRule.Valid() {
return cachedRule, nil, nil
}
}
client := newQTTeacherClient(asString(teadata["cookie"]))
ruleRes := client.examInfoRuleListWithContext(ctx, examGuid)
if ruleRes["getSuccess"] != true {
return qtTeacherRuleRef{}, nil, ruleRes
}
rule := qtTeacherSelectRule(ruleRes["data"])
if !rule.Valid() {
return qtTeacherRuleRef{}, ruleRes["data"], map[string]any{"getSuccess": true, "skip": true}
}
opWriteQTTeacherRuleCache(schoolKey, examGuid, rule.RuleGuid, rule.RuleType, qtTeacherRuleCacheTTL)
return rule, ruleRes["data"], nil
}
func qtTeacherDecodeOverallCachePayload(payload string) map[string]any {
if strings.TrimSpace(payload) == "" {
return nil
}
data := map[string]any{}
if err := json.Unmarshal([]byte(payload), &data); err != nil {
return nil
}
return map[string]any{
"getSuccess": true,
"data": data,
}
}
func (h *CommandHandler) qtTeacherResolveOverallReport(ctx *MessageContext, teadata map[string]any, examGuid string, rule qtTeacherRuleRef, forceRefresh bool) (map[string]any, map[string]any) {
schoolKey := asString(teadata["school"])
examRuCode := asString(teadata["cookie_fx"])
if schoolKey == "" || examRuCode == "" || examGuid == "" || !rule.Valid() {
return nil, map[string]any{"getSuccess": false}
}
if !forceRefresh {
if payload := opViewQTTeacherOverallCache(schoolKey, examRuCode, examGuid, rule.RuleGuid); payload != "" {
if cached := qtTeacherDecodeOverallCachePayload(payload); len(cached) > 0 {
return cached, nil
}
}
}
client := newQTTeacherClient(asString(teadata["cookie"]))
reportRes := client.singleSubjectAvgReportWithContext(ctx, examRuCode, examGuid, rule.RuleGuid)
if reportRes["getSuccess"] != true {
return nil, reportRes
}
if raw, err := json.Marshal(reportRes["data"]); err == nil {
opWriteQTTeacherOverallCache(schoolKey, examRuCode, examGuid, rule.RuleGuid, string(raw), qtTeacherOverallTTL)
}
return reportRes, nil
}
func (h *CommandHandler) qtTeacherResolveSupplementalRuleRef(ctx *MessageContext, teadata map[string]any, examGuid string, primary qtTeacherRuleRef, rulesData any) qtTeacherRuleRef {
if !primary.Valid() || primary.RuleType != 0 {
return qtTeacherRuleRef{}
}
if rulesData != nil {
rule := qtTeacherSelectSupplementalRule(rulesData)
if !rule.Valid() || rule.RuleGuid == primary.RuleGuid {
return qtTeacherRuleRef{}
}
return rule
}
client := newQTTeacherClient(asString(teadata["cookie"]))
ruleRes := client.examInfoRuleListWithContext(ctx, examGuid)
if ruleRes["getSuccess"] != true {
return qtTeacherRuleRef{}
}
rule := qtTeacherSelectSupplementalRule(ruleRes["data"])
if !rule.Valid() || rule.RuleGuid == primary.RuleGuid {
return qtTeacherRuleRef{}
}
return rule
}
type qtTeacherAnalysisMessages struct {
Main string
Group string
}
func qtTeacherWrapAnalysisTitle(title, text string) string {
if strings.TrimSpace(text) == "" {
return ""
}
if strings.TrimSpace(title) == "" {
return text
}
return "===== " + title + " =====\n" + text
}
func qtTeacherSupplementalGroupTitle(selectCombination int) string {
switch selectCombination {
case 10:
return "赋分报告(物理组)"
case 20:
return "赋分报告(历史组)"
default:
return "赋分报告(分组)"
}
}
func (h *CommandHandler) qtTeacherRenderRuleAnalysis(ctx *MessageContext, teadata map[string]any, examGuid, studentCode string, rule qtTeacherRuleRef, mainIncludeExam bool, groupTitle string) qtTeacherAnalysisMessages {
if !rule.Valid() {
return qtTeacherAnalysisMessages{}
}
overallRes, overallErr := h.qtTeacherResolveOverallReport(ctx, teadata, examGuid, rule, false)
if overallErr != nil {
return qtTeacherAnalysisMessages{}
}
client := newQTTeacherClient(asString(teadata["cookie"]))
scoreRes := client.comprehensiveScoreListV2WithContext(ctx, examGuid, rule.RuleGuid, studentCode, -1)
switch {
case qtTeacherIsForcedLogout(scoreRes):
return qtTeacherAnalysisMessages{}
case qtTeacherShouldRelogin(scoreRes):
return qtTeacherAnalysisMessages{}
case qtTeacherStudentNoPermission(scoreRes):
return qtTeacherAnalysisMessages{}
case scoreRes["getSuccess"] != true:
return qtTeacherAnalysisMessages{}
case !qtTeacherHasStudentResult(scoreRes):
return qtTeacherAnalysisMessages{}
}
groupRes := map[string]any{}
resolvedGroupTitle := groupTitle
if rule.RuleType == 2 {
combination := qtTeacherSelectCombination(qtTeacherFirstStudentRow(scoreRes))
if combination != 0 {
if strings.TrimSpace(resolvedGroupTitle) == "" {
resolvedGroupTitle = qtTeacherSupplementalGroupTitle(combination)
}
groupRes = client.comprehensiveScoreListV2WithContext(ctx, examGuid, rule.RuleGuid, studentCode, combination)
switch {
case qtTeacherIsForcedLogout(groupRes):
return qtTeacherAnalysisMessages{}
case qtTeacherShouldRelogin(groupRes):
return qtTeacherAnalysisMessages{}
case qtTeacherStudentNoPermission(groupRes):
groupRes = nil
case groupRes["getSuccess"] != true:
groupRes = nil
case !qtTeacherHasStudentResult(groupRes):
groupRes = nil
}
}
}
return qtTeacherAnalysisMessages{
Main: func() string {
if mainIncludeExam {
return qtTeacherRenderAnalysis(overallRes, scoreRes)
}
return qtTeacherRenderPersonalData(scoreRes)
}(),
Group: qtTeacherRenderGroupRankingWithTitle(groupRes, resolvedGroupTitle),
}
}
func (h *CommandHandler) qtLoadTeacherAnalysis(ctx *MessageContext, exam map[string]any) []string {
examGuid := asString(exam["examGuid"])
studentCode := asString(exam["studentCode"])
if examGuid == "" || studentCode == "" {
return nil
}
teadata := opViewTeacherQT(asString(h.userdata["school"]))
if !qtTeacherLookupEnabled(teadata) {
return nil
}
if asString(teadata["cookie"]) == "" || asString(teadata["cookie_fx"]) == "" {
updated, ok := h.qtTeacherLogin(ctx, teadata)
if !ok {
return nil
}
teadata = updated
}
forceRuleRefresh := false
reloginUsed := false
fallbackToAppUsed := false
for attempt := 0; attempt < 4; attempt++ {
rule, rulesData, ruleRes := h.qtTeacherResolveRuleRef(ctx, teadata, examGuid, forceRuleRefresh)
if ruleRes != nil {
switch {
case qtTeacherIsForcedLogout(ruleRes), qtTeacherShouldRelogin(ruleRes):
updated, ok := h.qtTeacherRecoverFromAuthError(ctx, teadata, ruleRes, &reloginUsed, &fallbackToAppUsed)
if !ok {
return nil
}
teadata = updated
continue
case ruleRes["skip"] == true:
return nil
default:
return nil
}
}
overallRes, overallErr := h.qtTeacherResolveOverallReport(ctx, teadata, examGuid, rule, forceRuleRefresh)
if overallErr != nil {
switch {
case qtTeacherIsForcedLogout(overallErr), qtTeacherShouldRelogin(overallErr):
updated, ok := h.qtTeacherRecoverFromAuthError(ctx, teadata, overallErr, &reloginUsed, &fallbackToAppUsed)
if !ok {
return nil
}
teadata = updated
continue
case qtTeacherRuleDeleted(overallErr):
if forceRuleRefresh {
return nil
}
opDeleteQTTeacherRuleCache(asString(teadata["school"]), examGuid)
forceRuleRefresh = true
continue
default:
return nil
}
}
client := newQTTeacherClient(asString(teadata["cookie"]))
scoreRes := client.comprehensiveScoreListV2WithContext(ctx, examGuid, rule.RuleGuid, studentCode, -1)
switch {
case qtTeacherIsForcedLogout(scoreRes), qtTeacherShouldRelogin(scoreRes):
updated, ok := h.qtTeacherRecoverFromAuthError(ctx, teadata, scoreRes, &reloginUsed, &fallbackToAppUsed)
if !ok {
return nil
}
teadata = updated
continue
case qtTeacherStudentNoPermission(scoreRes):
return nil
case scoreRes["getSuccess"] != true:
return nil
case !qtTeacherHasStudentResult(scoreRes):
continue
}
groupRes := map[string]any{}
if rule.RuleType == 2 {
combination := qtTeacherSelectCombination(qtTeacherFirstStudentRow(scoreRes))
if combination != 0 {
groupRes = client.comprehensiveScoreListV2WithContext(ctx, examGuid, rule.RuleGuid, studentCode, combination)
switch {
case qtTeacherIsForcedLogout(groupRes), qtTeacherShouldRelogin(groupRes):
updated, ok := h.qtTeacherRecoverFromAuthError(ctx, teadata, groupRes, &reloginUsed, &fallbackToAppUsed)
if !ok {
return nil
}
teadata = updated
continue
case qtTeacherStudentNoPermission(groupRes):
groupRes = nil
case groupRes["getSuccess"] != true:
groupRes = nil
case !qtTeacherHasStudentResult(groupRes):
groupRes = nil
}
}
}
messages := make([]string, 0, 4)
mainText := qtTeacherRenderAnalysis(overallRes, scoreRes)
if strings.TrimSpace(mainText) != "" {
messages = append(messages, mainText)
}
groupText := qtTeacherRenderGroupRankingWithTitle(groupRes, "物理/历史类排名")
if strings.TrimSpace(groupText) != "" {
messages = append(messages, groupText)
}
if len(messages) == 0 {
return nil
}
extraRule := h.qtTeacherResolveSupplementalRuleRef(ctx, teadata, examGuid, rule, rulesData)
extraMessages := h.qtTeacherRenderRuleAnalysis(ctx, teadata, examGuid, studentCode, extraRule, false, "")
if strings.TrimSpace(extraMessages.Main) != "" {
messages = append(messages, qtTeacherWrapAnalysisTitle("新高考赋分报告", extraMessages.Main))
}
if strings.TrimSpace(extraMessages.Group) != "" {
messages = append(messages, extraMessages.Group)
}
return messages
}
return nil
}
func (h *CommandHandler) reloginQT(ctx *MessageContext) (map[string]any, string) {
h.sender.SendText(messageContext(ctx), ctx, "* 凭证过期,正在尝试重新登录...")
response := qtStudentLoginWithContext(ctx, asString(h.userdata["zh"]), asString(h.userdata["pw"]))
if response["isSuccess"] != true {
return nil, qtStudentReloginErrorMessage(asString(response["msg"]))
}
opWrite(ctx.UserID, map[string]any{
"token": response["token"],
"name": response["name"],
"school": response["school"],
"grade": response["grade"],
})
h.userdata["token"] = response["token"]
h.userdata["name"] = response["name"]
h.userdata["school"] = response["school"]
h.userdata["grade"] = response["grade"]
userInfo := asMap(response["userInfo"])
h.qtProfileToken = asString(response["token"])
h.qtProfileData = userInfo
return userInfo, ""
}
func qtStudentReloginErrorMessage(msg string) string {
if strings.Contains(msg, "密码错误") {
msg = strings.TrimSpace(msg)
if msg == "" {
return messageQTPasswordChanged
}
return msg + "\n" + messageQTPasswordChanged
}
return msg
}
func qtStudentVisibleErrorMessage(msg string) string {
errMsg := "* 错误: " + defaultString(msg, "未知错误")
if errMsg == "* 错误: 访问受限!" {
errMsg += "\n本消息由七天服务器返回,具体原因暂时未知。"
}
return errMsg
}
func (h *CommandHandler) qtLoadUserInfoWithRelogin(ctx *MessageContext) (string, map[string]any, string) {
token := asString(h.userdata["token"])
if token == "" {
userInfo, errMsg := h.reloginQT(ctx)
if errMsg != "" {
return "", nil, "* 错误: 自动重登录失败: " + errMsg
}
return asString(h.userdata["token"]), userInfo, ""
}
if token == h.qtProfileToken && len(h.qtProfileData) > 0 {
return token, h.qtProfileData, ""
}
res := qtGetUserInfoWithContext(ctx, token)
if res["getSuccess"] == true {
userInfo := asMap(res["data"])
h.qtProfileToken = token
h.qtProfileData = userInfo
return token, userInfo, ""
}
if qtShouldRelogin(res) {
userInfo, errMsg := h.reloginQT(ctx)
if errMsg != "" {
return "", nil, "* 错误: 自动重登录失败: " + errMsg
}
return asString(h.userdata["token"]), userInfo, ""
}
return "", nil, qtStudentVisibleErrorMessage(asString(res["msg"]))
}
func (h *CommandHandler) qtExecuteWithProfile(ctx *MessageContext, operation string, fn func(string, map[string]any) map[string]any) (map[string]any, map[string]any, string) {
h.qtLastOperation = operation
token, userInfo, errMsg := h.qtLoadUserInfoWithRelogin(ctx)
if errMsg != "" {
return nil, nil, errMsg
}
res := fn(token, userInfo)
if res["getSuccess"] == true {
h.qtLastOperation = ""
return res, userInfo, ""
}
if qtShouldRelogin(res) {
userInfo, errMsg = h.reloginQT(ctx)
if errMsg != "" {
return nil, nil, "* 错误: 自动重登录失败: " + errMsg
}
res = fn(asString(h.userdata["token"]), userInfo)
if res["getSuccess"] == true {
h.qtLastOperation = ""
return res, userInfo, ""
}
}
return nil, nil, qtStudentVisibleErrorMessage(asString(res["msg"]))
}
func (h *CommandHandler) qtFetchAllClaimedExams(ctx *MessageContext) ([]map[string]any, map[string]any, string) {
all := make([]map[string]any, 0)
var userInfo map[string]any
for startIndex := 0; ; startIndex += 5 {
res, info, errMsg := h.qtExecuteWithProfile(ctx, fmt.Sprintf("get_claim_exams:start=%d rows=5", startIndex), func(token string, userInfo map[string]any) map[string]any {
return qtGetClaimExamsWithContext(ctx, token, userInfo, startIndex, 5)
})
if errMsg != "" {
return nil, nil, errMsg
}
userInfo = info
page := qtClaimedExamList(res["data"])
all = append(all, page...)
if len(page) < 5 {
break
}
}
return all, userInfo, ""
}
func (h *CommandHandler) qtLoadExamsWithAutoClaim(ctx *MessageContext) ([]map[string]any, []string, map[string]any, string) {
exams, userInfo, errMsg := h.qtFetchAllClaimedExams(ctx)
if errMsg != "" {
return nil, nil, nil, errMsg
}
unclaimedRes, latestUserInfo, errMsg := h.qtExecuteWithProfile(ctx, "get_unclaim_exams", func(token string, userInfo map[string]any) map[string]any {
return qtGetUnClaimExamsWithContext(ctx, token, userInfo)
})
if errMsg != "" {
return nil, nil, nil, errMsg
}
if len(latestUserInfo) > 0 {
userInfo = latestUserInfo
}
notes := make([]string, 0)
autoClaimed := 0
skippedMulti := 0
for _, item := range qtFlattenUnclaimedExams(unclaimedRes["data"]) {
candidates := asSlice(item["studentCodeList"])
if len(candidates) != 1 {
skippedMulti++
continue
}
_, _, claimErr := h.qtExecuteWithProfile(ctx, fmt.Sprintf("claim_exam:exam_guid=%s", asString(item["examGuid"])), func(token string, userInfo map[string]any) map[string]any {
return qtClaimExamWithContext(ctx, token, asString(item["examGuid"]), asString(candidates[0]))
})
if claimErr != "" {
notes = append(notes, fmt.Sprintf("* 提示: 自动认领考试[%s]失败:%s", asString(item["examName"]), strings.TrimPrefix(claimErr, "* 错误: ")))
continue
}