NotifyApiController.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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.*;
  25. /**
  26. * <p>
  27. * 回调接口前端控制器
  28. * </p>
  29. *
  30. * @author wangliang
  31. * @since 2022-04-26
  32. */
  33. @Api(tags = "回调接口Controller")
  34. @RestController
  35. @RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/${prefix.url.notify}")
  36. @Validated
  37. public class NotifyApiController {
  38. private final static Logger log = LoggerFactory.getLogger(NotifyApiController.class);
  39. @Resource
  40. DictionaryConfig dictionaryConfig;
  41. @ApiOperation(value = "教研分析进度回调")
  42. @ApiResponses({@ApiResponse(code = 200, message = "教研分析进度回调", response = CalculateNotifyResult.class)})
  43. @RequestMapping(value = "/analysis/progress", method = RequestMethod.POST)
  44. @Aac(auth = BOOL.FALSE)
  45. public Result analysisProgress(@ApiParam(value = "接收教研分析回调数据", required = true) @RequestBody String result) throws UnsupportedEncodingException {
  46. try {
  47. log.info("analysis_progress,result:{}", result);
  48. String decodeJson = URLDecoder.decode(result, SystemConstant.CHARSET_NAME);
  49. log.info("analysis_progress decodeJson:{}", decodeJson);
  50. String callbackPwd = dictionaryConfig.printOpenDomain().getCallbackPwd();
  51. Optional.ofNullable(callbackPwd).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("配置文件回调密码为空"));
  52. String sign = ServletUtil.getRequestAuthorization();
  53. Optional.ofNullable(sign).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("签名为空"));
  54. log.info("sign:{}", sign);
  55. String localSign = URLEncoder.encode(Base64Util.encode(ShaUtils.sha1(callbackPwd + decodeJson)), SystemConstant.CHARSET_NAME);
  56. log.info("localSign:{}", localSign);
  57. if (!Objects.equals(localSign, sign)) {
  58. throw ExceptionResultEnum.ERROR.exception("签名不匹配");
  59. }
  60. CalculateNotifyResult calculateNotifyResult = JSONObject.toJavaObject(JSONObject.parseObject(decodeJson), CalculateNotifyResult.class);
  61. List<String> courseCodeList = new ArrayList<>();
  62. courseCodeList.addAll(calculateNotifyResult.getCourseCode().keySet());
  63. Collections.sort(courseCodeList);
  64. String source = Base64Util.encode(ShaUtils.sha1(calculateNotifyResult.getExamId() + courseCodeList.toString()));
  65. log.info("source:{}", source);
  66. //TODO 更新grade_batch(状态和进度)和grade_batch_paper(状态)
  67. } catch (Exception e) {
  68. log.error(SystemConstant.LOG_ERROR, e);
  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. }