diff --git a/austin-common/src/main/java/com/java3y/austin/common/enums/RespStatusEnum.java b/austin-common/src/main/java/com/java3y/austin/common/enums/RespStatusEnum.java index 51d0085..c745ba0 100644 --- a/austin-common/src/main/java/com/java3y/austin/common/enums/RespStatusEnum.java +++ b/austin-common/src/main/java/com/java3y/austin/common/enums/RespStatusEnum.java @@ -14,6 +14,13 @@ import lombok.ToString; @ToString @AllArgsConstructor public enum RespStatusEnum { + + /** + * 错误 + */ + ERROR_500("500", "服务器未知错误"), + ERROR_400("400", "错误请求"), + /** * OK:操作成功 */ diff --git a/austin-web/src/main/java/com/java3y/austin/web/advice/AustinResponseBodyAdvice.java b/austin-web/src/main/java/com/java3y/austin/web/advice/AustinResponseBodyAdvice.java new file mode 100644 index 0000000..175a81a --- /dev/null +++ b/austin-web/src/main/java/com/java3y/austin/web/advice/AustinResponseBodyAdvice.java @@ -0,0 +1,42 @@ +package com.java3y.austin.web.advice; + +import com.java3y.austin.common.vo.BasicResultVO; +import com.java3y.austin.web.annotation.AustinResult; +import org.jetbrains.annotations.NotNull; +import org.springframework.core.MethodParameter; +import org.springframework.http.MediaType; +import org.springframework.http.server.ServerHttpRequest; +import org.springframework.http.server.ServerHttpResponse; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice; + +import java.util.Objects; + +/** + * @author kl + * @version 1.0.0 + * @description 统一返回结构 + * @date 2023/2/9 19:00 + */ +@ControllerAdvice(basePackages = "com.java3y.austin.web.controller") +public class AustinResponseBodyAdvice implements ResponseBodyAdvice { + + private static final String RETURN_CLASS = "BasicResultVO"; + + @Override + public boolean supports(MethodParameter methodParameter, Class aClass) { + return methodParameter.getContainingClass().isAnnotationPresent(AustinResult.class) || methodParameter.hasMethodAnnotation(AustinResult.class); + } + + @Override + public Object beforeBodyWrite(Object data, MethodParameter methodParameter, MediaType mediaType, Class aClass, + ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) { + if (Objects.nonNull(data) && Objects.nonNull(data.getClass())) { + String simpleName = data.getClass().getSimpleName(); + if (RETURN_CLASS.equalsIgnoreCase(simpleName)) { + return data; + } + } + return BasicResultVO.success(data); + } +} diff --git a/austin-web/src/main/java/com/java3y/austin/web/annotation/AustinResult.java b/austin-web/src/main/java/com/java3y/austin/web/annotation/AustinResult.java new file mode 100644 index 0000000..b89210b --- /dev/null +++ b/austin-web/src/main/java/com/java3y/austin/web/annotation/AustinResult.java @@ -0,0 +1,15 @@ +package com.java3y.austin.web.annotation; + +import java.lang.annotation.*; + +/** + * @author kl + * @version 1.0.0 + * @description 统一返回注解 + * @date 2023/2/9 19:00 + */ +@Target({ElementType.TYPE, ElementType.METHOD}) +@Retention(RetentionPolicy.RUNTIME) +@Documented +public @interface AustinResult { +} diff --git a/austin-web/src/main/java/com/java3y/austin/web/controller/ChannelAccountController.java b/austin-web/src/main/java/com/java3y/austin/web/controller/ChannelAccountController.java index 2a7f038..3a284fc 100644 --- a/austin-web/src/main/java/com/java3y/austin/web/controller/ChannelAccountController.java +++ b/austin-web/src/main/java/com/java3y/austin/web/controller/ChannelAccountController.java @@ -4,11 +4,13 @@ package com.java3y.austin.web.controller; import cn.hutool.core.util.StrUtil; import com.java3y.austin.common.constant.AustinConstant; import com.java3y.austin.common.enums.RespStatusEnum; -import com.java3y.austin.common.vo.BasicResultVO; import com.java3y.austin.support.domain.ChannelAccount; +import com.java3y.austin.web.annotation.AustinResult; +import com.java3y.austin.web.exception.CommonException; import com.java3y.austin.web.service.ChannelAccountService; import com.java3y.austin.web.utils.Convert4Amis; import com.java3y.austin.web.utils.LoginUtils; +import com.java3y.austin.web.vo.amis.CommonAmisVo; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; @@ -25,6 +27,7 @@ import java.util.stream.Collectors; * @author 3y */ @Slf4j +@AustinResult @RestController @RequestMapping("/account") @Api("渠道账号管理接口") @@ -42,13 +45,13 @@ public class ChannelAccountController { */ @PostMapping("/save") @ApiOperation("/保存数据") - public BasicResultVO saveOrUpdate(@RequestBody ChannelAccount channelAccount) { + public ChannelAccount saveOrUpdate(@RequestBody ChannelAccount channelAccount) { if (loginUtils.needLogin() && StrUtil.isBlank(channelAccount.getCreator())) { - return BasicResultVO.fail(RespStatusEnum.NO_LOGIN); + throw new CommonException(RespStatusEnum.NO_LOGIN); } channelAccount.setCreator(StrUtil.isBlank(channelAccount.getCreator()) ? AustinConstant.DEFAULT_CREATOR : channelAccount.getCreator()); - return BasicResultVO.success(channelAccountService.save(channelAccount)); + return channelAccountService.save(channelAccount); } /** @@ -56,14 +59,14 @@ public class ChannelAccountController { */ @GetMapping("/queryByChannelType") @ApiOperation("/根据渠道标识查询相关的记录") - public BasicResultVO query(Integer channelType, String creator) { + public List query(Integer channelType, String creator) { if (loginUtils.needLogin() && StrUtil.isBlank(creator)) { - return BasicResultVO.fail(RespStatusEnum.NO_LOGIN); + throw new CommonException(RespStatusEnum.NO_LOGIN); } creator = StrUtil.isBlank(creator) ? AustinConstant.DEFAULT_CREATOR : creator; List channelAccounts = channelAccountService.queryByChannelType(channelType, creator); - return BasicResultVO.success(Convert4Amis.getChannelAccountVo(channelAccounts)); + return Convert4Amis.getChannelAccountVo(channelAccounts); } /** @@ -71,13 +74,13 @@ public class ChannelAccountController { */ @GetMapping("/list") @ApiOperation("/渠道账号列表信息") - public BasicResultVO list(String creator) { + public List list(String creator) { if (loginUtils.needLogin() && StrUtil.isBlank(creator)) { - return BasicResultVO.fail(RespStatusEnum.NO_LOGIN); + throw new CommonException(RespStatusEnum.NO_LOGIN); } creator = StrUtil.isBlank(creator) ? AustinConstant.DEFAULT_CREATOR : creator; - return BasicResultVO.success(channelAccountService.list(creator)); + return channelAccountService.list(creator); } /** @@ -86,13 +89,11 @@ public class ChannelAccountController { */ @DeleteMapping("delete/{id}") @ApiOperation("/根据Ids删除") - public BasicResultVO deleteByIds(@PathVariable("id") String id) { + public void deleteByIds(@PathVariable("id") String id) { if (StrUtil.isNotBlank(id)) { - List idList = Arrays.stream(id.split(StrUtil.COMMA)).map(s -> Long.valueOf(s)).collect(Collectors.toList()); + List idList = Arrays.stream(id.split(StrUtil.COMMA)).map(Long::valueOf).collect(Collectors.toList()); channelAccountService.deleteByIds(idList); - return BasicResultVO.success(); } - return BasicResultVO.fail(); } } diff --git a/austin-web/src/main/java/com/java3y/austin/web/controller/DataController.java b/austin-web/src/main/java/com/java3y/austin/web/controller/DataController.java index 8c59ad8..66d3d85 100644 --- a/austin-web/src/main/java/com/java3y/austin/web/controller/DataController.java +++ b/austin-web/src/main/java/com/java3y/austin/web/controller/DataController.java @@ -1,8 +1,7 @@ package com.java3y.austin.web.controller; import cn.hutool.core.util.StrUtil; -import com.java3y.austin.common.enums.RespStatusEnum; -import com.java3y.austin.common.vo.BasicResultVO; +import com.java3y.austin.web.annotation.AustinResult; import com.java3y.austin.web.service.DataService; import com.java3y.austin.web.vo.DataParam; import com.java3y.austin.web.vo.amis.EchartsVo; @@ -11,6 +10,7 @@ import com.java3y.austin.web.vo.amis.UserTimeLineVo; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; +import org.assertj.core.util.Lists; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; @@ -26,6 +26,7 @@ import java.util.Objects; * @author 3y */ @Slf4j +@AustinResult @RestController @RequestMapping("/trace") @Api("获取数据接口(全链路追踪)") @@ -35,32 +36,27 @@ public class DataController { @PostMapping("/user") @ApiOperation("/获取【当天】用户接收消息的全链路数据") - public BasicResultVO getUserData(@RequestBody DataParam dataParam) { - UserTimeLineVo traceUserInfo = dataService.getTraceUserInfo(dataParam.getReceiver()); - - return BasicResultVO.success(traceUserInfo); + public UserTimeLineVo getUserData(@RequestBody DataParam dataParam) { + return dataService.getTraceUserInfo(dataParam.getReceiver()); } @PostMapping("/messageTemplate") @ApiOperation("/获取消息模板全链路数据") - public BasicResultVO getMessageTemplateData(@RequestBody DataParam dataParam) { + public EchartsVo getMessageTemplateData(@RequestBody DataParam dataParam) { EchartsVo echartsVo = EchartsVo.builder().build(); if (StrUtil.isNotBlank(dataParam.getBusinessId())) { echartsVo = dataService.getTraceMessageTemplateInfo(dataParam.getBusinessId()); } - return new BasicResultVO<>(RespStatusEnum.SUCCESS, echartsVo); + return echartsVo; } @PostMapping("/sms") @ApiOperation("/获取短信下发数据") - public BasicResultVO getSmsData(@RequestBody DataParam dataParam) { + public SmsTimeLineVo getSmsData(@RequestBody DataParam dataParam) { if (Objects.isNull(dataParam) || Objects.isNull(dataParam.getDateTime()) || StrUtil.isBlank(dataParam.getReceiver())) { - return new BasicResultVO<>(RespStatusEnum.SUCCESS, SmsTimeLineVo.builder().items(new ArrayList<>()).build()); + return SmsTimeLineVo.builder().items(Lists.newArrayList()).build(); } - - SmsTimeLineVo smsTimeLineVo = dataService.getTraceSmsInfo(dataParam); - - return new BasicResultVO<>(RespStatusEnum.SUCCESS, smsTimeLineVo); + return dataService.getTraceSmsInfo(dataParam); } } diff --git a/austin-web/src/main/java/com/java3y/austin/web/controller/MessageTemplateController.java b/austin-web/src/main/java/com/java3y/austin/web/controller/MessageTemplateController.java index f40edc3..b934458 100644 --- a/austin-web/src/main/java/com/java3y/austin/web/controller/MessageTemplateController.java +++ b/austin-web/src/main/java/com/java3y/austin/web/controller/MessageTemplateController.java @@ -14,6 +14,8 @@ import com.java3y.austin.service.api.enums.BusinessCode; import com.java3y.austin.service.api.service.RecallService; import com.java3y.austin.service.api.service.SendService; import com.java3y.austin.support.domain.MessageTemplate; +import com.java3y.austin.web.annotation.AustinResult; +import com.java3y.austin.web.exception.CommonException; import com.java3y.austin.web.service.MessageTemplateService; import com.java3y.austin.web.utils.Convert4Amis; import com.java3y.austin.web.utils.LoginUtils; @@ -31,10 +33,7 @@ import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.io.File; -import java.util.Arrays; -import java.util.List; -import java.util.Map; -import java.util.Objects; +import java.util.*; import java.util.stream.Collectors; @@ -44,6 +43,7 @@ import java.util.stream.Collectors; * @author 3y */ @Slf4j +@AustinResult @RestController @RequestMapping("/messageTemplate") @Api("发送消息") @@ -70,12 +70,12 @@ public class MessageTemplateController { */ @PostMapping("/save") @ApiOperation("/保存数据") - public BasicResultVO saveOrUpdate(@RequestBody MessageTemplate messageTemplate) { + public MessageTemplate saveOrUpdate(@RequestBody MessageTemplate messageTemplate) { if (loginUtils.needLogin() && StrUtil.isBlank(messageTemplate.getCreator())) { - return BasicResultVO.fail(RespStatusEnum.NO_LOGIN); + throw new CommonException(RespStatusEnum.NO_LOGIN); } MessageTemplate info = messageTemplateService.saveOrUpdate(messageTemplate); - return BasicResultVO.success(info); + return info; } /** @@ -83,14 +83,14 @@ public class MessageTemplateController { */ @GetMapping("/list") @ApiOperation("/列表页") - public BasicResultVO queryList(@Validated MessageTemplateParam messageTemplateParam) { + public MessageTemplateVo queryList(@Validated MessageTemplateParam messageTemplateParam) { if (loginUtils.needLogin() && StrUtil.isBlank(messageTemplateParam.getCreator())) { - return BasicResultVO.fail(RespStatusEnum.NO_LOGIN); + throw new CommonException(RespStatusEnum.NO_LOGIN); } Page messageTemplates = messageTemplateService.queryList(messageTemplateParam); List> result = Convert4Amis.flatListMap(messageTemplates.toList()); MessageTemplateVo messageTemplateVo = MessageTemplateVo.builder().count(messageTemplates.getTotalElements()).rows(result).build(); - return BasicResultVO.success(messageTemplateVo); + return messageTemplateVo; } /** @@ -98,9 +98,8 @@ public class MessageTemplateController { */ @GetMapping("query/{id}") @ApiOperation("/根据Id查找") - public BasicResultVO queryById(@PathVariable("id") Long id) { - Map result = Convert4Amis.flatSingleMap(messageTemplateService.queryById(id)); - return BasicResultVO.success(result); + public Map queryById(@PathVariable("id") Long id) { + return Convert4Amis.flatSingleMap(messageTemplateService.queryById(id)); } /** @@ -108,9 +107,8 @@ public class MessageTemplateController { */ @PostMapping("copy/{id}") @ApiOperation("/根据Id复制") - public BasicResultVO copyById(@PathVariable("id") Long id) { + public void copyById(@PathVariable("id") Long id) { messageTemplateService.copy(id); - return BasicResultVO.success(); } @@ -120,13 +118,11 @@ public class MessageTemplateController { */ @DeleteMapping("delete/{id}") @ApiOperation("/根据Ids删除") - public BasicResultVO deleteByIds(@PathVariable("id") String id) { + public void deleteByIds(@PathVariable("id") String id) { if (StrUtil.isNotBlank(id)) { - List idList = Arrays.stream(id.split(StrUtil.COMMA)).map(s -> Long.valueOf(s)).collect(Collectors.toList()); + List idList = Arrays.stream(id.split(StrUtil.COMMA)).map(Long::valueOf).collect(Collectors.toList()); messageTemplateService.deleteByIds(idList); - return BasicResultVO.success(); } - return BasicResultVO.fail(); } @@ -135,16 +131,16 @@ public class MessageTemplateController { */ @PostMapping("test") @ApiOperation("/测试发送接口") - public BasicResultVO test(@RequestBody MessageTemplateParam messageTemplateParam) { + public SendResponse test(@RequestBody MessageTemplateParam messageTemplateParam) { Map variables = JSON.parseObject(messageTemplateParam.getMsgContent(), Map.class); MessageParam messageParam = MessageParam.builder().receiver(messageTemplateParam.getReceiver()).variables(variables).build(); SendRequest sendRequest = SendRequest.builder().code(BusinessCode.COMMON_SEND.getCode()).messageTemplateId(messageTemplateParam.getId()).messageParam(messageParam).build(); SendResponse response = sendService.send(sendRequest); - if (response.getCode() != RespStatusEnum.SUCCESS.getCode()) { - return BasicResultVO.fail(response.getMsg()); + if (!Objects.equals(response.getCode(), RespStatusEnum.SUCCESS.getCode())) { + throw new CommonException(response.getMsg()); } - return BasicResultVO.success(response); + return response; } /** @@ -152,13 +148,9 @@ public class MessageTemplateController { */ @PostMapping("test/content") @ApiOperation("/测试发送接口") - public BasicResultVO test(Long id) { + public CommonAmisVo test(Long id) { MessageTemplate messageTemplate = messageTemplateService.queryById(id); - CommonAmisVo commonAmisVo = Convert4Amis.getTestContent(messageTemplate.getMsgContent()); - if (Objects.nonNull(commonAmisVo)) { - return BasicResultVO.success(commonAmisVo); - } - return BasicResultVO.success(); + return Convert4Amis.getTestContent(messageTemplate.getMsgContent()); } @@ -167,15 +159,14 @@ public class MessageTemplateController { */ @PostMapping("recall/{id}") @ApiOperation("/撤回消息接口") - public BasicResultVO recall(@PathVariable("id") String id) { - + public SendResponse recall(@PathVariable("id") String id) { SendRequest sendRequest = SendRequest.builder().code(BusinessCode.RECALL.getCode()). messageTemplateId(Long.valueOf(id)).build(); SendResponse response = recallService.recall(sendRequest); - if (response.getCode() != RespStatusEnum.SUCCESS.getCode()) { - return BasicResultVO.fail(response.getMsg()); + if (!Objects.equals(response.getCode(), RespStatusEnum.SUCCESS.getCode())) { + throw new CommonException(response.getMsg()); } - return BasicResultVO.success(response); + return response; } @@ -202,11 +193,10 @@ public class MessageTemplateController { */ @PostMapping("upload") @ApiOperation("/上传人群文件") - public BasicResultVO upload(@RequestParam("file") MultipartFile file) { - String filePath = new StringBuilder(dataPath) - .append(IdUtil.fastSimpleUUID()) - .append(file.getOriginalFilename()) - .toString(); + public HashMap upload(@RequestParam("file") MultipartFile file) { + String filePath = dataPath + + IdUtil.fastSimpleUUID() + + file.getOriginalFilename(); try { File localFile = new File(filePath); if (!localFile.exists()) { @@ -215,9 +205,9 @@ public class MessageTemplateController { file.transferTo(localFile); } catch (Exception e) { log.error("MessageTemplateController#upload fail! e:{},params{}", Throwables.getStackTraceAsString(e), JSON.toJSONString(file)); - return BasicResultVO.fail(RespStatusEnum.SERVICE_ERROR); + throw new CommonException(RespStatusEnum.SERVICE_ERROR); } - return BasicResultVO.success(MapUtil.of(new String[][]{{"value", filePath}})); + return MapUtil.of(new String[][]{{"value", filePath}}); } } diff --git a/austin-web/src/main/java/com/java3y/austin/web/controller/MiniProgramController.java b/austin-web/src/main/java/com/java3y/austin/web/controller/MiniProgramController.java index 800763d..9b46d47 100644 --- a/austin-web/src/main/java/com/java3y/austin/web/controller/MiniProgramController.java +++ b/austin-web/src/main/java/com/java3y/austin/web/controller/MiniProgramController.java @@ -5,8 +5,9 @@ import cn.binarywang.wx.miniapp.api.WxMaService; import cn.hutool.http.HttpUtil; import com.google.common.base.Throwables; import com.java3y.austin.common.enums.RespStatusEnum; -import com.java3y.austin.common.vo.BasicResultVO; import com.java3y.austin.support.utils.AccountUtils; +import com.java3y.austin.web.annotation.AustinResult; +import com.java3y.austin.web.exception.CommonException; import com.java3y.austin.web.utils.Convert4Amis; import com.java3y.austin.web.vo.amis.CommonAmisVo; import io.swagger.annotations.Api; @@ -29,6 +30,7 @@ import java.util.Objects; * @author 3y */ @Slf4j +@AustinResult @RestController @RequestMapping("/miniProgram") @Api("微信服务号") @@ -39,7 +41,7 @@ public class MiniProgramController { @GetMapping("/template/list") @ApiOperation("/根据账号Id获取模板列表") - public BasicResultVO queryList(Integer id) { + public List queryList(Integer id) { try { List result = new ArrayList<>(); WxMaService wxMaService = accountUtils.getAccountById(id, WxMaService.class); @@ -48,10 +50,10 @@ public class MiniProgramController { CommonAmisVo commonAmisVo = CommonAmisVo.builder().label(templateInfo.getTitle()).value(templateInfo.getPriTmplId()).build(); result.add(commonAmisVo); } - return BasicResultVO.success(result); + return result; } catch (Exception e) { log.error("MiniProgramController#queryList fail:{}", Throwables.getStackTraceAsString(e)); - return BasicResultVO.fail(RespStatusEnum.SERVICE_ERROR); + throw new CommonException(RespStatusEnum.SERVICE_ERROR); } } @@ -63,18 +65,17 @@ public class MiniProgramController { */ @PostMapping("/detailTemplate") @ApiOperation("/根据账号Id和模板ID获取模板列表") - public BasicResultVO queryDetailList(Integer id, String wxTemplateId) { + public CommonAmisVo queryDetailList(Integer id, String wxTemplateId) { if (Objects.isNull(id) || Objects.isNull(wxTemplateId)) { - return BasicResultVO.success(RespStatusEnum.CLIENT_BAD_PARAMETERS); + throw new CommonException(RespStatusEnum.CLIENT_BAD_PARAMETERS); } try { WxMaService wxMaService = accountUtils.getAccountById(id, WxMaService.class); List templateList = wxMaService.getSubscribeService().getTemplateList(); - CommonAmisVo wxMpTemplateParam = Convert4Amis.getWxMaTemplateParam(wxTemplateId, templateList); - return BasicResultVO.success(wxMpTemplateParam); + return Convert4Amis.getWxMaTemplateParam(wxTemplateId, templateList); } catch (Exception e) { log.error("MiniProgramController#queryDetailList fail:{}", Throwables.getStackTraceAsString(e)); - return BasicResultVO.fail(RespStatusEnum.SERVICE_ERROR); + throw new CommonException(RespStatusEnum.SERVICE_ERROR); } } @@ -87,10 +88,9 @@ public class MiniProgramController { */ @GetMapping("/sync/openid") @ApiOperation("登录凭证校验") - public BasicResultVO syncOpenId(String code, String appId, String secret) { + public String syncOpenId(String code, String appId, String secret) { String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appId + "&secret=" + secret + "&js_code=" + code + "&grant_type=authorization_code"; - String result = HttpUtil.get(url); - return BasicResultVO.success(result); + return HttpUtil.get(url); } } diff --git a/austin-web/src/main/java/com/java3y/austin/web/controller/OfficialAccountController.java b/austin-web/src/main/java/com/java3y/austin/web/controller/OfficialAccountController.java index a168fdf..4a566f7 100644 --- a/austin-web/src/main/java/com/java3y/austin/web/controller/OfficialAccountController.java +++ b/austin-web/src/main/java/com/java3y/austin/web/controller/OfficialAccountController.java @@ -11,9 +11,10 @@ import com.google.common.base.Throwables; import com.java3y.austin.common.constant.CommonConstant; import com.java3y.austin.common.constant.OfficialAccountParamConstant; import com.java3y.austin.common.enums.RespStatusEnum; -import com.java3y.austin.common.vo.BasicResultVO; import com.java3y.austin.support.utils.AccountUtils; +import com.java3y.austin.web.annotation.AustinResult; import com.java3y.austin.web.config.WeChatLoginConfig; +import com.java3y.austin.web.exception.CommonException; import com.java3y.austin.web.utils.Convert4Amis; import com.java3y.austin.web.utils.LoginUtils; import com.java3y.austin.web.vo.amis.CommonAmisVo; @@ -34,6 +35,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; +import java.lang.reflect.Type; import java.util.*; /** @@ -42,6 +44,7 @@ import java.util.*; * @author 3y */ @Slf4j +@AustinResult @RequestMapping("/officialAccount") @RestController @Api("微信服务号") @@ -63,7 +66,7 @@ public class OfficialAccountController { */ @GetMapping("/template/list") @ApiOperation("/根据账号Id获取模板列表") - public BasicResultVO queryList(Integer id) { + public List queryList(Integer id) { try { List result = new ArrayList<>(); WxMpService wxMpService = accountUtils.getAccountById(id, WxMpService.class); @@ -73,10 +76,10 @@ public class OfficialAccountController { CommonAmisVo commonAmisVo = CommonAmisVo.builder().label(wxMpTemplate.getTitle()).value(wxMpTemplate.getTemplateId()).build(); result.add(commonAmisVo); } - return BasicResultVO.success(result); + return result; } catch (Exception e) { log.error("OfficialAccountController#queryList fail:{}", Throwables.getStackTraceAsString(e)); - return BasicResultVO.fail(RespStatusEnum.SERVICE_ERROR); + throw new CommonException(RespStatusEnum.SERVICE_ERROR); } } @@ -88,18 +91,17 @@ public class OfficialAccountController { */ @PostMapping("/detailTemplate") @ApiOperation("/根据账号Id和模板ID获取模板列表") - public BasicResultVO queryDetailList(Integer id, String wxTemplateId) { + public CommonAmisVo queryDetailList(Integer id, String wxTemplateId) { if (Objects.isNull(id) || Objects.isNull(wxTemplateId)) { - return BasicResultVO.success(RespStatusEnum.CLIENT_BAD_PARAMETERS); + throw new CommonException(RespStatusEnum.CLIENT_BAD_PARAMETERS); } try { WxMpService wxMpService = accountUtils.getAccountById(id, WxMpService.class); List allPrivateTemplate = wxMpService.getTemplateMsgService().getAllPrivateTemplate(); - CommonAmisVo wxMpTemplateParam = Convert4Amis.getWxMpTemplateParam(wxTemplateId, allPrivateTemplate); - return BasicResultVO.success(wxMpTemplateParam); + return Convert4Amis.getWxMpTemplateParam(wxTemplateId, allPrivateTemplate); } catch (Exception e) { log.error("OfficialAccountController#queryDetailList fail:{}", Throwables.getStackTraceAsString(e)); - return BasicResultVO.fail(RespStatusEnum.SERVICE_ERROR); + throw new CommonException(RespStatusEnum.SERVICE_ERROR); } } @@ -164,20 +166,20 @@ public class OfficialAccountController { */ @PostMapping("/qrCode") @ApiOperation("/生成 服务号 二维码") - public BasicResultVO getQrCode() { + public CommonAmisVo getQrCode() { try { WeChatLoginConfig configService = loginUtils.getLoginConfig(); if (Objects.isNull(configService)) { - return BasicResultVO.fail(RespStatusEnum.DO_NOT_NEED_LOGIN); + throw new CommonException(RespStatusEnum.DO_NOT_NEED_LOGIN); } String id = IdUtil.getSnowflake().nextIdStr(); WxMpService wxMpService = configService.getOfficialAccountLoginService(); WxMpQrCodeTicket ticket = wxMpService.getQrcodeService().qrCodeCreateTmpTicket(id, 2592000); String url = wxMpService.getQrcodeService().qrCodePictureUrl(ticket.getTicket()); - return BasicResultVO.success(Convert4Amis.getWxMpQrCode(url, id)); + return Convert4Amis.getWxMpQrCode(url, id); } catch (Exception e) { log.error("OfficialAccountController#getQrCode fail:{}", Throwables.getStackTraceAsString(e)); - return BasicResultVO.fail(RespStatusEnum.SERVICE_ERROR); + throw new CommonException(RespStatusEnum.SERVICE_ERROR); } } @@ -188,14 +190,13 @@ public class OfficialAccountController { */ @RequestMapping("/check/login") @ApiOperation("/检查是否已经登录") - public BasicResultVO checkLogin(String sceneId) { + public WxMpUser checkLogin(String sceneId) { try { - String userInfo = redisTemplate.opsForValue().get(sceneId); if (StrUtil.isBlank(userInfo)) { - return BasicResultVO.success(RespStatusEnum.NO_LOGIN); + throw new CommonException(RespStatusEnum.NO_LOGIN); } - return BasicResultVO.success(JSON.parseObject(userInfo, WxMpUser.class)); + return JSON.parseObject(userInfo, (Type) WxMpUser.class); } catch (Exception e) { log.error("OfficialAccountController#checkLogin fail:{}", Throwables.getStackTraceAsString(e)); return null; @@ -213,7 +214,7 @@ public class OfficialAccountController { */ @RequestMapping("/delete/test/user") @ApiOperation("/删除测试号的测试用户") - public BasicResultVO deleteTestUser(HttpServletRequest request) { + public void deleteTestUser(HttpServletRequest request) { try { // 删除粉丝 @@ -229,10 +230,8 @@ public class OfficialAccountController { params.put("action", action); HttpUtil.createPost(testUrl).header(Header.COOKIE, cookie).form(params).execute(); } - return BasicResultVO.success(); } catch (Exception e) { log.error("OfficialAccountController#deleteTestUser fail:{}", Throwables.getStackTraceAsString(e)); - return null; } } } diff --git a/austin-web/src/main/java/com/java3y/austin/web/controller/RefreshTokenController.java b/austin-web/src/main/java/com/java3y/austin/web/controller/RefreshTokenController.java index a4f21f8..a558cbb 100644 --- a/austin-web/src/main/java/com/java3y/austin/web/controller/RefreshTokenController.java +++ b/austin-web/src/main/java/com/java3y/austin/web/controller/RefreshTokenController.java @@ -2,9 +2,9 @@ package com.java3y.austin.web.controller; import com.java3y.austin.common.enums.ChannelType; -import com.java3y.austin.common.vo.BasicResultVO; import com.java3y.austin.cron.handler.RefreshDingDingAccessTokenHandler; import com.java3y.austin.cron.handler.RefreshGeTuiAccessTokenHandler; +import com.java3y.austin.web.annotation.AustinResult; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; @@ -15,6 +15,7 @@ import org.springframework.web.bind.annotation.RestController; /** * @Author 3y */ +@AustinResult @Api(tags = {"手动刷新token的接口"}) @RestController public class RefreshTokenController { @@ -33,7 +34,7 @@ public class RefreshTokenController { */ @ApiOperation(value = "手动刷新token", notes = "钉钉/个推 token刷新") @GetMapping("/refresh") - public BasicResultVO refresh(Integer channelType) { + public String refresh(Integer channelType) { if (ChannelType.PUSH.getCode().equals(channelType)) { refreshGeTuiAccessTokenHandler.execute(); } @@ -41,7 +42,7 @@ public class RefreshTokenController { refreshDingDingAccessTokenHandler.execute(); } - return BasicResultVO.success("刷新成功"); + return "刷新成功"; } } diff --git a/austin-web/src/main/java/com/java3y/austin/web/exception/CommonException.java b/austin-web/src/main/java/com/java3y/austin/web/exception/CommonException.java new file mode 100644 index 0000000..eaa33bd --- /dev/null +++ b/austin-web/src/main/java/com/java3y/austin/web/exception/CommonException.java @@ -0,0 +1,40 @@ +package com.java3y.austin.web.exception; + +import com.java3y.austin.common.enums.RespStatusEnum; + +/** + * @author kl + * @version 1.0.0 + * @description 通用异常 + * @date 2023/2/9 19:00 + */ +public class CommonException extends RuntimeException { + private String code = RespStatusEnum.ERROR_400.getCode(); + + public CommonException(String message) { + super(message); + } + + public CommonException(RespStatusEnum respStatusEnum) { + super(respStatusEnum.getMsg()); + this.code = respStatusEnum.getCode(); + } + + public CommonException(String code, String message) { + super(message); + this.code = code; + } + + public CommonException(String message, Exception e) { + super(message, e); + } + + public CommonException(String code, String message, Exception e) { + super(message, e); + this.code = code; + } + + public String getCode() { + return code; + } +} \ No newline at end of file diff --git a/austin-web/src/main/java/com/java3y/austin/web/exception/ExceptionHandlerAdvice.java b/austin-web/src/main/java/com/java3y/austin/web/exception/ExceptionHandlerAdvice.java new file mode 100644 index 0000000..6b02614 --- /dev/null +++ b/austin-web/src/main/java/com/java3y/austin/web/exception/ExceptionHandlerAdvice.java @@ -0,0 +1,49 @@ +package com.java3y.austin.web.exception; + +import com.java3y.austin.common.enums.RespStatusEnum; +import com.java3y.austin.common.vo.BasicResultVO; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.ResponseStatus; + +import java.io.PrintWriter; +import java.io.StringWriter; + +/** + * @author kl + * @version 1.0.0 + * @description 拦截异常统一返回 + * @date 2023/2/9 19:03 + */ +@ControllerAdvice +@ResponseBody +public class ExceptionHandlerAdvice { + private static final Logger log = LoggerFactory.getLogger(ExceptionHandlerAdvice.class); + + public ExceptionHandlerAdvice() { + } + + @ExceptionHandler({Exception.class}) + @ResponseStatus(HttpStatus.OK) + public BasicResultVO exceptionResponse(Exception e) { + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + e.printStackTrace(pw); + BasicResultVO result = BasicResultVO.fail(RespStatusEnum.ERROR_500,"\r\n" + sw + "\r\n"); + log.error(e.getMessage(), e); + return result; + } + + @ExceptionHandler({CommonException.class}) + @ResponseStatus(HttpStatus.OK) + public BasicResultVO commonResponse(CommonException ce) { + BasicResultVO result = BasicResultVO.fail(RespStatusEnum.ERROR_400, ce.getMessage()); + log.error(ce.getMessage(), ce); + return result; + } +} +