main
lensfrex 1 year ago
parent 731f3b70ca
commit 65e94fae91
Signed by: lensfrex
GPG Key ID: 0F69A0A2FBEE98A0
  1. 4
      common/src/main/java/cn/wustlinghang/wusthelper/rpc/exception/CommonRpcException.java
  2. 30
      common/src/main/java/cn/wustlinghang/wusthelper/rpc/exception/RpcException.java
  3. 6
      sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/CookieApi.java
  4. 38
      sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/CourseTableApi.java
  5. 37
      sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/CreditStatusApi.java
  6. 49
      sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ExamActivitiesApi.java
  7. 52
      sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ExamDelayApplicationApi.java
  8. 32
      sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/ScoreApi.java
  9. 29
      sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/StudentInfoApi.java
  10. 31
      sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/TrainingPlanApi.java
  11. 14
      sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/ApiExceptionHandler.java
  12. 18
      sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/api/json/handler/RpcExceptionHandler.java
  13. 72
      sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/exception/UndergradRpcException.java
  14. 60
      sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/LoginService.java
  15. 96
      sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/ParseService.java
  16. 134
      sub-services/undergrad/src/main/java/cn/wustlinghang/wusthelper/internal/undergrad/services/RequestAgentService.java

@ -1,4 +0,0 @@
package cn.wustlinghang.wusthelper.rpc.exception;
public class CommonRpcException extends RuntimeException {
}

@ -0,0 +1,30 @@
package cn.wustlinghang.wusthelper.rpc.exception;
public class RpcException extends RuntimeException {
protected final int code;
public RpcException(int code, String message) {
super(message);
this.code = code;
}
public RpcException(int code) {
this(code, String.valueOf(code));
}
public RpcException(int module, int type, int exceptionCode) {
this((module * 1000_0000) + (type * 1_0000) + (exceptionCode));
}
public RpcException(int module, int type, int exceptionCode, String message) {
this((module * 1000_0000) + (type * 1_0000) + (exceptionCode), message);
}
public int getCode() {
return code;
}
public static RpcException ApiNotImplement() {
return new RpcException(-1, "接口未实现");
}
}

@ -1,7 +1,7 @@
package cn.wustlinghang.wusthelper.internal.undergrad.api.json; package cn.wustlinghang.wusthelper.internal.undergrad.api.json;
import cn.wustlinghang.mywust.exception.ApiException;
import cn.wustlinghang.wusthelper.internal.undergrad.services.LoginService; import cn.wustlinghang.wusthelper.internal.undergrad.services.LoginService;
import cn.wustlinghang.wusthelper.rpc.exception.RpcException;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
import jakarta.ws.rs.GET; import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path; import jakarta.ws.rs.Path;
@ -20,13 +20,13 @@ public class CookieApi {
@GET @GET
@Path("/") @Path("/")
public String login(@QueryParam("username") @NotNull String username, public String login(@QueryParam("username") @NotNull String username,
@QueryParam("password") @NotNull String password) throws IOException, ApiException { @QueryParam("password") @NotNull String password) throws RpcException {
return loginService.login(username, password, false); return loginService.login(username, password, false);
} }
@GET @GET
@Path("/verify") @Path("/verify")
public Boolean verify(@QueryParam("cookie") @NotNull String cookie) throws IOException { public Boolean verify(@QueryParam("cookie") @NotNull String cookie) throws RpcException {
return loginService.verify(cookie); return loginService.verify(cookie);
} }
} }

@ -1,12 +1,11 @@
package cn.wustlinghang.wusthelper.internal.undergrad.api.json; package cn.wustlinghang.wusthelper.internal.undergrad.api.json;
import cn.wustlinghang.mywust.core.parser.undergraduate.UndergradCourseTableParser;
import cn.wustlinghang.mywust.core.request.service.undergraduate.UndergradCourseTableApiService;
import cn.wustlinghang.mywust.data.global.Course; import cn.wustlinghang.mywust.data.global.Course;
import cn.wustlinghang.mywust.data.global.StudentInfo;
import cn.wustlinghang.mywust.exception.ApiException; import cn.wustlinghang.mywust.exception.ApiException;
import cn.wustlinghang.mywust.exception.ParseException; import cn.wustlinghang.mywust.exception.ParseException;
import cn.wustlinghang.wusthelper.rpc.response.RpcResponse; import cn.wustlinghang.wusthelper.internal.undergrad.services.ParseService;
import cn.wustlinghang.wusthelper.internal.undergrad.services.RequestAgentService;
import cn.wustlinghang.wusthelper.rpc.exception.RpcException;
import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.context.ApplicationScoped;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
import jakarta.ws.rs.GET; import jakarta.ws.rs.GET;
@ -20,33 +19,34 @@ import java.util.List;
@Path("/course_table") @Path("/course_table")
@ApplicationScoped @ApplicationScoped
public class CourseTableApi { public class CourseTableApi {
private final UndergradCourseTableApiService studentInfoApiService; private final RequestAgentService agentService;
private final UndergradCourseTableParser studentInfoPageParser; private final ParseService parseService;
public CourseTableApi(UndergradCourseTableApiService studentInfoApiService, public CourseTableApi(RequestAgentService agentService,
UndergradCourseTableParser studentInfoPageParser) { ParseService parseService) {
this.studentInfoApiService = studentInfoApiService; this.agentService = agentService;
this.studentInfoPageParser = studentInfoPageParser; this.parseService = parseService;
} }
@GET @GET
@Path("/") @Path("/")
public List<Course> get(@QueryParam("cookie") @NotNull String cookie, @QueryParam("term") String term) public List<Course> get(@QueryParam("cookie") @NotNull String cookie,
throws IOException, ApiException, ParseException { @QueryParam("term") String term)
String html = studentInfoApiService.getPage(term, cookie); throws RpcException {
return this.parse(html); return this.parse(this.agent(cookie, term));
} }
@GET @GET
@Path("/agent") @Path("/agent")
public String agent(@QueryParam("cookie") @NotNull String cookie, @QueryParam("term") String term) public String agent(@QueryParam("cookie") @NotNull String cookie,
throws IOException, ApiException { @QueryParam("term") String term)
return studentInfoApiService.getPage(term, cookie); throws RpcException {
return agentService.getCourseTable(cookie, term);
} }
@POST @POST
@Path("/parse") @Path("/parse")
public List<Course> parse(String html) throws ParseException { public List<Course> parse(String html) throws RpcException {
return studentInfoPageParser.parse(html); return parseService.parseCourseTable(html);
} }
} }

