diff --git a/fw-dalaran-server/src/main/java/cn/fw/dalaran/server/config/MyControllerAdvice.java b/fw-dalaran-server/src/main/java/cn/fw/dalaran/server/config/MyControllerAdvice.java new file mode 100644 index 0000000..18edcc1 --- /dev/null +++ b/fw-dalaran-server/src/main/java/cn/fw/dalaran/server/config/MyControllerAdvice.java @@ -0,0 +1,58 @@ +package cn.fw.dalaran.server.config; + +import cn.fw.dalaran.common.exception.BusinessException; +import cn.fw.data.base.domain.common.Message; +import org.springframework.validation.BindException; +import org.springframework.validation.FieldError; +import org.springframework.validation.ObjectError; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author wmy3969 + * @version 1.0 + * @date 2022/7/26 10:39 + * @Description 异常拦截器 + */ +@RestControllerAdvice +public class MyControllerAdvice { + + /** + * BusinessException + */ + @ExceptionHandler(value = {BusinessException.class}) + public Message handleException(BusinessException e) { + return Message.failure(50000, e.getMessage()); + } + + /** + * 参数校验未通过异常处理 + */ + @ExceptionHandler(value = {MethodArgumentNotValidException.class, BindException.class}) + public Message exception(Exception e) { + List allErrors = new ArrayList<>(); + if (e instanceof MethodArgumentNotValidException) + allErrors = ((MethodArgumentNotValidException) e).getBindingResult().getAllErrors(); + else if (e instanceof BindException) + allErrors = ((BindException) e).getBindingResult().getAllErrors(); + StringBuilder sb = new StringBuilder(); + int size = allErrors.size(); + if (size > 0) { + for (int i = 0; i < size; i++) { + FieldError error = (FieldError) allErrors.get(i); + sb.append("「"); + sb.append(error.getField()); + sb.append("」"); + sb.append(error.getDefaultMessage()); + if (i < size - 1) + sb.append(","); + } + } + return Message.failure(sb.toString()); + } + +} diff --git a/fw-dalaran-server/src/main/java/cn/fw/dalaran/server/controller/app/OtherController.java b/fw-dalaran-server/src/main/java/cn/fw/dalaran/server/controller/app/OtherController.java index f37f4c4..d641ab1 100644 --- a/fw-dalaran-server/src/main/java/cn/fw/dalaran/server/controller/app/OtherController.java +++ b/fw-dalaran-server/src/main/java/cn/fw/dalaran/server/controller/app/OtherController.java @@ -21,7 +21,6 @@ import javax.validation.constraints.NotNull; import java.io.File; import java.util.Objects; -import static cn.fw.common.web.util.ResultBuilder.failure; import static cn.fw.common.web.util.ResultBuilder.success; /** @@ -60,11 +59,8 @@ public class OtherController { * @return */ @PostMapping("/saveCheckResult") - public Message saveCheckResult(@CurrentUser LoginAuthBean user, @RequestBody @Valid LiveCheckParams param) { - final String result = otherBizService.saveCheckResult(user.getUserId(), param); - if (result.contains("成功")) - return success(result); - return failure(-1, result, result); + public Message saveCheckResult(@CurrentUser LoginAuthBean user, @RequestBody @Valid LiveCheckParams param) { + return success(otherBizService.saveCheckResult(user.getUserId(), param)); } /** diff --git a/fw-dalaran-server/src/main/java/cn/fw/dalaran/server/controller/web/AccountController.java b/fw-dalaran-server/src/main/java/cn/fw/dalaran/server/controller/web/AccountController.java index c2e3c9f..ec8e0b6 100644 --- a/fw-dalaran-server/src/main/java/cn/fw/dalaran/server/controller/web/AccountController.java +++ b/fw-dalaran-server/src/main/java/cn/fw/dalaran/server/controller/web/AccountController.java @@ -20,10 +20,8 @@ import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import javax.validation.constraints.NotNull; - import java.util.List; -import static cn.fw.common.web.util.ResultBuilder.failure; import static cn.fw.common.web.util.ResultBuilder.success; /** @@ -43,12 +41,9 @@ public class AccountController { private final AccountBizService accountBizService; @PostMapping("/add") - public Message add(@CurrentUser LoginAuthBean user, @RequestBody @Validated final AccountDTO accountDTO) { + public Message add(@CurrentUser LoginAuthBean user, @RequestBody @Validated final AccountDTO accountDTO) { accountDTO.setGroupId(user.getGroupId()); - final String result = accountBizService.saveAccount(accountDTO); - if (result.contains("成功")) - return success(result); - return failure(-1, result, result); + return success(accountBizService.saveAccount(accountDTO)); } @PutMapping("/authorized/report") diff --git a/fw-dalaran-server/src/main/java/cn/fw/dalaran/server/controller/web/ActivityThemeController.java b/fw-dalaran-server/src/main/java/cn/fw/dalaran/server/controller/web/ActivityThemeController.java index 16d620b..feff901 100644 --- a/fw-dalaran-server/src/main/java/cn/fw/dalaran/server/controller/web/ActivityThemeController.java +++ b/fw-dalaran-server/src/main/java/cn/fw/dalaran/server/controller/web/ActivityThemeController.java @@ -4,6 +4,7 @@ import cn.fw.common.page.AppPage; import cn.fw.common.web.annotation.ControllerMethod; import cn.fw.common.web.auth.LoginAuthBean; import cn.fw.common.web.auth.annotation.CurrentUser; +import cn.fw.dalaran.common.exception.BusinessException; import cn.fw.dalaran.domain.db.ActivityTheme; import cn.fw.dalaran.domain.param.ActivityThemePageParams; import cn.fw.dalaran.domain.param.AddThemeCoverParams; @@ -24,7 +25,6 @@ import javax.validation.constraints.NotNull; import java.util.Date; import java.util.List; -import static cn.fw.common.web.util.ResultBuilder.failure; import static cn.fw.common.web.util.ResultBuilder.success; /** @@ -50,11 +50,9 @@ public class ActivityThemeController { * @return 操作结果 */ @PostMapping("/save") - public Message save(@RequestBody @Valid ActivityThemeVo themeVo) { - if (activityThemeService.validThemeDateDuplicate(themeVo)) { - final String failResult = "指定门店在指定时间段内正在参与其他活动, 添加失败"; - return failure(-1, failResult, failResult); - } + public Message save(@RequestBody @Valid ActivityThemeVo themeVo) { + if (activityThemeService.validThemeDateDuplicate(themeVo)) + throw new BusinessException("指定门店在指定时间段内正在参与其他活动, 添加失败"); return success(activityThemeService.save(themeVo)); } @@ -65,11 +63,8 @@ public class ActivityThemeController { * @return 操作结果 */ @PostMapping("/addTopic") - public Message addTopic(@RequestBody @Valid AddThemeTopicParams params) { - final String result = activityThemeService.addTopic(params); - if (result.contains("成功")) - return success(result); - return failure(-1, result, result); + public Message addTopic(@RequestBody @Valid AddThemeTopicParams params) { + return success(activityThemeService.addTopic(params)); } /** diff --git a/fw-dalaran-server/src/main/java/cn/fw/dalaran/server/controller/web/ConfigGroupController.java b/fw-dalaran-server/src/main/java/cn/fw/dalaran/server/controller/web/ConfigGroupController.java index f880d53..fdeb1fc 100644 --- a/fw-dalaran-server/src/main/java/cn/fw/dalaran/server/controller/web/ConfigGroupController.java +++ b/fw-dalaran-server/src/main/java/cn/fw/dalaran/server/controller/web/ConfigGroupController.java @@ -17,7 +17,6 @@ import org.springframework.web.bind.annotation.*; import javax.validation.Valid; import java.util.List; -import static cn.fw.common.web.util.ResultBuilder.failure; import static cn.fw.common.web.util.ResultBuilder.success; /** @@ -56,11 +55,8 @@ public class ConfigGroupController { * @return 操作结果 */ @PostMapping("/update") - public Message saveOrUpdate(@CurrentUser LoginAuthBean user, @RequestBody @Valid ConfigGroupVo param) { - final String result = configGroupService.update(user.getUserId(), param); - if (result.contains("成功")) - return success(result); - return failure(-1, result, result); + public Message saveOrUpdate(@CurrentUser LoginAuthBean user, @RequestBody @Valid ConfigGroupVo param) { + return success(configGroupService.update(user.getUserId(), param)); } /** diff --git a/fw-dalaran-service/src/main/java/cn/fw/dalaran/service/biz/AccountBizService.java b/fw-dalaran-service/src/main/java/cn/fw/dalaran/service/biz/AccountBizService.java index 1ab9a7c..425df6f 100644 --- a/fw-dalaran-service/src/main/java/cn/fw/dalaran/service/biz/AccountBizService.java +++ b/fw-dalaran-service/src/main/java/cn/fw/dalaran/service/biz/AccountBizService.java @@ -3,6 +3,7 @@ package cn.fw.dalaran.service.biz; import cn.fw.common.page.AppPage; import cn.fw.common.web.annotation.DisLock; import cn.fw.dalaran.common.constants.DalaranConstants; +import cn.fw.dalaran.common.exception.BusinessException; import cn.fw.dalaran.common.utils.DateUtil; import cn.fw.dalaran.domain.db.Account; import cn.fw.dalaran.domain.db.TodoHistory; @@ -51,7 +52,7 @@ public class AccountBizService { */ @Transactional(rollbackFor = Exception.class) @DisLock(prefix = "':' + #this.getClass().getSimpleName()", key = "#accountDTO.groupId + ':' + #accountDTO.account", message = "请勿重复提交") - public String saveAccount(AccountDTO accountDTO) { + public boolean saveAccount(AccountDTO accountDTO) { checkExist(accountDTO); Account db = new Account(); BeanUtils.copyProperties(accountDTO, db); @@ -67,7 +68,7 @@ public class AccountBizService { Pattern p = Pattern.compile("^[0-9].*"); Matcher m = p.matcher(accountDTO.getAccount()); if (m.matches() && Objects.equals(accountDTO.getType(), PlatformEnum.KS)) - return "添加快手账号失败, 必须指定直播搜索关键词"; + throw new BusinessException("添加快手账号失败, 必须指定直播搜索关键词"); } } db.setYn(Boolean.TRUE); @@ -75,7 +76,7 @@ public class AccountBizService { if (Objects.isNull(accountDTO.getId())) { db.setCreateTime(DateUtil.localDateTime2Date(LocalDateTime.now().plusDays(-1))); } - return accountService.saveOrUpdate(db) ? "添加成功" : "添加失败"; + return accountService.saveOrUpdate(db); } /** diff --git a/fw-dalaran-service/src/main/java/cn/fw/dalaran/service/biz/OtherBizService.java b/fw-dalaran-service/src/main/java/cn/fw/dalaran/service/biz/OtherBizService.java index 0709905..deecbc3 100644 --- a/fw-dalaran-service/src/main/java/cn/fw/dalaran/service/biz/OtherBizService.java +++ b/fw-dalaran-service/src/main/java/cn/fw/dalaran/service/biz/OtherBizService.java @@ -1,6 +1,7 @@ package cn.fw.dalaran.service.biz; import cn.fw.dalaran.common.constants.DalaranConstants; +import cn.fw.dalaran.common.exception.BusinessException; import cn.fw.dalaran.common.utils.PublicUtil; import cn.fw.dalaran.common.utils.StringUtils; import cn.fw.dalaran.domain.db.*; @@ -117,17 +118,17 @@ public class OtherBizService { * @return 操作结果 */ @Transactional - public String saveCheckResult(Long userId, LiveCheckParams param) { + public boolean saveCheckResult(Long userId, LiveCheckParams param) { final Integer type = param.getType(); final Long dataId = param.getDataId(); if (Objects.equals(type, 0)) { final LiveCheckResult liveCheckResult = PublicUtil.copy(param.getCheckResult(), LiveCheckResult.class); final Long liveCheckResultId = liveCheckResult.getId(); if (Objects.equals(liveCheckResult.getCounterfeit(), -1)) - return "请选择是否空挂"; + throw new BusinessException("请选择是否空挂"); if (Objects.equals(liveCheckResult.getCounterfeit(), 1) && StringUtils.isEmpty(liveCheckResult.getOpinion())) - return "判断空挂必须给出审核意见"; + throw new BusinessException("判断空挂必须给出审核意见"); final List oldFileIds = Optional.ofNullable(themeFileService.lambdaQuery() .eq(ThemeFile::getType, FileTypeEnum.LIVE_CHECK.getValue()) .eq(ThemeFile::getExtraId, liveCheckResultId) @@ -163,11 +164,11 @@ public class OtherBizService { ); liveCheckResult.setStatus(type); liveCheckResult.setValid(Objects.equals(liveCheckResult.getCounterfeit(), 0) && Objects.equals(liveCheckResult.getPay(), 0) ? 1 : 0); - return liveCheckResultService.updateById(liveCheckResult) ? "保存成功" : "保存失败"; + return liveCheckResultService.updateById(liveCheckResult); } else if (Objects.equals(type, 1)) {// 完成待办 final List summaries = this.getLiveCheck(userId, dataId).getLiveList(); if (summaries.stream().anyMatch(item -> Objects.equals(item.getCounterfeit(), -1))) { - return "还有未完成审核的直播, 请完成所有直播审核后重新提交"; + throw new BusinessException("还有未完成审核的直播, 请完成所有直播审核后重新提交"); } else { if (!todoHistoryService.lambdaUpdate() .set(TodoHistory::getDone, true) @@ -175,7 +176,7 @@ public class OtherBizService { .eq(TodoHistory::getDataId, dataId) .eq(TodoHistory::getTodoCode, DalaranConstants.CHECK_LIVE) .update()) - return "完成待办失败"; + throw new BusinessException("完成待办失败"); final List invalidLiveIds = summaries.stream() .filter(item -> Objects.equals(item.getValid(), 0)) .map(LiveCheckVo.liveSummary::getLiveId) @@ -206,10 +207,10 @@ public class OtherBizService { .stream() .map(LiveCheckVo.liveSummary::getId) .collect(Collectors.toList()) - ).update() ? "提交成功" : "提交失败"; + ).update(); } } - return "未知错误"; + return false; } /** diff --git a/fw-dalaran-service/src/main/java/cn/fw/dalaran/service/data/ActivityThemeService.java b/fw-dalaran-service/src/main/java/cn/fw/dalaran/service/data/ActivityThemeService.java index 203c757..0271e23 100644 --- a/fw-dalaran-service/src/main/java/cn/fw/dalaran/service/data/ActivityThemeService.java +++ b/fw-dalaran-service/src/main/java/cn/fw/dalaran/service/data/ActivityThemeService.java @@ -44,7 +44,7 @@ public interface ActivityThemeService extends IService { * @param themeVo 参数 * @return */ - String save(ActivityThemeVo themeVo); + boolean save(ActivityThemeVo themeVo); /** * 增加话题 @@ -52,7 +52,7 @@ public interface ActivityThemeService extends IService { * @param params 增加话题带参 * @return */ - String addTopic(AddThemeTopicParams params); + boolean addTopic(AddThemeTopicParams params); /** * 增加封面 diff --git a/fw-dalaran-service/src/main/java/cn/fw/dalaran/service/data/ConfigGroupService.java b/fw-dalaran-service/src/main/java/cn/fw/dalaran/service/data/ConfigGroupService.java index d49c5c9..5b50966 100644 --- a/fw-dalaran-service/src/main/java/cn/fw/dalaran/service/data/ConfigGroupService.java +++ b/fw-dalaran-service/src/main/java/cn/fw/dalaran/service/data/ConfigGroupService.java @@ -29,7 +29,7 @@ public interface ConfigGroupService extends IService { * @param param 参数封装 * @return */ - String update(Long userId, ConfigGroupVo param); + boolean update(Long userId, ConfigGroupVo param); /** * 查询本集团的配置组列表 diff --git a/fw-dalaran-service/src/main/java/cn/fw/dalaran/service/data/impl/ActivityThemeServiceImpl.java b/fw-dalaran-service/src/main/java/cn/fw/dalaran/service/data/impl/ActivityThemeServiceImpl.java index c7ec09d..a63aff2 100644 --- a/fw-dalaran-service/src/main/java/cn/fw/dalaran/service/data/impl/ActivityThemeServiceImpl.java +++ b/fw-dalaran-service/src/main/java/cn/fw/dalaran/service/data/impl/ActivityThemeServiceImpl.java @@ -113,9 +113,8 @@ public class ActivityThemeServiceImpl extends ServiceImpl newTopic = params.getTopic().stream() .map(item -> { String replace = item.replace("#", ""); @@ -174,13 +173,14 @@ public class ActivityThemeServiceImpl extends ServiceImpl oldTopic = Arrays.asList(theme.getTopic().split(",").clone()); ArrayList result = new ArrayList<>(newTopic); result.retainAll(oldTopic); - if (new Date().compareTo(theme.getEndTime()) > 0) - return "已经结束的主题不允许修改话题"; + long timeSub = System.currentTimeMillis() - theme.getEndTime().getTime(); + if (timeSub > 24 * 3600 * 1000 + 1000) + throw new BusinessException("活动已经结束数据抓取, 不能再增加话题"); if (result.size() < oldTopic.size()) - return "不允许删除原有话题"; + throw new BusinessException("不允许删除原有话题"); if (Objects.equals(newTopic.size(), oldTopic.size())) - return "未进行任何添加操作"; - return this.updateById(activityTheme) ? "增加话题成功" : "增加话题失败"; + throw new BusinessException("未进行任何添加操作"); + return this.updateById(activityTheme); } /** @@ -198,8 +198,12 @@ public class ActivityThemeServiceImpl extends ServiceImpl Objects.isNull(item.getId())) .peek(item -> item.setType(FileTypeEnum.THEME_COVER.getValue())) .collect(Collectors.toList()); + ActivityTheme theme = this.getById(themeId); + long timeSub = System.currentTimeMillis() - theme.getEndTime().getTime(); + if (timeSub > 24 * 3600 * 1000 + 1000) + throw new BusinessException("活动已经结束数据抓取, 不能再增加封面"); this.saveFiles(themeId, fileList); - this.downloadAndConvertCover(this.getById(themeId), fileList); + this.downloadAndConvertCover(theme, fileList); return true; } diff --git a/fw-dalaran-service/src/main/java/cn/fw/dalaran/service/data/impl/ConfigGroupServiceImpl.java b/fw-dalaran-service/src/main/java/cn/fw/dalaran/service/data/impl/ConfigGroupServiceImpl.java index 6727522..9b2255b 100644 --- a/fw-dalaran-service/src/main/java/cn/fw/dalaran/service/data/impl/ConfigGroupServiceImpl.java +++ b/fw-dalaran-service/src/main/java/cn/fw/dalaran/service/data/impl/ConfigGroupServiceImpl.java @@ -1,6 +1,7 @@ package cn.fw.dalaran.service.data.impl; import cn.fw.dalaran.common.constants.DalaranConstants; +import cn.fw.dalaran.common.exception.BusinessException; import cn.fw.dalaran.dao.ConfigGroupDao; import cn.fw.dalaran.domain.db.ActivityTheme; import cn.fw.dalaran.domain.db.ConfigGroup; @@ -72,7 +73,7 @@ public class ConfigGroupServiceImpl extends ServiceImpl 0) - throw new RuntimeException("该用户存在一个已初始化但未赋值修改的配置组"); + throw new BusinessException("该用户存在一个已初始化但未赋值修改的配置组"); ConfigGroup configGroup = new ConfigGroup(); configGroup.setUserId(userId); configGroup.setGroupId(groupId); @@ -82,7 +83,7 @@ public class ConfigGroupServiceImpl extends ServiceImpl list = this.lambdaQuery() .eq(ConfigGroup::getGroupId, param.getGroupId()) @@ -104,9 +105,9 @@ public class ConfigGroupServiceImpl extends ServiceImpl 0) return "该集团已存在门店配置, 新配置不能设置为[全部门店]"; + if (list.size() > 0) throw new BusinessException("该集团已存在门店配置, 新配置不能设置为[全部门店]"); } else if (list.stream().anyMatch(item -> Objects.equals(item.getAllShop(), 1))) {// 已存在的配置组是否有全部门店的 - return "该集团已存在[全部门店]配置, 新配置不能生效"; + throw new BusinessException("该集团已存在[全部门店]配置, 新配置不能生效"); } else { List shopIds = param.getShopIds();// 本次配置指定生效门店 List shopNames = param.getShopNames();// 本次配置指定生效门店 @@ -130,7 +131,7 @@ public class ConfigGroupServiceImpl extends ServiceImpl