main
parent
7f8fabd46e
commit
d714db0891
@ -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> { |
||||||
|
} |
@ -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; |
||||||
|
} |
@ -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…
Reference in new issue