fix: 修复活动报名接口ID类型精度丢失问题

- 将活动ID从Long改为String类型,避免前端JSON反序列化时的精度丢失
- 增强报名接口的参数校验和异常处理
- 优化错误提示信息,提供更详细的错误原因
This commit is contained in:
Leo 2025-10-15 22:29:32 +08:00
parent 4bc8961605
commit e6bad39a56
4 changed files with 42 additions and 11 deletions

View File

@ -12,10 +12,10 @@ import lombok.Data;
public class EventRegistrationVo { public class EventRegistrationVo {
/** /**
* 活动ID * 活动ID使用String避免前端JSON反序列化时的精度丢失
*/ */
@NotNull(message = "活动ID不能为空") @NotNull(message = "活动ID不能为空")
private Long eventId; private String eventId;
/** /**
* 联系电话 * 联系电话

View File

@ -62,7 +62,7 @@ public class HrtEventController {
@Operation(summary = "取消报名", description = "取消已报名的活动,活动开始后无法取消") @Operation(summary = "取消报名", description = "取消已报名的活动,活动开始后无法取消")
@PostMapping("/cancel/{eventId}") @PostMapping("/cancel/{eventId}")
public String cancelRegistration( public String cancelRegistration(
@Parameter(description = "活动ID") @PathVariable Long eventId @Parameter(description = "活动ID") @PathVariable String eventId
) { ) {
return hrtEventService.cancelRegistration(eventId); return hrtEventService.cancelRegistration(eventId);
} }

View File

@ -31,7 +31,7 @@ public interface HrtEventService {
* @description [取消报名] * @description [取消报名]
* @author Leocoder * @author Leocoder
*/ */
String cancelRegistration(Long eventId); String cancelRegistration(String eventId);
/** /**
* @description [我报名的活动] * @description [我报名的活动]

View File

@ -158,9 +158,27 @@ public class HrtEventServiceImpl implements HrtEventService {
public String registerEvent(EventRegistrationVo vo) { public String registerEvent(EventRegistrationVo vo) {
Long userId = HrtStpUtil.getLoginIdAsLong(); Long userId = HrtStpUtil.getLoginIdAsLong();
HrtEvent event = hrtEventMapper.selectById(vo.getEventId()); // 参数校验
if (event == null || event.getPublishStatus() != 1) { if (vo == null || vo.getEventId() == null || vo.getEventId().trim().isEmpty()) {
throw new BusinessException(404, "活动不存在或未发布"); throw new BusinessException(400, "活动ID不能为空");
}
// 将String类型的eventId转换为Long避免前端JSON精度丢失
Long eventId;
try {
eventId = Long.parseLong(vo.getEventId());
} catch (NumberFormatException e) {
throw new BusinessException(400, "活动ID格式不正确");
}
// 查询活动
HrtEvent event = hrtEventMapper.selectById(eventId);
if (event == null) {
throw new BusinessException(404, "活动不存在(ID:" + eventId + ")");
}
if (event.getPublishStatus() != 1) {
throw new BusinessException(400, "活动未发布,无法报名");
} }
if (!event.getStatus().equals("upcoming")) { if (!event.getStatus().equals("upcoming")) {
@ -182,7 +200,7 @@ public class HrtEventServiceImpl implements HrtEventService {
LambdaQueryWrapper<HrtEventRegistration> wrapper = new LambdaQueryWrapper<>(); LambdaQueryWrapper<HrtEventRegistration> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(HrtEventRegistration::getUserId, userId) wrapper.eq(HrtEventRegistration::getUserId, userId)
.eq(HrtEventRegistration::getEventId, vo.getEventId()) .eq(HrtEventRegistration::getEventId, eventId)
.in(HrtEventRegistration::getStatus, List.of("pending", "approved")); .in(HrtEventRegistration::getStatus, List.of("pending", "approved"));
Long count = hrtEventRegistrationMapper.selectCount(wrapper); Long count = hrtEventRegistrationMapper.selectCount(wrapper);
if (count > 0) { if (count > 0) {
@ -195,7 +213,7 @@ public class HrtEventServiceImpl implements HrtEventService {
HrtEventRegistration registration = new HrtEventRegistration(); HrtEventRegistration registration = new HrtEventRegistration();
registration.setUserId(userId); registration.setUserId(userId);
registration.setEventId(vo.getEventId()); registration.setEventId(eventId);
registration.setStatus("approved"); registration.setStatus("approved");
registration.setPhone(vo.getPhone()); registration.setPhone(vo.getPhone());
registration.setRemark(vo.getRemark()); registration.setRemark(vo.getRemark());
@ -208,7 +226,7 @@ public class HrtEventServiceImpl implements HrtEventService {
hrtEventMapper.update(null, hrtEventMapper.update(null,
new LambdaUpdateWrapper<HrtEvent>() new LambdaUpdateWrapper<HrtEvent>()
.setSql("current_participants = current_participants + 1") .setSql("current_participants = current_participants + 1")
.eq(HrtEvent::getId, vo.getEventId()) .eq(HrtEvent::getId, eventId)
); );
return "报名成功"; return "报名成功";
@ -220,9 +238,22 @@ public class HrtEventServiceImpl implements HrtEventService {
*/ */
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public String cancelRegistration(Long eventId) { public String cancelRegistration(String eventIdStr) {
Long userId = HrtStpUtil.getLoginIdAsLong(); Long userId = HrtStpUtil.getLoginIdAsLong();
// 参数校验
if (eventIdStr == null || eventIdStr.trim().isEmpty()) {
throw new BusinessException(400, "活动ID不能为空");
}
// 将String转换为Long避免前端JSON精度丢失
Long eventId;
try {
eventId = Long.parseLong(eventIdStr);
} catch (NumberFormatException e) {
throw new BusinessException(400, "活动ID格式不正确");
}
LambdaQueryWrapper<HrtEventRegistration> wrapper = new LambdaQueryWrapper<>(); LambdaQueryWrapper<HrtEventRegistration> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(HrtEventRegistration::getUserId, userId) wrapper.eq(HrtEventRegistration::getUserId, userId)
.eq(HrtEventRegistration::getEventId, eventId) .eq(HrtEventRegistration::getEventId, eventId)