99package me.stageguard.sctimetable.api.edu_system.`super`
1010
1111import io.ktor.client.HttpClient
12+ import io.ktor.client.call.*
1213import io.ktor.client.engine.okhttp.*
1314import io.ktor.client.request.*
14- import io.ktor.client.statement.*
1515import io.ktor.utils.io.*
1616import kotlinx.coroutines.*
17- import kotlinx.serialization.ExperimentalSerializationApi
1817import kotlinx.serialization.decodeFromString
1918import kotlinx.serialization.json.Json
2019import me.stageguard.sctimetable.service.TimeProviderService
@@ -37,7 +36,6 @@ data class LoginInfoData(
3736 val password : String
3837)
3938
40- @ExperimentalSerializationApi
4139object SuperCourseApiService {
4240
4341 private const val BASE_URL : String = " http://120.55.151.61"
@@ -76,7 +74,7 @@ object SuperCourseApiService {
7674 password : String ,
7775 cookieBlock : ((LoginCookieData ) -> Unit )? = null
7876 ) : Either <ErrorLoginReceiptDTO , LoginReceiptDTO > = try {
79- client.post< HttpStatement > {
77+ val response = client.post {
8078 url(" $BASE_URL /V2/StudentSkip/loginCheckV4.action" )
8179 parameter(" account" , EncryptionUtils .encrypt(username))
8280 parameter(" password" , EncryptionUtils .encrypt(password))
@@ -87,36 +85,35 @@ object SuperCourseApiService {
8785 parameter(" phoneModel" , PHONE_MODEL )
8886 parameter(" updateInfo" , false )
8987 parameter(" channel" , " ppMarket" )
90- }.execute { response ->
91- var cookieList: List <String > = arrayListOf (" " , " " )
92- response.headers.forEach { s: String , list: List <String > ->
93- if (s.contains(" set-cookie" )) {
94- cookieList = list
95- return @forEach
96- }
97- }
98- cookieBlock?.invoke(LoginCookieData (cookieList[0 ].let {
99- val jSessionMatcher = jSessionIdRegExp.matcher(it)
100- if (jSessionMatcher.find()) {
101- jSessionMatcher.group(1 )
102- } else " "
103- }, cookieList[1 ].let {
104- val serverIdMatcher = serverIdRegexp.matcher(it)
105- if (serverIdMatcher.find()) {
106- serverIdMatcher.group(1 )
107- } else " "
108- }))
109- val result = (response.content.readUTF8Line() ? : " {\" data\" :{\" errorStr\" :\" Empty response content.\" },\" status\" :1}" )
110- if (Pattern .compile(" errorStr" ).matcher(result).find()) {
111- Either (try {
112- json.decodeFromString(result)
113- } catch (ignored: Exception ) {
114- ErrorLoginReceiptDTO (__InternalErrorLoginMsg (result), 1 )
115- } )
116- } else {
117- Either .invoke<ErrorLoginReceiptDTO , LoginReceiptDTO >(json.decodeFromString<LoginReceiptDTO >(result))
118- }
88+ }
11989
90+ var cookieList: List <String > = arrayListOf (" " , " " )
91+ response.headers.forEach { s: String , list: List <String > ->
92+ if (s.contains(" set-cookie" )) {
93+ cookieList = list
94+ return @forEach
95+ }
96+ }
97+ cookieBlock?.invoke(LoginCookieData (cookieList[0 ].let {
98+ val jSessionMatcher = jSessionIdRegExp.matcher(it)
99+ if (jSessionMatcher.find()) {
100+ jSessionMatcher.group(1 )
101+ } else " "
102+ }, cookieList[1 ].let {
103+ val serverIdMatcher = serverIdRegexp.matcher(it)
104+ if (serverIdMatcher.find()) {
105+ serverIdMatcher.group(1 )
106+ } else " "
107+ }))
108+ val result = (response.body<ByteReadChannel >().readUTF8Line() ? : " {\" data\" :{\" errorStr\" :\" Empty response content.\" },\" status\" :1}" )
109+ if (Pattern .compile(" errorStr" ).matcher(result).find()) {
110+ Either (try {
111+ json.decodeFromString(result)
112+ } catch (ignored: Exception ) {
113+ ErrorLoginReceiptDTO (__InternalErrorLoginMsg (result), 1 )
114+ } )
115+ } else {
116+ Either .invoke<ErrorLoginReceiptDTO , LoginReceiptDTO >(json.decodeFromString<LoginReceiptDTO >(result))
120117 }
121118 } catch (ex: Exception ) {
122119 Either (ErrorLoginReceiptDTO (__InternalErrorLoginMsg (ex.toString(),0 , 0 ), 1 ))
@@ -136,7 +133,7 @@ object SuperCourseApiService {
136133 * 成功则返回[CourseReceiptDTO],失败则返回[ErrorCourseReceiptDTO]
137134 */
138135 suspend fun getCourses (jSessionId : String , serverId : String ) : Either <ErrorCourseReceiptDTO , CourseReceiptDTO > = try {
139- client.post< HttpStatement > {
136+ val response = client.post {
140137 url(" $BASE_URL /V2/Course/getCourseTableFromServer.action" )
141138 header(" Cookie" , " JSESSIONID=$jSessionId ;SERVERID=$serverId " )
142139 parameter(" beginYear" , TimeProviderService .currentSemesterBeginYear)
@@ -146,16 +143,16 @@ object SuperCourseApiService {
146143 parameter(" phoneBrand" , PHONE_BRAND )
147144 parameter(" phoneVersion" , PHONE_VERSION )
148145 parameter(" phoneModel" , PHONE_MODEL )
149- }.execute {
150- val result = it.content.readUTF8Line() ? : " {\" message\" :\" Empty response content.\" ,\" title\" :\"\" }"
146+ }
147+
148+ val result = response.body<ByteReadChannel >().readUTF8Line() ? : " {\" message\" :\" Empty response content.\" ,\" title\" :\"\" }"
149+ try {
150+ Either .invoke<ErrorCourseReceiptDTO , CourseReceiptDTO >(json.decodeFromString<CourseReceiptDTO >(result))
151+ } catch (error: Exception ) {
151152 try {
152- Either .invoke<ErrorCourseReceiptDTO , CourseReceiptDTO >(json.decodeFromString<CourseReceiptDTO >(result))
153- } catch (error: Exception ) {
154- try {
155- Either .invoke(json.decodeFromString(result))
156- } catch (ex: Exception ) {
157- Either .invoke(ErrorCourseReceiptDTO (" decode error" , result))
158- }
153+ Either .invoke(json.decodeFromString(result))
154+ } catch (ex: Exception ) {
155+ Either .invoke(ErrorCourseReceiptDTO (" decode error" , result))
159156 }
160157 }
161158 } catch (ex: Exception ) {
0 commit comments