TSAuthController.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package com.qmth.distributed.print.api;
  2. import cn.hutool.http.HttpStatus;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.qmth.boot.api.constant.ApiConstant;
  5. import com.qmth.boot.api.exception.ApiException;
  6. import com.qmth.boot.core.solar.crypto.AppLicenseUtil;
  7. import com.qmth.boot.tools.signature.SignatureEntity;
  8. import com.qmth.boot.tools.signature.SignatureType;
  9. import com.qmth.distributed.print.business.service.AuthInfoService;
  10. import com.qmth.teachcloud.common.config.DictionaryConfig;
  11. import com.qmth.teachcloud.common.contant.SystemConstant;
  12. import com.qmth.teachcloud.common.entity.BasicSchool;
  13. import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
  14. import com.qmth.teachcloud.common.service.CommonCacheService;
  15. import com.qmth.teachcloud.common.util.*;
  16. import io.swagger.annotations.*;
  17. import org.apache.commons.io.IOUtils;
  18. import org.slf4j.Logger;
  19. import org.slf4j.LoggerFactory;
  20. import org.springframework.validation.annotation.Validated;
  21. import org.springframework.web.bind.annotation.RequestMapping;
  22. import org.springframework.web.bind.annotation.RequestMethod;
  23. import org.springframework.web.bind.annotation.RequestParam;
  24. import org.springframework.web.bind.annotation.RestController;
  25. import org.springframework.web.multipart.MultipartFile;
  26. import javax.annotation.Resource;
  27. import javax.servlet.http.HttpServletResponse;
  28. import java.io.ByteArrayInputStream;
  29. import java.io.IOException;
  30. import java.util.Arrays;
  31. import java.util.HashMap;
  32. import java.util.Map;
  33. import java.util.Objects;
  34. /**
  35. * <p>
  36. * 激活授权配置表 前端控制器
  37. * </p>
  38. *
  39. * @author wangliang
  40. * @since 2022-04-26
  41. */
  42. @Api(tags = "授权配置Controller")
  43. @RestController
  44. @RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/${prefix.url.auth}")
  45. @Validated
  46. public class TSAuthController {
  47. private final static Logger log = LoggerFactory.getLogger(TSAuthController.class);
  48. @Resource
  49. AuthInfoService authInfoService;
  50. @Resource
  51. CommonCacheService commonCacheService;
  52. @Resource
  53. DictionaryConfig dictionaryConfig;
  54. @ApiOperation(value = "导出硬件信息")
  55. @ApiResponses({@ApiResponse(code = 200, message = "导出硬件信息", response = Object.class)})
  56. @RequestMapping(value = "/export/device/info", method = RequestMethod.POST)
  57. public void info() {
  58. try {
  59. HttpServletResponse response = ServletUtil.getResponse();
  60. response.setHeader("Content-Disposition", "attachment; filename=" + SystemConstant.urlEncode("device.info"));
  61. IOUtils.copy(new ByteArrayInputStream(AppLicenseUtil.buildDeviceInfo().value()), response.getOutputStream());
  62. } catch (Exception e) {
  63. log.error(SystemConstant.LOG_ERROR, e);
  64. }
  65. }
  66. @ApiOperation(value = "离线激活")
  67. @ApiResponses({@ApiResponse(code = 200, message = "授权配置信息", response = Boolean.class)})
  68. @RequestMapping(value = "/offline/activation", method = RequestMethod.POST)
  69. public Result offlineActivation(@ApiParam(value = "上传文件", required = true) @RequestParam(required = true) MultipartFile file) throws Exception {
  70. try {
  71. authInfoService.updateLicense(file.getBytes());
  72. } catch (IOException e) {
  73. e.printStackTrace();
  74. }
  75. return ResultUtil.ok(true);
  76. }
  77. @ApiOperation(value = "查询激活信息")
  78. @ApiResponses({@ApiResponse(code = 200, message = "授权配置信息", response = Long.class)})
  79. @RequestMapping(value = "/select", method = RequestMethod.POST)
  80. public Result select() {
  81. return ResultUtil.ok(authInfoService.selectAuthInfo());
  82. }
  83. @ApiOperation(value = "测试推送信息给教研分析")
  84. @ApiResponses({@ApiResponse(code = 200, message = "测试推送信息给教研分析", response = Objects.class)})
  85. @RequestMapping(value = "/test/push", method = RequestMethod.POST)
  86. public void testPush() throws IOException {
  87. Long schoolId = SystemConstant.getHeadOrUserSchoolId();
  88. BasicSchool basicSchool = commonCacheService.schoolCache(schoolId);
  89. Long timestamp = System.currentTimeMillis();
  90. // /**
  91. // * 测试推送学期
  92. // */
  93. // BasicSemesterParams basicSemesterParams = new BasicSemesterParams("测试学期1", System.currentTimeMillis(), System.currentTimeMillis() + 1L);
  94. // String accessToken = SignatureEntity.build(SignatureType.SECRET, SystemConstant.METHOD, dictionaryConfig.reportOpenDomain().getSemesterApi(), timestamp, basicSchool.getAccessKey(), basicSchool.getAccessSecret());
  95. // String result = HttpUtil.postJson(dictionaryConfig.reportOpenDomain().getHostUrl() + dictionaryConfig.reportOpenDomain().getSemesterApi(), JacksonUtil.parseJson(basicSemesterParams), accessToken, timestamp);
  96. // if (Objects.nonNull(result)) {
  97. // log.info("result:{}", JacksonUtil.parseJson(result));
  98. // }
  99. // /**
  100. // * 测试推送考试
  101. // */
  102. // Map<String, Object> map = new HashMap<>();
  103. // map.computeIfAbsent("examName", v -> "测试考试1");
  104. // map.computeIfAbsent("examTime", v -> timestamp);
  105. // map.computeIfAbsent("semesterId", v -> 262908000564412416L);
  106. // String accessToken = SignatureEntity.build(SignatureType.SECRET, SystemConstant.METHOD, dictionaryConfig.reportOpenDomain().getExamApi(), timestamp, basicSchool.getAccessKey(), basicSchool.getAccessSecret());
  107. // String result = HttpUtil.postJson(dictionaryConfig.reportOpenDomain().getHostUrl() + dictionaryConfig.reportOpenDomain().getExamApi(), JacksonUtil.parseJson(map), accessToken, timestamp);
  108. // if (Objects.nonNull(result)) {
  109. // log.info("result:{}", JacksonUtil.parseJson(result));
  110. // }
  111. // /**
  112. // * 测试推送分析课程(试卷)
  113. // */
  114. // Map<String, Object> map = new HashMap<>();
  115. // map.computeIfAbsent("examId", v -> 262946761763454976L);
  116. // map.computeIfAbsent("courseCode", v -> 2022052700001L + "A");//试卷编号+卷型
  117. // map.computeIfAbsent("courseName", v -> "测试课程1");
  118. // map.computeIfAbsent("paperType", v -> "A");
  119. // map.computeIfAbsent("teachCollegeName", v -> "测试机构1");//机构名称
  120. // String accessToken = SignatureEntity.build(SignatureType.SECRET, SystemConstant.METHOD, dictionaryConfig.reportOpenDomain().getCourseEditApi(), timestamp, basicSchool.getAccessKey(), basicSchool.getAccessSecret());
  121. // String result = HttpUtil.postJson(dictionaryConfig.reportOpenDomain().getHostUrl() + dictionaryConfig.reportOpenDomain().getCourseEditApi(), JacksonUtil.parseJson(map), accessToken, timestamp);
  122. // if (Objects.nonNull(result)) {
  123. // log.info("result:{}", JacksonUtil.parseJson(result));
  124. // }
  125. // /**
  126. // * 测试单点登录
  127. // */
  128. // Map<String, Object> map = new HashMap<>();
  129. // map.computeIfAbsent("loginName", v -> "admin");
  130. // map.computeIfAbsent("role", v -> RoleTypeEnum.OFFICE_TEACHER);
  131. // map.computeIfAbsent("orgName", v -> "本科生院");
  132. // map.computeIfAbsent("realName", v -> "admin");
  133. // map.computeIfAbsent("mobileNumber", v -> "18008659001");
  134. // map.computeIfAbsent("returnUrl", v -> "https://www.baidu.com");
  135. // String accessToken = SignatureEntity.build(SignatureType.SECRET, SystemConstant.METHOD, dictionaryConfig.reportOpenDomain().getSsoLoginApi(), timestamp, basicSchool.getAccessKey(), basicSchool.getAccessSecret());
  136. // String result = HttpUtil.postJson(dictionaryConfig.reportOpenDomain().getHostUrl() + dictionaryConfig.reportOpenDomain().getSsoLoginApi(), JacksonUtil.parseJson(map), accessToken, timestamp);
  137. // if (Objects.nonNull(result)) {
  138. // log.info("result:{}", JacksonUtil.parseJson(result));
  139. // }
  140. /**
  141. * 测试生成课程(试卷)分析报告接口
  142. */
  143. try {
  144. Map<String, Object> map = new HashMap<>();
  145. map.computeIfAbsent("examId", v -> 262946761763454976L);
  146. map.computeIfAbsent("courseCode", v -> Arrays.asList(2022052700001L + "A",
  147. 2022052700001L + "B",
  148. 2022052700001L + "C",
  149. 2022052700001L + "D",
  150. 2022052700001L + "E",
  151. 2022052700001L + "F",
  152. 2022052700001L + "G"));//试卷编号+卷型
  153. String source = Base64Util.encode(ShaUtils.sha1(map.get("examId") + map.get("courseCode").toString()));
  154. log.info("source:{}", source);
  155. String accessToken = SignatureEntity.build(SignatureType.SECRET, SystemConstant.METHOD, dictionaryConfig.reportOpenDomain().getCalculateApi(), timestamp, basicSchool.getAccessKey(), basicSchool.getAccessSecret());
  156. String result = HttpUtil.postJson(dictionaryConfig.reportOpenDomain().getHostUrl() + dictionaryConfig.reportOpenDomain().getCalculateApi(), JacksonUtil.parseJson(map), accessToken, timestamp);
  157. if (Objects.nonNull(result)) {
  158. log.info("result:{}", JacksonUtil.parseJson(result));
  159. JSONObject jsonObject = JSONObject.parseObject(result);
  160. if (Objects.nonNull(jsonObject.get("code")) && jsonObject.getIntValue("code") != HttpStatus.HTTP_OK) {
  161. throw ExceptionResultEnum.ERROR.exception(jsonObject.getString("error"));
  162. }
  163. }
  164. } catch (Exception e) {
  165. log.error(SystemConstant.LOG_ERROR, e);
  166. //TODO 生成txt文件
  167. if (e instanceof ApiException) {
  168. ResultUtil.error((ApiException) e, e.getMessage());
  169. } else {
  170. ResultUtil.error(e.getMessage());
  171. }
  172. }
  173. }
  174. }