|
|
@@ -86,11 +86,6 @@ public class UserServiceImpl implements UserService {
|
|
|
//TODO: 注册DEMO:学生老师都可自行注册账号,管理员那套不需要修改/弃用,学生注册这套已经有完整逻辑,直接使用
|
|
|
@Override
|
|
|
public UserVO createUser(UserRegisterDTO userDTO) throws MyException{
|
|
|
- Role role = userDTO.getRole();
|
|
|
-// if (role != Role.STUDENT) { // 只允许注册学生账号
|
|
|
-// throw new MyException(HttpStatus.BAD_REQUEST.value(), HttpStatus.BAD_REQUEST.getReasonPhrase()+":"+"权限不够");
|
|
|
-// }
|
|
|
-
|
|
|
// 从Redis中获取验证码
|
|
|
final String key = "verifyCode:" + userDTO.getOfficialEmail();
|
|
|
String storedCode = (String) redisTemplate.opsForValue().get(key);
|
|
|
@@ -105,7 +100,9 @@ public class UserServiceImpl implements UserService {
|
|
|
targetUser.setPassword(new BCryptPasswordEncoder().encode(targetUser.getPassword())); // 密码加密存储
|
|
|
targetUser.setCreateTime(new Date()); // 设置创建时间
|
|
|
|
|
|
- if (isUserNotExists(targetUser.getOfficialNumber())) {
|
|
|
+ // 同一个邮箱、学号只能注册一次
|
|
|
+ if (isOfficialNumberNotExists(targetUser.getOfficialNumber())
|
|
|
+ && isEmailNotExists(targetUser.getOfficialEmail())) {
|
|
|
int status = userMapper.insert(targetUser);
|
|
|
if (status == 0) {
|
|
|
log.error("createUser -- 数据库插入错误:");
|
|
|
@@ -169,8 +166,9 @@ public class UserServiceImpl implements UserService {
|
|
|
|
|
|
targetUser.setCreateTime(new Date());
|
|
|
|
|
|
- // 检查用户是否存在
|
|
|
- if (isUserNotExists(targetUser.getOfficialNumber())) {
|
|
|
+ // 同一个邮箱、学号只能注册一次
|
|
|
+ if (isOfficialNumberNotExists(targetUser.getOfficialNumber())
|
|
|
+ && isEmailNotExists(targetUser.getOfficialEmail())) {
|
|
|
int code = userMapper.insert(targetUser);
|
|
|
if (code == 0) {
|
|
|
log.error("createUser -- 数据库插入错误:");
|
|
|
@@ -294,6 +292,11 @@ public class UserServiceImpl implements UserService {
|
|
|
processUsers(users);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 批量创建用户,从csv文件中读取数据
|
|
|
+ * @param file csv文件
|
|
|
+ */
|
|
|
@Override
|
|
|
public void batchCreateUsersFromCsv(MultipartFile file) throws Exception {
|
|
|
CSVReader csvReader = new CSVReader(new InputStreamReader(file.getInputStream()));
|
|
|
@@ -309,11 +312,13 @@ public class UserServiceImpl implements UserService {
|
|
|
String[] nextRecord;
|
|
|
while ((nextRecord = csvReader.readNext()) != null) {
|
|
|
String officialNumber = String.valueOf(nextRecord[numberCol]);
|
|
|
- if (isUserNotExists(officialNumber)) {
|
|
|
+ String email = String.valueOf(nextRecord[emailCol]);
|
|
|
+ if (isOfficialNumberNotExists(officialNumber)
|
|
|
+ && isEmailNotExists(email)) {
|
|
|
User user = new User();
|
|
|
user.setName(String.valueOf(nextRecord[nameCol]));
|
|
|
user.setOfficialNumber(officialNumber);
|
|
|
- user.setOfficialEmail(String.valueOf(nextRecord[emailCol]));
|
|
|
+ user.setOfficialEmail(String.valueOf(email));
|
|
|
user.setRole(Role.STUDENT); // 批量创建只创建学生账号
|
|
|
user.setCreateTime(new Date());
|
|
|
|
|
|
@@ -329,11 +334,11 @@ public class UserServiceImpl implements UserService {
|
|
|
|
|
|
|
|
|
/**
|
|
|
- * 查看用户是否不存在,如果用户不存在,则返回true
|
|
|
- * @param officialNumber
|
|
|
- * @return
|
|
|
+ * 查看学号是否不存在
|
|
|
+ * @param officialNumber 学号
|
|
|
+ * @return true表示不存在,false表示存在
|
|
|
*/
|
|
|
- private boolean isUserNotExists(String officialNumber) throws MyException{
|
|
|
+ private boolean isOfficialNumberNotExists(String officialNumber) throws MyException{
|
|
|
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
|
|
|
queryWrapper.eq("official_number", officialNumber);
|
|
|
User existingUser = userMapper.selectOne(queryWrapper);
|
|
|
@@ -344,8 +349,26 @@ public class UserServiceImpl implements UserService {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查看邮箱是否不存在
|
|
|
+ * @param email 邮箱
|
|
|
+ * @return true表示不存在,false表示存在
|
|
|
+ */
|
|
|
+ private boolean isEmailNotExists(String email) throws MyException{
|
|
|
+ QueryWrapper<User> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.eq("official_email", email);
|
|
|
+ User existingUser = userMapper.selectOne(queryWrapper);
|
|
|
+ if (existingUser != null) {
|
|
|
+ log.error("用户已存在,邮箱{}重复", email);
|
|
|
+ throw new MyException(400, "用户已存在,请检查邮箱信息");
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
- * 检查用户是否存在,如果存在则返回true;不存在则报错
|
|
|
+ * 检查用户ID是否存在,如果存在则返回true;不存在则报错
|
|
|
* @param id
|
|
|
* @return
|
|
|
* @throws MyException
|