@ -1,12 +1,10 @@
package cn.wustlinghang.wusthelper.internal.undergrad.api.json; package cn.wustlinghang.wusthelper.internal.undergrad.api.json;
import cn.wustlinghang.mywust.core.parser.undergraduate.UndergradCreditStatusParser;
import cn.wustlinghang.mywust.core.request.service.undergraduate.UndergradCreditStatusApiService;
import cn.wustlinghang.mywust.data.global.StudentInfo;
import cn.wustlinghang.mywust.exception.ApiException; import cn.wustlinghang.mywust.exception.ApiException;
import cn.wustlinghang.mywust.exception.ParseException; import cn.wustlinghang.mywust.exception.ParseException;
import cn.wustlinghang.mywust.network.RequestClientOption; import cn.wustlinghang.wusthelper.internal.undergrad.services.ParseService;
import cn.wustlinghang.wusthelper.rpc.response.RpcResponse; import cn.wustlinghang.wusthelper.internal.undergrad.services.RequestAgentService;
import cn.wustlinghang.wusthelper.rpc.exception.RpcException;
import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.context.ApplicationScoped;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
import jakarta.ws.rs.GET; import jakarta.ws.rs.GET;
@ -19,36 +17,33 @@ import java.io.IOException;
@Path("/credit_status") @Path("/credit_status")
@ApplicationScoped @ApplicationScoped
public class CreditStatusApi { public class CreditStatusApi {
private final UndergradCreditStatusApiService creditStatusApiService; private final RequestAgentService agentService;
private final UndergradCreditStatusParser creditStatusParser; private final ParseService parseService;
private final RequestClientOption option; public CreditStatusApi(RequestAgentService agentService,
ParseService parseService) {
public CreditStatusApi(UndergradCreditStatusApiService creditStatusApiService, this.agentService = agentService;
UndergradCreditStatusParser creditStatusParser, this.parseService = parseService;
RequestClientOption option) {
this.creditStatusApiService = creditStatusApiService;
this.creditStatusParser = creditStatusParser;
this.option = option;
} }
@GET @GET
@Path("/") @Path("/")
public String get(String cookie) throws IOException, ApiException, ParseException { public String get(@QueryParam("cookie") @NotNull String cookie)
String html = creditStatusApiService.getPage(cookie, option, false); throws RpcException {
String html = this.agent(cookie);
return this.parse(html); return this.parse(html);
} }
@GET @GET
@Path("/agent") @Path("/agent")
public String agent(@QueryParam("cookie") @NotNull String cookie) public String agent(@QueryParam("cookie") @NotNull String cookie)
throws IOException, ApiException { throws RpcException {
return creditStatusApiService.getPage(cookie, option, false); return agentService.getCreditStatus(cookie);
} }
@POST @POST
@Path("/parse") @Path("/parse")
public String parse(String html) throws ParseException { public String parse(String html) throws RpcException {
return creditStatusParser.parse(html); return parseService.parseCreditStatus(html);
} }
} }

@ -0,0 +1,49 @@
package cn.wustlinghang.wusthelper.internal.undergrad.api.json;
import cn.wustlinghang.mywust.core.request.service.undergraduate.UndergradExamDelayApiService;
import cn.wustlinghang.mywust.data.global.Score;
import cn.wustlinghang.wusthelper.internal.undergrad.services.ParseService;
import cn.wustlinghang.wusthelper.internal.undergrad.services.RequestAgentService;
import cn.wustlinghang.wusthelper.rpc.exception.RpcException;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.validation.constraints.NotNull;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.QueryParam;
import java.util.List;
@Path("/exam_activities")
@ApplicationScoped
public class ExamActivitiesApi {
private final RequestAgentService agentService;
public ExamActivitiesApi(RequestAgentService agentService) {
this.agentService = agentService;
}
@GET
@Path("/")
public UndergradExamDelayApiService.ExamActivity[] get(
@QueryParam("cookie") @NotNull String cookie,
@QueryParam("term") @NotNull String term)
throws RpcException {
return this.agent(cookie, term);
}
@GET
@Path("/agent")
public UndergradExamDelayApiService.ExamActivity[] agent(
@QueryParam("cookie") @NotNull String cookie,
@QueryParam("term") @NotNull String term)
throws RpcException {
return agentService.getExamActivities(cookie, term);
}
@POST
@Path("/parse")
public List<Score> parse(String html) throws RpcException {
throw RpcException.ApiNotImplement();
}
}

