Skip to content

Commit 7a80ffa

Browse files
yxrxyrenbaoshuo
andauthored
fix: move credit_format to server (#52)
* Update GetCreditV2 function to return only major when minor data is empty * fix: calculate total need credit by summing individual needs instead of total-gain difference * fix: rewrite if-else to switch statement and fix total need credit calculation * fix: credit format * fix: move buildCredit to server * fix: move buildCredit to server --------- Co-authored-by: Baoshuo <i@baoshuo.ren>
1 parent e7ab55f commit 7a80ffa

File tree

2 files changed

+6
-72
lines changed

2 files changed

+6
-72
lines changed

credit.go

Lines changed: 6 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package jwch
1818

1919
import (
2020
"fmt"
21-
"strconv"
2221
"strings"
2322

2423
"github.com/antchfx/htmlquery"
@@ -134,26 +133,24 @@ func (s *Student) GetGPA() (gpa *GPABean, err error) {
134133
return gpa, nil
135134
}
136135

137-
// GetCreditV2 用于获取V2版本的学分统计,返回指定的JSON结构
138-
func (s *Student) GetCreditV2() (CreditResponse, error) {
136+
// GetCreditV2 用于获取原始的学分统计
137+
func (s *Student) GetCreditV2() (majorCredits, minorCredits []*CreditStatistics, err error) {
139138
resp, err := s.GetWithIdentifier(constants.CreditQueryURL)
140139
if err != nil {
141-
return nil, err
140+
return nil, nil, err
142141
}
143142

144143
spanNode := htmlquery.FindOne(resp, `//*[@id="ContentPlaceHolder1_LB_kb"]`)
145144
if spanNode == nil {
146-
return nil, fmt.Errorf("failed to find the statistics span element")
145+
return nil, nil, fmt.Errorf("failed to find the statistics span element")
147146
}
148147

149148
tables := htmlquery.Find(spanNode, "//table")
150149
if len(tables) == 0 {
151-
return nil, fmt.Errorf("failed to find tables within the span element")
150+
return nil, nil, fmt.Errorf("failed to find tables within the span element")
152151
}
153152
tables = tables[:len(tables)-1] // 去掉最后一个表格
154153

155-
var majorCredits, minorCredits []*CreditStatistics
156-
157154
// 处理主修专业和辅修专业
158155
for tableIndex, table := range tables {
159156
rows := htmlquery.Find(table, "//tr")
@@ -194,53 +191,5 @@ func (s *Student) GetCreditV2() (CreditResponse, error) {
194191
}
195192
}
196193

197-
majorItem := buildCreditCategory("主修专业", majorCredits)
198-
199-
if len(minorCredits) > 0 {
200-
minorItem := buildCreditCategory("辅修专业", minorCredits)
201-
return CreditResponse{majorItem, minorItem}, nil
202-
}
203-
204-
return CreditResponse{majorItem}, nil
205-
}
206-
207-
func buildCreditCategory(categoryType string, credits []*CreditStatistics) *CreditCategory {
208-
category := &CreditCategory{
209-
Type: categoryType,
210-
Data: make([]*CreditDetail, 0, len(credits)),
211-
}
212-
213-
specialKeys := []string{"奖励", "其它", "重修", "正在修习", "CET"}
214-
for _, credit := range credits {
215-
isSpecial := false
216-
for _, key := range specialKeys {
217-
if strings.Contains(credit.Type, key) {
218-
isSpecial = true
219-
break
220-
}
221-
}
222-
223-
if isSpecial {
224-
category.Data = append(category.Data, &CreditDetail{
225-
Key: credit.Type,
226-
Value: credit.Gain,
227-
})
228-
} else {
229-
gain, _ := strconv.ParseFloat(credit.Gain, 64)
230-
total, _ := strconv.ParseFloat(credit.Total, 64)
231-
var value string
232-
233-
if gain >= total {
234-
value = fmt.Sprintf("%.1f/%.1f(已修满)", gain, total)
235-
} else {
236-
value = fmt.Sprintf("%.1f/%.1f(还需%.1f分)", gain, total, total-gain)
237-
}
238-
239-
category.Data = append(category.Data, &CreditDetail{
240-
Key: credit.Type,
241-
Value: value,
242-
})
243-
}
244-
}
245-
return category
194+
return majorCredits, minorCredits, err
246195
}

model.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -243,18 +243,3 @@ type TunnelResponse struct {
243243
Data []TunnelData `json:"data"` // 隧道数据数组
244244
RequestId string `json:"request_id"` // 请求ID
245245
}
246-
247-
// CreditDetail 用于表示学分的详细数据项
248-
type CreditDetail struct {
249-
Key string `json:"key"`
250-
Value string `json:"value"`
251-
}
252-
253-
// CreditCategory 用于表示学分分类
254-
type CreditCategory struct {
255-
Type string `json:"type"`
256-
Data []*CreditDetail `json:"data"`
257-
}
258-
259-
// CreditResponse 用于学分响应
260-
type CreditResponse []*CreditCategory

0 commit comments

Comments
 (0)