feat: 🎸 学员注册

This commit is contained in:
GoCo 2025-09-11 10:08:28 +08:00
parent 561eed3147
commit 625d8b1383

View File

@ -16,16 +16,22 @@ import org.jeecg.common.util.RedisUtil;
import org.jeecg.config.shiro.IgnoreAuth;
import org.jeecg.modules.aiol.dto.TeacherInfo;
import org.jeecg.modules.aiol.dto.UserInfoResponse;
import org.jeecg.modules.aiol.entity.AiolClass;
import org.jeecg.modules.aiol.entity.AiolClassStudent;
import org.jeecg.modules.aiol.entity.AiolUserInfo;
import org.jeecg.modules.aiol.mapper.AiolUserInfoMapper;
import org.jeecg.modules.aiol.service.IAiolClassService;
import org.jeecg.modules.aiol.service.IAiolClassStudentService;
import org.jeecg.modules.aiol.service.IAiolUserInfoService;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@ -46,6 +52,10 @@ public class AiolUserController {
private AiolUserInfoMapper userInfoMapper;
@Autowired
private ISysBaseAPI sysBaseApi;
@Autowired
private IAiolClassService aiolClassService;
@Autowired
private IAiolClassStudentService aiolClassStudentService;
@PostMapping("/login")
@Operation(summary = "用户登录")
@ -191,4 +201,83 @@ public class AiolUserController {
.collect(Collectors.toList());
return Result.OK(schools);
}
@PostMapping("/register")
@Operation(summary = "学生注册", description = "学生通过学号、邀请码和密码进行注册,注册成功后自动加入对应班级")
@IgnoreAuth
@Transactional(rollbackFor = Exception.class)
public Result<JSONObject> studentRegister(@RequestBody Map<String, String> registerData) {
Result<JSONObject> result = new Result<>();
try {
// 1. 获取注册参数
String studentNumber = registerData.get("studentNumber");
String inviteCode = registerData.get("inviteCode");
String password = registerData.get("password");
// 2. 参数验证
if (studentNumber == null || studentNumber.trim().isEmpty()) {
return result.error500("学号不能为空");
}
if (inviteCode == null || inviteCode.trim().isEmpty()) {
return result.error500("邀请码不能为空");
}
if (password == null || password.trim().isEmpty()) {
return result.error500("密码不能为空");
}
// 3. 检查学号是否已被注册
SysUser existingUser = sysUserService.getUserByName(studentNumber);
if (existingUser != null) {
return result.error500("学号已被注册,请使用其他学号");
}
// 4. 检查邀请码是否对应某个班级
LambdaQueryWrapper<AiolClass> classQuery = new LambdaQueryWrapper<>();
classQuery.eq(AiolClass::getInviteCode, inviteCode);
AiolClass targetClass = aiolClassService.getOne(classQuery);
if (targetClass == null) {
return result.error500("邀请码无效,请检查邀请码是否正确");
}
// 5. 使用通用方法创建学生用户
SysUser studentUser = sysUserService.createStudentUser(studentNumber, studentNumber, password);
// 6. 设置工号学号
studentUser.setWorkNo(studentNumber);
sysUserService.updateById(studentUser);
// 7. 自动加入班级
AiolClassStudent classStudent = new AiolClassStudent();
classStudent.setClassId(targetClass.getId());
classStudent.setStudentId(studentUser.getId());
classStudent.setCreateBy("system"); // 系统创建
classStudent.setCreateTime(new Date());
boolean classStudentSaved = aiolClassStudentService.save(classStudent);
if (!classStudentSaved) {
throw new RuntimeException("加入班级失败");
}
// 8. 构建返回结果
JSONObject response = new JSONObject();
response.put("userId", studentUser.getId());
response.put("username", studentUser.getUsername());
response.put("workNo", studentUser.getWorkNo());
response.put("classId", targetClass.getId());
response.put("className", targetClass.getName());
response.put("message", "注册成功,已自动加入班级:" + targetClass.getName());
log.info("学生注册成功: 学号={}, 班级ID={}, 班级名={}",
studentNumber, targetClass.getId(), targetClass.getName());
result.setResult(response);
result.success("注册成功");
return result;
} catch (Exception e) {
log.error("学生注册失败: {}", e.getMessage(), e);
return result.error500("注册失败: " + e.getMessage());
}
}
}