lensfrex 1 year ago
parent 7f8fabd46e
commit d714db0891
Signed by: lensfrex
GPG Key ID: 0F69A0A2FBEE98A0
  1. 9
      backend-main/backend-data/src/main/java/wusthelper/data/dao/mapper/UserBasicMapper.java
  2. 6
      backend-main/backend-data/src/main/java/wusthelper/data/entity/Student.java
  3. 54
      backend-main/backend-data/src/main/java/wusthelper/data/entity/UserBasic.java
  4. 14
      backend-main/backend-web/src/main/java/wusthelper/web/api/v2/undergrade/UndergradLoginController.java
  5. 99
      backend-main/backend-web/src/main/java/wusthelper/web/service/campus/GeneralUserLoginService.java
  6. 116
      backend-main/backend-web/src/main/java/wusthelper/web/service/campus/GeneralUserService.java
  7. 2
      external-library/mywust

@ -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<UserBasic> {
}

@ -1,8 +1,14 @@
package wusthelper.data.entity; package wusthelper.data.entity;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor;
@Data @Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Student { public class Student {
/** /**
* 本科生id * 本科生id

@ -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;
}

@ -1,19 +1,17 @@
package wusthelper.web.api.v2.undergrade; package wusthelper.web.api.v2.undergrade;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import wusthelper.internal.rpc.response.RpcCommonResponseCode;
import wusthelper.web.api.v2.Token; import wusthelper.web.api.v2.Token;
import wusthelper.web.response.Response; import wusthelper.web.response.Response;
import wusthelper.web.service.campus.GeneralUserService; import wusthelper.web.service.campus.GeneralUserLoginService;
import wusthelper.web.service.campus.undergrad.UndergradCookieService;
@RestController @RestController
@RequestMapping("/jwc") @RequestMapping("/jwc")
public class UndergradLoginController { public class UndergradLoginController {
private final GeneralUserService generalUserService; private final GeneralUserLoginService generalUserLoginService;
public UndergradLoginController(GeneralUserService generalUserService) { public UndergradLoginController(GeneralUserLoginService generalUserLoginService) {
this.generalUserService = generalUserService; this.generalUserLoginService = generalUserLoginService;
} }
@PostMapping("/login") @PostMapping("/login")
@ -21,9 +19,9 @@ public class UndergradLoginController {
@RequestParam(value = "jwcPwd") String password, @RequestParam(value = "jwcPwd") String password,
@RequestHeader(name = "Platform", required = false) String platform) { @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); return Response.success(token);
} }

@ -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<UserBasic>()
.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;
}
}

@ -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<Student>()
.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;
}
}

@ -1 +1 @@
Subproject commit 367ddbb930b50cadbb56bd97acee05d721b9427b Subproject commit 0ca9d1a48da91b30933a3dd35e3428bb5207078f
Loading…
Cancel
Save