@ -0,0 +1,52 @@
package cn.wustlinghang.wusthelper.internal.undergrad.api.json;
import cn.wustlinghang.mywust.data.undergrad.ExamDelayApplication;
import cn.wustlinghang.wusthelper.internal.undergrad.services.ParseService;
import cn.wustlinghang.wusthelper.internal.undergrad.services.RequestAgentService;
import cn.wustlinghang.wusthelper.rpc.exception.RpcException;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.validation.constraints.NotNull;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.QueryParam;
import java.util.List;
@Path("/exam_delay_application")
@ApplicationScoped
public class ExamDelayApplicationApi {
private final RequestAgentService agentService;
private final ParseService parseService;
public ExamDelayApplicationApi(RequestAgentService agentService,
ParseService parseService) {
this.agentService = agentService;
this.parseService = parseService;
}
@GET
@Path("/")
public List<ExamDelayApplication> get(
@QueryParam("cookie") @NotNull String cookie,
@QueryParam("term") @NotNull String term,
@QueryParam("activities") @NotNull String activityId)
throws RpcException {
return this.parse(this.agent(cookie, term, activityId));
}
@GET
@Path("/agent")
public String agent(@QueryParam("cookie") @NotNull String cookie,
@QueryParam("term") @NotNull String term,
@QueryParam("activities") @NotNull String activityId)
throws RpcException {
return agentService.getExamDelayApplications(cookie, term, activityId);
}
@POST
@Path("/parse")
public List<ExamDelayApplication> parse(String html) throws RpcException {
return parseService.parseExamDelayApplications(html);
}
}

@ -1,12 +1,11 @@
package cn.wustlinghang.wusthelper.internal.undergrad.api.json; package cn.wustlinghang.wusthelper.internal.undergrad.api.json;
import cn.wustlinghang.mywust.core.parser.undergraduate.UndergradScoreParser;
import cn.wustlinghang.mywust.core.request.service.undergraduate.UndergradScoreApiService;
import cn.wustlinghang.mywust.data.global.Score; import cn.wustlinghang.mywust.data.global.Score;
import cn.wustlinghang.mywust.data.global.StudentInfo;
import cn.wustlinghang.mywust.exception.ApiException; import cn.wustlinghang.mywust.exception.ApiException;
import cn.wustlinghang.mywust.exception.ParseException; import cn.wustlinghang.mywust.exception.ParseException;
import cn.wustlinghang.wusthelper.rpc.response.RpcResponse; import cn.wustlinghang.wusthelper.internal.undergrad.services.ParseService;
import cn.wustlinghang.wusthelper.internal.undergrad.services.RequestAgentService;
import cn.wustlinghang.wusthelper.rpc.exception.RpcException;
import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.context.ApplicationScoped;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
import jakarta.ws.rs.GET; import jakarta.ws.rs.GET;
@ -20,31 +19,32 @@ import java.util.List;
@Path("/score") @Path("/score")
@ApplicationScoped @ApplicationScoped
public class ScoreApi { public class ScoreApi {
private final UndergradScoreApiService scoreApiService; private final RequestAgentService agentService;
private final UndergradScoreParser scoreParser; private final ParseService parseService;
public ScoreApi(UndergradScoreApiService scoreApiService, UndergradScoreParser scoreParser) { public ScoreApi(RequestAgentService agentService,
this.scoreApiService = scoreApiService; ParseService parseService) {
this.scoreParser = scoreParser; this.agentService = agentService;
this.parseService = parseService;
} }
@GET @GET
@Path("/") @Path("/")
public List<Score> get(String cookie) throws IOException, ApiException, ParseException { public List<Score> get(@QueryParam("cookie") @NotNull String cookie)
String html = scoreApiService.getPage(cookie); throws RpcException {
return this.parse(html); return this.parse(this.agent(cookie));
} }
@GET @GET
@Path("/agent") @Path("/agent")
public String agent(@QueryParam("cookie") @NotNull String cookie) public String agent(@QueryParam("cookie") @NotNull String cookie)
throws IOException, ApiException { throws RpcException {
return scoreApiService.getPage(cookie); return agentService.getScore(cookie);
} }
@POST @POST
@Path("/parse") @Path("/parse")
public List<Score> parse(String html) throws ParseException { public List<Score> parse(String html) throws RpcException {
return scoreParser.parse(html); return parseService.parseScore(html);
} }
} }

