From d714db08916e5c663dfdebfd5e2bde37a04af6d9 Mon Sep 17 00:00:00 2001 From: lensferno Date: Fri, 8 Sep 2023 11:34:16 +0800 Subject: [PATCH] . --- .../data/dao/mapper/UserBasicMapper.java | 9 ++ .../java/wusthelper/data/entity/Student.java | 6 + .../wusthelper/data/entity/UserBasic.java | 54 ++++++++ .../undergrade/UndergradLoginController.java | 14 +-- .../campus/GeneralUserLoginService.java | 99 +++++++++++++++ .../service/campus/GeneralUserService.java | 116 ------------------ external-library/mywust | 2 +- 7 files changed, 175 insertions(+), 125 deletions(-) create mode 100644 backend-main/backend-data/src/main/java/wusthelper/data/dao/mapper/UserBasicMapper.java create mode 100644 backend-main/backend-data/src/main/java/wusthelper/data/entity/UserBasic.java create mode 100644 backend-main/backend-web/src/main/java/wusthelper/web/service/campus/GeneralUserLoginService.java delete mode 100644 backend-main/backend-web/src/main/java/wusthelper/web/service/campus/GeneralUserService.java diff --git a/backend-main/backend-data/src/main/java/wusthelper/data/dao/mapper/UserBasicMapper.java b/backend-main/backend-data/src/main/java/wusthelper/data/dao/mapper/UserBasicMapper.java new file mode 100644 index 0000000..60cf19b --- /dev/null +++ b/backend-main/backend-data/src/main/java/wusthelper/data/dao/mapper/UserBasicMapper.java @@ -0,0 +1,9 @@ +package wusthelper.data.dao.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import org.apache.ibatis.annotations.Mapper; +import wusthelper.data.entity.UserBasic; + +@Mapper +public interface UserBasicMapper extends BaseMapper { +} diff --git a/backend-main/backend-data/src/main/java/wusthelper/data/entity/Student.java b/backend-main/backend-data/src/main/java/wusthelper/data/entity/Student.java index a5bd300..d3029f9 100644 --- a/backend-main/backend-data/src/main/java/wusthelper/data/entity/Student.java +++ b/backend-main/backend-data/src/main/java/wusthelper/data/entity/Student.java @@ -1,8 +1,14 @@ package wusthelper.data.entity; +import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Data; +import lombok.NoArgsConstructor; @Data +@Builder +@NoArgsConstructor +@AllArgsConstructor public class Student { /** * 本科生id diff --git a/backend-main/backend-data/src/main/java/wusthelper/data/entity/UserBasic.java b/backend-main/backend-data/src/main/java/wusthelper/data/entity/UserBasic.java new file mode 100644 index 0000000..2a3517c --- /dev/null +++ b/backend-main/backend-data/src/main/java/wusthelper/data/entity/UserBasic.java @@ -0,0 +1,54 @@ +package wusthelper.data.entity; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.time.LocalDateTime; + +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class UserBasic { + /** + * uid + */ + private Long uid; + + /** + * 学号 + */ + private String stuNum; + + /** + * 教务处密码 + */ + private String officialPwd;// 官方 + + /** + * 图书馆密码 + */ + private String libPwd; + + /** + * 物理实验系统密码 + */ + private String physicsPwd; + + /** + * create_time + */ + private LocalDateTime createTime; + + /** + * update_time + */ + private LocalDateTime updateTime; + + /** + * deleted + */ + private Integer deleted; +} 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 index 6541eb6..ad6cbfa 100644 --- 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 @@ -1,19 +1,17 @@ package wusthelper.web.api.v2.undergrade; import org.springframework.web.bind.annotation.*; -import wusthelper.internal.rpc.response.RpcCommonResponseCode; import wusthelper.web.api.v2.Token; import wusthelper.web.response.Response; -import wusthelper.web.service.campus.GeneralUserService; -import wusthelper.web.service.campus.undergrad.UndergradCookieService; +import wusthelper.web.service.campus.GeneralUserLoginService; @RestController @RequestMapping("/jwc") public class UndergradLoginController { - private final GeneralUserService generalUserService; + private final GeneralUserLoginService generalUserLoginService; - public UndergradLoginController(GeneralUserService generalUserService) { - this.generalUserService = generalUserService; + public UndergradLoginController(GeneralUserLoginService generalUserLoginService) { + this.generalUserLoginService = generalUserLoginService; } @PostMapping("/login") @@ -21,9 +19,9 @@ public class UndergradLoginController { @RequestParam(value = "jwcPwd") String password, @RequestHeader(name = "Platform", required = false) String platform) { - var user = generalUserService.login(username, password, GeneralUserService.UserType.Undergrad); + var user = generalUserLoginService.login(username, password, GeneralUserLoginService.UserType.Undergrad); - String token = Token.signToken(user.getId(), user.getStuNum()); + String token = Token.signToken(user.getUid(), user.getStuNum()); return Response.success(token); } diff --git a/backend-main/backend-web/src/main/java/wusthelper/web/service/campus/GeneralUserLoginService.java b/backend-main/backend-web/src/main/java/wusthelper/web/service/campus/GeneralUserLoginService.java new file mode 100644 index 0000000..2e92325 --- /dev/null +++ b/backend-main/backend-web/src/main/java/wusthelper/web/service/campus/GeneralUserLoginService.java @@ -0,0 +1,99 @@ +package wusthelper.web.service.campus; + +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.github.yitter.idgen.YitIdHelper; +import org.springframework.stereotype.Service; +import wusthelper.data.dao.mapper.StudentMapper; +import wusthelper.data.dao.mapper.UserBasicMapper; +import wusthelper.data.entity.Student; +import wusthelper.data.entity.UserBasic; +import wusthelper.web.data.entity.CookieType; +import wusthelper.web.service.campus.graduate.GraduateCookieService; +import wusthelper.web.service.campus.undergrad.UndergradCookieService; +import wusthelper.web.service.cookie.CookieManager; +import wusthelper.web.util.PasswordCodec; + +/** + * 通用的用户登录信息服务,包括登录和(已存储的)用户信息获取等,同时自动存入新用户基本()信息 + */ +@Service +public class GeneralUserLoginService { + private final UserBasicMapper studentMapper; + private final PasswordCodec passwordCodec; + + private final UndergradCookieService undergradCookieService; + private final GraduateCookieService graduateCookieService; + + + public GeneralUserLoginService(UserBasicMapper studentMapper, + PasswordCodec passwordCodec, + + UndergradCookieService undergradCookieService, + GraduateCookieService graduateCookieService) { + + this.studentMapper = studentMapper; + this.passwordCodec = passwordCodec; + + this.undergradCookieService = undergradCookieService; + this.graduateCookieService = graduateCookieService; + } + + public enum UserType {Undergrad, Graduate} + + /** + * 登录验证,同时自动存库(基本信息) + * + * @param username 用户名(即学号) + * @param password 密码(明文原文) + * @param userType 用户类型,现在有本科生和研究生 + * @return 用户信息 + */ + public UserBasic login(String username, String password, UserType userType) { + var cookie = switch (userType) { + case Undergrad -> undergradCookieService.getLoginCookie(username, password); + case Graduate -> graduateCookieService.getLoginCookie(username, password); + }; + + var cookieType = switch (userType) { + case Undergrad -> CookieType.Undergrad; + case Graduate -> CookieType.Graduate; + }; + + // 保存cookie,并将用户基本信息存库 + CookieManager.store(username, cookie, cookieType); + return this.saveOrUpdateUser(username, password, userType); + } + + private UserBasic saveOrUpdateUser(String username, String password, UserType userType) { + var query = new QueryWrapper() + .eq("stu_num", username) + .eq("deleted", 0) + .last("limit 1"); + + var user = studentMapper.selectOne(query); + if (user != null) { + var encrypted = passwordCodec.encode(password); + user.setOfficialPwd(encrypted); + if (userType == UserType.Undergrad) { + user.setLibPwd(encrypted); + } + studentMapper.update(user, query); + } else { + var uid = YitIdHelper.nextId(); + user = UserBasic.builder() + .uid(uid) + .stuNum(username) + .build(); + + var encrypted = passwordCodec.encode(password); + user.setOfficialPwd(encrypted); + if (userType == UserType.Undergrad) { + user.setLibPwd(encrypted); + } + + studentMapper.insert(user); + } + + return user; + } +} diff --git a/backend-main/backend-web/src/main/java/wusthelper/web/service/campus/GeneralUserService.java b/backend-main/backend-web/src/main/java/wusthelper/web/service/campus/GeneralUserService.java deleted file mode 100644 index 2c6afb8..0000000 --- a/backend-main/backend-web/src/main/java/wusthelper/web/service/campus/GeneralUserService.java +++ /dev/null @@ -1,116 +0,0 @@ -package wusthelper.web.service.campus; - -import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; -import com.github.yitter.idgen.YitIdHelper; -import org.springframework.stereotype.Service; -import wusthelper.code.ServiceCode; -import wusthelper.data.dao.mapper.StudentMapper; -import wusthelper.data.entity.Student; -import wusthelper.web.data.entity.CookieType; -import wusthelper.web.exception.ServiceException; -import wusthelper.web.rpc.graduate.GraduateStudentInfoRemote; -import wusthelper.web.rpc.undergrad.UndergradStudentInfoRemote; -import wusthelper.web.service.campus.graduate.GraduateCookieService; -import wusthelper.web.service.campus.undergrad.UndergradCookieService; -import wusthelper.web.service.cookie.CookieManager; -import wusthelper.web.util.Converter; -import wusthelper.web.util.PasswordCodec; - -/** - * 通用的用户信息服务,包括登录和(已存储的)用户信息获取等,同时自动存入新用户信息 - */ -@Service -public class GeneralUserService { - private final StudentMapper studentMapper; - private final PasswordCodec passwordCodec; - - private final UndergradCookieService undergradCookieService; - private final GraduateCookieService graduateCookieService; - - private final GraduateStudentInfoRemote graduateStudentInfoRemote; - private final UndergradStudentInfoRemote undergradStudentInfoRemote; - - - public GeneralUserService(StudentMapper studentMapper, - PasswordCodec passwordCodec, - - UndergradCookieService undergradCookieService, - GraduateCookieService graduateCookieService, - - GraduateStudentInfoRemote graduateStudentInfoRemote, - UndergradStudentInfoRemote undergradStudentInfoRemote) { - - this.studentMapper = studentMapper; - this.passwordCodec = passwordCodec; - - this.undergradCookieService = undergradCookieService; - this.graduateCookieService = graduateCookieService; - - this.graduateStudentInfoRemote = graduateStudentInfoRemote; - this.undergradStudentInfoRemote = undergradStudentInfoRemote; - } - - public enum UserType {Undergrad, Graduate} - - /** - * 登录验证并获取用户信息,同时自动存库 - * - * @param username 用户名(即学号) - * @param password 密码(明文原文) - * @param userType 用户类型,现在有本科生和研究生 - * @return 用户信息 - */ - public Student login(String username, String password, UserType userType) { - var cookie = switch (userType) { - case Undergrad -> undergradCookieService.getLoginCookie(username, password); - case Graduate -> graduateCookieService.getLoginCookie(username, password); - }; - - var cookieType = switch (userType) { - case Undergrad -> CookieType.Undergrad; - case Graduate -> CookieType.Graduate; - }; - - // 保存cookie,并获取用户信息并存库(基本的操作,非异步,信息获取失败意味登陆失败) - CookieManager.store(username, cookie, cookieType); - return this.saveOrUpdateUser(username, cookie, password, userType); - } - - private Student saveOrUpdateUser(String username, String cookie, String password, UserType userType) { - var rpcResp = switch (userType) { - case Undergrad -> undergradStudentInfoRemote.get(cookie); - case Graduate -> graduateStudentInfoRemote.get(cookie); - }; - - if (rpcResp.code() != ServiceCode.Ok) { - ServiceException.of(rpcResp.code()); - } - - var studentInfo = rpcResp.data(); - var student = Converter.convert(studentInfo); - if (student.getStuNum() == null) { - student.setStuNum(username); - } - - var query = new QueryWrapper() - .eq("stu_num", username) - .eq("deleted", 0); - - // (按学号)检查用户信息是否已经存在数据库中,已存在则更新,否则新增信息 - var studentExists = studentMapper.exists(query); - if (studentExists) { - studentMapper.update(student, query); - } else { - student.setId(YitIdHelper.nextId()); - var encrypted = passwordCodec.encode(password); - student.setJwcPwd(encrypted); - if (userType == UserType.Undergrad) { - student.setLibPwd(encrypted); - } - - studentMapper.insert(student); - } - - return student; - } -} diff --git a/external-library/mywust b/external-library/mywust index 367ddbb..0ca9d1a 160000 --- a/external-library/mywust +++ b/external-library/mywust @@ -1 +1 @@ -Subproject commit 367ddbb930b50cadbb56bd97acee05d721b9427b +Subproject commit 0ca9d1a48da91b30933a3dd35e3428bb5207078f