@@ -18,7 +18,6 @@ package jwch
1818
1919import (
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}
0 commit comments