diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml index 3018bff..38ff681 100644 --- a/.idea/inspectionProfiles/Project_Default.xml +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -32,5 +32,12 @@ + + + \ No newline at end of file diff --git a/backend-main/backend-web/pom.xml b/backend-main/backend-web/pom.xml index 694d298..54e75bb 100644 --- a/backend-main/backend-web/pom.xml +++ b/backend-main/backend-web/pom.xml @@ -40,6 +40,11 @@ spring-cloud-starter-openfeign ${feign.version} + + org.springframework.boot + spring-boot-configuration-processor + true + cn.wustlinghang.wusthelper @@ -64,10 +69,10 @@ - - org.graalvm.buildtools - native-maven-plugin - + + + + org.springframework.boot spring-boot-maven-plugin diff --git a/backend-main/backend-web/src/main/java/wusthelper/WebBackendMain.java b/backend-main/backend-web/src/main/java/wusthelper/WebBackendMain.java index aa37ac8..cccba7b 100644 --- a/backend-main/backend-web/src/main/java/wusthelper/WebBackendMain.java +++ b/backend-main/backend-web/src/main/java/wusthelper/WebBackendMain.java @@ -9,13 +9,13 @@ import org.springframework.scheduling.annotation.EnableScheduling; @EnableAsync @EnableScheduling -@EnableFeignClients(basePackages = {"cn.wustlinghang.wusthelper.web.rpc"}) +@EnableFeignClients(basePackages = {"wusthelper.web.rpc"}) @SpringBootApplication(scanBasePackages = { // 指定springboot的bean扫描路径,有新增请及时更新 - "cn.wustlinghang.wusthelper.data", - "cn.wustlinghang.wusthelper.web", + "wusthelper.data", + "wusthelper.web", }) -@MapperScan("cn.wustlinghang.wusthelper.data.dao.mapper") +@MapperScan("wusthelper.data.dao.mapper") public class WebBackendMain { public static void main(String[] args) { SpringApplication.run(WebBackendMain.class); diff --git a/backend-main/backend-web/src/main/java/wusthelper/web/api/v2/Token.java b/backend-main/backend-web/src/main/java/wusthelper/web/api/v2/TokenTool.java similarity index 98% rename from backend-main/backend-web/src/main/java/wusthelper/web/api/v2/Token.java rename to backend-main/backend-web/src/main/java/wusthelper/web/api/v2/TokenTool.java index 0f1772f..ba9a7f1 100644 --- a/backend-main/backend-web/src/main/java/wusthelper/web/api/v2/Token.java +++ b/backend-main/backend-web/src/main/java/wusthelper/web/api/v2/TokenTool.java @@ -9,7 +9,7 @@ import java.time.Duration; import java.util.Date; @Slf4j -public class Token { +public class TokenTool { private final static String SECRET = "LingHangStudio.WustHelper"; diff --git a/backend-main/backend-web/src/main/java/wusthelper/web/api/v2/dto/response/CombineLoginResponse.java b/backend-main/backend-web/src/main/java/wusthelper/web/api/v2/dto/response/CombineLoginResponse.java new file mode 100644 index 0000000..b6aa63b --- /dev/null +++ b/backend-main/backend-web/src/main/java/wusthelper/web/api/v2/dto/response/CombineLoginResponse.java @@ -0,0 +1,12 @@ +package wusthelper.web.api.v2.dto.response; + +import cn.wustlinghang.mywust.data.global.Course; + +import java.util.List; + +public record CombineLoginResponse( + String token, + List courses, + StudentInfoResponse studentInfoResponse +) { +} diff --git a/backend-main/backend-web/src/main/java/wusthelper/web/api/v2/dto/response/CourseResponse.java b/backend-main/backend-web/src/main/java/wusthelper/web/api/v2/dto/response/CourseResponse.java new file mode 100644 index 0000000..b90f844 --- /dev/null +++ b/backend-main/backend-web/src/main/java/wusthelper/web/api/v2/dto/response/CourseResponse.java @@ -0,0 +1,55 @@ +package wusthelper.web.api.v2.dto.response; + +import cn.wustlinghang.mywust.data.global.Course; +import lombok.Data; + +@Data +public class CourseResponse { + /** + * 课程名称 + */ + private String className; + /** + * 教学班 + */ + private String teachClass; + /** + * 教师 + */ + private String teacher; + /** + * 开始周次 + */ + private Integer startWeek; + /** + * 结束周 + */ + private Integer endWeek; + /** + * 节数 + */ + private Integer section; + /** + * 星期 + */ + private Integer weekDay; + /** + * 教室 + */ + private String classroom; + + public static CourseResponse from(Course course) { + CourseResponse courseResponse = new CourseResponse(); + courseResponse.setClassName(course.getName()); + courseResponse.setTeachClass(course.getTeachClass()); + courseResponse.setTeacher(course.getTeacher()); + courseResponse.setStartWeek(course.getStartWeek()); + courseResponse.setEndWeek(course.getEndWeek()); + courseResponse.setSection((course.getStartSection() + 1) / 2); + courseResponse.setWeekDay(course.getWeekDay()); + courseResponse.setClassroom(course.getClassroom().getRoom()); + + return courseResponse; + + } +} diff --git a/backend-main/backend-web/src/main/java/wusthelper/web/api/v2/dto/response/ScoreResponse.java b/backend-main/backend-web/src/main/java/wusthelper/web/api/v2/dto/response/ScoreResponse.java new file mode 100644 index 0000000..bac8220 --- /dev/null +++ b/backend-main/backend-web/src/main/java/wusthelper/web/api/v2/dto/response/ScoreResponse.java @@ -0,0 +1,128 @@ +package wusthelper.web.api.v2.dto.response; + +import cn.wustlinghang.mywust.data.global.Score; +import lombok.Data; +import wusthelper.web.util.StringUtil; + +@Data +public class ScoreResponse { + /** + * 课程编号 + */ + private String courseNum; + /** + * 课程名称 + */ + private String courseName; + /** + * 成绩 + */ + private String grade; + /** + * 学分 + */ + private Float courseCredit; + /** + * 总学时 + */ + private Float courseHours; + /** + * 绩点 + */ + private Float gradePoint; + /** + * 考核方式 + */ + private String evaluationMode; + /** + * 考试性质 + */ + private String examNature; + /** + * 课程性质 + */ + private String courseNature; + /** + * 开课学期 + */ + private String schoolTerm; + /** + * 重修标记 0--未重修 1--重修 + */ + private Integer rebuildTag; + /** + * 补考标记 0--正常考试 1--补考 + */ + private Integer reExamTag; + /** + * 缺考/缓考标记 0--正常 1--缺考 2--缓考 + */ + private Integer missExamTag; + + public static ScoreResponse from(Score score) { + ScoreResponse scoreResponse = new ScoreResponse(); + scoreResponse.setCourseNum(score.getCourseName()); + scoreResponse.setCourseName(score.getCourseName()); + scoreResponse.setGrade(score.getScore()); + //学分 + if ("".equals(score.getCredit())) { + scoreResponse.setCourseCredit(0.0F); + } else { + scoreResponse.setCourseCredit(Float.parseFloat(score.getCredit())); + } + + //总学时 + if ("".equals(score.getCourseHours())) { + scoreResponse.setCourseHours(0.0F); + } else { + scoreResponse.setCourseHours(Float.parseFloat(score.getCourseHours())); + } + + //绩点 + if (StringUtil.isPositiveNumber(score.getGradePoint())) { + scoreResponse.setGradePoint(Float.parseFloat(score.getGradePoint())); + } else { + scoreResponse.setGradePoint(convertGradePoint(score.getScore())); + } + + scoreResponse.setEvaluationMode(score.getEvaluateMethod()); + scoreResponse.setExamNature(score.getKind()); + scoreResponse.setCourseNature(score.getCourseKind()); + scoreResponse.setSchoolTerm(score.getTerm()); +// scoreResponse.setRebuildTag(); +// scoreResponse.setReExamTag(); +// scoreResponse.setMissExamTag(); + //补考判断 0--正常考试 1--补考 + + scoreResponse.setReExamTag(0); + if (score.getFlag().contains("补考") || score.getKind().contains("补考")) { + scoreResponse.setReExamTag(1); + } + //缓考/缺考判断 0--正常 1--缺考 2--缓考 + scoreResponse.setMissExamTag(0); + if (score.getFlag().contains("缺考") || score.getKind().contains("缺考")) { + scoreResponse.setMissExamTag(1); + } + if (score.getFlag().contains("缓考") || score.getKind().contains("缓考")) { + scoreResponse.setMissExamTag(2); + } + + scoreResponse.setRebuildTag(0); + + return scoreResponse; + } + + /** + * 转换成绩为学分,支持数字成绩和等级成绩 + * + * @param gradeStr 成绩字符串 + * @return 换算后的学分 + */ + private static float convertGradePoint(String gradeStr) { + if (StringUtil.isPositiveNumber(gradeStr)) { + return StringUtil.numberGradeToGradePoint(gradeStr); + } else { + return StringUtil.levelGradeToGradePoint(gradeStr); + } + } +} diff --git a/backend-main/backend-web/src/main/java/wusthelper/web/api/v2/dto/response/StudentInfoResponse.java b/backend-main/backend-web/src/main/java/wusthelper/web/api/v2/dto/response/StudentInfoResponse.java new file mode 100644 index 0000000..b5f5b1e --- /dev/null +++ b/backend-main/backend-web/src/main/java/wusthelper/web/api/v2/dto/response/StudentInfoResponse.java @@ -0,0 +1,31 @@ +package wusthelper.web.api.v2.dto.response; + +import cn.wustlinghang.mywust.data.global.StudentInfo; +import lombok.Data; + +@Data +public class StudentInfoResponse { + private String stuNum; + + private String stuName; + + private String nickName; + + private String college; + + private String major; + + private String classes; + + public static StudentInfoResponse from(StudentInfo studentInfo) { + StudentInfoResponse studentInfoResponse = new StudentInfoResponse(); + studentInfoResponse.setStuNum(studentInfo.getStudentNumber()); + studentInfoResponse.setStuName(studentInfo.getName()); + studentInfoResponse.setNickName(""); + studentInfoResponse.setCollege(studentInfo.getCollege()); + studentInfoResponse.setMajor(studentInfo.getMajor()); + studentInfoResponse.setClasses(studentInfo.getClazz()); + + return studentInfoResponse; + } +} \ No newline at end of file diff --git a/backend-main/backend-web/src/main/java/wusthelper/web/api/v2/handler/GlobalExceptionHandler.java b/backend-main/backend-web/src/main/java/wusthelper/web/api/v2/handler/GlobalExceptionHandler.java new file mode 100644 index 0000000..41ac441 --- /dev/null +++ b/backend-main/backend-web/src/main/java/wusthelper/web/api/v2/handler/GlobalExceptionHandler.java @@ -0,0 +1,127 @@ +package wusthelper.web.api.v2.handler; + +import cn.wustlinghang.mywust.exception.ApiException; +import cn.wustlinghang.mywust.exception.ParseException; +import com.fasterxml.jackson.databind.JsonMappingException; +import feign.FeignException; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.converter.HttpMessageNotReadableException; +import org.springframework.web.HttpMediaTypeNotSupportedException; +import org.springframework.web.HttpRequestMethodNotSupportedException; +import org.springframework.web.bind.MissingServletRequestParameterException; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; +import wusthelper.code.ServiceCode; +import wusthelper.web.exception.ServiceException; +import wusthelper.web.response.Response; + +@Slf4j +@ControllerAdvice +public class GlobalExceptionHandler { + @ResponseBody + @ExceptionHandler(ServiceException.class) + public Response baseException(ServiceException e) { + return Response.error(e.getCodeValue(), e.getMessage()); + } + + @ResponseBody + @ExceptionHandler(FeignException.class) + public Response baseException(FeignException e) { + log.error("rpc服务异常:{}", e.getMessage()); + log.info("堆栈跟踪:", e); + return Response.error(ServiceCode.RpcError); + } + + @ResponseBody + @ExceptionHandler(ApiException.class) + public Response baseException(ApiException e) { + return Response.error(ServiceCode.ServerInternalError, e.getMessage()); + } + + @ResponseBody + @ExceptionHandler(Exception.class) + public Response baseException(Exception e) { + log.error("未处理的异常:", e); + return Response.error(ServiceCode.ServerInternalError); + } + + /** + * 处理参数不完整的请求异常 + * + * @param e 异常 + * @return 统一响应 + */ + @ResponseBody + @ExceptionHandler(MissingServletRequestParameterException.class) + public Response handler(MissingServletRequestParameterException e) { + log.debug("请求的参数不完整: " + e.getMessage()); + return Response.error(ServiceCode.ParamWrong); + } + + /** + * 处理参数类型错误的请求异常(请求参数类型错误) + * + * @param e 异常 + * @return 统一响应 + */ + @ResponseBody + @ExceptionHandler(MethodArgumentTypeMismatchException.class) + public Response handler(MethodArgumentTypeMismatchException e) { + log.debug(String.format("请求错误(%s): %s", e.getClass().getName(), e.getMessage())); + return Response.error(ServiceCode.ParamWrong); + } + + /** + * 处理参数类型错误的请求异常2(Json解析错误) + * + * @param e 异常 + * @return 统一响应 + */ + @ResponseBody + @ExceptionHandler(JsonMappingException.class) + public Response handler(JsonMappingException e) { + log.debug(String.format("请求错误(%s): %s", e.getClass().getName(), e.getMessage())); + return Response.error(ServiceCode.ParamWrong); + } + + /** + * 处理参数类型错误的请求异常3(字段映射错误) + * + * @param e 异常 + * @return 统一响应 + */ + @ResponseBody + @ExceptionHandler(HttpMessageNotReadableException.class) + public Response handler(HttpMessageNotReadableException e) { + log.debug(String.format("请求错误(%s): %s", e.getClass().getName(), e.getMessage())); + return Response.error(ServiceCode.ParamWrong); + } + + /** + * 处理请求头中“Content-Type”字段不正确的异常 + * + * @param e 异常 + * @return 统一响应 + */ + @ResponseBody + @ExceptionHandler(HttpMediaTypeNotSupportedException.class) + public Response handler(HttpMediaTypeNotSupportedException e) { + log.debug(String.format("请求错误(%s): %s", e.getClass().getName(), e.getMessage())); + return Response.error(ServiceCode.ParamWrong, "请求头\"Contene-Type\"字段有误"); + } + + /** + * 处理请求方法错误的情况 + * + * @param e 异常 + * @return 统一响应 + */ + @ResponseBody + @ExceptionHandler(HttpRequestMethodNotSupportedException.class) + public Response handler(HttpRequestMethodNotSupportedException e) { + log.debug(String.format("请求错误(%s): %s", e.getClass().getName(), e.getMessage())); + return Response.error(ServiceCode.RequestInvalid); + } +} \ No newline at end of file diff --git a/backend-main/backend-web/src/main/java/wusthelper/web/api/v2/module/undergrade/UndergradController.java b/backend-main/backend-web/src/main/java/wusthelper/web/api/v2/module/undergrade/UndergradController.java new file mode 100644 index 0000000..d47f870 --- /dev/null +++ b/backend-main/backend-web/src/main/java/wusthelper/web/api/v2/module/undergrade/UndergradController.java @@ -0,0 +1,114 @@ +package wusthelper.web.api.v2.module.undergrade; + +import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import wusthelper.web.api.v2.TokenTool; +import wusthelper.web.api.v2.dto.response.CourseResponse; +import wusthelper.web.api.v2.dto.response.ScoreResponse; +import wusthelper.web.api.v2.dto.response.StudentInfoResponse; +import wusthelper.web.response.Response; +import wusthelper.web.service.campus.undergrad.*; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +@RestController +@RequestMapping("/v2/jwc") +public class UndergradController { + + private final UndergradStudentInfoService studentInfoService; + private final UndergradCourseTableService courseTableService; + private final UndergradScoreService scoreService; + private final UndergradCreditStatusService creditStatusService; + private final UndergradTrainingPlanService trainingPlanService; + + public UndergradController(UndergradStudentInfoService studentInfoService, + UndergradCourseTableService courseTableService, + UndergradScoreService scoreService, + UndergradCreditStatusService creditStatusService, + UndergradTrainingPlanService trainingPlanService) { + + this.studentInfoService = studentInfoService; + this.courseTableService = courseTableService; + this.scoreService = scoreService; + this.creditStatusService = creditStatusService; + this.trainingPlanService = trainingPlanService; + } + + @RequestMapping("/get-student-info") + public Response getStudentInfo(@RequestHeader("Token") String token) { + var user = TokenTool.getStudentNumber(token); + var studentInfo = studentInfoService.getStudentInfo(user); + + return Response.success(StudentInfoResponse.from(studentInfo)); + } + + @RequestMapping("/get-curriculum") + public Response> getCourses(@RequestHeader("Token") String token, + @RequestParam(value = "schoolTerm") String term) { + var user = TokenTool.getStudentNumber(token); + var courses = courseTableService.getCourseTable(user, term); + var courseResponsesList = new ArrayList(courses.size()); + for (var course : courses) { + courseResponsesList.add(CourseResponse.from(course)); + } + + return Response.success(courseResponsesList); + } + + @RequestMapping("/get-grade") + public Response> getScore(@RequestHeader("Token") String token) { + var user = TokenTool.getStudentNumber(token); + var scores = scoreService.getScore(user); + var scoreResponseList = new ArrayList(scores.size()); + + // 课程 + Set courseSet = new HashSet<>(scores.size()); + // 课程+学期 + Set courseTermSet = new HashSet<>(scores.size()); + + for (var score : scores) { + var scoreResponse = ScoreResponse.from(score); + + // 重修判断 0--未重修 1--重修 + // 课程是否重复出现 + scoreResponse.setRebuildTag(0); + String courseNumAndSchoolTerm = score.getCourseNumber() + score.getTerm(); + if (courseSet.contains(score.getCourseNumber())) { + // 本次考试非补考 且本学期只有一次本课程号的课程,则表明本此考试为重修 + if (scoreResponse.getReExamTag() == 0 && !courseTermSet.contains(courseNumAndSchoolTerm)) { + scoreResponse.setRebuildTag(1); + } + } + courseSet.add(score.getCourseNumber()); + courseTermSet.add(courseNumAndSchoolTerm); + + // 缓考不显示 + if (scoreResponse.getMissExamTag() != 2) { + scoreResponseList.add(scoreResponse); + } + } + + return Response.success(scoreResponseList); + } + + @RequestMapping("/get-credit") + public Response getCreditStatus(@RequestHeader("Token") String token) { + var user = TokenTool.getStudentNumber(token); + var page = creditStatusService.getCreditStatus(user); + + return Response.success(page); + } + + @RequestMapping("/get-scheme") + public Response getTrainingPlan(@RequestHeader("Token") String token) { + var user = TokenTool.getStudentNumber(token); + var page = trainingPlanService.getTrainingPlan(user); + + return Response.success(page); + } +} diff --git a/backend-main/backend-web/src/main/java/wusthelper/web/api/v2/module/undergrade/UndergradLoginController.java b/backend-main/backend-web/src/main/java/wusthelper/web/api/v2/module/undergrade/UndergradLoginController.java new file mode 100644 index 0000000..d4c9419 --- /dev/null +++ b/backend-main/backend-web/src/main/java/wusthelper/web/api/v2/module/undergrade/UndergradLoginController.java @@ -0,0 +1,76 @@ +package wusthelper.web.api.v2.module.undergrade; + +import org.springframework.web.bind.annotation.*; +import wusthelper.web.api.v2.TokenTool; +import wusthelper.web.api.v2.dto.response.CombineLoginResponse; +import wusthelper.web.api.v2.dto.response.CourseResponse; +import wusthelper.web.api.v2.dto.response.StudentInfoResponse; +import wusthelper.web.response.Response; +import wusthelper.web.service.campus.GeneralUserLoginService; +import wusthelper.web.service.campus.undergrad.UndergradCourseTableService; +import wusthelper.web.service.campus.undergrad.UndergradStudentInfoService; + +import java.util.ArrayList; + +@RestController +@RequestMapping("/v2/jwc") +public class UndergradLoginController { + private final GeneralUserLoginService generalUserLoginService; + + private final UndergradStudentInfoService studentInfoService; + private final UndergradCourseTableService courseTableService; + + public UndergradLoginController(GeneralUserLoginService generalUserLoginService, + UndergradStudentInfoService studentInfoService, + UndergradCourseTableService courseTableService) { + + this.generalUserLoginService = generalUserLoginService; + this.studentInfoService = studentInfoService; + this.courseTableService = courseTableService; + } + + @PostMapping("/login") + public Response login(@RequestParam(value = "stuNum") String username, + @RequestParam(value = "jwcPwd") String password, + @RequestHeader(name = "Platform", required = false) String platform) { + + var user = generalUserLoginService.login(username, password, GeneralUserLoginService.UserType.Undergrad); + + String token = TokenTool.signToken(user.getUid(), user.getStuNum()); + + return Response.success(token); + } + + /** + * v2接口的组合登录,其实没必要,所谓的请求减负在这里没啥太大用处,这点压力算不上什么, + * 不仅没必要,还增大了登陆耗时,容易超时, + * v3版本时将会移除 + * + * @param username 用户名 + * @param password 密码 + * @param term 学期 + * @param platform 平台 + * @return . + */ + @PostMapping("/combine-login") + public Response combineLogin(@RequestParam(value = "stuNum") String username, + @RequestParam(value = "jwcPwd") String password, + @RequestParam(value = "term") String term, + @RequestHeader(name = "Platform", required = false) String platform) { + + var user = generalUserLoginService.login(username, password, GeneralUserLoginService.UserType.Undergrad); + var studentInfo = studentInfoService.getStudentInfo(username); + var courses = courseTableService.getCourseTable(username, term); + + var studentResponse = StudentInfoResponse.from(studentInfo); + var courseResponses = new ArrayList(courses.size()); + for (var course : courses) { + courseResponses.add(CourseResponse.from(course)); + } + + String token = TokenTool.signToken(user.getUid(), user.getStuNum()); + + var response = new CombineLoginResponse(token, courseResponses, studentResponse); + return Response.success(response); + } +} \ No newline at end of file diff --git a/backend-main/backend-web/src/main/java/wusthelper/web/api/v2/undergrade/UndergradController.java b/backend-main/backend-web/src/main/java/wusthelper/web/api/v2/undergrade/UndergradController.java deleted file mode 100644 index 06249ae..0000000 --- a/backend-main/backend-web/src/main/java/wusthelper/web/api/v2/undergrade/UndergradController.java +++ /dev/null @@ -1,5 +0,0 @@ -package wusthelper.web.api.v2.undergrade; - -public class UndergradController { - -} diff --git a/backend-main/backend-web/src/main/java/wusthelper/web/api/v2/undergrade/UndergradLoginController.java b/backend-main/backend-web/src/main/java/wusthelper/web/api/v2/undergrade/UndergradLoginController.java deleted file mode 100644 index ad6cbfa..0000000 --- a/backend-main/backend-web/src/main/java/wusthelper/web/api/v2/undergrade/UndergradLoginController.java +++ /dev/null @@ -1,28 +0,0 @@ -package wusthelper.web.api.v2.undergrade; - -import org.springframework.web.bind.annotation.*; -import wusthelper.web.api.v2.Token; -import wusthelper.web.response.Response; -import wusthelper.web.service.campus.GeneralUserLoginService; - -@RestController -@RequestMapping("/jwc") -public class UndergradLoginController { - private final GeneralUserLoginService generalUserLoginService; - - public UndergradLoginController(GeneralUserLoginService generalUserLoginService) { - this.generalUserLoginService = generalUserLoginService; - } - - @PostMapping("/login") - public Response login(@RequestParam(value = "username") String username, - @RequestParam(value = "jwcPwd") String password, - @RequestHeader(name = "Platform", required = false) String platform) { - - var user = generalUserLoginService.login(username, password, GeneralUserLoginService.UserType.Undergrad); - - String token = Token.signToken(user.getUid(), user.getStuNum()); - - return Response.success(token); - } -} \ No newline at end of file diff --git a/backend-main/backend-web/src/main/java/wusthelper/web/rpc/graduate/GraduateCookieRemote.java b/backend-main/backend-web/src/main/java/wusthelper/web/rpc/graduate/GraduateCookieRemote.java index 78ff569..3cb76dd 100644 --- a/backend-main/backend-web/src/main/java/wusthelper/web/rpc/graduate/GraduateCookieRemote.java +++ b/backend-main/backend-web/src/main/java/wusthelper/web/rpc/graduate/GraduateCookieRemote.java @@ -5,7 +5,7 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import wusthelper.internal.rpc.response.RpcResponseDto; -@FeignClient(name = "wusthelper.graduate", contextId = "cookie") +@FeignClient(name = "wusthelper.graduate", contextId = "graduate-cookie") public interface GraduateCookieRemote { String COOKIE_ROOT_PATH = "/cookie"; diff --git a/backend-main/backend-web/src/main/java/wusthelper/web/rpc/graduate/GraduateStudentInfoRemote.java b/backend-main/backend-web/src/main/java/wusthelper/web/rpc/graduate/GraduateStudentInfoRemote.java index ef7ca87..f10b602 100644 --- a/backend-main/backend-web/src/main/java/wusthelper/web/rpc/graduate/GraduateStudentInfoRemote.java +++ b/backend-main/backend-web/src/main/java/wusthelper/web/rpc/graduate/GraduateStudentInfoRemote.java @@ -8,7 +8,7 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import wusthelper.internal.rpc.response.RpcResponseDto; -@FeignClient(name = "wusthelper.graduate", contextId = "studentInfo") +@FeignClient(name = "wusthelper.graduate", contextId = "graduate-studentInfo") public interface GraduateStudentInfoRemote { String ROOT_PATH = "/student_info"; diff --git a/backend-main/backend-web/src/main/java/wusthelper/web/rpc/undergrad/CourseTableRemote.java b/backend-main/backend-web/src/main/java/wusthelper/web/rpc/undergrad/CourseTableRemote.java index e843b71..0eb7ce2 100644 --- a/backend-main/backend-web/src/main/java/wusthelper/web/rpc/undergrad/CourseTableRemote.java +++ b/backend-main/backend-web/src/main/java/wusthelper/web/rpc/undergrad/CourseTableRemote.java @@ -1,6 +1,7 @@ package wusthelper.web.rpc.undergrad; import cn.wustlinghang.mywust.data.global.Course; +import org.springframework.stereotype.Component; import wusthelper.internal.rpc.response.RpcResponseDto; import jakarta.validation.constraints.NotNull; import org.springframework.cloud.openfeign.FeignClient; @@ -10,7 +11,8 @@ import org.springframework.web.bind.annotation.RequestParam; import java.util.List; -@FeignClient(name = "wusthelper.undergrad", contextId = "courseTable") +@Component +@FeignClient(name = "wusthelper.undergrad", contextId = "undergrad-courseTable") public interface CourseTableRemote { String ROOT_PATH = "/course_table"; diff --git a/backend-main/backend-web/src/main/java/wusthelper/web/rpc/undergrad/CreditStatusRemote.java b/backend-main/backend-web/src/main/java/wusthelper/web/rpc/undergrad/CreditStatusRemote.java index deac101..974fb4e 100644 --- a/backend-main/backend-web/src/main/java/wusthelper/web/rpc/undergrad/CreditStatusRemote.java +++ b/backend-main/backend-web/src/main/java/wusthelper/web/rpc/undergrad/CreditStatusRemote.java @@ -1,5 +1,6 @@ package wusthelper.web.rpc.undergrad; +import org.springframework.stereotype.Component; import wusthelper.internal.rpc.response.RpcResponseDto; import jakarta.validation.constraints.NotNull; import org.springframework.cloud.openfeign.FeignClient; @@ -7,7 +8,8 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; -@FeignClient(name = "wusthelper.undergrad", contextId = "creditStatus") +@Component +@FeignClient(name = "wusthelper.undergrad", contextId = "undergrad-creditStatus") public interface CreditStatusRemote { String ROOT_PATH = "/credit_status"; diff --git a/backend-main/backend-web/src/main/java/wusthelper/web/rpc/undergrad/ExamDelayApplicationRemote.java b/backend-main/backend-web/src/main/java/wusthelper/web/rpc/undergrad/ExamDelayApplicationRemote.java index d2e81ab..120f96a 100644 --- a/backend-main/backend-web/src/main/java/wusthelper/web/rpc/undergrad/ExamDelayApplicationRemote.java +++ b/backend-main/backend-web/src/main/java/wusthelper/web/rpc/undergrad/ExamDelayApplicationRemote.java @@ -1,6 +1,7 @@ package wusthelper.web.rpc.undergrad; import cn.wustlinghang.mywust.data.undergrad.ExamDelayApplication; +import org.springframework.stereotype.Component; import wusthelper.internal.rpc.response.RpcResponseDto; import jakarta.validation.constraints.NotNull; import org.springframework.cloud.openfeign.FeignClient; @@ -8,7 +9,8 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; -@FeignClient(name = "wusthelper.undergrad", contextId = "examDelayApplication") +@Component +@FeignClient(name = "wusthelper.undergrad", contextId = "undergrad-examDelayApplication") public interface ExamDelayApplicationRemote { String ROOT_PATH = "/exam_delay_application"; diff --git a/backend-main/backend-web/src/main/java/wusthelper/web/rpc/undergrad/ScoreRemote.java b/backend-main/backend-web/src/main/java/wusthelper/web/rpc/undergrad/ScoreRemote.java index 81cdd79..7bbb3e3 100644 --- a/backend-main/backend-web/src/main/java/wusthelper/web/rpc/undergrad/ScoreRemote.java +++ b/backend-main/backend-web/src/main/java/wusthelper/web/rpc/undergrad/ScoreRemote.java @@ -1,6 +1,7 @@ package wusthelper.web.rpc.undergrad; import cn.wustlinghang.mywust.data.global.Score; +import org.springframework.stereotype.Component; import wusthelper.internal.rpc.response.RpcResponseDto; import jakarta.validation.constraints.NotNull; import org.springframework.cloud.openfeign.FeignClient; @@ -10,7 +11,8 @@ import org.springframework.web.bind.annotation.RequestParam; import java.util.List; -@FeignClient(name = "wusthelper.undergrad", contextId = "score") +@Component +@FeignClient(name = "wusthelper.undergrad", contextId = "undergrad-score") public interface ScoreRemote { String ROOT_PATH = "/score"; diff --git a/backend-main/backend-web/src/main/java/wusthelper/web/rpc/undergrad/TrainingPlanRemote.java b/backend-main/backend-web/src/main/java/wusthelper/web/rpc/undergrad/TrainingPlanRemote.java index 864cd18..f64d582 100644 --- a/backend-main/backend-web/src/main/java/wusthelper/web/rpc/undergrad/TrainingPlanRemote.java +++ b/backend-main/backend-web/src/main/java/wusthelper/web/rpc/undergrad/TrainingPlanRemote.java @@ -1,5 +1,6 @@ package wusthelper.web.rpc.undergrad; +import org.springframework.stereotype.Component; import wusthelper.internal.rpc.response.RpcResponseDto; import jakarta.validation.constraints.NotNull; import org.springframework.cloud.openfeign.FeignClient; @@ -7,7 +8,8 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; -@FeignClient(name = "wusthelper.undergrad", contextId = "trainingPlan") +@Component +@FeignClient(name = "wusthelper.undergrad", contextId = "undergrad-trainingPlan") public interface TrainingPlanRemote { String ROOT_PATH = "/training_plan"; diff --git a/backend-main/backend-web/src/main/java/wusthelper/web/rpc/undergrad/UndergradCookieRemote.java b/backend-main/backend-web/src/main/java/wusthelper/web/rpc/undergrad/UndergradCookieRemote.java index ce2a191..2668b39 100644 --- a/backend-main/backend-web/src/main/java/wusthelper/web/rpc/undergrad/UndergradCookieRemote.java +++ b/backend-main/backend-web/src/main/java/wusthelper/web/rpc/undergrad/UndergradCookieRemote.java @@ -1,11 +1,13 @@ package wusthelper.web.rpc.undergrad; +import org.springframework.stereotype.Component; import wusthelper.internal.rpc.response.RpcResponseDto; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; -@FeignClient(name = "wusthelper.undergrad", contextId = "cookie") +@Component +@FeignClient(name = "wusthelper.undergrad", contextId = "undergrad-cookie") public interface UndergradCookieRemote { String COOKIE_ROOT_PATH = "/cookie"; diff --git a/backend-main/backend-web/src/main/java/wusthelper/web/rpc/undergrad/UndergradStudentInfoRemote.java b/backend-main/backend-web/src/main/java/wusthelper/web/rpc/undergrad/UndergradStudentInfoRemote.java index aa12eaf..64875d5 100644 --- a/backend-main/backend-web/src/main/java/wusthelper/web/rpc/undergrad/UndergradStudentInfoRemote.java +++ b/backend-main/backend-web/src/main/java/wusthelper/web/rpc/undergrad/UndergradStudentInfoRemote.java @@ -1,6 +1,7 @@ package wusthelper.web.rpc.undergrad; import cn.wustlinghang.mywust.data.global.StudentInfo; +import org.springframework.stereotype.Component; import wusthelper.internal.rpc.response.RpcResponseDto; import jakarta.validation.constraints.NotNull; import org.springframework.cloud.openfeign.FeignClient; @@ -8,7 +9,8 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; -@FeignClient(name = "wusthelper.undergrad", contextId = "studentInfo") +@Component +@FeignClient(name = "wusthelper.undergrad", contextId = "undergrad-studentInfo") public interface UndergradStudentInfoRemote { String ROOT_PATH = "/student_info"; diff --git a/backend-main/backend-web/src/main/java/wusthelper/web/service/campus/graduate/GraduateCookieService.java b/backend-main/backend-web/src/main/java/wusthelper/web/service/campus/graduate/GraduateCookieService.java index 51241bd..025549f 100644 --- a/backend-main/backend-web/src/main/java/wusthelper/web/service/campus/graduate/GraduateCookieService.java +++ b/backend-main/backend-web/src/main/java/wusthelper/web/service/campus/graduate/GraduateCookieService.java @@ -16,7 +16,7 @@ public class GraduateCookieService { public String getLoginCookie(String username, String password) throws ServiceException { var rpcResp = graduateCookieRemote.login(username, password); if (rpcResp.code() != ServiceCode.Ok) { - ServiceException.of(rpcResp.code()); + ServiceException.error(rpcResp.code()); } return rpcResp.data(); diff --git a/backend-main/backend-web/src/main/java/wusthelper/web/service/campus/graduate/GraduateStudentInfoService.java b/backend-main/backend-web/src/main/java/wusthelper/web/service/campus/graduate/GraduateStudentInfoService.java index 788dacb..8dfc3d3 100644 --- a/backend-main/backend-web/src/main/java/wusthelper/web/service/campus/graduate/GraduateStudentInfoService.java +++ b/backend-main/backend-web/src/main/java/wusthelper/web/service/campus/graduate/GraduateStudentInfoService.java @@ -35,7 +35,7 @@ public class GraduateStudentInfoService { public StudentInfo getStudentInfo(String user, String cookie) { var response = graduateStudentInfoRemote.get(cookie); if (response.code() != ServiceCode.Ok) { - ServiceException.of(response.code()); + ServiceException.error(response.code()); } var studentInfo = response.data(); diff --git a/backend-main/backend-web/src/main/java/wusthelper/web/service/campus/undergrad/ExamDelayApplicationService.java b/backend-main/backend-web/src/main/java/wusthelper/web/service/campus/undergrad/ExamDelayApplicationService.java index 9aa46f7..efbd3ff 100644 --- a/backend-main/backend-web/src/main/java/wusthelper/web/service/campus/undergrad/ExamDelayApplicationService.java +++ b/backend-main/backend-web/src/main/java/wusthelper/web/service/campus/undergrad/ExamDelayApplicationService.java @@ -6,7 +6,6 @@ import wusthelper.web.rpc.undergrad.ExamDelayApplicationRemote; import wusthelper.web.data.entity.CookieType; import wusthelper.web.service.cookie.CookieManager; import cn.wustlinghang.mywust.data.undergrad.ExamDelayApplication; -import wusthelper.internal.rpc.response.RpcCommonResponseCode; import org.springframework.stereotype.Service; @Service @@ -24,7 +23,7 @@ public class ExamDelayApplicationService { String cookie = cookieManager.getCookie(user, CookieType.Undergrad); var rpcResp = examDelayApplicationRemote.get(cookie, term, activityId); if (rpcResp.code() != ServiceCode.Ok) { - ServiceException.of(rpcResp.code()); + ServiceException.error(rpcResp.code()); } // todo 异步存用户课表,异常判断等等 diff --git a/backend-main/backend-web/src/main/java/wusthelper/web/service/campus/undergrad/UndergradCookieService.java b/backend-main/backend-web/src/main/java/wusthelper/web/service/campus/undergrad/UndergradCookieService.java index 836ea20..8749559 100644 --- a/backend-main/backend-web/src/main/java/wusthelper/web/service/campus/undergrad/UndergradCookieService.java +++ b/backend-main/backend-web/src/main/java/wusthelper/web/service/campus/undergrad/UndergradCookieService.java @@ -17,7 +17,7 @@ public class UndergradCookieService { public String getLoginCookie(String username, String password) throws ServiceException { var rpcResp = undergradCookieRemote.login(username, password); if (rpcResp.code() != ServiceCode.Ok) { - ServiceException.of(rpcResp.code()); + ServiceException.error(rpcResp.code()); } return rpcResp.data(); diff --git a/backend-main/backend-web/src/main/java/wusthelper/web/service/campus/undergrad/CourseTableService.java b/backend-main/backend-web/src/main/java/wusthelper/web/service/campus/undergrad/UndergradCourseTableService.java similarity index 80% rename from backend-main/backend-web/src/main/java/wusthelper/web/service/campus/undergrad/CourseTableService.java rename to backend-main/backend-web/src/main/java/wusthelper/web/service/campus/undergrad/UndergradCourseTableService.java index dc7b782..4256bb2 100644 --- a/backend-main/backend-web/src/main/java/wusthelper/web/service/campus/undergrad/CourseTableService.java +++ b/backend-main/backend-web/src/main/java/wusthelper/web/service/campus/undergrad/UndergradCourseTableService.java @@ -6,18 +6,17 @@ import wusthelper.web.exception.ServiceException; import wusthelper.web.rpc.undergrad.CourseTableRemote; import wusthelper.web.service.cookie.CookieManager; import cn.wustlinghang.mywust.data.global.Course; -import wusthelper.internal.rpc.response.RpcCommonResponseCode; import org.springframework.stereotype.Service; import java.util.List; @Service -public class CourseTableService { +public class UndergradCourseTableService { private final CourseTableRemote courseTableRemote; private final CookieManager cookieManager; - public CourseTableService(CourseTableRemote courseTableRemote, CookieManager cookieManager) { + public UndergradCourseTableService(CourseTableRemote courseTableRemote, CookieManager cookieManager) { this.courseTableRemote = courseTableRemote; this.cookieManager = cookieManager; } @@ -26,7 +25,7 @@ public class CourseTableService { String cookie = cookieManager.getCookie(user, CookieType.Undergrad); var rpcResp = courseTableRemote.get(cookie, term); if (rpcResp.code() != ServiceCode.Ok) { - ServiceException.of(rpcResp.code()); + ServiceException.error(rpcResp.code()); } // todo 异步存用户课表,异常判断等等 diff --git a/backend-main/backend-web/src/main/java/wusthelper/web/service/campus/undergrad/CreditStatusService.java b/backend-main/backend-web/src/main/java/wusthelper/web/service/campus/undergrad/UndergradCreditStatusService.java similarity index 78% rename from backend-main/backend-web/src/main/java/wusthelper/web/service/campus/undergrad/CreditStatusService.java rename to backend-main/backend-web/src/main/java/wusthelper/web/service/campus/undergrad/UndergradCreditStatusService.java index ef12c49..f738d16 100644 --- a/backend-main/backend-web/src/main/java/wusthelper/web/service/campus/undergrad/CreditStatusService.java +++ b/backend-main/backend-web/src/main/java/wusthelper/web/service/campus/undergrad/UndergradCreditStatusService.java @@ -5,15 +5,14 @@ import wusthelper.web.data.entity.CookieType; import wusthelper.web.exception.ServiceException; import wusthelper.web.rpc.undergrad.CreditStatusRemote; import wusthelper.web.service.cookie.CookieManager; -import wusthelper.internal.rpc.response.RpcCommonResponseCode; import org.springframework.stereotype.Service; @Service -public class CreditStatusService { +public class UndergradCreditStatusService { private final CreditStatusRemote creditStatusRemote; private final CookieManager cookieManager; - public CreditStatusService(CreditStatusRemote creditStatusRemote, CookieManager cookieManager) { + public UndergradCreditStatusService(CreditStatusRemote creditStatusRemote, CookieManager cookieManager) { this.creditStatusRemote = creditStatusRemote; this.cookieManager = cookieManager; } @@ -22,7 +21,7 @@ public class CreditStatusService { String cookie = cookieManager.getCookie(user, CookieType.Undergrad); var rpcResp = creditStatusRemote.get(cookie); if (rpcResp.code() != ServiceCode.Ok) { - ServiceException.of(rpcResp.code()); + ServiceException.error(rpcResp.code()); } // todo 异步存用户课表,异常判断等等 diff --git a/backend-main/backend-web/src/main/java/wusthelper/web/service/campus/undergrad/ScoreService.java b/backend-main/backend-web/src/main/java/wusthelper/web/service/campus/undergrad/UndergradScoreService.java similarity index 80% rename from backend-main/backend-web/src/main/java/wusthelper/web/service/campus/undergrad/ScoreService.java rename to backend-main/backend-web/src/main/java/wusthelper/web/service/campus/undergrad/UndergradScoreService.java index 44368b5..1e47c51 100644 --- a/backend-main/backend-web/src/main/java/wusthelper/web/service/campus/undergrad/ScoreService.java +++ b/backend-main/backend-web/src/main/java/wusthelper/web/service/campus/undergrad/UndergradScoreService.java @@ -6,17 +6,16 @@ import wusthelper.web.exception.ServiceException; import wusthelper.web.rpc.undergrad.ScoreRemote; import wusthelper.web.service.cookie.CookieManager; import cn.wustlinghang.mywust.data.global.Score; -import wusthelper.internal.rpc.response.RpcCommonResponseCode; import org.springframework.stereotype.Service; import java.util.List; @Service -public class ScoreService { +public class UndergradScoreService { private final ScoreRemote scoreRemote; private final CookieManager cookieManager; - public ScoreService(ScoreRemote scoreRemote, CookieManager cookieManager) { + public UndergradScoreService(ScoreRemote scoreRemote, CookieManager cookieManager) { this.scoreRemote = scoreRemote; this.cookieManager = cookieManager; } @@ -25,7 +24,7 @@ public class ScoreService { String cookie = cookieManager.getCookie(user, CookieType.Undergrad); var rpcResp = scoreRemote.get(cookie); if (rpcResp.code() != ServiceCode.Ok) { - ServiceException.of(rpcResp.code()); + ServiceException.error(rpcResp.code()); } // todo 异步存用户课表,异常判断等等 diff --git a/backend-main/backend-web/src/main/java/wusthelper/web/service/campus/undergrad/UndergradStudentInfoService.java b/backend-main/backend-web/src/main/java/wusthelper/web/service/campus/undergrad/UndergradStudentInfoService.java index 3fe1a76..43cba70 100644 --- a/backend-main/backend-web/src/main/java/wusthelper/web/service/campus/undergrad/UndergradStudentInfoService.java +++ b/backend-main/backend-web/src/main/java/wusthelper/web/service/campus/undergrad/UndergradStudentInfoService.java @@ -8,7 +8,6 @@ import wusthelper.web.exception.ServiceException; import wusthelper.web.rpc.undergrad.UndergradStudentInfoRemote; import wusthelper.web.service.cookie.CookieManager; import wusthelper.web.service.student.StudentService; -import wusthelper.web.util.PasswordCodec; @Service public class UndergradStudentInfoService { @@ -28,21 +27,21 @@ public class UndergradStudentInfoService { this.studentService = studentService; } - public StudentInfo getStudentInfo(String user) { - String cookie = cookieManager.getCookie(user, CookieType.Undergrad); + public StudentInfo getStudentInfo(String studentNumber) { + String cookie = cookieManager.getCookie(studentNumber, CookieType.Undergrad); - return getStudentInfo(user, cookie); + return getStudentInfo(studentNumber, cookie); } - public StudentInfo getStudentInfo(String user, String cookie) { + public StudentInfo getStudentInfo(String studentNumber, String cookie) { var rpcResp = undergradStudentInfoRemote.get(cookie); if (rpcResp.code() != ServiceCode.Ok) { - ServiceException.of(rpcResp.code()); + ServiceException.error(rpcResp.code()); } var studentInfo = rpcResp.data(); - studentService.saveOrUpdateUserAsync(user, studentInfo); + studentService.saveOrUpdateUserAsync(studentNumber, studentInfo); return studentInfo; } } \ No newline at end of file diff --git a/backend-main/backend-web/src/main/java/wusthelper/web/service/campus/undergrad/TrainingPlanService.java b/backend-main/backend-web/src/main/java/wusthelper/web/service/campus/undergrad/UndergradTrainingPlanService.java similarity index 78% rename from backend-main/backend-web/src/main/java/wusthelper/web/service/campus/undergrad/TrainingPlanService.java rename to backend-main/backend-web/src/main/java/wusthelper/web/service/campus/undergrad/UndergradTrainingPlanService.java index 5afc444..032aa93 100644 --- a/backend-main/backend-web/src/main/java/wusthelper/web/service/campus/undergrad/TrainingPlanService.java +++ b/backend-main/backend-web/src/main/java/wusthelper/web/service/campus/undergrad/UndergradTrainingPlanService.java @@ -5,15 +5,14 @@ import wusthelper.web.exception.ServiceException; import wusthelper.web.rpc.undergrad.TrainingPlanRemote; import wusthelper.web.data.entity.CookieType; import wusthelper.web.service.cookie.CookieManager; -import wusthelper.internal.rpc.response.RpcCommonResponseCode; import org.springframework.stereotype.Service; @Service -public class TrainingPlanService { +public class UndergradTrainingPlanService { private final TrainingPlanRemote trainingPlanRemote; private final CookieManager cookieManager; - public TrainingPlanService(TrainingPlanRemote trainingPlanRemote, CookieManager cookieManager) { + public UndergradTrainingPlanService(TrainingPlanRemote trainingPlanRemote, CookieManager cookieManager) { this.trainingPlanRemote = trainingPlanRemote; this.cookieManager = cookieManager; } @@ -22,7 +21,7 @@ public class TrainingPlanService { String cookie = cookieManager.getCookie(user, CookieType.Undergrad); var rpcResp = trainingPlanRemote.get(cookie); if (rpcResp.code() != ServiceCode.Ok) { - ServiceException.of(rpcResp.code()); + ServiceException.error(rpcResp.code()); } // todo 异步存用户课表,异常判断等等 diff --git a/backend-main/backend-web/src/main/java/wusthelper/web/service/cookie/CookieManager.java b/backend-main/backend-web/src/main/java/wusthelper/web/service/cookie/CookieManager.java index 1a2f22e..507bfdf 100644 --- a/backend-main/backend-web/src/main/java/wusthelper/web/service/cookie/CookieManager.java +++ b/backend-main/backend-web/src/main/java/wusthelper/web/service/cookie/CookieManager.java @@ -78,7 +78,7 @@ public class CookieManager { ); if (student == null) { - ServiceException.of(ServiceCode.UserNotExists); + ServiceException.error(ServiceCode.UserNotExists); } String password = switch (cookieType) { @@ -88,7 +88,7 @@ public class CookieManager { }; if (password == null) { - ServiceException.of(ServiceCode.ParamWrong); + ServiceException.error(ServiceCode.ParamWrong); } return switch (cookieType) { diff --git a/backend-main/backend-web/src/main/java/wusthelper/web/util/StringUtil.java b/backend-main/backend-web/src/main/java/wusthelper/web/util/StringUtil.java new file mode 100644 index 0000000..2904558 --- /dev/null +++ b/backend-main/backend-web/src/main/java/wusthelper/web/util/StringUtil.java @@ -0,0 +1,86 @@ +package wusthelper.web.util; + +public class StringUtil { + + public static boolean isPositiveNumber(final String str) { + if (str == null || "".equals(str)) { + return false; + } + + int length = str.length(); + for (int i = 0; i < length; i++) { + int ch = str.charAt(i); + // 判断字符0-9,还有. + // 负数不考虑 + if ((ch < 48 || ch > 57 ) && ch != 46) { + return false; + } + } + + return true; + } + + /** + * 成绩换算成学分 + * + * @param gradeString 数字成绩字符串 + * @return 换算后的学分 + */ + public static float numberGradeToGradePoint(String gradeString) { + float gradeFloat = Float.parseFloat(gradeString); + if (gradeFloat >= 90.0F) { + return 4.0F; + } else if (gradeFloat >= 85.0F) { + return 3.7F; + } else if (gradeFloat >= 82.0F) { + return 3.3F; + } else if (gradeFloat >= 78.0F) { + return 3.0F; + } else if (gradeFloat >= 75.0F) { + return 2.7F; + } else if (gradeFloat >= 72.0F) { + return 2.3F; + } else if (gradeFloat >= 68.0F) { + return 2.0F; + } else if (gradeFloat >= 64.0F) { + return 1.5F; + } else if (gradeFloat >= 60.0F) { + return 1.0F; + } else { + return 0.0F; + } + } + + /** + * 等级成绩换算成学分 + * + * @param gradeLevel 成绩等级 + * @return 换算后的学分 + */ + public static float levelGradeToGradePoint(String gradeLevel) { + switch (gradeLevel) { + case "A": + return 4.0F; + case "A-": + return 3.7F; + case "B+": + return 3.3F; + case "B": + return 3.0F; + case "B-": + return 2.7F; + case "C+": + return 2.3F; + case "C": + return 2.0F; + case "C-": + return 1.5F; + case "D": + return 1.0F; + case "F": + return 0.0F; + default: + return 0.0F; + } + } +} diff --git a/backend-main/backend-web/src/main/resources/application.yml b/backend-main/backend-web/src/main/resources/application.yml index d9c6271..3ea2993 100644 --- a/backend-main/backend-web/src/main/resources/application.yml +++ b/backend-main/backend-web/src/main/resources/application.yml @@ -7,6 +7,9 @@ spring: import: optional:file:.env[.properties] application: name: wusthelper-backend-main +# main: +# allow-bean-definition-overriding: true +# main.allow-bean-definition-overriding = true cloud: consul: @@ -22,3 +25,6 @@ spring: server: port: ${RUN_PORT} + +wusthelper: + password-encode-key: wusthelper \ No newline at end of file diff --git a/backend-main/backend-web/src/test/java/wusthelper/TestMain.java b/backend-main/backend-web/src/test/java/wusthelper/TestMain.java index b31d62c..55d3d12 100644 --- a/backend-main/backend-web/src/test/java/wusthelper/TestMain.java +++ b/backend-main/backend-web/src/test/java/wusthelper/TestMain.java @@ -11,7 +11,7 @@ import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit.jupiter.SpringExtension; -import wusthelper.web.api.v2.Token; +import wusthelper.web.api.v2.TokenTool; @Slf4j @EnableAsync @@ -27,19 +27,19 @@ import wusthelper.web.api.v2.Token; "wusthelper.data", "wusthelper.web", }) -@MapperScan("cn.wustlinghang.wusthelper.data.dao.mapper") +@MapperScan("wusthelper.data.dao.mapper") public class TestMain { @Test public void jwtTest() { - String token = Token.signToken(1233487L, "202118194039"); + String token = TokenTool.signToken(1233487L, "202118194039"); log.info(token); } public static void main(String[] args) { - String token = Token.signToken(1233487L, "202118194039"); - boolean valid = Token.verifyToken(token); - var uid = Token.getUid(token); - var studentNumber = Token.getStudentNumber(token); + String token = TokenTool.signToken(1233487L, "202118194039"); + boolean valid = TokenTool.verifyToken(token); + var uid = TokenTool.getUid(token); + var studentNumber = TokenTool.getStudentNumber(token); log.info(token); log.info("{}", valid); log.info(uid); diff --git a/common/src/main/java/wusthelper/code/ServiceCode.java b/common/src/main/java/wusthelper/code/ServiceCode.java index 22998cd..f158b3a 100644 --- a/common/src/main/java/wusthelper/code/ServiceCode.java +++ b/common/src/main/java/wusthelper/code/ServiceCode.java @@ -21,12 +21,13 @@ public class ServiceCode { public static final int UserNotExists = 20_001; // 用户不存在 // 本科生 - public static final int UndergradUserNotExists = 30_001; // 本科生:用户不存在 - public static final int UndergradUserBanned = 30_002; // 本科生:封号 - public static final int UndergradUserDisabled = 30_003; // 本科生:用户账号禁用 - public static final int UndergradPasswordNeedModify = 30_004; // 本科生:用户密码需要更改 - public static final int UndergradBannedInExclusiveTime = 30_005; // 本科生:专属选课时间段账号被禁用(--> _ --) - public static final int UndergradNeedEvaluate = 30_006; // 本科生:需要评教 + public static final int UndergradPasswordWrong = 30_001; // 本科生:密码错误 + public static final int UndergradUserNotExists = 30_002; // 本科生:用户不存在 + public static final int UndergradUserBanned = 30_003; // 本科生:封号 + public static final int UndergradUserDisabled = 30_004; // 本科生:用户账号禁用 + public static final int UndergradPasswordNeedModify = 30_005; // 本科生:用户密码需要更改 + public static final int UndergradBannedInExclusiveTime = 30_006; // 本科生:专属选课时间段账号被禁用(--> _ --) + public static final int UndergradNeedEvaluate = 30_007; // 本科生:需要评教 // 研究生 public static final int GraduatePasswordWrong = 40_001; // 研究生:密码错误 @@ -62,6 +63,7 @@ public class ServiceCode { text.put(ParseError, "解析错误"); // 本科生 + text.put(UndergradPasswordWrong, "本科生:密码错误"); text.put(UndergradUserNotExists, "本科生:用户不存在"); text.put(UndergradUserBanned, "本科生:封号"); text.put(UndergradUserDisabled, "本科生:用户账号禁用"); diff --git a/common/src/main/java/wusthelper/internal/rpc/exception/GraduateRpcException.java b/common/src/main/java/wusthelper/internal/rpc/exception/GraduateRpcException.java deleted file mode 100644 index d4389e9..0000000 --- a/common/src/main/java/wusthelper/internal/rpc/exception/GraduateRpcException.java +++ /dev/null @@ -1,62 +0,0 @@ -package wusthelper.internal.rpc.exception; - -import java.util.StringJoiner; - -public class GraduateRpcException extends RpcException { - - public GraduateRpcException(TypeCode typeCode, - SubModuleCode subModuleCode, - ErrorCode errorCode) { - super(GRADUATE_MODULE, - typeCode.ordinal(), - subModuleCode.ordinal() * 100 + errorCode.ordinal(), - new StringJoiner("/") - .add("GRADUATE") - .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 { - COMMON, AUTH, COURSE_TABLE, SCORE, STUDENT_INFO, TRAINING_PLAN - } - - /** - * 具体错误编码 - */ - public enum ErrorCode { - REQUEST_INVALID, - PARAM_INVALID, - COOKIE_INVALID, - NETWORK_ERROR, - PARSE_ERROR, - - // 需要评教 - NEED_EVALUATE, - - AUTH_PASSWORD_WRONG, - AUTH_CAPTCHA_WRONG, - AUTH_UNKNOWN_ERROR - } -} diff --git a/common/src/main/java/wusthelper/internal/rpc/exception/LibraryRpcException.java b/common/src/main/java/wusthelper/internal/rpc/exception/LibraryRpcException.java deleted file mode 100644 index bbb13b2..0000000 --- a/common/src/main/java/wusthelper/internal/rpc/exception/LibraryRpcException.java +++ /dev/null @@ -1,68 +0,0 @@ -package wusthelper.internal.rpc.exception; - -import java.util.StringJoiner; - -public class LibraryRpcException extends RpcException { - - public LibraryRpcException(TypeCode typeCode, - SubModuleCode subModuleCode, - ErrorCode errorCode) { - super(LIBRARY_MODULE, - typeCode.ordinal(), - subModuleCode.ordinal() * 100 + errorCode.ordinal(), - new StringJoiner("/") - .add("LIBRARY") - .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 { - COMMON, AUTH, - COVER_IMAGE, BOOK_DETAIL, BOOK_HOLDING, SEARCH, - CURRENT_LOAN, LOAN_HISTORY, OVERDUE_SOON, - } - - /** - * 具体错误编码 - */ - public enum ErrorCode { - REQUEST_INVALID, - PARAM_INVALID, - COOKIE_INVALID, - NETWORK_ERROR, - PARSE_ERROR, - - AUTH_PASSWORD_WRONG, - // 用户不存在 - AUTH_USER_NOT_EXISTS, - // 封号 - AUTH_USER_BANNED, - // 用户账号禁用 - AUTH_USER_DISABLED, - // 用户密码需要更改 - AUTH_NEED_CHANGE_PASSWORD, - AUTH_UNKNOWN_ERROR - } -} diff --git a/common/src/main/java/wusthelper/internal/rpc/exception/PhysicsRpcException.java b/common/src/main/java/wusthelper/internal/rpc/exception/PhysicsRpcException.java deleted file mode 100644 index 7fd4b41..0000000 --- a/common/src/main/java/wusthelper/internal/rpc/exception/PhysicsRpcException.java +++ /dev/null @@ -1,59 +0,0 @@ -package wusthelper.internal.rpc.exception; - -import java.util.StringJoiner; - -public class PhysicsRpcException extends RpcException { - public PhysicsRpcException(TypeCode typeCode, - SubModuleCode subModuleCode, - ErrorCode errorCode) { - super(PHYSICS_MODULE, - typeCode.ordinal(), - subModuleCode.ordinal() * 100 + errorCode.ordinal(), - new StringJoiner("/") - .add("PHYSICS") - .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 { - COMMON, AUTH, COURSE_TABLE, SCORE - } - - /** - * 具体错误编码 - */ - public enum ErrorCode { - REQUEST_INVALID, - PARAM_INVALID, - COOKIE_INVALID, - NETWORK_ERROR, - PARSE_ERROR, - - AUTH_PASSWORD_WRONG, - AUTH_UNKNOWN_ERROR, - - AUTH_USER_NOT_CURRENT_TERM - } -} diff --git a/common/src/main/java/wusthelper/internal/rpc/exception/RpcException.java b/common/src/main/java/wusthelper/internal/rpc/exception/RpcException.java index d135c4a..771a8d5 100644 --- a/common/src/main/java/wusthelper/internal/rpc/exception/RpcException.java +++ b/common/src/main/java/wusthelper/internal/rpc/exception/RpcException.java @@ -1,31 +1,17 @@ package wusthelper.internal.rpc.exception; -public abstract class RpcException extends RuntimeException { - protected final int code; +import wusthelper.code.ServiceCode; - public static final int UNDERGRAD_MODULE = 1; - public static final int GRADUATE_MODULE = 2; - public static final int LIBRARY_MODULE = 3; - public static final int PHYSICS_MODULE = 4; +public class RpcException extends RuntimeException { + protected final int code; - private static final int MODULE_CODE_OFFSET = 100_0000; - private static final int TYPE_CODE_OFFSET = 1_0000; - 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 * MODULE_CODE_OFFSET) + (type * TYPE_CODE_OFFSET) + (exceptionCode)); - } - - public RpcException(int module, int type, int exceptionCode, String message) { - this((module * MODULE_CODE_OFFSET) + (type * TYPE_CODE_OFFSET) + (exceptionCode), message); + this(code, ServiceCode.getDescribe(code)); } public int getCode() { @@ -33,10 +19,10 @@ public abstract class RpcException extends RuntimeException { } public static RpcException ApiNotImplement() { - return new RpcException(-1, "接口未实现") {}; + return new RpcException(-1, null); } - public static int toIntCode(int module, int type, int exceptionCode) { - return (module * MODULE_CODE_OFFSET) + (type * TYPE_CODE_OFFSET) + (exceptionCode); + public static void error(int code) { + throw new RpcException(code); } } diff --git a/common/src/main/java/wusthelper/internal/rpc/exception/UndergradRpcException.java b/common/src/main/java/wusthelper/internal/rpc/exception/UndergradRpcException.java deleted file mode 100644 index ca9572a..0000000 --- a/common/src/main/java/wusthelper/internal/rpc/exception/UndergradRpcException.java +++ /dev/null @@ -1,71 +0,0 @@ -package wusthelper.internal.rpc.exception; - -import java.util.StringJoiner; - -public class UndergradRpcException extends RpcException { - 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 { - COMMON, AUTH, COURSE_TABLE, CREDIT_STATUS, SCORE, - STUDENT_INFO, TRAINING_PLAN, EXAM_ACTIVITIES, EXAM_DELAY_APPLICATION - } - - /** - * 具体错误编码 - */ - public enum ErrorCode { - REQUEST_INVALID, - PARAM_INVALID, - COOKIE_INVALID, - NETWORK_ERROR, - PARSE_ERROR, - - AUTH_PASSWORD_WRONG, - // 用户不存在 - AUTH_USER_NOT_EXISTS, - // 封号 - AUTH_USER_BANNED, - // 用户账号禁用 - AUTH_USER_DISABLED, - // 用户密码需要更改 - AUTH_NEED_CHANGE_PASSWORD, - // 专属选课时间段账号被禁用(--> _ --) - AUTH_BANNED_IN_EXCLUSIVE_TIME, - AUTH_UNKNOWN_ERROR, - - // 需要评教 - NEED_EVALUATE, - } -} diff --git a/common/src/main/java/wusthelper/internal/rpc/response/RpcCommonResponseCode.java b/common/src/main/java/wusthelper/internal/rpc/response/RpcCommonResponseCode.java deleted file mode 100644 index 60ab70b..0000000 --- a/common/src/main/java/wusthelper/internal/rpc/response/RpcCommonResponseCode.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Class created by lensfrex. - */ - -package wusthelper.internal.rpc.response; - -public enum RpcCommonResponseCode { - Success(20000, "成功"), - ParamWrong(30001, "参数错误"), - ServerInternalError(50000, "服务器内部错误"), - ApiNotImplement(0, "接口未实现"), - - ; - - private final int code; - - private final String message; - - RpcCommonResponseCode(int code, String message) { - this.code = code; - this.message = message; - } - - public int getCode() { - return code; - } - - public String getMessage() { - return message; - } -} \ No newline at end of file diff --git a/common/src/main/java/wusthelper/internal/rpc/response/RpcResponseDto.java b/common/src/main/java/wusthelper/internal/rpc/response/RpcResponseDto.java index da1c47d..bdf3662 100644 --- a/common/src/main/java/wusthelper/internal/rpc/response/RpcResponseDto.java +++ b/common/src/main/java/wusthelper/internal/rpc/response/RpcResponseDto.java @@ -1,22 +1,19 @@ package wusthelper.internal.rpc.response; import com.fasterxml.jackson.annotation.JsonInclude; +import wusthelper.code.ServiceCode; @JsonInclude(JsonInclude.Include.NON_NULL) public record RpcResponseDto(int code, T data) { public static RpcResponseDto success(T data) { - return new RpcResponseDto<>(RpcCommonResponseCode.Success.getCode(), data); + return new RpcResponseDto<>(ServiceCode.Ok, data); } public static RpcResponseDto success() { return success(null); } - public static RpcResponseDto error(int code, String message) { + public static RpcResponseDto error(int code) { return new RpcResponseDto<>(code, null); } - - public static RpcResponseDto error(RpcCommonResponseCode code) { - return error(code.getCode(), code.getMessage()); - } } \ No newline at end of file diff --git a/common/src/main/java/wusthelper/web/exception/ServiceException.java b/common/src/main/java/wusthelper/web/exception/ServiceException.java index 2409a56..a12bd1c 100644 --- a/common/src/main/java/wusthelper/web/exception/ServiceException.java +++ b/common/src/main/java/wusthelper/web/exception/ServiceException.java @@ -37,7 +37,7 @@ public class ServiceException extends RuntimeException { * @param code 错误码 * @throws ServiceException . */ - public static void of(int code) throws ServiceException { + public static void error(int code) throws ServiceException { throw new ServiceException(code); } } diff --git a/sub-services/graduate/src/main/java/wusthelper/internal/graduate/api/http/v1/handler/BaseExceptionHandler.java b/sub-services/graduate/src/main/java/wusthelper/internal/graduate/api/http/v1/handler/BaseExceptionHandler.java index 2f819b8..6a9df4e 100644 --- a/sub-services/graduate/src/main/java/wusthelper/internal/graduate/api/http/v1/handler/BaseExceptionHandler.java +++ b/sub-services/graduate/src/main/java/wusthelper/internal/graduate/api/http/v1/handler/BaseExceptionHandler.java @@ -1,35 +1,31 @@ package wusthelper.internal.graduate.api.http.v1.handler; -import wusthelper.internal.rpc.response.RpcCommonResponseCode; -import wusthelper.internal.rpc.response.RpcResponseDto; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.Response; +import wusthelper.code.ServiceCode; +import wusthelper.internal.rpc.response.RpcResponseDto; public abstract class BaseExceptionHandler { public static final String EXCEPTION_HEADER_KEY = "X-exception-handler"; private static final ObjectMapper objectMapper = new ObjectMapper(); - public Response toResponse(RpcCommonResponseCode code, String msg, String handlerName) { - return toResponse(code.getCode(), msg, handlerName); - } - - public Response toResponse(int code, String msg, String handlerName) { - return toResponse(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), code, msg, handlerName); + public Response toResponse(int code, String handlerName) { + return toResponse(Response.Status.OK, code, handlerName); } - public Response toResponse(int status, int code, String msg, String handlerName) { - return toResponse(Response.Status.fromStatusCode(status), code, msg, handlerName); + public Response toResponse(int status, int code, String handlerName) { + return toResponse(Response.Status.fromStatusCode(status), code, handlerName); } - public Response toResponse(Response.Status status, int code, String msg, String handlerName) { + public Response toResponse(Response.Status status, int code, String handlerName) { Object response; try { - response = objectMapper.writeValueAsString(RpcResponseDto.error(code, msg)); + response = objectMapper.writeValueAsString(RpcResponseDto.error(code)); } catch (JsonProcessingException e) { - response = RpcResponseDto.error(RpcCommonResponseCode.ServerInternalError); + response = RpcResponseDto.error(ServiceCode.ServerInternalError); } return Response.status(status) diff --git a/sub-services/graduate/src/main/java/wusthelper/internal/graduate/api/http/v1/handler/DefaultExceptionHandler.java b/sub-services/graduate/src/main/java/wusthelper/internal/graduate/api/http/v1/handler/DefaultExceptionHandler.java index 7805e7f..d3f7b20 100644 --- a/sub-services/graduate/src/main/java/wusthelper/internal/graduate/api/http/v1/handler/DefaultExceptionHandler.java +++ b/sub-services/graduate/src/main/java/wusthelper/internal/graduate/api/http/v1/handler/DefaultExceptionHandler.java @@ -1,21 +1,19 @@ package wusthelper.internal.graduate.api.http.v1.handler; -import wusthelper.internal.rpc.response.RpcCommonResponseCode; +import jakarta.enterprise.context.ApplicationScoped; import jakarta.ws.rs.core.Response; import jakarta.ws.rs.ext.ExceptionMapper; import jakarta.ws.rs.ext.Provider; import lombok.extern.slf4j.Slf4j; +import wusthelper.code.ServiceCode; @Slf4j @Provider +@ApplicationScoped public class DefaultExceptionHandler extends BaseExceptionHandler implements ExceptionMapper { @Override public Response toResponse(Exception e) { log.error("未知异常:", e); - return super.toResponse( - RpcCommonResponseCode.ServerInternalError, - e.toString(), - "DefaultExceptionHandler" - ); + return super.toResponse(ServiceCode.ServerInternalError, "DefaultExceptionHandler"); } } diff --git a/sub-services/graduate/src/main/java/wusthelper/internal/graduate/api/http/v1/handler/IOExceptionHandler.java b/sub-services/graduate/src/main/java/wusthelper/internal/graduate/api/http/v1/handler/IOExceptionHandler.java index 4318d53..91601b4 100644 --- a/sub-services/graduate/src/main/java/wusthelper/internal/graduate/api/http/v1/handler/IOExceptionHandler.java +++ b/sub-services/graduate/src/main/java/wusthelper/internal/graduate/api/http/v1/handler/IOExceptionHandler.java @@ -1,23 +1,21 @@ package wusthelper.internal.graduate.api.http.v1.handler; -import wusthelper.internal.rpc.response.RpcCommonResponseCode; +import jakarta.enterprise.context.ApplicationScoped; import jakarta.ws.rs.core.Response; import jakarta.ws.rs.ext.ExceptionMapper; import jakarta.ws.rs.ext.Provider; import lombok.extern.slf4j.Slf4j; +import wusthelper.code.ServiceCode; import java.io.IOException; @Slf4j @Provider +@ApplicationScoped public class IOExceptionHandler extends BaseExceptionHandler implements ExceptionMapper { @Override public Response toResponse(IOException e) { log.error("IO异常:", e); - return super.toResponse( - RpcCommonResponseCode.ServerInternalError, - e.toString(), - "IOExceptionHandler" - ); + return super.toResponse(ServiceCode.NetworkError, "IOExceptionHandler"); } } diff --git a/sub-services/graduate/src/main/java/wusthelper/internal/graduate/api/http/v1/handler/ParseExceptionHandler.java b/sub-services/graduate/src/main/java/wusthelper/internal/graduate/api/http/v1/handler/ParseExceptionHandler.java index b5dc85d..0d313f8 100644 --- a/sub-services/graduate/src/main/java/wusthelper/internal/graduate/api/http/v1/handler/ParseExceptionHandler.java +++ b/sub-services/graduate/src/main/java/wusthelper/internal/graduate/api/http/v1/handler/ParseExceptionHandler.java @@ -1,22 +1,20 @@ package wusthelper.internal.graduate.api.http.v1.handler; import cn.wustlinghang.mywust.exception.ParseException; -import wusthelper.internal.rpc.response.RpcCommonResponseCode; +import jakarta.enterprise.context.ApplicationScoped; import jakarta.ws.rs.core.Response; import jakarta.ws.rs.ext.ExceptionMapper; import jakarta.ws.rs.ext.Provider; import lombok.extern.slf4j.Slf4j; +import wusthelper.code.ServiceCode; @Slf4j @Provider +@ApplicationScoped public class ParseExceptionHandler extends BaseExceptionHandler implements ExceptionMapper { @Override public Response toResponse(ParseException e) { log.error("解析异常:", e); - return super.toResponse( - RpcCommonResponseCode.ServerInternalError, - e.toString(), - "ParseExceptionHandler" - ); + return super.toResponse(ServiceCode.ParseError, "ParseExceptionHandler"); } } diff --git a/sub-services/graduate/src/main/java/wusthelper/internal/graduate/api/http/v1/handler/RpcExceptionHandler.java b/sub-services/graduate/src/main/java/wusthelper/internal/graduate/api/http/v1/handler/RpcExceptionHandler.java index fa85aa2..30ade6e 100644 --- a/sub-services/graduate/src/main/java/wusthelper/internal/graduate/api/http/v1/handler/RpcExceptionHandler.java +++ b/sub-services/graduate/src/main/java/wusthelper/internal/graduate/api/http/v1/handler/RpcExceptionHandler.java @@ -1,17 +1,19 @@ package wusthelper.internal.graduate.api.http.v1.handler; -import wusthelper.internal.rpc.exception.RpcException; +import jakarta.enterprise.context.ApplicationScoped; import jakarta.ws.rs.core.Response; import jakarta.ws.rs.ext.ExceptionMapper; import jakarta.ws.rs.ext.Provider; +import lombok.extern.slf4j.Slf4j; +import wusthelper.internal.rpc.exception.RpcException; +@Slf4j @Provider +@ApplicationScoped public class RpcExceptionHandler extends BaseExceptionHandler implements ExceptionMapper { @Override public Response toResponse(RpcException e) { - return super.toResponse( - e.getCode(), - e.toString(), - "RpcExceptionHandler"); + log.debug("RpcException:", e); + return super.toResponse(e.getCode(), "RpcExceptionHandler"); } } diff --git a/sub-services/graduate/src/main/java/wusthelper/internal/graduate/api/http/v1/handler/ValidationExceptionHandler.java b/sub-services/graduate/src/main/java/wusthelper/internal/graduate/api/http/v1/handler/ValidationExceptionHandler.java index bd6b741..f87bc74 100644 --- a/sub-services/graduate/src/main/java/wusthelper/internal/graduate/api/http/v1/handler/ValidationExceptionHandler.java +++ b/sub-services/graduate/src/main/java/wusthelper/internal/graduate/api/http/v1/handler/ValidationExceptionHandler.java @@ -1,22 +1,20 @@ package wusthelper.internal.graduate.api.http.v1.handler; -import wusthelper.internal.rpc.response.RpcCommonResponseCode; +import jakarta.enterprise.context.ApplicationScoped; import jakarta.validation.ValidationException; import jakarta.ws.rs.core.Response; import jakarta.ws.rs.ext.ExceptionMapper; import jakarta.ws.rs.ext.Provider; import lombok.extern.slf4j.Slf4j; +import wusthelper.code.ServiceCode; @Slf4j @Provider +@ApplicationScoped public class ValidationExceptionHandler extends BaseExceptionHandler implements ExceptionMapper { @Override public Response toResponse(ValidationException e) { - return super.toResponse( - RpcCommonResponseCode.ParamWrong, - "参数错误:" + e.toString(), - "ValidationExceptionHandler" - ); + return super.toResponse(ServiceCode.ParamWrong, "ValidationExceptionHandler"); } } diff --git a/sub-services/graduate/src/main/java/wusthelper/internal/graduate/api/http/v1/handler/WebApplicationExceptionHandler.java b/sub-services/graduate/src/main/java/wusthelper/internal/graduate/api/http/v1/handler/WebApplicationExceptionHandler.java index f40c02e..9512601 100644 --- a/sub-services/graduate/src/main/java/wusthelper/internal/graduate/api/http/v1/handler/WebApplicationExceptionHandler.java +++ b/sub-services/graduate/src/main/java/wusthelper/internal/graduate/api/http/v1/handler/WebApplicationExceptionHandler.java @@ -1,26 +1,17 @@ package wusthelper.internal.graduate.api.http.v1.handler; -import wusthelper.internal.rpc.exception.GraduateRpcException; -import wusthelper.internal.rpc.exception.RpcException; +import jakarta.enterprise.context.ApplicationScoped; import jakarta.ws.rs.WebApplicationException; import jakarta.ws.rs.core.Response; import jakarta.ws.rs.ext.ExceptionMapper; import jakarta.ws.rs.ext.Provider; +import wusthelper.code.ServiceCode; @Provider +@ApplicationScoped public class WebApplicationExceptionHandler extends BaseExceptionHandler implements ExceptionMapper { @Override public Response toResponse(WebApplicationException e) { - return super.toResponse( - e.getResponse().getStatus(), - RpcException.toIntCode( - GraduateRpcException.GRADUATE_MODULE, - GraduateRpcException.SubModuleCode.COMMON.ordinal(), - GraduateRpcException.TypeCode.OTHER_EXCEPTION.ordinal() * 100 - + GraduateRpcException.ErrorCode.REQUEST_INVALID.ordinal() - ), - e.getMessage(), - "WebApplicationExceptionHandler" - ); + return super.toResponse(ServiceCode.RequestInvalid, "WebApplicationExceptionHandler"); } } diff --git a/sub-services/graduate/src/main/java/wusthelper/internal/graduate/services/BaseService.java b/sub-services/graduate/src/main/java/wusthelper/internal/graduate/services/BaseService.java index 42fc710..6fdd0f4 100644 --- a/sub-services/graduate/src/main/java/wusthelper/internal/graduate/services/BaseService.java +++ b/sub-services/graduate/src/main/java/wusthelper/internal/graduate/services/BaseService.java @@ -1,31 +1,20 @@ package wusthelper.internal.graduate.services; import cn.wustlinghang.mywust.exception.ApiException; -import wusthelper.internal.rpc.exception.GraduateRpcException; import lombok.extern.slf4j.Slf4j; +import wusthelper.code.ServiceCode; +import wusthelper.internal.rpc.exception.RpcException; @Slf4j public abstract class BaseService { - protected GraduateRpcException wrapApiException(ApiException e, GraduateRpcException.SubModuleCode subModuleCode) { + protected RpcException wrapApiException(ApiException e) { return switch (e.getCode()) { - case NETWORK_EXCEPTION -> new GraduateRpcException( - GraduateRpcException.TypeCode.NETWORK_EXCEPTION, - subModuleCode, - GraduateRpcException.ErrorCode.NETWORK_ERROR - ); - case COOKIE_INVALID -> new GraduateRpcException( - GraduateRpcException.TypeCode.AUTH_EXCEPTION, - subModuleCode, - GraduateRpcException.ErrorCode.COOKIE_INVALID - ); + case NETWORK_EXCEPTION -> new RpcException(ServiceCode.NetworkError); + case COOKIE_INVALID -> new RpcException(ServiceCode.CookieInvalid); default -> { - log.error("研究生:{}代理请求异常,异常未处理", subModuleCode.name()); + log.error("研究生:代理请求异常,异常未处理"); log.error("异常:", e); - yield new GraduateRpcException( - GraduateRpcException.TypeCode.AUTH_EXCEPTION, - subModuleCode, - GraduateRpcException.ErrorCode.COOKIE_INVALID - ); + yield new RpcException(ServiceCode.Unknown); } }; } diff --git a/sub-services/graduate/src/main/java/wusthelper/internal/graduate/services/CourseTableService.java b/sub-services/graduate/src/main/java/wusthelper/internal/graduate/services/CourseTableService.java index ab40ec2..550c4fa 100644 --- a/sub-services/graduate/src/main/java/wusthelper/internal/graduate/services/CourseTableService.java +++ b/sub-services/graduate/src/main/java/wusthelper/internal/graduate/services/CourseTableService.java @@ -5,9 +5,9 @@ import cn.wustlinghang.mywust.core.request.service.graduate.GraduateCourseTableA import cn.wustlinghang.mywust.data.global.Course; import cn.wustlinghang.mywust.exception.ApiException; import cn.wustlinghang.mywust.network.RequestClientOption; -import wusthelper.internal.rpc.exception.GraduateRpcException; -import wusthelper.internal.rpc.exception.RpcException; import jakarta.enterprise.context.ApplicationScoped; +import wusthelper.code.ServiceCode; +import wusthelper.internal.rpc.exception.RpcException; import java.io.IOException; import java.util.List; @@ -33,25 +33,17 @@ public class CourseTableService extends BaseService { try { return courseTableApiService.getPage(cookie, requestClientOption); } catch (ApiException e) { - throw wrapApiException(e, GraduateRpcException.SubModuleCode.COURSE_TABLE); + throw wrapApiException(e); } catch (IOException e) { - throw new GraduateRpcException( - GraduateRpcException.TypeCode.NETWORK_EXCEPTION, - GraduateRpcException.SubModuleCode.COURSE_TABLE, - GraduateRpcException.ErrorCode.NETWORK_ERROR - ); + throw new RpcException(ServiceCode.NetworkError); } } - public List parseCourseTable(String data) throws GraduateRpcException { + public List parseCourseTable(String data) throws RpcException { try { return courseTableParser.parse(data); } catch (Exception e) { - throw new GraduateRpcException( - GraduateRpcException.TypeCode.PARSE_EXCEPTION, - GraduateRpcException.SubModuleCode.COURSE_TABLE, - GraduateRpcException.ErrorCode.PARSE_ERROR - ); + throw new RpcException(ServiceCode.ParseError); } } } diff --git a/sub-services/graduate/src/main/java/wusthelper/internal/graduate/services/LoginService.java b/sub-services/graduate/src/main/java/wusthelper/internal/graduate/services/LoginService.java index fa13edd..1a477e2 100644 --- a/sub-services/graduate/src/main/java/wusthelper/internal/graduate/services/LoginService.java +++ b/sub-services/graduate/src/main/java/wusthelper/internal/graduate/services/LoginService.java @@ -3,10 +3,11 @@ package wusthelper.internal.graduate.services; import cn.wustlinghang.mywust.core.request.service.auth.GraduateLogin; import cn.wustlinghang.mywust.exception.ApiException; import cn.wustlinghang.mywust.network.RequestClientOption; -import wusthelper.internal.rpc.exception.GraduateRpcException; import jakarta.enterprise.context.ApplicationScoped; import lombok.extern.slf4j.Slf4j; import org.eclipse.microprofile.config.inject.ConfigProperty; +import wusthelper.code.ServiceCode; +import wusthelper.internal.rpc.exception.RpcException; import java.io.IOException; @@ -29,59 +30,43 @@ public class LoginService { this.maxRetryTimes = maxRetryTimes; } - public String login(String username, String password) throws GraduateRpcException { + public String login(String username, String password) throws RpcException { try { return graduateLogin.getLoginCookie(username, password, maxRetryTimes, requestClientOption); } catch (ApiException e) { throw wrapApiException(e); } catch (IOException e) { - throw new GraduateRpcException( - GraduateRpcException.TypeCode.NETWORK_EXCEPTION, - GraduateRpcException.SubModuleCode.AUTH, - GraduateRpcException.ErrorCode.NETWORK_ERROR - ); + throw new RpcException(ServiceCode.NetworkError); } } - public boolean verify(String cookie) throws GraduateRpcException { + public boolean verify(String cookie) throws RpcException { try { graduateLogin.checkCookies(cookie, requestClientOption); return true; } catch (IOException e) { - throw new GraduateRpcException( - GraduateRpcException.TypeCode.NETWORK_EXCEPTION, - GraduateRpcException.SubModuleCode.AUTH, - GraduateRpcException.ErrorCode.NETWORK_ERROR - ); + throw new RpcException(ServiceCode.NetworkError); } catch (ApiException e) { if (e.getCode() == ApiException.Code.NETWORK_EXCEPTION) { - throw new GraduateRpcException( - GraduateRpcException.TypeCode.NETWORK_EXCEPTION, - GraduateRpcException.SubModuleCode.AUTH, - GraduateRpcException.ErrorCode.NETWORK_ERROR - ); + throw new RpcException(ServiceCode.NetworkError); } return false; } } - private GraduateRpcException wrapApiException(ApiException e) { - GraduateRpcException.ErrorCode errorCode = switch (e.getCode()) { - case NETWORK_EXCEPTION -> GraduateRpcException.ErrorCode.NETWORK_ERROR; - case GRADUATE_PASSWORD_WRONG -> GraduateRpcException.ErrorCode.AUTH_PASSWORD_WRONG; - case GRADUATE_CAPTCHA_WRONG -> GraduateRpcException.ErrorCode.AUTH_CAPTCHA_WRONG; + private RpcException wrapApiException(ApiException e) { + int errorCode = switch (e.getCode()) { + case NETWORK_EXCEPTION -> ServiceCode.NetworkError; + case GRADUATE_PASSWORD_WRONG -> ServiceCode.GraduatePasswordWrong; + case GRADUATE_CAPTCHA_WRONG -> ServiceCode.GraduateCaptchaWrong; default -> { log.error("研究生:登录代理请求异常,异常未处理"); log.error("异常:", e); - yield GraduateRpcException.ErrorCode.AUTH_UNKNOWN_ERROR; + yield ServiceCode.Unknown; } }; - return new GraduateRpcException( - GraduateRpcException.TypeCode.AUTH_EXCEPTION, - GraduateRpcException.SubModuleCode.AUTH, - errorCode - ); + return new RpcException(errorCode); } } diff --git a/sub-services/graduate/src/main/java/wusthelper/internal/graduate/services/ScoreService.java b/sub-services/graduate/src/main/java/wusthelper/internal/graduate/services/ScoreService.java index 3764501..101324c 100644 --- a/sub-services/graduate/src/main/java/wusthelper/internal/graduate/services/ScoreService.java +++ b/sub-services/graduate/src/main/java/wusthelper/internal/graduate/services/ScoreService.java @@ -5,9 +5,9 @@ import cn.wustlinghang.mywust.core.request.service.graduate.GraduateScoreApiServ import cn.wustlinghang.mywust.data.global.Score; import cn.wustlinghang.mywust.exception.ApiException; import cn.wustlinghang.mywust.network.RequestClientOption; -import wusthelper.internal.rpc.exception.GraduateRpcException; -import wusthelper.internal.rpc.exception.RpcException; import jakarta.enterprise.context.ApplicationScoped; +import wusthelper.code.ServiceCode; +import wusthelper.internal.rpc.exception.RpcException; import java.io.IOException; import java.util.List; @@ -24,7 +24,7 @@ public class ScoreService extends BaseService { GraduateScoreApiService scoreApiService, GraduateScoreParser scoreParser) { this.requestClientOption = requestClientOption; - + this.scoreApiService = scoreApiService; this.scoreParser = scoreParser; } @@ -33,13 +33,9 @@ public class ScoreService extends BaseService { try { return scoreApiService.getPage(cookie, requestClientOption); } catch (ApiException e) { - throw wrapApiException(e, GraduateRpcException.SubModuleCode.SCORE); + throw wrapApiException(e); } catch (IOException e) { - throw new GraduateRpcException( - GraduateRpcException.TypeCode.NETWORK_EXCEPTION, - GraduateRpcException.SubModuleCode.SCORE, - GraduateRpcException.ErrorCode.NETWORK_ERROR - ); + throw new RpcException(ServiceCode.NetworkError); } } @@ -47,11 +43,7 @@ public class ScoreService extends BaseService { try { return scoreParser.parse(data); } catch (Exception e) { - throw new GraduateRpcException( - GraduateRpcException.TypeCode.PARSE_EXCEPTION, - GraduateRpcException.SubModuleCode.SCORE, - GraduateRpcException.ErrorCode.PARSE_ERROR - ); + throw new RpcException(ServiceCode.ParseError); } } } diff --git a/sub-services/graduate/src/main/java/wusthelper/internal/graduate/services/StudentInfoService.java b/sub-services/graduate/src/main/java/wusthelper/internal/graduate/services/StudentInfoService.java index 0855918..50bf6cf 100644 --- a/sub-services/graduate/src/main/java/wusthelper/internal/graduate/services/StudentInfoService.java +++ b/sub-services/graduate/src/main/java/wusthelper/internal/graduate/services/StudentInfoService.java @@ -5,9 +5,9 @@ import cn.wustlinghang.mywust.core.request.service.graduate.GraduateStudentInfoA import cn.wustlinghang.mywust.data.global.StudentInfo; import cn.wustlinghang.mywust.exception.ApiException; import cn.wustlinghang.mywust.network.RequestClientOption; -import wusthelper.internal.rpc.exception.GraduateRpcException; -import wusthelper.internal.rpc.exception.RpcException; import jakarta.enterprise.context.ApplicationScoped; +import wusthelper.code.ServiceCode; +import wusthelper.internal.rpc.exception.RpcException; import java.io.IOException; @@ -23,7 +23,7 @@ public class StudentInfoService extends BaseService { GraduateStudentInfoApiService studentInfoApiService, GraduateStudentInfoPageParser studentInfoPageParser) { this.requestClientOption = requestClientOption; - + this.studentInfoApiService = studentInfoApiService; this.studentInfoPageParser = studentInfoPageParser; } @@ -32,13 +32,9 @@ public class StudentInfoService extends BaseService { try { return studentInfoApiService.getPage(cookie, requestClientOption); } catch (ApiException e) { - throw wrapApiException(e, GraduateRpcException.SubModuleCode.STUDENT_INFO); + throw wrapApiException(e); } catch (IOException e) { - throw new GraduateRpcException( - GraduateRpcException.TypeCode.NETWORK_EXCEPTION, - GraduateRpcException.SubModuleCode.STUDENT_INFO, - GraduateRpcException.ErrorCode.NETWORK_ERROR - ); + throw new RpcException(ServiceCode.NetworkError); } } @@ -46,11 +42,7 @@ public class StudentInfoService extends BaseService { try { return studentInfoPageParser.parse(data); } catch (Exception e) { - throw new GraduateRpcException( - GraduateRpcException.TypeCode.PARSE_EXCEPTION, - GraduateRpcException.SubModuleCode.STUDENT_INFO, - GraduateRpcException.ErrorCode.PARSE_ERROR - ); + throw new RpcException(ServiceCode.ParseError); } } } diff --git a/sub-services/graduate/src/main/java/wusthelper/internal/graduate/services/TrainingPlanService.java b/sub-services/graduate/src/main/java/wusthelper/internal/graduate/services/TrainingPlanService.java index accc2fc..dde6391 100644 --- a/sub-services/graduate/src/main/java/wusthelper/internal/graduate/services/TrainingPlanService.java +++ b/sub-services/graduate/src/main/java/wusthelper/internal/graduate/services/TrainingPlanService.java @@ -4,9 +4,9 @@ import cn.wustlinghang.mywust.core.parser.graduate.GraduateTrainingPlanPageParse import cn.wustlinghang.mywust.core.request.service.graduate.GraduateTrainingPlanApiService; import cn.wustlinghang.mywust.exception.ApiException; import cn.wustlinghang.mywust.network.RequestClientOption; -import wusthelper.internal.rpc.exception.GraduateRpcException; -import wusthelper.internal.rpc.exception.RpcException; import jakarta.enterprise.context.ApplicationScoped; +import wusthelper.code.ServiceCode; +import wusthelper.internal.rpc.exception.RpcException; import java.io.IOException; @@ -31,13 +31,9 @@ public class TrainingPlanService extends BaseService { try { return trainingPlanApiService.getPage(cookie, requestClientOption); } catch (ApiException e) { - throw wrapApiException(e, GraduateRpcException.SubModuleCode.TRAINING_PLAN); + throw wrapApiException(e); } catch (IOException e) { - throw new GraduateRpcException( - GraduateRpcException.TypeCode.NETWORK_EXCEPTION, - GraduateRpcException.SubModuleCode.TRAINING_PLAN, - GraduateRpcException.ErrorCode.NETWORK_ERROR - ); + throw new RpcException(ServiceCode.NetworkError); } } @@ -45,11 +41,7 @@ public class TrainingPlanService extends BaseService { try { return trainingPlanPageParser.parse(data); } catch (Exception e) { - throw new GraduateRpcException( - GraduateRpcException.TypeCode.PARSE_EXCEPTION, - GraduateRpcException.SubModuleCode.TRAINING_PLAN, - GraduateRpcException.ErrorCode.PARSE_ERROR - ); + throw new RpcException(ServiceCode.ParseError); } } } diff --git a/sub-services/library/src/main/java/wusthelper/internal/library/api/http/v1/handler/BaseExceptionHandler.java b/sub-services/library/src/main/java/wusthelper/internal/library/api/http/v1/handler/BaseExceptionHandler.java index 2f104cd..c8992d1 100644 --- a/sub-services/library/src/main/java/wusthelper/internal/library/api/http/v1/handler/BaseExceptionHandler.java +++ b/sub-services/library/src/main/java/wusthelper/internal/library/api/http/v1/handler/BaseExceptionHandler.java @@ -1,35 +1,31 @@ package wusthelper.internal.library.api.http.v1.handler; -import wusthelper.internal.rpc.response.RpcCommonResponseCode; -import wusthelper.internal.rpc.response.RpcResponseDto; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.Response; +import wusthelper.code.ServiceCode; +import wusthelper.internal.rpc.response.RpcResponseDto; public abstract class BaseExceptionHandler { public static final String EXCEPTION_HEADER_KEY = "X-exception-handler"; private static final ObjectMapper objectMapper = new ObjectMapper(); - public Response toResponse(RpcCommonResponseCode code, String msg, String handlerName) { - return toResponse(code.getCode(), msg, handlerName); - } - - public Response toResponse(int code, String msg, String handlerName) { - return toResponse(Response.Status.OK.getStatusCode(), code, msg, handlerName); + public Response toResponse(int code, String handlerName) { + return toResponse(Response.Status.OK, code, handlerName); } - public Response toResponse(int status, int code, String msg, String handlerName) { - return toResponse(Response.Status.fromStatusCode(status), code, msg, handlerName); + public Response toResponse(int status, int code, String handlerName) { + return toResponse(Response.Status.fromStatusCode(status), code, handlerName); } - public Response toResponse(Response.Status status, int code, String msg, String handlerName) { + public Response toResponse(Response.Status status, int code, String handlerName) { Object response; try { - response = objectMapper.writeValueAsString(RpcResponseDto.error(code, msg)); + response = objectMapper.writeValueAsString(RpcResponseDto.error(code)); } catch (JsonProcessingException e) { - response = RpcResponseDto.error(RpcCommonResponseCode.ServerInternalError); + response = RpcResponseDto.error(ServiceCode.ServerInternalError); } return Response.status(status) diff --git a/sub-services/library/src/main/java/wusthelper/internal/library/api/http/v1/handler/DefaultExceptionHandler.java b/sub-services/library/src/main/java/wusthelper/internal/library/api/http/v1/handler/DefaultExceptionHandler.java index 800acb0..a135882 100644 --- a/sub-services/library/src/main/java/wusthelper/internal/library/api/http/v1/handler/DefaultExceptionHandler.java +++ b/sub-services/library/src/main/java/wusthelper/internal/library/api/http/v1/handler/DefaultExceptionHandler.java @@ -1,21 +1,19 @@ package wusthelper.internal.library.api.http.v1.handler; -import wusthelper.internal.rpc.response.RpcCommonResponseCode; +import jakarta.enterprise.context.ApplicationScoped; import jakarta.ws.rs.core.Response; import jakarta.ws.rs.ext.ExceptionMapper; import jakarta.ws.rs.ext.Provider; import lombok.extern.slf4j.Slf4j; +import wusthelper.code.ServiceCode; @Slf4j @Provider +@ApplicationScoped public class DefaultExceptionHandler extends BaseExceptionHandler implements ExceptionMapper { @Override public Response toResponse(Exception e) { log.error("未知异常:", e); - return super.toResponse( - RpcCommonResponseCode.ServerInternalError, - e.toString(), - "DefaultExceptionHandler" - ); + return super.toResponse(ServiceCode.ServerInternalError, "DefaultExceptionHandler"); } } diff --git a/sub-services/library/src/main/java/wusthelper/internal/library/api/http/v1/handler/IOExceptionHandler.java b/sub-services/library/src/main/java/wusthelper/internal/library/api/http/v1/handler/IOExceptionHandler.java index 732c50c..5d03959 100644 --- a/sub-services/library/src/main/java/wusthelper/internal/library/api/http/v1/handler/IOExceptionHandler.java +++ b/sub-services/library/src/main/java/wusthelper/internal/library/api/http/v1/handler/IOExceptionHandler.java @@ -1,23 +1,21 @@ package wusthelper.internal.library.api.http.v1.handler; -import wusthelper.internal.rpc.response.RpcCommonResponseCode; +import jakarta.enterprise.context.ApplicationScoped; import jakarta.ws.rs.core.Response; import jakarta.ws.rs.ext.ExceptionMapper; import jakarta.ws.rs.ext.Provider; import lombok.extern.slf4j.Slf4j; +import wusthelper.code.ServiceCode; import java.io.IOException; @Slf4j @Provider +@ApplicationScoped public class IOExceptionHandler extends BaseExceptionHandler implements ExceptionMapper { @Override public Response toResponse(IOException e) { log.error("IO异常:", e); - return super.toResponse( - RpcCommonResponseCode.ServerInternalError, - e.toString(), - "IOExceptionHandler" - ); + return super.toResponse(ServiceCode.NetworkError, "IOExceptionHandler"); } } diff --git a/sub-services/library/src/main/java/wusthelper/internal/library/api/http/v1/handler/ParseExceptionHandler.java b/sub-services/library/src/main/java/wusthelper/internal/library/api/http/v1/handler/ParseExceptionHandler.java index 31e9433..0e0f75a 100644 --- a/sub-services/library/src/main/java/wusthelper/internal/library/api/http/v1/handler/ParseExceptionHandler.java +++ b/sub-services/library/src/main/java/wusthelper/internal/library/api/http/v1/handler/ParseExceptionHandler.java @@ -1,22 +1,20 @@ package wusthelper.internal.library.api.http.v1.handler; import cn.wustlinghang.mywust.exception.ParseException; -import wusthelper.internal.rpc.response.RpcCommonResponseCode; +import jakarta.enterprise.context.ApplicationScoped; import jakarta.ws.rs.core.Response; import jakarta.ws.rs.ext.ExceptionMapper; import jakarta.ws.rs.ext.Provider; import lombok.extern.slf4j.Slf4j; +import wusthelper.code.ServiceCode; @Slf4j @Provider +@ApplicationScoped public class ParseExceptionHandler extends BaseExceptionHandler implements ExceptionMapper { @Override public Response toResponse(ParseException e) { log.error("解析异常:", e); - return super.toResponse( - RpcCommonResponseCode.ServerInternalError, - e.toString(), - "ParseExceptionHandler" - ); + return super.toResponse(ServiceCode.ParseError, "ParseExceptionHandler"); } } diff --git a/sub-services/library/src/main/java/wusthelper/internal/library/api/http/v1/handler/RpcExceptionHandler.java b/sub-services/library/src/main/java/wusthelper/internal/library/api/http/v1/handler/RpcExceptionHandler.java index ba0c70a..4ba9377 100644 --- a/sub-services/library/src/main/java/wusthelper/internal/library/api/http/v1/handler/RpcExceptionHandler.java +++ b/sub-services/library/src/main/java/wusthelper/internal/library/api/http/v1/handler/RpcExceptionHandler.java @@ -1,17 +1,19 @@ package wusthelper.internal.library.api.http.v1.handler; -import wusthelper.internal.rpc.exception.RpcException; +import jakarta.enterprise.context.ApplicationScoped; import jakarta.ws.rs.core.Response; import jakarta.ws.rs.ext.ExceptionMapper; import jakarta.ws.rs.ext.Provider; +import lombok.extern.slf4j.Slf4j; +import wusthelper.internal.rpc.exception.RpcException; +@Slf4j @Provider +@ApplicationScoped public class RpcExceptionHandler extends BaseExceptionHandler implements ExceptionMapper { @Override public Response toResponse(RpcException e) { - return super.toResponse( - e.getCode(), - e.toString(), - "RpcExceptionHandler"); + log.debug("RpcException:", e); + return super.toResponse(e.getCode(), "RpcExceptionHandler"); } } diff --git a/sub-services/library/src/main/java/wusthelper/internal/library/api/http/v1/handler/ValidationExceptionHandler.java b/sub-services/library/src/main/java/wusthelper/internal/library/api/http/v1/handler/ValidationExceptionHandler.java index e73b3cf..5e8b64c 100644 --- a/sub-services/library/src/main/java/wusthelper/internal/library/api/http/v1/handler/ValidationExceptionHandler.java +++ b/sub-services/library/src/main/java/wusthelper/internal/library/api/http/v1/handler/ValidationExceptionHandler.java @@ -1,22 +1,20 @@ package wusthelper.internal.library.api.http.v1.handler; -import wusthelper.internal.rpc.response.RpcCommonResponseCode; +import jakarta.enterprise.context.ApplicationScoped; import jakarta.validation.ValidationException; import jakarta.ws.rs.core.Response; import jakarta.ws.rs.ext.ExceptionMapper; import jakarta.ws.rs.ext.Provider; import lombok.extern.slf4j.Slf4j; +import wusthelper.code.ServiceCode; @Slf4j @Provider +@ApplicationScoped public class ValidationExceptionHandler extends BaseExceptionHandler implements ExceptionMapper { @Override public Response toResponse(ValidationException e) { - return super.toResponse( - RpcCommonResponseCode.ParamWrong, - "参数错误:" + e.toString(), - "ValidationExceptionHandler" - ); + return super.toResponse(ServiceCode.ParamWrong, "ValidationExceptionHandler"); } } diff --git a/sub-services/library/src/main/java/wusthelper/internal/library/api/http/v1/handler/WebApplicationExceptionHandler.java b/sub-services/library/src/main/java/wusthelper/internal/library/api/http/v1/handler/WebApplicationExceptionHandler.java index f25d346..1d2d202 100644 --- a/sub-services/library/src/main/java/wusthelper/internal/library/api/http/v1/handler/WebApplicationExceptionHandler.java +++ b/sub-services/library/src/main/java/wusthelper/internal/library/api/http/v1/handler/WebApplicationExceptionHandler.java @@ -1,26 +1,17 @@ package wusthelper.internal.library.api.http.v1.handler; -import wusthelper.internal.rpc.exception.LibraryRpcException; -import wusthelper.internal.rpc.exception.RpcException; +import jakarta.enterprise.context.ApplicationScoped; import jakarta.ws.rs.WebApplicationException; import jakarta.ws.rs.core.Response; import jakarta.ws.rs.ext.ExceptionMapper; import jakarta.ws.rs.ext.Provider; +import wusthelper.code.ServiceCode; @Provider +@ApplicationScoped public class WebApplicationExceptionHandler extends BaseExceptionHandler implements ExceptionMapper { @Override public Response toResponse(WebApplicationException e) { - return super.toResponse( - e.getResponse().getStatus(), - RpcException.toIntCode( - LibraryRpcException.LIBRARY_MODULE, - LibraryRpcException.SubModuleCode.COMMON.ordinal(), - LibraryRpcException.TypeCode.OTHER_EXCEPTION.ordinal() * 100 - + LibraryRpcException.ErrorCode.REQUEST_INVALID.ordinal() - ), - e.getMessage(), - "WebApplicationExceptionHandler" - ); + return super.toResponse(ServiceCode.RequestInvalid, "WebApplicationExceptionHandler"); } } diff --git a/sub-services/library/src/main/java/wusthelper/internal/library/services/BaseService.java b/sub-services/library/src/main/java/wusthelper/internal/library/services/BaseService.java index 5912fd5..ab0d8d9 100644 --- a/sub-services/library/src/main/java/wusthelper/internal/library/services/BaseService.java +++ b/sub-services/library/src/main/java/wusthelper/internal/library/services/BaseService.java @@ -1,31 +1,20 @@ package wusthelper.internal.library.services; import cn.wustlinghang.mywust.exception.ApiException; -import wusthelper.internal.rpc.exception.LibraryRpcException; +import wusthelper.code.ServiceCode; import lombok.extern.slf4j.Slf4j; +import wusthelper.internal.rpc.exception.RpcException; @Slf4j public abstract class BaseService { - protected LibraryRpcException wrapApiException(ApiException e, LibraryRpcException.SubModuleCode subModuleCode) { + protected RpcException wrapApiException(ApiException e) { return switch (e.getCode()) { - case NETWORK_EXCEPTION -> new LibraryRpcException( - LibraryRpcException.TypeCode.NETWORK_EXCEPTION, - subModuleCode, - LibraryRpcException.ErrorCode.NETWORK_ERROR - ); - case COOKIE_INVALID -> new LibraryRpcException( - LibraryRpcException.TypeCode.AUTH_EXCEPTION, - subModuleCode, - LibraryRpcException.ErrorCode.COOKIE_INVALID - ); + case NETWORK_EXCEPTION -> new RpcException(ServiceCode.NetworkError); + case COOKIE_INVALID -> new RpcException(ServiceCode.CookieInvalid); default -> { - log.error("图书馆:{}代理请求异常,异常未处理", subModuleCode.name()); + log.error("图书馆:代理请求异常,异常未处理"); log.error("异常:", e); - yield new LibraryRpcException( - LibraryRpcException.TypeCode.AUTH_EXCEPTION, - subModuleCode, - LibraryRpcException.ErrorCode.COOKIE_INVALID - ); + yield new RpcException(ServiceCode.Unknown); } }; } diff --git a/sub-services/library/src/main/java/wusthelper/internal/library/services/BookCoverImageUrlService.java b/sub-services/library/src/main/java/wusthelper/internal/library/services/BookCoverImageUrlService.java index be6ba27..2d0f0bf 100644 --- a/sub-services/library/src/main/java/wusthelper/internal/library/services/BookCoverImageUrlService.java +++ b/sub-services/library/src/main/java/wusthelper/internal/library/services/BookCoverImageUrlService.java @@ -2,9 +2,9 @@ package wusthelper.internal.library.services; import cn.wustlinghang.mywust.core.request.service.library.BookCoverImageUrlApiService; import cn.wustlinghang.mywust.exception.ApiException; -import wusthelper.internal.rpc.exception.LibraryRpcException; -import wusthelper.internal.rpc.exception.RpcException; import jakarta.enterprise.context.ApplicationScoped; +import wusthelper.code.ServiceCode; +import wusthelper.internal.rpc.exception.RpcException; import java.io.IOException; import java.util.List; @@ -22,13 +22,9 @@ public class BookCoverImageUrlService extends BaseService { try { return api.getBookCoverImageUrl(isbn); } catch (ApiException e) { - throw wrapApiException(e, LibraryRpcException.SubModuleCode.COVER_IMAGE); + throw wrapApiException(e); } catch (IOException e) { - throw new LibraryRpcException( - LibraryRpcException.TypeCode.NETWORK_EXCEPTION, - LibraryRpcException.SubModuleCode.COVER_IMAGE, - LibraryRpcException.ErrorCode.NETWORK_ERROR - ); + throw new RpcException(ServiceCode.NetworkError); } } @@ -36,13 +32,9 @@ public class BookCoverImageUrlService extends BaseService { try { return api.getBookCoverImageUrl(isbnList); } catch (ApiException e) { - throw wrapApiException(e, LibraryRpcException.SubModuleCode.COVER_IMAGE); + throw wrapApiException(e); } catch (IOException e) { - throw new LibraryRpcException( - LibraryRpcException.TypeCode.NETWORK_EXCEPTION, - LibraryRpcException.SubModuleCode.COVER_IMAGE, - LibraryRpcException.ErrorCode.NETWORK_ERROR - ); + throw new RpcException(ServiceCode.NetworkError); } } diff --git a/sub-services/library/src/main/java/wusthelper/internal/library/services/BookDetailService.java b/sub-services/library/src/main/java/wusthelper/internal/library/services/BookDetailService.java index cbe271b..b7a8c50 100644 --- a/sub-services/library/src/main/java/wusthelper/internal/library/services/BookDetailService.java +++ b/sub-services/library/src/main/java/wusthelper/internal/library/services/BookDetailService.java @@ -3,9 +3,9 @@ package wusthelper.internal.library.services; import cn.wustlinghang.mywust.core.request.service.library.BookDetailApiService; import cn.wustlinghang.mywust.data.library.parsed.BookDetail; import cn.wustlinghang.mywust.exception.ApiException; -import wusthelper.internal.rpc.exception.LibraryRpcException; -import wusthelper.internal.rpc.exception.RpcException; import jakarta.enterprise.context.ApplicationScoped; +import wusthelper.code.ServiceCode; +import wusthelper.internal.rpc.exception.RpcException; import java.io.IOException; @@ -21,13 +21,9 @@ public class BookDetailService extends BaseService { try { return api.getBookDetail(bookId); } catch (ApiException e) { - throw wrapApiException(e, LibraryRpcException.SubModuleCode.BOOK_DETAIL); + throw wrapApiException(e); } catch (IOException e) { - throw new LibraryRpcException( - LibraryRpcException.TypeCode.NETWORK_EXCEPTION, - LibraryRpcException.SubModuleCode.BOOK_DETAIL, - LibraryRpcException.ErrorCode.NETWORK_ERROR - ); + throw new RpcException(ServiceCode.NetworkError); } } diff --git a/sub-services/library/src/main/java/wusthelper/internal/library/services/BookHoldingService.java b/sub-services/library/src/main/java/wusthelper/internal/library/services/BookHoldingService.java index 6e65ff1..28adcc6 100644 --- a/sub-services/library/src/main/java/wusthelper/internal/library/services/BookHoldingService.java +++ b/sub-services/library/src/main/java/wusthelper/internal/library/services/BookHoldingService.java @@ -4,7 +4,7 @@ import cn.wustlinghang.mywust.core.request.service.library.BookHoldingApiService import cn.wustlinghang.mywust.data.library.parsed.BookHolding; import cn.wustlinghang.mywust.exception.ApiException; import cn.wustlinghang.mywust.exception.ParseException; -import wusthelper.internal.rpc.exception.LibraryRpcException; +import wusthelper.code.ServiceCode; import wusthelper.internal.rpc.exception.RpcException; import jakarta.enterprise.context.ApplicationScoped; @@ -24,19 +24,11 @@ public class BookHoldingService extends BaseService { try { return api.getHoldingList(id); } catch (ApiException e) { - throw wrapApiException(e, LibraryRpcException.SubModuleCode.BOOK_HOLDING); + throw wrapApiException(e); } catch (IOException e) { - throw new LibraryRpcException( - LibraryRpcException.TypeCode.NETWORK_EXCEPTION, - LibraryRpcException.SubModuleCode.BOOK_HOLDING, - LibraryRpcException.ErrorCode.NETWORK_ERROR - ); + throw new RpcException(ServiceCode.NetworkError); } catch (ParseException e) { - throw new LibraryRpcException( - LibraryRpcException.TypeCode.PARSE_EXCEPTION, - LibraryRpcException.SubModuleCode.BOOK_HOLDING, - LibraryRpcException.ErrorCode.PARSE_ERROR - ); + throw new RpcException(ServiceCode.ParseError); } } } diff --git a/sub-services/library/src/main/java/wusthelper/internal/library/services/CurrentLoanService.java b/sub-services/library/src/main/java/wusthelper/internal/library/services/CurrentLoanService.java index 216430a..ed2492a 100644 --- a/sub-services/library/src/main/java/wusthelper/internal/library/services/CurrentLoanService.java +++ b/sub-services/library/src/main/java/wusthelper/internal/library/services/CurrentLoanService.java @@ -5,9 +5,9 @@ import cn.wustlinghang.mywust.data.library.PagingResult; import cn.wustlinghang.mywust.data.library.origin.CurrentLoanBook; import cn.wustlinghang.mywust.exception.ApiException; import cn.wustlinghang.mywust.exception.ParseException; -import wusthelper.internal.rpc.exception.LibraryRpcException; -import wusthelper.internal.rpc.exception.RpcException; import jakarta.enterprise.context.ApplicationScoped; +import wusthelper.code.ServiceCode; +import wusthelper.internal.rpc.exception.RpcException; import java.io.IOException; import java.util.List; @@ -25,19 +25,11 @@ public class CurrentLoanService extends BaseService { try { return api.getCurrentLoan(cookie, page, pageSize); } catch (ApiException e) { - throw wrapApiException(e, LibraryRpcException.SubModuleCode.CURRENT_LOAN); + throw wrapApiException(e); } catch (IOException e) { - throw new LibraryRpcException( - LibraryRpcException.TypeCode.NETWORK_EXCEPTION, - LibraryRpcException.SubModuleCode.CURRENT_LOAN, - LibraryRpcException.ErrorCode.NETWORK_ERROR - ); + throw new RpcException(ServiceCode.NetworkError); } catch (ParseException e) { - throw new LibraryRpcException( - LibraryRpcException.TypeCode.PARSE_EXCEPTION, - LibraryRpcException.SubModuleCode.CURRENT_LOAN, - LibraryRpcException.ErrorCode.PARSE_ERROR - ); + throw new RpcException(ServiceCode.ParseError); } } } diff --git a/sub-services/library/src/main/java/wusthelper/internal/library/services/LoanHistoryService.java b/sub-services/library/src/main/java/wusthelper/internal/library/services/LoanHistoryService.java index 7c274a2..e5e8d09 100644 --- a/sub-services/library/src/main/java/wusthelper/internal/library/services/LoanHistoryService.java +++ b/sub-services/library/src/main/java/wusthelper/internal/library/services/LoanHistoryService.java @@ -5,10 +5,10 @@ import cn.wustlinghang.mywust.data.library.PagingResult; import cn.wustlinghang.mywust.data.library.origin.HistoryLoanBook; import cn.wustlinghang.mywust.exception.ApiException; import cn.wustlinghang.mywust.exception.ParseException; -import wusthelper.internal.rpc.exception.LibraryRpcException; -import wusthelper.internal.rpc.exception.RpcException; import jakarta.enterprise.context.ApplicationScoped; import lombok.extern.slf4j.Slf4j; +import wusthelper.code.ServiceCode; +import wusthelper.internal.rpc.exception.RpcException; import java.io.IOException; import java.util.List; @@ -27,20 +27,12 @@ public class LoanHistoryService extends BaseService { try { return api.getLoanHistory(cookie, page, pageSize); } catch (ApiException e) { - throw wrapApiException(e, LibraryRpcException.SubModuleCode.LOAN_HISTORY); + throw wrapApiException(e); } catch (IOException e) { - throw new LibraryRpcException( - LibraryRpcException.TypeCode.NETWORK_EXCEPTION, - LibraryRpcException.SubModuleCode.LOAN_HISTORY, - LibraryRpcException.ErrorCode.NETWORK_ERROR - ); + throw new RpcException(ServiceCode.NetworkError); } catch (ParseException e) { log.error("解析错误:", e); - throw new LibraryRpcException( - LibraryRpcException.TypeCode.PARSE_EXCEPTION, - LibraryRpcException.SubModuleCode.LOAN_HISTORY, - LibraryRpcException.ErrorCode.PARSE_ERROR - ); + throw new RpcException(ServiceCode.ParseError); } } } diff --git a/sub-services/library/src/main/java/wusthelper/internal/library/services/LoginService.java b/sub-services/library/src/main/java/wusthelper/internal/library/services/LoginService.java index 1f12caf..17a8615 100644 --- a/sub-services/library/src/main/java/wusthelper/internal/library/services/LoginService.java +++ b/sub-services/library/src/main/java/wusthelper/internal/library/services/LoginService.java @@ -3,9 +3,10 @@ package wusthelper.internal.library.services; import cn.wustlinghang.mywust.core.request.service.auth.LibraryLogin; import cn.wustlinghang.mywust.exception.ApiException; import cn.wustlinghang.mywust.network.RequestClientOption; -import wusthelper.internal.rpc.exception.LibraryRpcException; import jakarta.enterprise.context.ApplicationScoped; import lombok.extern.slf4j.Slf4j; +import wusthelper.code.ServiceCode; +import wusthelper.internal.rpc.exception.RpcException; import java.io.IOException; @@ -21,53 +22,41 @@ public class LoginService { this.libraryLogin = libraryLogin; } - public String login(String username, String password) throws LibraryRpcException { + public String login(String username, String password) throws RpcException { try { return libraryLogin.getLoginCookie(username, password, option); } catch (ApiException e) { throw wrapApiException(e); } catch (IOException e) { - throw new LibraryRpcException( - LibraryRpcException.TypeCode.NETWORK_EXCEPTION, - LibraryRpcException.SubModuleCode.AUTH, - LibraryRpcException.ErrorCode.NETWORK_ERROR - ); + throw new RpcException(ServiceCode.NetworkError); } } - public boolean verify(String cookie) throws LibraryRpcException { + public boolean verify(String cookie) throws RpcException { try { return libraryLogin.checkCookie(cookie, option); } catch (IOException e) { - throw new LibraryRpcException( - LibraryRpcException.TypeCode.NETWORK_EXCEPTION, - LibraryRpcException.SubModuleCode.AUTH, - LibraryRpcException.ErrorCode.NETWORK_ERROR - ); + throw new RpcException(ServiceCode.NetworkError); } } - private LibraryRpcException wrapApiException(ApiException e) { - LibraryRpcException.ErrorCode errorCode = switch (e.getCode()) { - case NETWORK_EXCEPTION -> LibraryRpcException.ErrorCode.NETWORK_ERROR; - case UNI_LOGIN_PASSWORD_WRONG -> LibraryRpcException.ErrorCode.AUTH_PASSWORD_WRONG; - case UNI_LOGIN_USER_NOT_EXISTS -> LibraryRpcException.ErrorCode.AUTH_USER_NOT_EXISTS; - case UNI_LOGIN_USER_BANNED -> LibraryRpcException.ErrorCode.AUTH_USER_BANNED; - case UNI_LOGIN_USER_DISABLED -> LibraryRpcException.ErrorCode.AUTH_USER_DISABLED; - case UNI_LOGIN_NEED_CHANGE_PASSWORD -> LibraryRpcException.ErrorCode.AUTH_NEED_CHANGE_PASSWORD; - case UNI_LOGIN_USER_NOT_ONLY, UNI_LOGIN_NEED_TFA, UNI_LOGIN_NO_REGISTER - -> LibraryRpcException.ErrorCode.AUTH_UNKNOWN_ERROR; + private RpcException wrapApiException(ApiException e) { + int errorCode = switch (e.getCode()) { + case NETWORK_EXCEPTION -> ServiceCode.NetworkError; + case UNI_LOGIN_PASSWORD_WRONG -> ServiceCode.UndergradPasswordWrong; + case UNI_LOGIN_USER_NOT_EXISTS -> ServiceCode.UndergradUserNotExists; + case UNI_LOGIN_USER_BANNED -> ServiceCode.UndergradUserBanned; + case UNI_LOGIN_USER_DISABLED -> ServiceCode.UndergradUserDisabled; + case UNI_LOGIN_NEED_CHANGE_PASSWORD -> ServiceCode.UndergradPasswordNeedModify; + case UNDERGRAD_BANNED_IN_EXCLUSIVE_TIME -> ServiceCode.UndergradBannedInExclusiveTime; + case UNI_LOGIN_USER_NOT_ONLY, UNI_LOGIN_NO_REGISTER, UNI_LOGIN_NEED_TFA -> ServiceCode.Unknown; default -> { log.error("图书馆:登录代理请求异常,异常未处理"); log.error("异常:", e); - yield LibraryRpcException.ErrorCode.AUTH_UNKNOWN_ERROR; + yield ServiceCode.Unknown; } }; - return new LibraryRpcException( - LibraryRpcException.TypeCode.AUTH_EXCEPTION, - LibraryRpcException.SubModuleCode.AUTH, - errorCode - ); + return new RpcException(errorCode); } } diff --git a/sub-services/library/src/main/java/wusthelper/internal/library/services/OverdueSoonService.java b/sub-services/library/src/main/java/wusthelper/internal/library/services/OverdueSoonService.java index 3b8a9bd..488fb08 100644 --- a/sub-services/library/src/main/java/wusthelper/internal/library/services/OverdueSoonService.java +++ b/sub-services/library/src/main/java/wusthelper/internal/library/services/OverdueSoonService.java @@ -5,9 +5,9 @@ import cn.wustlinghang.mywust.data.library.PagingResult; import cn.wustlinghang.mywust.data.library.origin.BaseLoanBook; import cn.wustlinghang.mywust.exception.ApiException; import cn.wustlinghang.mywust.exception.ParseException; -import wusthelper.internal.rpc.exception.LibraryRpcException; -import wusthelper.internal.rpc.exception.RpcException; import jakarta.enterprise.context.ApplicationScoped; +import wusthelper.code.ServiceCode; +import wusthelper.internal.rpc.exception.RpcException; import java.io.IOException; import java.util.List; @@ -25,19 +25,11 @@ public class OverdueSoonService extends BaseService { try { return api.getOverdueSoon(cookie, page, pageSize); } catch (ApiException e) { - throw wrapApiException(e, LibraryRpcException.SubModuleCode.OVERDUE_SOON); + throw wrapApiException(e); } catch (IOException e) { - throw new LibraryRpcException( - LibraryRpcException.TypeCode.NETWORK_EXCEPTION, - LibraryRpcException.SubModuleCode.OVERDUE_SOON, - LibraryRpcException.ErrorCode.NETWORK_ERROR - ); + throw new RpcException(ServiceCode.NetworkError); } catch (ParseException e) { - throw new LibraryRpcException( - LibraryRpcException.TypeCode.PARSE_EXCEPTION, - LibraryRpcException.SubModuleCode.OVERDUE_SOON, - LibraryRpcException.ErrorCode.PARSE_ERROR - ); + throw new RpcException(ServiceCode.ParseError); } } } diff --git a/sub-services/library/src/main/java/wusthelper/internal/library/services/SearchService.java b/sub-services/library/src/main/java/wusthelper/internal/library/services/SearchService.java index a24d2cc..912af5a 100644 --- a/sub-services/library/src/main/java/wusthelper/internal/library/services/SearchService.java +++ b/sub-services/library/src/main/java/wusthelper/internal/library/services/SearchService.java @@ -5,9 +5,9 @@ import cn.wustlinghang.mywust.data.library.PagingResult; import cn.wustlinghang.mywust.data.library.origin.BookSearchResult; import cn.wustlinghang.mywust.exception.ApiException; import cn.wustlinghang.mywust.exception.ParseException; -import wusthelper.internal.rpc.exception.LibraryRpcException; -import wusthelper.internal.rpc.exception.RpcException; import jakarta.enterprise.context.ApplicationScoped; +import wusthelper.code.ServiceCode; +import wusthelper.internal.rpc.exception.RpcException; import java.io.IOException; import java.util.List; @@ -25,19 +25,11 @@ public class SearchService extends BaseService { try { return api.search(keyword, page, pageSize); } catch (ApiException e) { - throw wrapApiException(e, LibraryRpcException.SubModuleCode.SEARCH); + throw wrapApiException(e); } catch (IOException e) { - throw new LibraryRpcException( - LibraryRpcException.TypeCode.NETWORK_EXCEPTION, - LibraryRpcException.SubModuleCode.SEARCH, - LibraryRpcException.ErrorCode.NETWORK_ERROR - ); + throw new RpcException(ServiceCode.NetworkError); } catch (ParseException e) { - throw new LibraryRpcException( - LibraryRpcException.TypeCode.PARSE_EXCEPTION, - LibraryRpcException.SubModuleCode.SEARCH, - LibraryRpcException.ErrorCode.PARSE_ERROR - ); + throw new RpcException(ServiceCode.ParseError); } } } \ No newline at end of file diff --git a/sub-services/physics/src/main/java/wusthelper/internal/physics/api/http/v1/handler/BaseExceptionHandler.java b/sub-services/physics/src/main/java/wusthelper/internal/physics/api/http/v1/handler/BaseExceptionHandler.java index 6354878..6895736 100644 --- a/sub-services/physics/src/main/java/wusthelper/internal/physics/api/http/v1/handler/BaseExceptionHandler.java +++ b/sub-services/physics/src/main/java/wusthelper/internal/physics/api/http/v1/handler/BaseExceptionHandler.java @@ -1,35 +1,31 @@ package wusthelper.internal.physics.api.http.v1.handler; -import wusthelper.internal.rpc.response.RpcCommonResponseCode; -import wusthelper.internal.rpc.response.RpcResponseDto; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.Response; +import wusthelper.code.ServiceCode; +import wusthelper.internal.rpc.response.RpcResponseDto; public abstract class BaseExceptionHandler { public static final String EXCEPTION_HEADER_KEY = "X-exception-handler"; private static final ObjectMapper objectMapper = new ObjectMapper(); - public Response toResponse(RpcCommonResponseCode code, String msg, String handlerName) { - return toResponse(code.getCode(), msg, handlerName); - } - - public Response toResponse(int code, String msg, String handlerName) { - return toResponse(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), code, msg, handlerName); + public Response toResponse(int code, String handlerName) { + return toResponse(Response.Status.OK, code, handlerName); } - public Response toResponse(int status, int code, String msg, String handlerName) { - return toResponse(Response.Status.fromStatusCode(status), code, msg, handlerName); + public Response toResponse(int status, int code, String handlerName) { + return toResponse(Response.Status.fromStatusCode(status), code, handlerName); } - public Response toResponse(Response.Status status, int code, String msg, String handlerName) { + public Response toResponse(Response.Status status, int code, String handlerName) { Object response; try { - response = objectMapper.writeValueAsString(RpcResponseDto.error(code, msg)); + response = objectMapper.writeValueAsString(RpcResponseDto.error(code)); } catch (JsonProcessingException e) { - response = RpcResponseDto.error(RpcCommonResponseCode.ServerInternalError); + response = RpcResponseDto.error(ServiceCode.ServerInternalError); } return Response.status(status) diff --git a/sub-services/physics/src/main/java/wusthelper/internal/physics/api/http/v1/handler/DefaultExceptionHandler.java b/sub-services/physics/src/main/java/wusthelper/internal/physics/api/http/v1/handler/DefaultExceptionHandler.java index 842c610..d169e7f 100644 --- a/sub-services/physics/src/main/java/wusthelper/internal/physics/api/http/v1/handler/DefaultExceptionHandler.java +++ b/sub-services/physics/src/main/java/wusthelper/internal/physics/api/http/v1/handler/DefaultExceptionHandler.java @@ -1,21 +1,19 @@ package wusthelper.internal.physics.api.http.v1.handler; -import wusthelper.internal.rpc.response.RpcCommonResponseCode; +import jakarta.enterprise.context.ApplicationScoped; import jakarta.ws.rs.core.Response; import jakarta.ws.rs.ext.ExceptionMapper; import jakarta.ws.rs.ext.Provider; import lombok.extern.slf4j.Slf4j; +import wusthelper.code.ServiceCode; @Slf4j @Provider +@ApplicationScoped public class DefaultExceptionHandler extends BaseExceptionHandler implements ExceptionMapper { @Override public Response toResponse(Exception e) { log.error("未知异常:", e); - return super.toResponse( - RpcCommonResponseCode.ServerInternalError, - e.toString(), - "DefaultExceptionHandler" - ); + return super.toResponse(ServiceCode.ServerInternalError, "DefaultExceptionHandler"); } } diff --git a/sub-services/physics/src/main/java/wusthelper/internal/physics/api/http/v1/handler/IOExceptionHandler.java b/sub-services/physics/src/main/java/wusthelper/internal/physics/api/http/v1/handler/IOExceptionHandler.java index 7200407..d280df5 100644 --- a/sub-services/physics/src/main/java/wusthelper/internal/physics/api/http/v1/handler/IOExceptionHandler.java +++ b/sub-services/physics/src/main/java/wusthelper/internal/physics/api/http/v1/handler/IOExceptionHandler.java @@ -1,23 +1,21 @@ package wusthelper.internal.physics.api.http.v1.handler; -import wusthelper.internal.rpc.response.RpcCommonResponseCode; +import jakarta.enterprise.context.ApplicationScoped; import jakarta.ws.rs.core.Response; import jakarta.ws.rs.ext.ExceptionMapper; import jakarta.ws.rs.ext.Provider; import lombok.extern.slf4j.Slf4j; +import wusthelper.code.ServiceCode; import java.io.IOException; @Slf4j @Provider +@ApplicationScoped public class IOExceptionHandler extends BaseExceptionHandler implements ExceptionMapper { @Override public Response toResponse(IOException e) { log.error("IO异常:", e); - return super.toResponse( - RpcCommonResponseCode.ServerInternalError, - e.toString(), - "IOExceptionHandler" - ); + return super.toResponse(ServiceCode.NetworkError, "IOExceptionHandler"); } } diff --git a/sub-services/physics/src/main/java/wusthelper/internal/physics/api/http/v1/handler/ParseExceptionHandler.java b/sub-services/physics/src/main/java/wusthelper/internal/physics/api/http/v1/handler/ParseExceptionHandler.java index f09688e..4e9d54b 100644 --- a/sub-services/physics/src/main/java/wusthelper/internal/physics/api/http/v1/handler/ParseExceptionHandler.java +++ b/sub-services/physics/src/main/java/wusthelper/internal/physics/api/http/v1/handler/ParseExceptionHandler.java @@ -1,22 +1,20 @@ package wusthelper.internal.physics.api.http.v1.handler; import cn.wustlinghang.mywust.exception.ParseException; -import wusthelper.internal.rpc.response.RpcCommonResponseCode; +import jakarta.enterprise.context.ApplicationScoped; import jakarta.ws.rs.core.Response; import jakarta.ws.rs.ext.ExceptionMapper; import jakarta.ws.rs.ext.Provider; import lombok.extern.slf4j.Slf4j; +import wusthelper.code.ServiceCode; @Slf4j @Provider +@ApplicationScoped public class ParseExceptionHandler extends BaseExceptionHandler implements ExceptionMapper { @Override public Response toResponse(ParseException e) { log.error("解析异常:", e); - return super.toResponse( - RpcCommonResponseCode.ServerInternalError, - e.toString(), - "ParseExceptionHandler" - ); + return super.toResponse(ServiceCode.ParseError, "ParseExceptionHandler"); } } diff --git a/sub-services/physics/src/main/java/wusthelper/internal/physics/api/http/v1/handler/RpcExceptionHandler.java b/sub-services/physics/src/main/java/wusthelper/internal/physics/api/http/v1/handler/RpcExceptionHandler.java index 2c91200..679995c 100644 --- a/sub-services/physics/src/main/java/wusthelper/internal/physics/api/http/v1/handler/RpcExceptionHandler.java +++ b/sub-services/physics/src/main/java/wusthelper/internal/physics/api/http/v1/handler/RpcExceptionHandler.java @@ -1,17 +1,19 @@ package wusthelper.internal.physics.api.http.v1.handler; -import wusthelper.internal.rpc.exception.RpcException; +import jakarta.enterprise.context.ApplicationScoped; import jakarta.ws.rs.core.Response; import jakarta.ws.rs.ext.ExceptionMapper; import jakarta.ws.rs.ext.Provider; +import lombok.extern.slf4j.Slf4j; +import wusthelper.internal.rpc.exception.RpcException; +@Slf4j @Provider +@ApplicationScoped public class RpcExceptionHandler extends BaseExceptionHandler implements ExceptionMapper { @Override public Response toResponse(RpcException e) { - return super.toResponse( - e.getCode(), - e.toString(), - "RpcExceptionHandler"); + log.debug("RpcException:", e); + return super.toResponse(e.getCode(), "RpcExceptionHandler"); } } diff --git a/sub-services/physics/src/main/java/wusthelper/internal/physics/api/http/v1/handler/ValidationExceptionHandler.java b/sub-services/physics/src/main/java/wusthelper/internal/physics/api/http/v1/handler/ValidationExceptionHandler.java index f032722..027b8bf 100644 --- a/sub-services/physics/src/main/java/wusthelper/internal/physics/api/http/v1/handler/ValidationExceptionHandler.java +++ b/sub-services/physics/src/main/java/wusthelper/internal/physics/api/http/v1/handler/ValidationExceptionHandler.java @@ -1,22 +1,20 @@ package wusthelper.internal.physics.api.http.v1.handler; -import wusthelper.internal.rpc.response.RpcCommonResponseCode; +import jakarta.enterprise.context.ApplicationScoped; import jakarta.validation.ValidationException; import jakarta.ws.rs.core.Response; import jakarta.ws.rs.ext.ExceptionMapper; import jakarta.ws.rs.ext.Provider; import lombok.extern.slf4j.Slf4j; +import wusthelper.code.ServiceCode; @Slf4j @Provider +@ApplicationScoped public class ValidationExceptionHandler extends BaseExceptionHandler implements ExceptionMapper { @Override public Response toResponse(ValidationException e) { - return super.toResponse( - RpcCommonResponseCode.ParamWrong, - "参数错误:" + e.toString(), - "ValidationExceptionHandler" - ); + return super.toResponse(ServiceCode.ParamWrong, "ValidationExceptionHandler"); } } diff --git a/sub-services/physics/src/main/java/wusthelper/internal/physics/api/http/v1/handler/WebApplicationExceptionHandler.java b/sub-services/physics/src/main/java/wusthelper/internal/physics/api/http/v1/handler/WebApplicationExceptionHandler.java index 871b60e..50b700c 100644 --- a/sub-services/physics/src/main/java/wusthelper/internal/physics/api/http/v1/handler/WebApplicationExceptionHandler.java +++ b/sub-services/physics/src/main/java/wusthelper/internal/physics/api/http/v1/handler/WebApplicationExceptionHandler.java @@ -1,26 +1,17 @@ package wusthelper.internal.physics.api.http.v1.handler; -import wusthelper.internal.rpc.exception.PhysicsRpcException; -import wusthelper.internal.rpc.exception.RpcException; +import jakarta.enterprise.context.ApplicationScoped; import jakarta.ws.rs.WebApplicationException; import jakarta.ws.rs.core.Response; import jakarta.ws.rs.ext.ExceptionMapper; import jakarta.ws.rs.ext.Provider; +import wusthelper.code.ServiceCode; @Provider +@ApplicationScoped public class WebApplicationExceptionHandler extends BaseExceptionHandler implements ExceptionMapper { @Override public Response toResponse(WebApplicationException e) { - return super.toResponse( - e.getResponse().getStatus(), - RpcException.toIntCode( - PhysicsRpcException.PHYSICS_MODULE, - PhysicsRpcException.SubModuleCode.COMMON.ordinal(), - PhysicsRpcException.TypeCode.OTHER_EXCEPTION.ordinal() * 100 - + PhysicsRpcException.ErrorCode.REQUEST_INVALID.ordinal() - ), - e.getMessage(), - "WebApplicationExceptionHandler" - ); + return super.toResponse(ServiceCode.RequestInvalid, "WebApplicationExceptionHandler"); } } diff --git a/sub-services/physics/src/main/java/wusthelper/internal/physics/services/BaseService.java b/sub-services/physics/src/main/java/wusthelper/internal/physics/services/BaseService.java index 614b592..9c81b18 100644 --- a/sub-services/physics/src/main/java/wusthelper/internal/physics/services/BaseService.java +++ b/sub-services/physics/src/main/java/wusthelper/internal/physics/services/BaseService.java @@ -1,31 +1,20 @@ package wusthelper.internal.physics.services; import cn.wustlinghang.mywust.exception.ApiException; -import wusthelper.internal.rpc.exception.PhysicsRpcException; import lombok.extern.slf4j.Slf4j; +import wusthelper.code.ServiceCode; +import wusthelper.internal.rpc.exception.RpcException; @Slf4j public abstract class BaseService { - protected PhysicsRpcException wrapApiException(ApiException e, PhysicsRpcException.SubModuleCode subModuleCode) { + protected RpcException wrapApiException(ApiException e) { return switch (e.getCode()) { - case NETWORK_EXCEPTION -> new PhysicsRpcException( - PhysicsRpcException.TypeCode.NETWORK_EXCEPTION, - subModuleCode, - PhysicsRpcException.ErrorCode.NETWORK_ERROR - ); - case COOKIE_INVALID -> new PhysicsRpcException( - PhysicsRpcException.TypeCode.AUTH_EXCEPTION, - subModuleCode, - PhysicsRpcException.ErrorCode.COOKIE_INVALID - ); + case NETWORK_EXCEPTION -> new RpcException(ServiceCode.NetworkError); + case COOKIE_INVALID -> new RpcException(ServiceCode.CookieInvalid); default -> { - log.error("物理实验:{}代理请求异常,异常未处理", subModuleCode.name()); + log.error("物理实验:代理请求异常,异常未处理"); log.error("异常:", e); - yield new PhysicsRpcException( - PhysicsRpcException.TypeCode.AUTH_EXCEPTION, - subModuleCode, - PhysicsRpcException.ErrorCode.COOKIE_INVALID - ); + yield new RpcException(ServiceCode.Unknown); } }; } diff --git a/sub-services/physics/src/main/java/wusthelper/internal/physics/services/CourseTableService.java b/sub-services/physics/src/main/java/wusthelper/internal/physics/services/CourseTableService.java index b561cdf..de7eac5 100644 --- a/sub-services/physics/src/main/java/wusthelper/internal/physics/services/CourseTableService.java +++ b/sub-services/physics/src/main/java/wusthelper/internal/physics/services/CourseTableService.java @@ -5,9 +5,9 @@ import cn.wustlinghang.mywust.core.request.service.physics.PhysicsCourseApiServi import cn.wustlinghang.mywust.data.physics.PhysicsCourse; import cn.wustlinghang.mywust.exception.ApiException; import cn.wustlinghang.mywust.network.RequestClientOption; -import wusthelper.internal.rpc.exception.PhysicsRpcException; -import wusthelper.internal.rpc.exception.RpcException; import jakarta.enterprise.context.ApplicationScoped; +import wusthelper.code.ServiceCode; +import wusthelper.internal.rpc.exception.RpcException; import java.io.IOException; import java.util.List; @@ -33,25 +33,17 @@ public class CourseTableService extends BaseService { try { return agent.getPage(cookie, requestClientOption); } catch (ApiException e) { - throw wrapApiException(e, PhysicsRpcException.SubModuleCode.COURSE_TABLE); + throw wrapApiException(e); } catch (IOException e) { - throw new PhysicsRpcException( - PhysicsRpcException.TypeCode.NETWORK_EXCEPTION, - PhysicsRpcException.SubModuleCode.COURSE_TABLE, - PhysicsRpcException.ErrorCode.NETWORK_ERROR - ); + throw new RpcException(ServiceCode.NetworkError); } } - public List parseCourseTable(String data) throws PhysicsRpcException { + public List parseCourseTable(String data) throws RpcException { try { return parser.parse(data); } catch (Exception e) { - throw new PhysicsRpcException( - PhysicsRpcException.TypeCode.PARSE_EXCEPTION, - PhysicsRpcException.SubModuleCode.COURSE_TABLE, - PhysicsRpcException.ErrorCode.PARSE_ERROR - ); + throw new RpcException(ServiceCode.ParseError); } } } diff --git a/sub-services/physics/src/main/java/wusthelper/internal/physics/services/LoginService.java b/sub-services/physics/src/main/java/wusthelper/internal/physics/services/LoginService.java index 80ac51b..d2aabf8 100644 --- a/sub-services/physics/src/main/java/wusthelper/internal/physics/services/LoginService.java +++ b/sub-services/physics/src/main/java/wusthelper/internal/physics/services/LoginService.java @@ -4,9 +4,10 @@ import cn.wustlinghang.mywust.core.request.service.auth.PhysicsLogin; import cn.wustlinghang.mywust.exception.ApiException; import cn.wustlinghang.mywust.exception.ParseException; import cn.wustlinghang.mywust.network.RequestClientOption; -import wusthelper.internal.rpc.exception.PhysicsRpcException; import jakarta.enterprise.context.ApplicationScoped; import lombok.extern.slf4j.Slf4j; +import wusthelper.code.ServiceCode; +import wusthelper.internal.rpc.exception.RpcException; import java.io.IOException; @@ -23,55 +24,39 @@ public class LoginService { this.physicsLogin = physicsLogin; } - public String login(String username, String password) throws PhysicsRpcException { + public String login(String username, String password) throws RpcException { try { return physicsLogin.getLoginCookie(username, password, option); } catch (ApiException e) { throw wrapApiException(e); } catch (IOException e) { - throw new PhysicsRpcException( - PhysicsRpcException.TypeCode.NETWORK_EXCEPTION, - PhysicsRpcException.SubModuleCode.AUTH, - PhysicsRpcException.ErrorCode.NETWORK_ERROR - ); + throw new RpcException(ServiceCode.NetworkError); } catch (ParseException e) { log.error("物理实验:登录时出现页面参数解析问题", e); - throw new PhysicsRpcException( - PhysicsRpcException.TypeCode.PARSE_EXCEPTION, - PhysicsRpcException.SubModuleCode.AUTH, - PhysicsRpcException.ErrorCode.AUTH_UNKNOWN_ERROR - ); + throw new RpcException(ServiceCode.ParseError); } } - public boolean verify(String cookie) throws PhysicsRpcException { + public boolean verify(String cookie) throws RpcException { try { return physicsLogin.checkCookie(cookie, option); } catch (IOException e) { - throw new PhysicsRpcException( - PhysicsRpcException.TypeCode.NETWORK_EXCEPTION, - PhysicsRpcException.SubModuleCode.AUTH, - PhysicsRpcException.ErrorCode.NETWORK_ERROR - ); + throw new RpcException(ServiceCode.NetworkError); } } - private PhysicsRpcException wrapApiException(ApiException e) { - PhysicsRpcException.ErrorCode errorCode = switch (e.getCode()) { - case NETWORK_EXCEPTION -> PhysicsRpcException.ErrorCode.NETWORK_ERROR; - case PHYSICS_PASSWORD_WRONG -> PhysicsRpcException.ErrorCode.AUTH_PASSWORD_WRONG; - case PHYSICS_NOT_CURRENT_TERM -> PhysicsRpcException.ErrorCode.AUTH_USER_NOT_CURRENT_TERM; + private RpcException wrapApiException(ApiException e) { + int errorCode = switch (e.getCode()) { + case NETWORK_EXCEPTION -> ServiceCode.NetworkError; + case PHYSICS_PASSWORD_WRONG -> ServiceCode.PhysicsPasswordWrong; + case PHYSICS_NOT_CURRENT_TERM -> ServiceCode.PhysicsUserNotCurrentTerm; default -> { log.error("物理实验:登录代理请求异常,异常未处理"); log.error("异常:", e); - yield PhysicsRpcException.ErrorCode.AUTH_UNKNOWN_ERROR; + yield ServiceCode.Unknown; } }; - return new PhysicsRpcException( - PhysicsRpcException.TypeCode.AUTH_EXCEPTION, - PhysicsRpcException.SubModuleCode.AUTH, - errorCode - ); + return new RpcException(errorCode); } } diff --git a/sub-services/physics/src/main/java/wusthelper/internal/physics/services/ScoreService.java b/sub-services/physics/src/main/java/wusthelper/internal/physics/services/ScoreService.java index aec37b5..526e0ee 100644 --- a/sub-services/physics/src/main/java/wusthelper/internal/physics/services/ScoreService.java +++ b/sub-services/physics/src/main/java/wusthelper/internal/physics/services/ScoreService.java @@ -6,9 +6,9 @@ import cn.wustlinghang.mywust.data.global.Score; import cn.wustlinghang.mywust.exception.ApiException; import cn.wustlinghang.mywust.exception.ParseException; import cn.wustlinghang.mywust.network.RequestClientOption; -import wusthelper.internal.rpc.exception.PhysicsRpcException; -import wusthelper.internal.rpc.exception.RpcException; import jakarta.enterprise.context.ApplicationScoped; +import wusthelper.code.ServiceCode; +import wusthelper.internal.rpc.exception.RpcException; import java.io.IOException; import java.util.ArrayList; @@ -35,19 +35,11 @@ public class ScoreService extends BaseService { try { return agent.getAllPages(cookie, requestClientOption); } catch (ApiException e) { - throw wrapApiException(e, PhysicsRpcException.SubModuleCode.SCORE); + throw wrapApiException(e); } catch (IOException e) { - throw new PhysicsRpcException( - PhysicsRpcException.TypeCode.NETWORK_EXCEPTION, - PhysicsRpcException.SubModuleCode.SCORE, - PhysicsRpcException.ErrorCode.NETWORK_ERROR - ); + throw new RpcException(ServiceCode.NetworkError); } catch (ParseException e) { - throw new PhysicsRpcException( - PhysicsRpcException.TypeCode.OTHER_EXCEPTION, - PhysicsRpcException.SubModuleCode.SCORE, - PhysicsRpcException.ErrorCode.PARSE_ERROR - ); + throw new RpcException(ServiceCode.ParseError); } } @@ -55,14 +47,10 @@ public class ScoreService extends BaseService { public List parseScore(String data) throws RpcException { try { return parser.parse(data); - } catch (PhysicsRpcException e) { + } catch (RpcException e) { throw e; } catch (Exception e) { - throw new PhysicsRpcException( - PhysicsRpcException.TypeCode.PARSE_EXCEPTION, - PhysicsRpcException.SubModuleCode.SCORE, - PhysicsRpcException.ErrorCode.PARSE_ERROR - ); + throw new RpcException(ServiceCode.ParseError); } } @@ -74,14 +62,10 @@ public class ScoreService extends BaseService { } return scores; - } catch (PhysicsRpcException e) { + } catch (RpcException e) { throw e; } catch (Exception e) { - throw new PhysicsRpcException( - PhysicsRpcException.TypeCode.PARSE_EXCEPTION, - PhysicsRpcException.SubModuleCode.SCORE, - PhysicsRpcException.ErrorCode.PARSE_ERROR - ); + throw new RpcException(ServiceCode.ParseError); } } } diff --git a/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/api/http/v1/handler/BaseExceptionHandler.java b/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/api/http/v1/handler/BaseExceptionHandler.java index 823e6b7..c5e146c 100644 --- a/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/api/http/v1/handler/BaseExceptionHandler.java +++ b/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/api/http/v1/handler/BaseExceptionHandler.java @@ -1,6 +1,6 @@ package wusthelper.internal.undergrad.api.http.v1.handler; -import wusthelper.internal.rpc.response.RpcCommonResponseCode; +import wusthelper.code.ServiceCode; import wusthelper.internal.rpc.response.RpcResponseDto; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; @@ -12,24 +12,20 @@ public abstract class BaseExceptionHandler { private static final ObjectMapper objectMapper = new ObjectMapper(); - public Response toResponse(RpcCommonResponseCode code, String msg, String handlerName) { - return toResponse(code.getCode(), msg, handlerName); + public Response toResponse(int code, String handlerName) { + return toResponse(Response.Status.OK, code, handlerName); } - public Response toResponse(int code, String msg, String handlerName) { - return toResponse(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), code, msg, handlerName); + public Response toResponse(int status, int code, String handlerName) { + return toResponse(Response.Status.fromStatusCode(status), code, handlerName); } - public Response toResponse(int status, int code, String msg, String handlerName) { - return toResponse(Response.Status.fromStatusCode(status), code, msg, handlerName); - } - - public Response toResponse(Response.Status status, int code, String msg, String handlerName) { + public Response toResponse(Response.Status status, int code, String handlerName) { Object response; try { - response = objectMapper.writeValueAsString(RpcResponseDto.error(code, msg)); + response = objectMapper.writeValueAsString(RpcResponseDto.error(code)); } catch (JsonProcessingException e) { - response = RpcResponseDto.error(RpcCommonResponseCode.ServerInternalError); + response = RpcResponseDto.error(ServiceCode.ServerInternalError); } return Response.status(status) diff --git a/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/api/http/v1/handler/DefaultExceptionHandler.java b/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/api/http/v1/handler/DefaultExceptionHandler.java index fab3e88..8c272a7 100644 --- a/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/api/http/v1/handler/DefaultExceptionHandler.java +++ b/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/api/http/v1/handler/DefaultExceptionHandler.java @@ -1,21 +1,19 @@ package wusthelper.internal.undergrad.api.http.v1.handler; -import wusthelper.internal.rpc.response.RpcCommonResponseCode; +import jakarta.enterprise.context.ApplicationScoped; import jakarta.ws.rs.core.Response; import jakarta.ws.rs.ext.ExceptionMapper; import jakarta.ws.rs.ext.Provider; import lombok.extern.slf4j.Slf4j; +import wusthelper.code.ServiceCode; @Slf4j @Provider +@ApplicationScoped public class DefaultExceptionHandler extends BaseExceptionHandler implements ExceptionMapper { @Override public Response toResponse(Exception e) { log.error("未知异常:", e); - return super.toResponse( - RpcCommonResponseCode.ServerInternalError, - e.toString(), - "DefaultExceptionHandler" - ); + return super.toResponse(ServiceCode.ServerInternalError, "DefaultExceptionHandler"); } } diff --git a/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/api/http/v1/handler/IOExceptionHandler.java b/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/api/http/v1/handler/IOExceptionHandler.java index 290a815..cc514c2 100644 --- a/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/api/http/v1/handler/IOExceptionHandler.java +++ b/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/api/http/v1/handler/IOExceptionHandler.java @@ -1,23 +1,21 @@ package wusthelper.internal.undergrad.api.http.v1.handler; -import wusthelper.internal.rpc.response.RpcCommonResponseCode; +import jakarta.enterprise.context.ApplicationScoped; import jakarta.ws.rs.core.Response; import jakarta.ws.rs.ext.ExceptionMapper; import jakarta.ws.rs.ext.Provider; import lombok.extern.slf4j.Slf4j; +import wusthelper.code.ServiceCode; import java.io.IOException; @Slf4j @Provider +@ApplicationScoped public class IOExceptionHandler extends BaseExceptionHandler implements ExceptionMapper { @Override public Response toResponse(IOException e) { log.error("IO异常:", e); - return super.toResponse( - RpcCommonResponseCode.ServerInternalError, - e.toString(), - "IOExceptionHandler" - ); + return super.toResponse(ServiceCode.NetworkError, "IOExceptionHandler"); } } diff --git a/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/api/http/v1/handler/ParseExceptionHandler.java b/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/api/http/v1/handler/ParseExceptionHandler.java index 67be536..4498866 100644 --- a/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/api/http/v1/handler/ParseExceptionHandler.java +++ b/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/api/http/v1/handler/ParseExceptionHandler.java @@ -1,7 +1,8 @@ package wusthelper.internal.undergrad.api.http.v1.handler; import cn.wustlinghang.mywust.exception.ParseException; -import wusthelper.internal.rpc.response.RpcCommonResponseCode; +import jakarta.enterprise.context.ApplicationScoped; +import wusthelper.code.ServiceCode; import jakarta.ws.rs.core.Response; import jakarta.ws.rs.ext.ExceptionMapper; import jakarta.ws.rs.ext.Provider; @@ -9,14 +10,11 @@ import lombok.extern.slf4j.Slf4j; @Slf4j @Provider +@ApplicationScoped public class ParseExceptionHandler extends BaseExceptionHandler implements ExceptionMapper { @Override public Response toResponse(ParseException e) { log.error("解析异常:", e); - return super.toResponse( - RpcCommonResponseCode.ServerInternalError, - e.toString(), - "ParseExceptionHandler" - ); + return super.toResponse(ServiceCode.ParseError, "ParseExceptionHandler"); } } diff --git a/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/api/http/v1/handler/RpcExceptionHandler.java b/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/api/http/v1/handler/RpcExceptionHandler.java index eeae286..f0c0bd9 100644 --- a/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/api/http/v1/handler/RpcExceptionHandler.java +++ b/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/api/http/v1/handler/RpcExceptionHandler.java @@ -1,17 +1,19 @@ package wusthelper.internal.undergrad.api.http.v1.handler; -import wusthelper.internal.rpc.exception.RpcException; +import jakarta.enterprise.context.ApplicationScoped; import jakarta.ws.rs.core.Response; import jakarta.ws.rs.ext.ExceptionMapper; import jakarta.ws.rs.ext.Provider; +import lombok.extern.slf4j.Slf4j; +import wusthelper.internal.rpc.exception.RpcException; +@Slf4j @Provider +@ApplicationScoped public class RpcExceptionHandler extends BaseExceptionHandler implements ExceptionMapper { @Override public Response toResponse(RpcException e) { - return super.toResponse( - e.getCode(), - e.toString(), - "RpcExceptionHandler"); + log.debug("RpcException:", e); + return super.toResponse(e.getCode(), "RpcExceptionHandler"); } } diff --git a/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/api/http/v1/handler/ValidationExceptionHandler.java b/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/api/http/v1/handler/ValidationExceptionHandler.java index 0a79a3c..001c75f 100644 --- a/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/api/http/v1/handler/ValidationExceptionHandler.java +++ b/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/api/http/v1/handler/ValidationExceptionHandler.java @@ -1,22 +1,20 @@ package wusthelper.internal.undergrad.api.http.v1.handler; -import wusthelper.internal.rpc.response.RpcCommonResponseCode; +import jakarta.enterprise.context.ApplicationScoped; import jakarta.validation.ValidationException; import jakarta.ws.rs.core.Response; import jakarta.ws.rs.ext.ExceptionMapper; import jakarta.ws.rs.ext.Provider; import lombok.extern.slf4j.Slf4j; +import wusthelper.code.ServiceCode; @Slf4j @Provider +@ApplicationScoped public class ValidationExceptionHandler extends BaseExceptionHandler implements ExceptionMapper { @Override public Response toResponse(ValidationException e) { - return super.toResponse( - RpcCommonResponseCode.ParamWrong, - "参数错误:" + e.toString(), - "ValidationExceptionHandler" - ); + return super.toResponse(ServiceCode.ParamWrong, "ValidationExceptionHandler"); } } diff --git a/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/api/http/v1/handler/WebApplicationExceptionHandler.java b/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/api/http/v1/handler/WebApplicationExceptionHandler.java index 0e94280..b248958 100644 --- a/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/api/http/v1/handler/WebApplicationExceptionHandler.java +++ b/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/api/http/v1/handler/WebApplicationExceptionHandler.java @@ -1,26 +1,17 @@ package wusthelper.internal.undergrad.api.http.v1.handler; -import wusthelper.internal.rpc.exception.UndergradRpcException; -import wusthelper.internal.rpc.exception.RpcException; +import jakarta.enterprise.context.ApplicationScoped; import jakarta.ws.rs.WebApplicationException; import jakarta.ws.rs.core.Response; import jakarta.ws.rs.ext.ExceptionMapper; import jakarta.ws.rs.ext.Provider; +import wusthelper.code.ServiceCode; @Provider +@ApplicationScoped public class WebApplicationExceptionHandler extends BaseExceptionHandler implements ExceptionMapper { @Override public Response toResponse(WebApplicationException e) { - return super.toResponse( - e.getResponse().getStatus(), - RpcException.toIntCode( - UndergradRpcException.UNDERGRAD_MODULE, - UndergradRpcException.SubModuleCode.COMMON.ordinal(), - UndergradRpcException.TypeCode.OTHER_EXCEPTION.ordinal() * 100 - + UndergradRpcException.ErrorCode.REQUEST_INVALID.ordinal() - ), - e.getMessage(), - "WebApplicationExceptionHandler" - ); + return super.toResponse(ServiceCode.RequestInvalid, "WebApplicationExceptionHandler"); } } diff --git a/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/api/http/v1/interceptor/ResponseWrapperInterceptor.java b/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/api/http/v1/interceptor/ResponseWrapperInterceptor.java index 7203f84..f2e05c0 100644 --- a/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/api/http/v1/interceptor/ResponseWrapperInterceptor.java +++ b/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/api/http/v1/interceptor/ResponseWrapperInterceptor.java @@ -1,5 +1,6 @@ package wusthelper.internal.undergrad.api.http.v1.interceptor; +import jakarta.enterprise.context.ApplicationScoped; import wusthelper.internal.undergrad.api.http.v1.handler.BaseExceptionHandler; import wusthelper.internal.rpc.response.RpcResponseDto; import com.fasterxml.jackson.databind.ObjectMapper; @@ -13,6 +14,7 @@ import jakarta.ws.rs.ext.WriterInterceptorContext; import java.io.IOException; @Provider +@ApplicationScoped public class ResponseWrapperInterceptor implements WriterInterceptor { private final ObjectMapper objectMapper; @@ -22,6 +24,7 @@ public class ResponseWrapperInterceptor implements WriterInterceptor { @Override public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException { + System.out.println("ResponseWrapperInterceptor.aroundWriteTo"); boolean hasException = context.getHeaders().get(BaseExceptionHandler.EXCEPTION_HEADER_KEY) != null; if (!hasException) { Object data = context.getEntity(); diff --git a/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/rpc/Register.java b/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/rpc/Register.java index ae0d263..81b6684 100644 --- a/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/rpc/Register.java +++ b/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/rpc/Register.java @@ -28,6 +28,7 @@ public class Register { .consulAddress(rpcConfig.getConsulAddress()) .localServicePort(rpcConfig.getLocalServicePort()) .serviceName(rpcConfig.getServiceName()) + .token("") .build(); this.register = new FrpConsulRegister(registerConfig, frpConfig, requester, objectMapper); diff --git a/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/services/BaseService.java b/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/services/BaseService.java index 365f44a..dd81cc8 100644 --- a/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/services/BaseService.java +++ b/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/services/BaseService.java @@ -1,31 +1,20 @@ package wusthelper.internal.undergrad.services; import cn.wustlinghang.mywust.exception.ApiException; -import wusthelper.internal.rpc.exception.UndergradRpcException; +import wusthelper.code.ServiceCode; +import wusthelper.internal.rpc.exception.RpcException; import lombok.extern.slf4j.Slf4j; @Slf4j public abstract class BaseService { - protected UndergradRpcException wrapApiException(ApiException e, UndergradRpcException.SubModuleCode subModuleCode) { + protected RpcException wrapApiException(ApiException e) { 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 - ); + case NETWORK_EXCEPTION -> new RpcException(ServiceCode.NetworkError); + case COOKIE_INVALID -> new RpcException(ServiceCode.CookieInvalid); default -> { - log.error("本科生:{}代理请求异常,异常未处理", subModuleCode.name()); + log.error("本科生:代理请求异常,异常未处理"); log.error("异常:", e); - yield new UndergradRpcException( - UndergradRpcException.TypeCode.AUTH_EXCEPTION, - subModuleCode, - UndergradRpcException.ErrorCode.COOKIE_INVALID - ); + yield new RpcException(ServiceCode.Unknown); } }; } diff --git a/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/services/CourseTableService.java b/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/services/CourseTableService.java index eb0ee22..1c6422b 100644 --- a/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/services/CourseTableService.java +++ b/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/services/CourseTableService.java @@ -4,13 +4,15 @@ import cn.wustlinghang.mywust.core.parser.undergraduate.UndergradCourseTablePars import cn.wustlinghang.mywust.core.request.service.undergraduate.UndergradCourseTableApiService; import cn.wustlinghang.mywust.data.global.Course; import cn.wustlinghang.mywust.exception.ApiException; -import wusthelper.internal.rpc.exception.UndergradRpcException; -import wusthelper.internal.rpc.exception.RpcException; import jakarta.enterprise.context.ApplicationScoped; +import lombok.extern.slf4j.Slf4j; +import wusthelper.code.ServiceCode; +import wusthelper.internal.rpc.exception.RpcException; import java.io.IOException; import java.util.List; +@Slf4j @ApplicationScoped public class CourseTableService extends BaseService { private final UndergradCourseTableParser courseTableParser; @@ -26,33 +28,22 @@ public class CourseTableService extends BaseService { try { return courseTableApiService.getPage(term, cookie); } catch (ApiException e) { - throw wrapApiException(e, UndergradRpcException.SubModuleCode.COURSE_TABLE); + throw wrapApiException(e); } catch (IOException e) { - throw new UndergradRpcException( - UndergradRpcException.TypeCode.NETWORK_EXCEPTION, - UndergradRpcException.SubModuleCode.COURSE_TABLE, - UndergradRpcException.ErrorCode.NETWORK_ERROR - ); + throw new RpcException(ServiceCode.NetworkError); } } - public List parseCourseTable(String data) throws UndergradRpcException { + public List parseCourseTable(String data) throws RpcException { try { if (data.contains("评教")) { - throw new UndergradRpcException( - UndergradRpcException.TypeCode.PARSE_EXCEPTION, - UndergradRpcException.SubModuleCode.COURSE_TABLE, - UndergradRpcException.ErrorCode.NEED_EVALUATE - ); + throw new RpcException(ServiceCode.UndergradNeedEvaluate); } return courseTableParser.parse(data); } catch (Exception e) { - throw new UndergradRpcException( - UndergradRpcException.TypeCode.PARSE_EXCEPTION, - UndergradRpcException.SubModuleCode.COURSE_TABLE, - UndergradRpcException.ErrorCode.PARSE_ERROR - ); + log.error("解析错误:", e); + throw new RpcException(ServiceCode.ParseError); } } } diff --git a/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/services/CreditStatusService.java b/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/services/CreditStatusService.java index 7354f04..7e8c3d3 100644 --- a/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/services/CreditStatusService.java +++ b/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/services/CreditStatusService.java @@ -4,18 +4,21 @@ import cn.wustlinghang.mywust.core.parser.undergraduate.UndergradCreditStatusPar import cn.wustlinghang.mywust.core.request.service.undergraduate.UndergradCreditStatusApiService; import cn.wustlinghang.mywust.exception.ApiException; import cn.wustlinghang.mywust.network.RequestClientOption; -import wusthelper.internal.rpc.exception.UndergradRpcException; -import wusthelper.internal.rpc.exception.RpcException; import jakarta.enterprise.context.ApplicationScoped; +import lombok.extern.slf4j.Slf4j; +import wusthelper.code.ServiceCode; +import wusthelper.internal.rpc.exception.RpcException; import java.io.IOException; +@Slf4j @ApplicationScoped public class CreditStatusService extends BaseService { private final UndergradCreditStatusApiService creditStatusApiService; private final UndergradCreditStatusParser creditStatusParser; private final RequestClientOption requestClientOption; + public CreditStatusService(UndergradCreditStatusApiService creditStatusApiService, UndergradCreditStatusParser creditStatusParser, RequestClientOption requestClientOption) { @@ -29,13 +32,9 @@ public class CreditStatusService extends BaseService { try { return creditStatusApiService.getPage(cookie, requestClientOption, false); } catch (ApiException e) { - throw wrapApiException(e, UndergradRpcException.SubModuleCode.CREDIT_STATUS); + throw wrapApiException(e); } catch (IOException e) { - throw new UndergradRpcException( - UndergradRpcException.TypeCode.NETWORK_EXCEPTION, - UndergradRpcException.SubModuleCode.CREDIT_STATUS, - UndergradRpcException.ErrorCode.NETWORK_ERROR - ); + throw new RpcException(ServiceCode.NetworkError); } } @@ -43,11 +42,8 @@ public class CreditStatusService extends BaseService { try { return creditStatusParser.parse(data); } catch (Exception e) { - throw new UndergradRpcException( - UndergradRpcException.TypeCode.PARSE_EXCEPTION, - UndergradRpcException.SubModuleCode.CREDIT_STATUS, - UndergradRpcException.ErrorCode.PARSE_ERROR - ); + log.error("解析错误:", e); + throw new RpcException(ServiceCode.ParseError); } } } diff --git a/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/services/ExamActivitiesService.java b/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/services/ExamActivitiesService.java index 3b00f7f..6921905 100644 --- a/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/services/ExamActivitiesService.java +++ b/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/services/ExamActivitiesService.java @@ -2,12 +2,14 @@ package wusthelper.internal.undergrad.services; import cn.wustlinghang.mywust.core.request.service.undergraduate.UndergradExamDelayApiService; import cn.wustlinghang.mywust.exception.ApiException; -import wusthelper.internal.rpc.exception.UndergradRpcException; +import lombok.extern.slf4j.Slf4j; +import wusthelper.code.ServiceCode; import wusthelper.internal.rpc.exception.RpcException; import jakarta.enterprise.context.ApplicationScoped; import java.io.IOException; +@Slf4j @ApplicationScoped public class ExamActivitiesService extends BaseService { private final UndergradExamDelayApiService examDelayApiService; @@ -21,13 +23,10 @@ public class ExamActivitiesService extends BaseService { try { return examDelayApiService.getActivities(term, cookie); } catch (ApiException e) { - throw wrapApiException(e, UndergradRpcException.SubModuleCode.EXAM_ACTIVITIES); + throw wrapApiException(e); } catch (IOException e) { - throw new UndergradRpcException( - UndergradRpcException.TypeCode.NETWORK_EXCEPTION, - UndergradRpcException.SubModuleCode.EXAM_ACTIVITIES, - UndergradRpcException.ErrorCode.NETWORK_ERROR - ); + log.error("解析错误:", e); + throw new RpcException(ServiceCode.NetworkError); } } diff --git a/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/services/ExamDelayApplicationService.java b/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/services/ExamDelayApplicationService.java index 7d72446..26aaa8c 100644 --- a/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/services/ExamDelayApplicationService.java +++ b/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/services/ExamDelayApplicationService.java @@ -4,13 +4,15 @@ import cn.wustlinghang.mywust.core.parser.undergraduate.UndergradExamDelayParser import cn.wustlinghang.mywust.core.request.service.undergraduate.UndergradExamDelayApiService; import cn.wustlinghang.mywust.data.undergrad.ExamDelayApplication; import cn.wustlinghang.mywust.exception.ApiException; -import wusthelper.internal.rpc.exception.UndergradRpcException; +import lombok.extern.slf4j.Slf4j; +import wusthelper.code.ServiceCode; import wusthelper.internal.rpc.exception.RpcException; import jakarta.enterprise.context.ApplicationScoped; import java.io.IOException; import java.util.List; +@Slf4j @ApplicationScoped public class ExamDelayApplicationService extends BaseService { private final UndergradExamDelayApiService examDelayApiService; @@ -27,13 +29,9 @@ public class ExamDelayApplicationService extends BaseService { try { return examDelayApiService.getPage(term, activityId, cookie); } catch (ApiException e) { - throw wrapApiException(e, UndergradRpcException.SubModuleCode.EXAM_DELAY_APPLICATION); + throw wrapApiException(e); } catch (IOException e) { - throw new UndergradRpcException( - UndergradRpcException.TypeCode.NETWORK_EXCEPTION, - UndergradRpcException.SubModuleCode.EXAM_DELAY_APPLICATION, - UndergradRpcException.ErrorCode.NETWORK_ERROR - ); + throw new RpcException(ServiceCode.NetworkError); } } @@ -41,11 +39,8 @@ public class ExamDelayApplicationService extends BaseService { try { return examDelayParser.parse(data); } catch (Exception e) { - throw new UndergradRpcException( - UndergradRpcException.TypeCode.PARSE_EXCEPTION, - UndergradRpcException.SubModuleCode.EXAM_DELAY_APPLICATION, - UndergradRpcException.ErrorCode.PARSE_ERROR - ); + log.error("解析错误:", e); + throw new RpcException(ServiceCode.ParseError); } } } diff --git a/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/services/LoginService.java b/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/services/LoginService.java index b9b3980..01f333f 100644 --- a/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/services/LoginService.java +++ b/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/services/LoginService.java @@ -3,7 +3,8 @@ package wusthelper.internal.undergrad.services; import cn.wustlinghang.mywust.core.request.service.auth.UndergraduateLogin; import cn.wustlinghang.mywust.exception.ApiException; import cn.wustlinghang.mywust.network.RequestClientOption; -import wusthelper.internal.rpc.exception.UndergradRpcException; +import wusthelper.code.ServiceCode; +import wusthelper.internal.rpc.exception.RpcException; import jakarta.enterprise.context.ApplicationScoped; import lombok.extern.slf4j.Slf4j; @@ -21,7 +22,7 @@ public class LoginService { this.undergraduateLogin = undergraduateLogin; } - public String login(String username, String password, boolean legacy) throws UndergradRpcException { + public String login(String username, String password, boolean legacy) throws RpcException { try { if (legacy) { return undergraduateLogin.getLoginCookieLegacy(username, password, option); @@ -31,50 +32,38 @@ public class LoginService { } catch (ApiException e) { throw wrapApiException(e); } catch (IOException e) { - throw new UndergradRpcException( - UndergradRpcException.TypeCode.NETWORK_EXCEPTION, - UndergradRpcException.SubModuleCode.AUTH, - UndergradRpcException.ErrorCode.NETWORK_ERROR - ); + throw new RpcException(ServiceCode.NetworkError); } } - public boolean verify(String cookie) throws UndergradRpcException { + public boolean verify(String cookie) throws RpcException { try { return undergraduateLogin.testCookie(cookie); } catch (ApiException e) { return false; } catch (IOException e) { - throw new UndergradRpcException( - UndergradRpcException.TypeCode.NETWORK_EXCEPTION, - UndergradRpcException.SubModuleCode.AUTH, - UndergradRpcException.ErrorCode.NETWORK_ERROR - ); + throw new RpcException(ServiceCode.NetworkError); } } - 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 UNDERGRAD_BANNED_IN_EXCLUSIVE_TIME -> UndergradRpcException.ErrorCode.AUTH_BANNED_IN_EXCLUSIVE_TIME; + private RpcException wrapApiException(ApiException e) { + int code = switch (e.getCode()) { + case NETWORK_EXCEPTION -> ServiceCode.NetworkError; + case UNI_LOGIN_PASSWORD_WRONG -> ServiceCode.UndergradPasswordWrong; + case UNI_LOGIN_USER_NOT_EXISTS -> ServiceCode.UndergradUserNotExists; + case UNI_LOGIN_USER_BANNED -> ServiceCode.UndergradUserBanned; + case UNI_LOGIN_USER_DISABLED -> ServiceCode.UndergradUserDisabled; + case UNI_LOGIN_NEED_CHANGE_PASSWORD -> ServiceCode.UndergradPasswordNeedModify; + case UNDERGRAD_BANNED_IN_EXCLUSIVE_TIME -> ServiceCode.UndergradBannedInExclusiveTime; case UNI_LOGIN_USER_NOT_ONLY, UNI_LOGIN_NO_REGISTER, UNI_LOGIN_NEED_TFA - -> UndergradRpcException.ErrorCode.AUTH_UNKNOWN_ERROR; + -> ServiceCode.Unknown; default -> { log.error("本科生:登录代理请求异常,异常未处理"); log.error("异常:", e); - yield UndergradRpcException.ErrorCode.AUTH_UNKNOWN_ERROR; + yield ServiceCode.Unknown; } }; - return new UndergradRpcException( - UndergradRpcException.TypeCode.AUTH_EXCEPTION, - UndergradRpcException.SubModuleCode.AUTH, - errorCode - ); + return new RpcException(code); } } diff --git a/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/services/ScoreService.java b/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/services/ScoreService.java index 12a0a8a..87ff275 100644 --- a/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/services/ScoreService.java +++ b/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/services/ScoreService.java @@ -4,13 +4,15 @@ 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.exception.ApiException; -import wusthelper.internal.rpc.exception.UndergradRpcException; +import lombok.extern.slf4j.Slf4j; +import wusthelper.code.ServiceCode; import wusthelper.internal.rpc.exception.RpcException; import jakarta.enterprise.context.ApplicationScoped; import java.io.IOException; import java.util.List; +@Slf4j @ApplicationScoped public class ScoreService extends BaseService { private final UndergradScoreApiService scoreApiService; @@ -26,33 +28,22 @@ public class ScoreService extends BaseService { try { return scoreApiService.getPage(cookie); } catch (ApiException e) { - throw wrapApiException(e, UndergradRpcException.SubModuleCode.SCORE); + throw wrapApiException(e); } catch (IOException e) { - throw new UndergradRpcException( - UndergradRpcException.TypeCode.NETWORK_EXCEPTION, - UndergradRpcException.SubModuleCode.SCORE, - UndergradRpcException.ErrorCode.NETWORK_ERROR - ); + throw new RpcException(ServiceCode.NetworkError); } } public List parseScore(String data) throws RpcException { try { if (data.contains("评教")) { - throw new UndergradRpcException( - UndergradRpcException.TypeCode.PARSE_EXCEPTION, - UndergradRpcException.SubModuleCode.SCORE, - UndergradRpcException.ErrorCode.NEED_EVALUATE - ); + throw new RpcException(ServiceCode.UndergradNeedEvaluate); } return scoreParser.parse(data); } catch (Exception e) { - throw new UndergradRpcException( - UndergradRpcException.TypeCode.PARSE_EXCEPTION, - UndergradRpcException.SubModuleCode.SCORE, - UndergradRpcException.ErrorCode.PARSE_ERROR - ); + log.error("解析错误:", e); + throw new RpcException(ServiceCode.ParseError); } } } diff --git a/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/services/StudentInfoService.java b/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/services/StudentInfoService.java index 4ec1396..147ff8a 100644 --- a/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/services/StudentInfoService.java +++ b/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/services/StudentInfoService.java @@ -4,12 +4,14 @@ import cn.wustlinghang.mywust.core.parser.undergraduate.UndergradStudentInfoPage import cn.wustlinghang.mywust.core.request.service.undergraduate.UndergradStudentInfoApiService; import cn.wustlinghang.mywust.data.global.StudentInfo; import cn.wustlinghang.mywust.exception.ApiException; -import wusthelper.internal.rpc.exception.UndergradRpcException; +import lombok.extern.slf4j.Slf4j; +import wusthelper.code.ServiceCode; import wusthelper.internal.rpc.exception.RpcException; import jakarta.enterprise.context.ApplicationScoped; import java.io.IOException; +@Slf4j @ApplicationScoped public class StudentInfoService extends BaseService { private final UndergradStudentInfoApiService studentInfoApiService; @@ -25,13 +27,9 @@ public class StudentInfoService extends BaseService { try { return studentInfoApiService.getPage(cookie); } catch (ApiException e) { - throw wrapApiException(e, UndergradRpcException.SubModuleCode.STUDENT_INFO); + throw wrapApiException(e); } catch (IOException e) { - throw new UndergradRpcException( - UndergradRpcException.TypeCode.NETWORK_EXCEPTION, - UndergradRpcException.SubModuleCode.STUDENT_INFO, - UndergradRpcException.ErrorCode.NETWORK_ERROR - ); + throw new RpcException(ServiceCode.NetworkError); } } @@ -39,11 +37,8 @@ public class StudentInfoService extends BaseService { try { return studentInfoPageParser.parse(data); } catch (Exception e) { - throw new UndergradRpcException( - UndergradRpcException.TypeCode.PARSE_EXCEPTION, - UndergradRpcException.SubModuleCode.STUDENT_INFO, - UndergradRpcException.ErrorCode.PARSE_ERROR - ); + log.error("解析错误:", e); + throw new RpcException(ServiceCode.ParseError); } } } diff --git a/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/services/TrainingPlanService.java b/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/services/TrainingPlanService.java index 6786bce..06c2917 100644 --- a/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/services/TrainingPlanService.java +++ b/sub-services/undergrad/src/main/java/wusthelper/internal/undergrad/services/TrainingPlanService.java @@ -3,12 +3,14 @@ package wusthelper.internal.undergrad.services; import cn.wustlinghang.mywust.core.parser.undergraduate.UndergradTrainingPlanPageParser; import cn.wustlinghang.mywust.core.request.service.undergraduate.UndergradTrainingPlanApiService; import cn.wustlinghang.mywust.exception.ApiException; -import wusthelper.internal.rpc.exception.UndergradRpcException; -import wusthelper.internal.rpc.exception.RpcException; import jakarta.enterprise.context.ApplicationScoped; +import lombok.extern.slf4j.Slf4j; +import wusthelper.code.ServiceCode; +import wusthelper.internal.rpc.exception.RpcException; import java.io.IOException; +@Slf4j @ApplicationScoped public class TrainingPlanService extends BaseService { private final UndergradTrainingPlanApiService trainingPlanApiService; @@ -24,13 +26,9 @@ public class TrainingPlanService extends BaseService { try { return trainingPlanApiService.getPage(cookie); } catch (ApiException e) { - throw wrapApiException(e, UndergradRpcException.SubModuleCode.TRAINING_PLAN); + throw wrapApiException(e); } catch (IOException e) { - throw new UndergradRpcException( - UndergradRpcException.TypeCode.NETWORK_EXCEPTION, - UndergradRpcException.SubModuleCode.TRAINING_PLAN, - UndergradRpcException.ErrorCode.NETWORK_ERROR - ); + throw new RpcException(ServiceCode.NetworkError); } } @@ -38,11 +36,8 @@ public class TrainingPlanService extends BaseService { try { return trainingPlanPageParser.parse(data); } catch (Exception e) { - throw new UndergradRpcException( - UndergradRpcException.TypeCode.PARSE_EXCEPTION, - UndergradRpcException.SubModuleCode.TRAINING_PLAN, - UndergradRpcException.ErrorCode.PARSE_ERROR - ); + log.error("解析错误:", e); + throw new RpcException(ServiceCode.ParseError); } } } diff --git a/sub-services/undergrad/src/main/resources/application.properties b/sub-services/undergrad/src/main/resources/application.properties index 8e2f395..4fcf37c 100644 --- a/sub-services/undergrad/src/main/resources/application.properties +++ b/sub-services/undergrad/src/main/resources/application.properties @@ -1,7 +1,7 @@ quarkus.http.port=22800 quarkus.http.enable-compression=true +quarkus.log.level=DEBUG -#quarkus.log.level=DEBUG quarkus.log.file.encoding=UTF-8 # 打包成jar时附带依赖