@ -1,10 +1,12 @@
package cn.wustlinghang.wusthelper.internal.undergrad.api.json; package cn.wustlinghang.wusthelper.internal.undergrad.api.json;
import cn.wustlinghang.mywust.core.parser.undergraduate.UndergradStudentInfoPageParser;
import cn.wustlinghang.mywust.core.request.service.undergraduate.UndergradStudentInfoApiService;
import cn.wustlinghang.mywust.data.global.StudentInfo; import cn.wustlinghang.mywust.data.global.StudentInfo;
import cn.wustlinghang.mywust.exception.ApiException; import cn.wustlinghang.mywust.exception.ApiException;
import cn.wustlinghang.mywust.exception.ParseException; import cn.wustlinghang.mywust.exception.ParseException;
import cn.wustlinghang.wusthelper.internal.undergrad.exception.UndergradRpcException;
import cn.wustlinghang.wusthelper.internal.undergrad.services.ParseService;
import cn.wustlinghang.wusthelper.internal.undergrad.services.RequestAgentService;
import cn.wustlinghang.wusthelper.rpc.exception.RpcException;
import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.context.ApplicationScoped;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
import jakarta.ws.rs.GET; import jakarta.ws.rs.GET;
@ -17,31 +19,32 @@ import java.io.IOException;
@Path("/student_info") @Path("/student_info")
@ApplicationScoped @ApplicationScoped
public class StudentInfoApi { public class StudentInfoApi {
private final UndergradStudentInfoApiService studentInfoApiService; private final RequestAgentService agentService;
private final UndergradStudentInfoPageParser studentInfoPageParser; private final ParseService parseService;
public StudentInfoApi(UndergradStudentInfoApiService studentInfoApiService, public StudentInfoApi(RequestAgentService agentService,
UndergradStudentInfoPageParser studentInfoPageParser) { ParseService parseService) {
this.studentInfoApiService = studentInfoApiService; this.agentService = agentService;
this.studentInfoPageParser = studentInfoPageParser; this.parseService = parseService;
} }
@GET @GET
@Path("/") @Path("/")
public StudentInfo get(@QueryParam("cookie") @NotNull String cookie) throws IOException, ApiException, ParseException { public StudentInfo get(@QueryParam("cookie") @NotNull String cookie)
throws RpcException {
return this.parse(this.agent(cookie)); return this.parse(this.agent(cookie));
} }
@GET @GET
@Path("/agent") @Path("/agent")
public String agent(@QueryParam("cookie") @NotNull String cookie) public String agent(@QueryParam("cookie") @NotNull String cookie)
throws IOException, ApiException { throws RpcException {
return studentInfoApiService.getPage(cookie); return agentService.getStudentInfoPage(cookie);
} }
@POST @POST
@Path("/parse") @Path("/parse")
public StudentInfo parse(String html) throws ParseException { public StudentInfo parse(String html) throws RpcException {
return studentInfoPageParser.parse(html); return parseService.parseStudentInfo(html);
} }
} }

@ -1,9 +1,10 @@
package cn.wustlinghang.wusthelper.internal.undergrad.api.json; package cn.wustlinghang.wusthelper.internal.undergrad.api.json;
import cn.wustlinghang.mywust.core.parser.undergraduate.UndergradTrainingPlanPageParser;
import cn.wustlinghang.mywust.core.request.service.undergraduate.UndergradTrainingPlanApiService;
import cn.wustlinghang.mywust.exception.ApiException; import cn.wustlinghang.mywust.exception.ApiException;
import cn.wustlinghang.mywust.exception.ParseException; import cn.wustlinghang.mywust.exception.ParseException;
import cn.wustlinghang.wusthelper.internal.undergrad.services.ParseService;
import cn.wustlinghang.wusthelper.internal.undergrad.services.RequestAgentService;
import cn.wustlinghang.wusthelper.rpc.exception.RpcException;
import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.context.ApplicationScoped;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
import jakarta.ws.rs.GET; import jakarta.ws.rs.GET;
@ -16,32 +17,32 @@ import java.io.IOException;
@Path("/training_plan") @Path("/training_plan")
@ApplicationScoped @ApplicationScoped
public class TrainingPlanApi { public class TrainingPlanApi {
private final UndergradTrainingPlanApiService trainingPlanApiService; private final RequestAgentService agentService;
private final UndergradTrainingPlanPageParser trainingPlanPageParser; private final ParseService parseService;
public TrainingPlanApi(UndergradTrainingPlanApiService trainingPlanApiService, public TrainingPlanApi(RequestAgentService agentService,
UndergradTrainingPlanPageParser trainingPlanPageParser) { ParseService parseService) {
this.trainingPlanApiService = trainingPlanApiService; this.agentService = agentService;
this.trainingPlanPageParser = trainingPlanPageParser; this.parseService = parseService;
} }
@GET @GET
@Path("/") @Path("/")
public String get(String cookie) throws IOException, ApiException, ParseException { public String get(@QueryParam("cookie") @NotNull String cookie)
String html = trainingPlanApiService.getPage(cookie); throws RpcException {
return this.parse(html); return this.parse(this.agent(cookie));
} }
@GET @GET
@Path("/agent") @Path("/agent")
public String agent(@QueryParam("cookie") @NotNull String cookie) public String agent(@QueryParam("cookie") @NotNull String cookie)
throws IOException, ApiException { throws RpcException {
return trainingPlanApiService.getPage(cookie); return agentService.getTrainingPlan(cookie);
} }
@POST @POST
@Path("/parse") @Path("/parse")
public String parse(String html) throws ParseException { public String parse(String html) throws RpcException {
return trainingPlanPageParser.parse(html); return parseService.parseTrainingPlan(html);
} }
} }

