OpenApiController.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package com.qmth.xjtu.api;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.qmth.boot.api.annotation.Aac;
  5. import com.qmth.boot.api.annotation.BOOL;
  6. import com.qmth.boot.api.constant.ApiConstant;
  7. import com.qmth.teachcloud.exchange.common.bean.params.OpenParams;
  8. import com.qmth.teachcloud.exchange.common.contant.SystemConstant;
  9. import com.qmth.teachcloud.exchange.common.enums.ExceptionResultEnum;
  10. import com.qmth.teachcloud.exchange.common.service.AuthInfoService;
  11. import com.qmth.teachcloud.exchange.common.service.CommonService;
  12. import com.qmth.teachcloud.exchange.common.util.HttpUtil;
  13. import com.qmth.teachcloud.exchange.common.util.JacksonUtil;
  14. import com.qmth.teachcloud.exchange.common.util.Result;
  15. import io.swagger.annotations.*;
  16. import org.slf4j.Logger;
  17. import org.slf4j.LoggerFactory;
  18. import org.springframework.validation.annotation.Validated;
  19. import org.springframework.web.bind.annotation.RequestMapping;
  20. import org.springframework.web.bind.annotation.RequestMethod;
  21. import org.springframework.web.bind.annotation.RequestParam;
  22. import org.springframework.web.bind.annotation.RestController;
  23. import javax.annotation.Resource;
  24. import java.io.IOException;
  25. import java.util.LinkedHashMap;
  26. import java.util.Map;
  27. import java.util.Objects;
  28. import java.util.StringJoiner;
  29. /**
  30. * <p>
  31. * 西安交通大学开放接口前端控制器
  32. * </p>
  33. *
  34. * @author wangliang
  35. * @since 2022-04-26
  36. */
  37. @Api(tags = "西安交通大学开放接口Controller")
  38. @RestController
  39. @RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/${prefix.url.open}")
  40. @Validated
  41. public class OpenApiController {
  42. private static final Logger log = LoggerFactory.getLogger(OpenApiController.class);
  43. private static final String ACCESS_TOKEN_URL = "https://org.xjtu.edu.cn/openplatform/oauth/getAccessToken";
  44. private static final String USER_INFO_URL = "https://org.xjtu.edu.cn/openplatform/oauth/open/getUserInfo";
  45. private static final String LOGOUT_URL = "http://org.xjtu.edu.cn/openplatform/oauth/logout";
  46. @Resource
  47. CommonService commonService;
  48. @Resource
  49. AuthInfoService authInfoService;
  50. @ApiOperation(value = "西安交通大学cas鉴权接口")
  51. @ApiResponses({@ApiResponse(code = 200, message = "返回消息", response = Result.class)})
  52. @RequestMapping(value = "/authentication", method = RequestMethod.GET)
  53. @Aac(auth = BOOL.FALSE)
  54. public void authentication(@ApiParam(value = "工号") @RequestParam(required = false) String code,
  55. @ApiParam(value = "系统参数") @RequestParam(required = false) String state,
  56. @ApiParam(value = "用户类型") @RequestParam(required = false) String userType,
  57. @ApiParam(value = "员工工号") @RequestParam(required = false) String employeeNo,
  58. @ApiParam(value = "返回url") @RequestParam(required = false) String returnUrl) throws IOException {
  59. String schoolCode = "xjtu";//测试学校code,正式改成xjtu
  60. authInfoService.appHasExpired(schoolCode);
  61. Map<String, Object> accessTokenParams = new LinkedHashMap<>();
  62. accessTokenParams.put("code", code);
  63. String accessTokenResult = HttpUtil.post(ACCESS_TOKEN_URL, accessTokenParams, null);
  64. String accessToken = null, gsessionId = null;
  65. //获取accessToken
  66. if (Objects.nonNull(accessTokenResult)) {
  67. log.info("accessTokenResult:{}", JacksonUtil.parseJson(accessTokenResult));
  68. JSONObject jsonObject = JSONObject.parseObject(accessTokenResult);
  69. JSONObject object = jsonObject.getJSONObject("data");
  70. String message = jsonObject.getString("message");
  71. if (Objects.nonNull(object) && Objects.equals(message, "成功")) {
  72. accessToken = object.getString("accessToken");
  73. gsessionId = object.getString("gsessionId");
  74. } else {
  75. throw ExceptionResultEnum.ERROR.exception(message);
  76. }
  77. }
  78. OpenParams openParams = null;
  79. //获取用户信息
  80. if (Objects.nonNull(accessToken)) {
  81. String userInfoResult = HttpUtil.post(USER_INFO_URL, null, accessToken);
  82. if (Objects.nonNull(userInfoResult)) {
  83. log.info("userInfoResult:{}", JacksonUtil.parseJson(userInfoResult));
  84. openParams = new OpenParams();
  85. openParams.setResult(JacksonUtil.parseJson(userInfoResult));
  86. JSONObject jsonObject = JSONObject.parseObject(userInfoResult);
  87. JSONObject object = jsonObject.getJSONObject("data");
  88. String message = jsonObject.getString("message");
  89. JSONArray userTypeJsonArray = object.getJSONArray("userTypes");
  90. JSONArray deptInfoJsonArray = object.getJSONArray("deptInfos");
  91. if (Objects.nonNull(object) && Objects.equals(message, "成功")) {
  92. openParams.setOrgName(object.getString("orgName"));
  93. if (Objects.nonNull(userTypeJsonArray) && userTypeJsonArray.size() > 0) {
  94. JSONObject userTypeJsonObject = userTypeJsonArray.getJSONObject(0);
  95. openParams.setName(userTypeJsonObject.getString("memberName"));
  96. Integer userTypeRole = userTypeJsonObject.getInteger("userType");
  97. if (Objects.nonNull(userTypeRole) && userTypeRole.intValue() == 1) {
  98. openParams.setRoleName("学生");
  99. } else if (Objects.nonNull(userTypeRole) && userTypeRole.intValue() == 2) {
  100. openParams.setRoleName("教职工");
  101. }
  102. }
  103. if (Objects.nonNull(deptInfoJsonArray) && deptInfoJsonArray.size() > 0) {
  104. JSONObject deptInfoJsonArrayJsonObject = deptInfoJsonArray.getJSONObject(0);
  105. openParams.setDeptName(deptInfoJsonArrayJsonObject.getString("deptName"));
  106. }
  107. } else {
  108. throw ExceptionResultEnum.ERROR.exception(message);
  109. }
  110. }
  111. }
  112. //登出
  113. if (Objects.nonNull(gsessionId)) {
  114. // Map<String, Object> logoutParams = new LinkedHashMap<>();
  115. // logoutParams.put("gSessionId", gsessionId);
  116. // String logoutResult = HttpUtil.post(LOGOUT_URL, logoutParams, null);
  117. // if (Objects.nonNull(logoutResult)) {
  118. // log.info("logoutResult:{}", JacksonUtil.parseJson(logoutResult));
  119. // }
  120. StringJoiner stringJoiner = new StringJoiner("");
  121. stringJoiner.add(LOGOUT_URL).add(SystemConstant.GET_UNKNOWN).add("gSessionId")
  122. .add(SystemConstant.GET_EQUAL).add(gsessionId);
  123. returnUrl = stringJoiner.toString();
  124. }
  125. commonService.redirectLogic(employeeNo, schoolCode, returnUrl, Objects.nonNull(openParams) ? JacksonUtil.parseJson(openParams) : null);
  126. }
  127. }