|
@@ -0,0 +1,139 @@
|
|
|
|
|
+package com.shkpr.service.customgateway.zydma.controller;
|
|
|
|
|
+
|
|
|
|
|
+import com.fasterxml.jackson.core.type.TypeReference;
|
|
|
|
|
+import com.global.base.log.LogLevelFlag;
|
|
|
|
|
+import com.global.base.log.LogPrintMgr;
|
|
|
|
|
+import com.shkpr.service.customgateway.core.constants.Api;
|
|
|
|
|
+import com.shkpr.service.customgateway.core.constants.LogFlagBusiType;
|
|
|
|
|
+import com.shkpr.service.customgateway.core.constants.ResponseCode;
|
|
|
|
|
+import com.shkpr.service.customgateway.core.domain.IntegrationKey;
|
|
|
|
|
+import com.shkpr.service.customgateway.core.domain.ResultResponse;
|
|
|
|
|
+import com.shkpr.service.customgateway.core.exception.SelfException;
|
|
|
|
|
+import com.shkpr.service.customgateway.core.properties.CallingProperties;
|
|
|
|
|
+import com.shkpr.service.customgateway.core.utils.CallingUtil;
|
|
|
|
|
+import com.shkpr.service.customgateway.core.utils.CommTool;
|
|
|
|
|
+import com.shkpr.service.customgateway.core.utils.TokenUtil;
|
|
|
|
|
+import com.shkpr.service.customgateway.zydma.constants.MiddlePlatformMetadata;
|
|
|
|
|
+import com.shkpr.service.customgateway.zydma.domain.MiddlePlatformResult;
|
|
|
|
|
+import com.shkpr.service.customgateway.zydma.domain.po.PersonnelInfo;
|
|
|
|
|
+import com.shkpr.service.customgateway.zydma.service.PersonnelInfoService;
|
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
+import org.apache.http.Header;
|
|
|
|
|
+import org.apache.http.message.BasicHeader;
|
|
|
|
|
+import org.springframework.http.HttpMethod;
|
|
|
|
|
+import org.springframework.stereotype.Controller;
|
|
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
|
|
+import org.springframework.web.bind.annotation.RequestHeader;
|
|
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
|
|
+
|
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
+import java.util.Arrays;
|
|
|
|
|
+import java.util.Collections;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
+import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
|
+
|
|
|
|
|
+import static com.shkpr.service.customgateway.core.constants.Api.URI_USERS_H;
|
|
|
|
|
+
|
|
|
|
|
+@Controller
|
|
|
|
|
+@RequestMapping("${gateway.routes.zydma}" + URI_USERS_H)
|
|
|
|
|
+public class UserController {
|
|
|
|
|
+ //log
|
|
|
|
|
+ private final static String CLASS_NAME = "ApiGisSurveyController";
|
|
|
|
|
+ private final static String BIZ_TYPE = LogFlagBusiType.BUSI_DB_USER.toStrValue();
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 请求序列
|
|
|
|
|
+ */
|
|
|
|
|
+ final Map<String, AtomicInteger> requestSeq = new ConcurrentHashMap<>();
|
|
|
|
|
+ final
|
|
|
|
|
+ CallingProperties callingProperties;
|
|
|
|
|
+ final
|
|
|
|
|
+ PersonnelInfoService personnelInfoService;
|
|
|
|
|
+ final
|
|
|
|
|
+ TokenUtil tokenUtil;
|
|
|
|
|
+ final
|
|
|
|
|
+ CallingUtil callingUtil;
|
|
|
|
|
+
|
|
|
|
|
+ public UserController(CallingProperties callingProperties, PersonnelInfoService personnelInfoService, TokenUtil tokenUtil, CallingUtil callingUtil) {
|
|
|
|
|
+ this.callingProperties = callingProperties;
|
|
|
|
|
+ this.personnelInfoService = personnelInfoService;
|
|
|
|
|
+ this.tokenUtil = tokenUtil;
|
|
|
|
|
+ this.callingUtil = callingUtil;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * ticket交换临时token
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param ticket 门户token
|
|
|
|
|
+ * @return 临时token
|
|
|
|
|
+ */
|
|
|
|
|
+ @GetMapping(value = Api.URI_XXX_TICKET_EXCHANGE)
|
|
|
|
|
+ public ResultResponse<String> getUser(
|
|
|
|
|
+ HttpServletRequest request,
|
|
|
|
|
+ @RequestHeader(value = Api.HEADER_CLIENT_TYPE, required = false) String strClientType,
|
|
|
|
|
+ @RequestHeader(value = Api.HEADER_USER_AGENT, required = false) String strUserAgent,
|
|
|
|
|
+ @RequestParam(value = "ticket", required = false) String ticket) throws SelfException {
|
|
|
|
|
+ //初始化序列
|
|
|
|
|
+ requestSeq.putIfAbsent(Api.URI_XXX_TICKET_EXCHANGE, new AtomicInteger(0));
|
|
|
|
|
+ final String URI_PATH = request.getRequestURI();
|
|
|
|
|
+ final String strPlatform = CommTool.getPlatformByAgent(strClientType, strUserAgent);
|
|
|
|
|
+ //参数校验
|
|
|
|
|
+ if (ticket == null || StringUtils.isBlank(ticket)) {
|
|
|
|
|
+ throw new SelfException(ResponseCode.STATUS_ERROR_JSON_FORMAT.getCode() + ""
|
|
|
|
|
+ , String.format(Api.EXCEPTION_FORMAT
|
|
|
|
|
+ , strPlatform
|
|
|
|
|
+ , URI_PATH
|
|
|
|
|
+ , ResponseCode.STATUS_ERROR_JSON_FORMAT.getMessage()));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //begin
|
|
|
|
|
+ long begin = System.currentTimeMillis();
|
|
|
|
|
+ String seqMsg = String.format("%d-%d", begin, requestSeq.get(Api.URI_XXX_TICKET_EXCHANGE).incrementAndGet());
|
|
|
|
|
+ LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, BIZ_TYPE, CLASS_NAME
|
|
|
|
|
+ , String.format("%s:%s seq:{%s} param:%s begin====>", strPlatform, URI_PATH, seqMsg, ticket));
|
|
|
|
|
+ //构建result
|
|
|
|
|
+ ResultResponse<String> resultResponse = ResultResponse.failed();
|
|
|
|
|
+
|
|
|
|
|
+ //===========================请求用户信息===========================
|
|
|
|
|
+ //接入点
|
|
|
|
|
+ CallingProperties.CallingEndpoint endpoint = callingProperties.getEndpoints().get(MiddlePlatformMetadata.NAME);
|
|
|
|
|
+ //地址
|
|
|
|
|
+ String url = endpoint.getUrl() + MiddlePlatformMetadata.Uri.GET_USER_BY_TICKET;
|
|
|
|
|
+ //参数
|
|
|
|
|
+ Map<String, String> params = Collections.singletonMap(MiddlePlatformMetadata.Params.TICKET, "ed23d0c4afed4a9e96a23a99a874fe6425536f4364e649c4874968b29027d1fc");
|
|
|
|
|
+ //密钥
|
|
|
|
|
+ IntegrationKey key = MiddlePlatformMetadata.getKey(endpoint.getAccessKey(), endpoint.getSecretKey(), params);
|
|
|
|
|
+ //请求头
|
|
|
|
|
+ List<Header> headers = Arrays.asList(
|
|
|
|
|
+ new BasicHeader(MiddlePlatformMetadata.Headers.APP_KEY, key.getAccessKey()),
|
|
|
|
|
+ new BasicHeader(MiddlePlatformMetadata.Headers.TIMESTAMP, key.getTimestamp() + ""),
|
|
|
|
|
+ new BasicHeader(MiddlePlatformMetadata.Headers.SIGN, key.getSecretKey())
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ //获取用户
|
|
|
|
|
+ Map<String, Object> user = callingUtil.requestObject(url, HttpMethod.GET, params, headers, new TypeReference<MiddlePlatformResult<Map<String, Object>>>() {
|
|
|
|
|
+ });
|
|
|
|
|
+ //获取用户id
|
|
|
|
|
+ String userId = user != null ? String.valueOf(user.getOrDefault(MiddlePlatformMetadata.Results.ID, null)) : null;
|
|
|
|
|
+
|
|
|
|
|
+ //===========================签发临时token===========================
|
|
|
|
|
+ //查询用户
|
|
|
|
|
+ PersonnelInfo personnelInfo = personnelInfoService.findByUserId(userId);
|
|
|
|
|
+ if (personnelInfo != null && personnelInfo.getAccount() != null) {
|
|
|
|
|
+ //签发临时token
|
|
|
|
|
+ String tempToken = tokenUtil.generateTempToken(personnelInfo.getAccount());
|
|
|
|
|
+ if (StringUtils.isNotBlank(tempToken)) resultResponse.success(tempToken);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //end
|
|
|
|
|
+ resultResponse.setTimestamp(System.currentTimeMillis());
|
|
|
|
|
+ LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, BIZ_TYPE, CLASS_NAME,
|
|
|
|
|
+ String.format("%s:%s seq:{%s} rescode:{%s} resmsg:{%s} time:{%d ms} end<===="
|
|
|
|
|
+ , strPlatform, URI_PATH, seqMsg, resultResponse.getRescode(), resultResponse.getResmsg()
|
|
|
|
|
+ , resultResponse.getTimestamp() - begin));
|
|
|
|
|
+ return resultResponse;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|