@ -1,14 +0,0 @@
package cn.wustlinghang.wusthelper.internal.undergrad.api.json.handler;
import cn.wustlinghang.mywust.exception.ApiException;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.ExceptionMapper;
import jakarta.ws.rs.ext.Provider;
@Provider
public class ApiExceptionHandler extends ExceptionHandlerBase implements ExceptionMapper<ApiException> {
@Override
public Response toResponse(ApiException e) {
return super.toResponse(e.getCodeValue(), e.toString(), "ApiExceptionHandler");
}
}

@ -0,0 +1,18 @@
package cn.wustlinghang.wusthelper.internal.undergrad.api.json.handler;
import cn.wustlinghang.mywust.exception.ApiException;
import cn.wustlinghang.wusthelper.rpc.exception.RpcException;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.ExceptionMapper;
import jakarta.ws.rs.ext.Provider;
@Provider
public class RpcExceptionHandler extends ExceptionHandlerBase implements ExceptionMapper<RpcException> {
@Override
public Response toResponse(RpcException e) {
return super.toResponse(
e.getCode(),
e.toString(),
"ApiExceptionHandler");
}
}

@ -0,0 +1,72 @@
package cn.wustlinghang.wusthelper.internal.undergrad.exception;
import cn.wustlinghang.wusthelper.rpc.exception.RpcException;
import java.util.StringJoiner;
public class UndergradRpcException extends RpcException {
public static final int UNDERGRAD_MODULE = 1;
public UndergradRpcException(TypeCode typeCode,
SubModuleCode subModuleCode,
ErrorCode errorCode) {
super(UNDERGRAD_MODULE,
typeCode.ordinal(),
subModuleCode.ordinal() * 100 + errorCode.ordinal(),
new StringJoiner("-")
.add("UNDERGRAD")
.add(typeCode.name())
.add(subModuleCode.name())
.add(errorCode.name())
.toString()
);
}
/**
* 异常类型编码
*/
public enum TypeCode {
// 网络异常类型
NETWORK_EXCEPTION,
// 参数异常类型
PARAM_EXCEPTION,
// 权限认证异常类型
AUTH_EXCEPTION,
// 网页解析异常类型
PARSE_EXCEPTION,
// 其他的异常类型
OTHER_EXCEPTION
}
/**
* 子模块编码
*/
public enum SubModuleCode {
AUTH, COURSE_TABLE, CREDIT_STATUS, SCORE,
STUDENT_INFO, TRAINING_PLAN, EXAM_ACTIVITIES, EXAM_DELAY_APPLICATION
}
/**
* 具体错误编码
*/
public enum ErrorCode {
PARAM_INVALID,
COOKIE_INVALID,
NETWORK_ERROR,
PARSE_ERROR,
// 需要评教
NEED_EVALUATE,
AUTH_PASSWORD_WRONG,
// 用户不存在
AUTH_USER_NOT_EXISTS,
// 封号
AUTH_USER_BANNED,
// 用户账号禁用
AUTH_USER_DISABLED,
// 用户账号需要更改
AUTH_NEED_CHANGE_PASSWORD,
AUTH_UNKNOWN_ERROR
}
}

