NotifyApiController.java 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package com.qmth.distributed.print.api;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.qmth.boot.api.annotation.Aac;
  4. import com.qmth.boot.api.annotation.BOOL;
  5. import com.qmth.boot.api.constant.ApiConstant;
  6. import com.qmth.boot.api.exception.ApiException;
  7. import com.qmth.distributed.print.business.bean.result.CalculateNotifyResult;
  8. import com.qmth.teachcloud.common.config.DictionaryConfig;
  9. import com.qmth.teachcloud.common.contant.SystemConstant;
  10. import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
  11. import com.qmth.teachcloud.common.util.*;
  12. import io.swagger.annotations.*;
  13. import org.slf4j.Logger;
  14. import org.slf4j.LoggerFactory;
  15. import org.springframework.validation.annotation.Validated;
  16. import org.springframework.web.bind.annotation.RequestBody;
  17. import org.springframework.web.bind.annotation.RequestMapping;
  18. import org.springframework.web.bind.annotation.RequestMethod;
  19. import org.springframework.web.bind.annotation.RestController;
  20. import javax.annotation.Resource;
  21. import java.io.UnsupportedEncodingException;
  22. import java.net.URLDecoder;
  23. import java.net.URLEncoder;
  24. import java.util.Objects;
  25. import java.util.Optional;
  26. /**
  27. * <p>
  28. * 回调接口前端控制器
  29. * </p>
  30. *
  31. * @author wangliang
  32. * @since 2022-04-26
  33. */
  34. @Api(tags = "回调接口Controller")
  35. @RestController
  36. @RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/${prefix.url.notify}")
  37. @Validated
  38. public class NotifyApiController {
  39. private final static Logger log = LoggerFactory.getLogger(NotifyApiController.class);
  40. @Resource
  41. DictionaryConfig dictionaryConfig;
  42. @ApiOperation(value = "教研分析进度回调")
  43. @ApiResponses({@ApiResponse(code = 200, message = "教研分析进度回调", response = CalculateNotifyResult.class)})
  44. @RequestMapping(value = "/analysis/progress", method = RequestMethod.POST)
  45. @Aac(auth = BOOL.FALSE)
  46. public Result analysisProgress(@ApiParam(value = "接收教研分析回调数据", required = true) @RequestBody String result) throws UnsupportedEncodingException {
  47. try {
  48. log.info("analysis_progress,result:{}", result);
  49. String decodeJson = URLDecoder.decode(result, SystemConstant.CHARSET_NAME);
  50. log.info("analysis_progress decodeJson:{}", decodeJson);
  51. String callbackPwd = dictionaryConfig.printOpenDomain().getCallbackPwd();
  52. Optional.ofNullable(callbackPwd).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("配置文件回调密码为空"));
  53. String sign = ServletUtil.getRequestAuthorization();
  54. Optional.ofNullable(sign).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("签名为空"));
  55. log.info("sign:{}", sign);
  56. String localSign = URLEncoder.encode(Base64Util.encode(ShaUtils.sha1(callbackPwd + decodeJson)), SystemConstant.CHARSET_NAME);
  57. log.info("localSign:{}", localSign);
  58. if (!Objects.equals(localSign, sign)) {
  59. throw ExceptionResultEnum.ERROR.exception("签名不匹配");
  60. }
  61. CalculateNotifyResult calculateNotifyResult = JSONObject.toJavaObject(JSONObject.parseObject(decodeJson), CalculateNotifyResult.class);
  62. String source = Base64Util.encode(ShaUtils.sha1(calculateNotifyResult.getExamId() + calculateNotifyResult.getCourseCode().toString()));
  63. log.info("source:{}", source);
  64. //TODO 此处加批次数据状态更新service方法
  65. //更新grade_batch(状态和进度)和grade_batch_paper(状态)
  66. } catch (Exception e) {
  67. log.error(SystemConstant.LOG_ERROR, e);
  68. //todo 生成txt文件
  69. if (e instanceof ApiException) {
  70. ResultUtil.error((ApiException) e, e.getMessage());
  71. } else {
  72. ResultUtil.error(e.getMessage());
  73. }
  74. }
  75. return ResultUtil.ok(System.currentTimeMillis());
  76. }
  77. }