@ -3,10 +3,13 @@ package cn.wustlinghang.wusthelper.internal.undergrad.services;
import cn.wustlinghang.mywust.core.request.service.auth.UndergraduateLogin; import cn.wustlinghang.mywust.core.request.service.auth.UndergraduateLogin;
import cn.wustlinghang.mywust.exception.ApiException; import cn.wustlinghang.mywust.exception.ApiException;
import cn.wustlinghang.mywust.network.RequestClientOption; import cn.wustlinghang.mywust.network.RequestClientOption;
import cn.wustlinghang.wusthelper.internal.undergrad.exception.UndergradRpcException;
import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.context.ApplicationScoped;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException; import java.io.IOException;
@Slf4j
@ApplicationScoped @ApplicationScoped
public class LoginService { public class LoginService {
private final RequestClientOption option; private final RequestClientOption option;
@ -18,15 +21,58 @@ public class LoginService {
this.undergraduateLogin = undergraduateLogin; this.undergraduateLogin = undergraduateLogin;
} }
public String login(String username, String password, boolean legacy) throws IOException, ApiException { public String login(String username, String password, boolean legacy) throws UndergradRpcException {
if (legacy) { try {
return undergraduateLogin.getLoginCookieLegacy(username, password, option); if (legacy) {
} else { return undergraduateLogin.getLoginCookieLegacy(username, password, option);
return undergraduateLogin.getLoginCookie(username, password, option); } else {
return undergraduateLogin.getLoginCookie(username, password, option);
}
} catch (ApiException e) {
throw wrapApiException(e);
} catch (IOException e) {
throw new UndergradRpcException(
UndergradRpcException.TypeCode.NETWORK_EXCEPTION,
UndergradRpcException.SubModuleCode.AUTH,
UndergradRpcException.ErrorCode.NETWORK_ERROR
);
} }
} }
public boolean verify(String cookie) throws IOException { public boolean verify(String cookie) throws UndergradRpcException {
return !undergraduateLogin.checkCookiesFail(cookie); try {
return !undergraduateLogin.checkCookiesFail(cookie);
} catch (IOException e) {
throw new UndergradRpcException(
UndergradRpcException.TypeCode.NETWORK_EXCEPTION,
UndergradRpcException.SubModuleCode.AUTH,
UndergradRpcException.ErrorCode.NETWORK_ERROR
);
}
}
private UndergradRpcException wrapApiException(ApiException e) {
UndergradRpcException.ErrorCode errorCode = switch (e.getCode()) {
case NETWORK_EXCEPTION -> UndergradRpcException.ErrorCode.NETWORK_ERROR;
case UNI_LOGIN_PASSWORD_WRONG -> UndergradRpcException.ErrorCode.AUTH_PASSWORD_WRONG;
case UNI_LOGIN_USER_NOT_EXISTS -> UndergradRpcException.ErrorCode.AUTH_USER_NOT_EXISTS;
case UNI_LOGIN_USER_BANNED -> UndergradRpcException.ErrorCode.AUTH_USER_BANNED;
case UNI_LOGIN_USER_DISABLED -> UndergradRpcException.ErrorCode.AUTH_USER_DISABLED;
case UNI_LOGIN_NEED_CHANGE_PASSWORD -> UndergradRpcException.ErrorCode.AUTH_NEED_CHANGE_PASSWORD;
case UNI_LOGIN_USER_NOT_ONLY -> UndergradRpcException.ErrorCode.AUTH_UNKNOWN_ERROR;
case UNI_LOGIN_NO_REGISTER -> UndergradRpcException.ErrorCode.AUTH_UNKNOWN_ERROR;
case UNI_LOGIN_NEED_TFA -> UndergradRpcException.ErrorCode.AUTH_UNKNOWN_ERROR;
default -> {
log.error("本科生:登录代理请求异常,异常未处理");
log.error("异常:", e);
yield UndergradRpcException.ErrorCode.AUTH_UNKNOWN_ERROR;
}
};
return new UndergradRpcException(
UndergradRpcException.TypeCode.NETWORK_EXCEPTION,
UndergradRpcException.SubModuleCode.AUTH,
errorCode
);
} }
} }

@ -6,6 +6,8 @@ import cn.wustlinghang.mywust.data.global.Score;
import cn.wustlinghang.mywust.data.global.StudentInfo; import cn.wustlinghang.mywust.data.global.StudentInfo;
import cn.wustlinghang.mywust.data.undergrad.ExamDelayApplication; import cn.wustlinghang.mywust.data.undergrad.ExamDelayApplication;
import cn.wustlinghang.mywust.exception.ParseException; import cn.wustlinghang.mywust.exception.ParseException;
import cn.wustlinghang.wusthelper.internal.undergrad.exception.UndergradRpcException;
import cn.wustlinghang.wusthelper.rpc.exception.RpcException;
import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.context.ApplicationScoped;
import java.util.List; import java.util.List;
@ -34,27 +36,97 @@ public class ParseService {
this.examDelayParser = examDelayParser; this.examDelayParser = examDelayParser;
} }
public List<Course> parseCourseTable(String data) throws ParseException { public List<Course> parseCourseTable(String data) throws UndergradRpcException {
return courseTableParser.parse(data); try {
if (data.contains("评教")) {
throw new UndergradRpcException(
UndergradRpcException.TypeCode.PARSE_EXCEPTION,
UndergradRpcException.SubModuleCode.COURSE_TABLE,
UndergradRpcException.ErrorCode.NEED_EVALUATE
);
}
return courseTableParser.parse(data);
} catch (UndergradRpcException e) {
throw e;
} catch (Exception e) {
throw new UndergradRpcException(
UndergradRpcException.TypeCode.PARSE_EXCEPTION,
UndergradRpcException.SubModuleCode.COURSE_TABLE,
UndergradRpcException.ErrorCode.PARSE_ERROR
);
}
} }
public List<Score> parseScore(String data) throws ParseException { public List<Score> parseScore(String data) throws RpcException {
return scoreParser.parse(data); try {
if (data.contains("评教")) {
throw new UndergradRpcException(
UndergradRpcException.TypeCode.PARSE_EXCEPTION,
UndergradRpcException.SubModuleCode.SCORE,
UndergradRpcException.ErrorCode.NEED_EVALUATE
);
}
return scoreParser.parse(data);
} catch (UndergradRpcException e) {
throw e;
} catch (Exception e) {
throw new UndergradRpcException(
UndergradRpcException.TypeCode.PARSE_EXCEPTION,
UndergradRpcException.SubModuleCode.SCORE,
UndergradRpcException.ErrorCode.PARSE_ERROR
);
}
} }
public StudentInfo parseStudentInfo(String data) throws ParseException { public StudentInfo parseStudentInfo(String data) throws RpcException {
return studentInfoPageParser.parse(data); try {
return studentInfoPageParser.parse(data);
} catch (Exception e) {
throw new UndergradRpcException(
UndergradRpcException.TypeCode.PARSE_EXCEPTION,
UndergradRpcException.SubModuleCode.STUDENT_INFO,
UndergradRpcException.ErrorCode.PARSE_ERROR
);
}
} }
public String parseTrainingPlan(String data) throws ParseException { public String parseTrainingPlan(String data) throws RpcException {
return trainingPlanPageParser.parse(data); try {
return trainingPlanPageParser.parse(data);
} catch (Exception e) {
throw new UndergradRpcException(
UndergradRpcException.TypeCode.PARSE_EXCEPTION,
UndergradRpcException.SubModuleCode.TRAINING_PLAN,
UndergradRpcException.ErrorCode.PARSE_ERROR
);
}
} }
public String parseCreditStatus(String data) throws ParseException { public String parseCreditStatus(String data) throws RpcException {
return creditStatusParser.parse(data); try {
return creditStatusParser.parse(data);
} catch (Exception e) {
throw new UndergradRpcException(
UndergradRpcException.TypeCode.PARSE_EXCEPTION,
UndergradRpcException.SubModuleCode.CREDIT_STATUS,
UndergradRpcException.ErrorCode.PARSE_ERROR
);
}
} }
public List<ExamDelayApplication> parseExamDelayApplications(String data) throws ParseException { public List<ExamDelayApplication> parseExamDelayApplications(String data) throws RpcException {
return examDelayParser.parse(data); try {
return examDelayParser.parse(data);
} catch (Exception e) {
throw new UndergradRpcException(
UndergradRpcException.TypeCode.PARSE_EXCEPTION,
UndergradRpcException.SubModuleCode.EXAM_DELAY_APPLICATION,
UndergradRpcException.ErrorCode.PARSE_ERROR
);
}
} }
} }

@ -2,10 +2,15 @@ package cn.wustlinghang.wusthelper.internal.undergrad.services;
import cn.wustlinghang.mywust.core.request.service.undergraduate.*; import cn.wustlinghang.mywust.core.request.service.undergraduate.*;
import cn.wustlinghang.mywust.exception.ApiException; import cn.wustlinghang.mywust.exception.ApiException;
import cn.wustlinghang.mywust.network.RequestClientOption;
import cn.wustlinghang.wusthelper.internal.undergrad.exception.UndergradRpcException;
import cn.wustlinghang.wusthelper.rpc.exception.RpcException;
import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.context.ApplicationScoped;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException; import java.io.IOException;
@Slf4j
@ApplicationScoped @ApplicationScoped
public class RequestAgentService { public class RequestAgentService {
private final UndergradCourseTableApiService courseTableApiService; private final UndergradCourseTableApiService courseTableApiService;
@ -15,12 +20,15 @@ public class RequestAgentService {
private final UndergradCreditStatusApiService creditStatusApiService; private final UndergradCreditStatusApiService creditStatusApiService;
private final UndergradExamDelayApiService examDelayApiService; private final UndergradExamDelayApiService examDelayApiService;
private final RequestClientOption requestClientOption;
public RequestAgentService(UndergradCourseTableApiService courseTableApiService, public RequestAgentService(UndergradCourseTableApiService courseTableApiService,
UndergradScoreApiService scoreApiService, UndergradScoreApiService scoreApiService,
UndergradStudentInfoApiService studentInfoApiService, UndergradStudentInfoApiService studentInfoApiService,
UndergradTrainingPlanApiService trainingPlanApiService, UndergradTrainingPlanApiService trainingPlanApiService,
UndergradCreditStatusApiService creditStatusApiService, UndergradCreditStatusApiService creditStatusApiService,
UndergradExamDelayApiService examDelayApiService) { UndergradExamDelayApiService examDelayApiService,
RequestClientOption requestClientOption) {
this.courseTableApiService = courseTableApiService; this.courseTableApiService = courseTableApiService;
this.scoreApiService = scoreApiService; this.scoreApiService = scoreApiService;
@ -28,35 +36,131 @@ public class RequestAgentService {
this.trainingPlanApiService = trainingPlanApiService; this.trainingPlanApiService = trainingPlanApiService;
this.creditStatusApiService = creditStatusApiService; this.creditStatusApiService = creditStatusApiService;
this.examDelayApiService = examDelayApiService; this.examDelayApiService = examDelayApiService;
this.requestClientOption = requestClientOption;
} }
public String getStudentInfoPage(String cookie) throws IOException, ApiException { public String getStudentInfoPage(String cookie) throws RpcException {
return studentInfoApiService.getPage(cookie); try {
return studentInfoApiService.getPage(cookie);
} catch (ApiException e) {
throw wrapApiException(e, UndergradRpcException.SubModuleCode.STUDENT_INFO);
} catch (IOException e) {
throw new UndergradRpcException(
UndergradRpcException.TypeCode.NETWORK_EXCEPTION,
UndergradRpcException.SubModuleCode.STUDENT_INFO,
UndergradRpcException.ErrorCode.NETWORK_ERROR
);
}
} }
public String getCourseTable(String cookie, String term) throws IOException, ApiException { public String getCourseTable(String cookie, String term) throws RpcException {
return courseTableApiService.getPage(term, cookie); try {
return courseTableApiService.getPage(term, cookie);
} catch (ApiException e) {
throw wrapApiException(e, UndergradRpcException.SubModuleCode.COURSE_TABLE);
} catch (IOException e) {
throw new UndergradRpcException(
UndergradRpcException.TypeCode.NETWORK_EXCEPTION,
UndergradRpcException.SubModuleCode.COURSE_TABLE,
UndergradRpcException.ErrorCode.NETWORK_ERROR
);
}
} }
public String getScore(String cookie) throws IOException, ApiException { public String getScore(String cookie) throws RpcException {
return scoreApiService.getPage(cookie); try {
return scoreApiService.getPage(cookie);
} catch (ApiException e) {
throw wrapApiException(e, UndergradRpcException.SubModuleCode.SCORE);
} catch (IOException e) {
throw new UndergradRpcException(
UndergradRpcException.TypeCode.NETWORK_EXCEPTION,
UndergradRpcException.SubModuleCode.SCORE,
UndergradRpcException.ErrorCode.NETWORK_ERROR
);
}
} }
public String getTrainingPlan(String cookie) throws IOException, ApiException { public String getTrainingPlan(String cookie) throws RpcException {
return trainingPlanApiService.getPage(cookie); try {
return trainingPlanApiService.getPage(cookie);
} catch (ApiException e) {
throw wrapApiException(e, UndergradRpcException.SubModuleCode.TRAINING_PLAN);
} catch (IOException e) {
throw new UndergradRpcException(
UndergradRpcException.TypeCode.NETWORK_EXCEPTION,
UndergradRpcException.SubModuleCode.TRAINING_PLAN,
UndergradRpcException.ErrorCode.NETWORK_ERROR
);
}
} }
public String getCreditStatus(String cookie) throws IOException, ApiException { public String getCreditStatus(String cookie) throws RpcException {
return creditStatusApiService.getPage(cookie, null, false); try {
return creditStatusApiService.getPage(cookie, requestClientOption, false);
} catch (ApiException e) {
throw wrapApiException(e, UndergradRpcException.SubModuleCode.CREDIT_STATUS);
} catch (IOException e) {
throw new UndergradRpcException(
UndergradRpcException.TypeCode.NETWORK_EXCEPTION,
UndergradRpcException.SubModuleCode.CREDIT_STATUS,
UndergradRpcException.ErrorCode.NETWORK_ERROR
);
}
} }
public UndergradExamDelayApiService.ExamActivity[] getExamActivities(String cookie, String term) public UndergradExamDelayApiService.ExamActivity[] getExamActivities(String cookie, String term)
throws IOException, ApiException { throws RpcException {
return examDelayApiService.getActivities(term, cookie); try {
return examDelayApiService.getActivities(term, cookie);
} catch (ApiException e) {
throw wrapApiException(e, UndergradRpcException.SubModuleCode.EXAM_ACTIVITIES);
} catch (IOException e) {
throw new UndergradRpcException(
UndergradRpcException.TypeCode.NETWORK_EXCEPTION,
UndergradRpcException.SubModuleCode.EXAM_ACTIVITIES,
UndergradRpcException.ErrorCode.NETWORK_ERROR
);
}
} }
public String getExamDelayApplications(String cookie, String term, String activityId) public String getExamDelayApplications(String cookie, String term, String activityId)
throws IOException, ApiException { throws RpcException {
return examDelayApiService.getPage(term, activityId, cookie); try {
return examDelayApiService.getPage(term, activityId, cookie);
} catch (ApiException e) {
throw wrapApiException(e, UndergradRpcException.SubModuleCode.EXAM_DELAY_APPLICATION);
} catch (IOException e) {
throw new UndergradRpcException(
UndergradRpcException.TypeCode.NETWORK_EXCEPTION,
UndergradRpcException.SubModuleCode.EXAM_DELAY_APPLICATION,
UndergradRpcException.ErrorCode.NETWORK_ERROR
);
}
}
private UndergradRpcException wrapApiException(ApiException e, UndergradRpcException.SubModuleCode subModuleCode) {
return switch (e.getCode()) {
case NETWORK_EXCEPTION -> new UndergradRpcException(
UndergradRpcException.TypeCode.NETWORK_EXCEPTION,
subModuleCode,
UndergradRpcException.ErrorCode.NETWORK_ERROR
);
case COOKIE_INVALID -> new UndergradRpcException(
UndergradRpcException.TypeCode.AUTH_EXCEPTION,
subModuleCode,
UndergradRpcException.ErrorCode.COOKIE_INVALID
);
default -> {
log.error("本科生:{}代理请求异常,异常未处理", subModuleCode.name());
log.error("异常:", e);
yield new UndergradRpcException(
UndergradRpcException.TypeCode.AUTH_EXCEPTION,
subModuleCode,
UndergradRpcException.ErrorCode.COOKIE_INVALID
);
}
};
} }
} }

Loading…
Cancel
Save