From f872354f633567c7801f51d9afbc616a624ad536 Mon Sep 17 00:00:00 2001 From: wangxianzhang Date: Thu, 3 Nov 2022 14:46:44 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9A=E6=8E=A5=E5=85=A5?= =?UTF-8?q?=E5=B1=B1=E4=B8=9C=E9=80=9A=E7=9A=84sso=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ThirdLoginController.java | 14 ++ .../com/epmet/service/ThirdLoginService.java | 9 + .../service/impl/ThirdLoginServiceImpl.java | 52 ++++++ .../epmet/commons/tools/redis/RedisKeys.java | 11 ++ .../com/epmet/feign/ThirdOpenFeignClient.java | 10 ++ .../java/com/epmet/sdt/SdtBaseResult.java | 28 ++++ .../epmet/sdt/SdtGetAccessTokenResult.java | 12 ++ .../com/epmet/sdt/SdtStaffDetailResult.java | 17 ++ .../com/epmet/sdt/SdtStaffInfoResult.java | 17 ++ .../com/epmet/controller/SdtController.java | 47 ++++++ .../java/com/epmet/dao/SdtAppInfoDao.java | 16 ++ .../com/epmet/entity/SdtAppInfoEntity.java | 54 ++++++ .../java/com/epmet/service/SdtService.java | 35 ++++ .../epmet/service/impl/SdtServiceImpl.java | 156 ++++++++++++++++++ .../V0.0.19__sdt_create_corp_info.sql | 19 +++ .../main/resources/mapper/SdtAppInfoDao.xml | 22 +++ 16 files changed, 519 insertions(+) create mode 100644 epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/sdt/SdtBaseResult.java create mode 100644 epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/sdt/SdtGetAccessTokenResult.java create mode 100644 epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/sdt/SdtStaffDetailResult.java create mode 100644 epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/sdt/SdtStaffInfoResult.java create mode 100644 epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/controller/SdtController.java create mode 100755 epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/dao/SdtAppInfoDao.java create mode 100755 epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/entity/SdtAppInfoEntity.java create mode 100644 epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/SdtService.java create mode 100644 epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/impl/SdtServiceImpl.java create mode 100644 epmet-module/epmet-third/epmet-third-server/src/main/resources/db/migration/V0.0.19__sdt_create_corp_info.sql create mode 100755 epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/SdtAppInfoDao.xml diff --git a/epmet-auth/src/main/java/com/epmet/controller/ThirdLoginController.java b/epmet-auth/src/main/java/com/epmet/controller/ThirdLoginController.java index 3f23ee88c8..dbf4156922 100644 --- a/epmet-auth/src/main/java/com/epmet/controller/ThirdLoginController.java +++ b/epmet-auth/src/main/java/com/epmet/controller/ThirdLoginController.java @@ -228,4 +228,18 @@ public class ThirdLoginController { return new Result().ok(thirdLoginService.yanTaiSSOLogin(authCode)); } + /** + * 山东通sso登录 + * @author wxz + * @date 2022/11/3 10:19 + * * @param authCode + * * @return Result + */ + @PostMapping("sso-govlogin-sdt/{authCode}") + public Result sdtSSOLogin(@PathVariable(value = "authCode") String authCode) { + // 烟台客户id,暂且写死。 + String customerId = "1535072605621841922"; + return new Result().ok(thirdLoginService.sdtSSOLogin(authCode, customerId)); + } + } diff --git a/epmet-auth/src/main/java/com/epmet/service/ThirdLoginService.java b/epmet-auth/src/main/java/com/epmet/service/ThirdLoginService.java index f939656023..eb93a5bb97 100644 --- a/epmet-auth/src/main/java/com/epmet/service/ThirdLoginService.java +++ b/epmet-auth/src/main/java/com/epmet/service/ThirdLoginService.java @@ -124,4 +124,13 @@ public interface ThirdLoginService { * @return */ UserTokenResultDTO yanTaiSSOLogin(String authCode); + + /** + * 山东通sso登录 + * @author wxz + * @date 2022/11/3 10:20 + * * @param authCode + * * @return UserTokenResultDTO + */ + UserTokenResultDTO sdtSSOLogin(String authCode, String customerId); } diff --git a/epmet-auth/src/main/java/com/epmet/service/impl/ThirdLoginServiceImpl.java b/epmet-auth/src/main/java/com/epmet/service/impl/ThirdLoginServiceImpl.java index 3d2c2ebee0..8b60fab849 100644 --- a/epmet-auth/src/main/java/com/epmet/service/impl/ThirdLoginServiceImpl.java +++ b/epmet-auth/src/main/java/com/epmet/service/impl/ThirdLoginServiceImpl.java @@ -41,6 +41,8 @@ import com.epmet.feign.*; import com.epmet.jwt.JwtTokenProperties; import com.epmet.jwt.JwtTokenUtils; import com.epmet.redis.CaptchaRedis; +import com.epmet.sdt.SdtStaffDetailResult; +import com.epmet.sdt.SdtStaffInfoResult; import com.epmet.service.GovWebService; import com.epmet.service.ThirdLoginService; import com.taobao.api.ApiException; @@ -1168,4 +1170,54 @@ public class ThirdLoginServiceImpl implements ThirdLoginService, ResultDataResol } return customerId; } + + @Override + public UserTokenResultDTO sdtSSOLogin(String authCode, String customerId) { + String accessToken = getResultDataOrThrowsException(thirdOpenFeignClient.getSdtCachedAccessToken(customerId), ServiceConstant.EPMET_THIRD_SERVER, + EpmetErrorCode.SERVER_ERROR.getCode(), "获取山东通access_token失败", null); + + // 1.获取用户编码 + HashMap params = new HashMap<>(); + params.put("access_token", accessToken); + params.put("code", authCode); + Result sdtResult = HttpClientManager.getInstance().sendGet("/cgi-bin/user/getuserinfo", params); + if (!sdtResult.success()) { + // http状态判断 + log.error("【山东通登录】查询用户编码-http错误:{}", sdtResult.getMsg()); + throw new EpmetException(EpmetErrorCode.SERVER_ERROR.getCode(), "调用山东通查询用户编码失败", null); + } + + SdtStaffInfoResult staffInfoResult = JSON.parseObject(sdtResult.getData(), SdtStaffInfoResult.class); + if (!staffInfoResult.success()) { + // 山东通业务返回状态判断 + log.error("【山东通登录】查询用户编码失败,错误信息:{}", staffInfoResult.getErrmsg()); + throw new EpmetException(EpmetErrorCode.SERVER_ERROR.getCode(), "调用山东通查询用户编码失败", null); + } + + // 2.获取用户详细信息 + HashMap userDetailParams = new HashMap<>(); + userDetailParams.put("access_token", accessToken); + userDetailParams.put("userid", staffInfoResult.getUserId()); + Result userDetailResult = HttpClientManager.getInstance().sendGet("/cgi-bin/user/get", userDetailParams); + if (!userDetailResult.success()) { + // http状态判断 + log.error("【山东通登录】查询用户详细信息-http错误:{}", sdtResult.getMsg()); + throw new EpmetException(EpmetErrorCode.SERVER_ERROR.getCode(), "调用山东通查询用户详细信息失败", null); + } + + SdtStaffDetailResult staffDetailResult = JSON.parseObject(userDetailResult.getData(), SdtStaffDetailResult.class); + if (!staffDetailResult.success()) { + // 山东通业务返回状态判断 + log.error("【山东通登录】查询用户详细信息失败,错误信息:{}", staffInfoResult.getErrmsg()); + throw new EpmetException(EpmetErrorCode.SERVER_ERROR.getCode(), "调用山东通查询用户详细信息失败", null); + } + + // 3.有了手机号,可以做登录了 + String mobile = staffDetailResult.getMobile(); + GovWebLoginFormDTO loginGovParam = new GovWebLoginFormDTO(); + loginGovParam.setCustomerId(customerId); + loginGovParam.setPhone(mobile); + + return govWebService.loginByThirdPlatform(loginGovParam); + } } diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisKeys.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisKeys.java index 63f27f195b..14a25b09db 100644 --- a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisKeys.java +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisKeys.java @@ -922,4 +922,15 @@ public class RedisKeys { public static String getOperExamineResourceUrls() { return rootPrefix.concat("oper:access:examineresources"); } + + /** + * 山东通的Accesstoken缓存 + * @author wxz + * @date 2022/11/2 22:59 + * * @param customerId + * * @return String + */ + public static String getSdtAccessToken(String customerId) { + return rootPrefix.concat("thirdplatform:sdt:accesstoken:").concat(customerId); + }; } diff --git a/epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/feign/ThirdOpenFeignClient.java b/epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/feign/ThirdOpenFeignClient.java index 1f0f16c314..003ae99e03 100644 --- a/epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/feign/ThirdOpenFeignClient.java +++ b/epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/feign/ThirdOpenFeignClient.java @@ -6,6 +6,7 @@ import com.epmet.dto.form.*; import com.epmet.dto.result.ThirdplatformResultDTO; import com.epmet.feign.fallback.ThirdOpenFeignClientFallbackFactory; import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestParam; @@ -89,4 +90,13 @@ public interface ThirdOpenFeignClient { @PostMapping("/third/dataSync/yanTai/sync/user") Result getYanTaiUserInfo(@RequestParam("orgId") String orgId); + /** + * 查询山东通AccesToken + * @author wxz + * @date 2022/11/3 10:12 + * + * * @return Result + */ + @GetMapping("/third/sdt/getCachedAccessToken") + Result getSdtCachedAccessToken(@RequestParam("customerId") String customerId); } diff --git a/epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/sdt/SdtBaseResult.java b/epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/sdt/SdtBaseResult.java new file mode 100644 index 0000000000..5d8c16f157 --- /dev/null +++ b/epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/sdt/SdtBaseResult.java @@ -0,0 +1,28 @@ +package com.epmet.sdt; + +import lombok.Data; + +/** + * 山东通返回结果封装 + */ +@Data +public class SdtBaseResult { + + /** + * 成功状态码 + */ + public static final Integer SUCCESS_CODE = 0; + + private Integer errcode; + private String errmsg; + + /** + * 请求是否成功 + * @author wxz + * @date 2022/11/2 22:43 + * @return boolean + */ + public boolean success() { + return (errcode != null && errcode == SUCCESS_CODE) ? true : false; + } +} \ No newline at end of file diff --git a/epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/sdt/SdtGetAccessTokenResult.java b/epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/sdt/SdtGetAccessTokenResult.java new file mode 100644 index 0000000000..d4a7214557 --- /dev/null +++ b/epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/sdt/SdtGetAccessTokenResult.java @@ -0,0 +1,12 @@ +package com.epmet.sdt; + +import lombok.Data; + +/** + * 获取山东通AccessToken接口的返回Result + */ +@Data +public class SdtGetAccessTokenResult extends SdtBaseResult { + private String access_token; + private Long expires_in; +} \ No newline at end of file diff --git a/epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/sdt/SdtStaffDetailResult.java b/epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/sdt/SdtStaffDetailResult.java new file mode 100644 index 0000000000..c26bacaed9 --- /dev/null +++ b/epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/sdt/SdtStaffDetailResult.java @@ -0,0 +1,17 @@ +package com.epmet.sdt; + +import lombok.Data; + +/** + * 返回了挺多属性,但是我只取手机号 + * @ClassName SdtStaffInfoResult + * @Description TODO + * @Author wangxianzhang + * @Date 2022/11/3 10:58 + */ +@Data +public class SdtStaffDetailResult extends SdtBaseResult { + private String userid; + private String name; + private String mobile; +} diff --git a/epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/sdt/SdtStaffInfoResult.java b/epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/sdt/SdtStaffInfoResult.java new file mode 100644 index 0000000000..99b7928126 --- /dev/null +++ b/epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/sdt/SdtStaffInfoResult.java @@ -0,0 +1,17 @@ +package com.epmet.sdt; + +import lombok.Data; + +/** + * @ClassName SdtStaffInfoResult + * @Description TODO + * @Author wangxianzhang + * @Date 2022/11/3 10:58 + */ +@Data +public class SdtStaffInfoResult extends SdtBaseResult { + private String UserId; + private String DeviceId; + private String user_ticket; + private String expires_in; +} diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/controller/SdtController.java b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/controller/SdtController.java new file mode 100644 index 0000000000..ae16a669dc --- /dev/null +++ b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/controller/SdtController.java @@ -0,0 +1,47 @@ +package com.epmet.controller; + +import com.epmet.commons.tools.utils.EpmetRequestHolder; +import com.epmet.commons.tools.utils.Result; +import com.epmet.service.SdtService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +/** + * 山东通controller + */ +@RestController +@RequestMapping("sdt") +public class SdtController { + + @Autowired + private SdtService sdtService; + + /** + * 刷新山东通token + * @author wxz + * @date 2022/11/2 22:11 + * @return Result + */ + @GetMapping("refreshAccessToken") + public Result refreshAccessToken() { + sdtService.refreshAccessToken(null); + return new Result(); + } + + /** + * 查询山东通AccesToken + * @author wxz + * @date 2022/11/3 10:12 + * + * * @return Result + */ + @GetMapping("getCachedAccessToken") + public Result getCachedAccessToken(@RequestParam("customerId") String customerId) { + String cachedAccessToken = sdtService.getCachedAccessToken(EpmetRequestHolder.getLoginUserCustomerId()); + return new Result().ok(cachedAccessToken); + } + +} diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/dao/SdtAppInfoDao.java b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/dao/SdtAppInfoDao.java new file mode 100755 index 0000000000..cf4ff7019e --- /dev/null +++ b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/dao/SdtAppInfoDao.java @@ -0,0 +1,16 @@ +package com.epmet.dao; + +import com.epmet.commons.mybatis.dao.BaseDao; +import com.epmet.entity.SdtAppInfoEntity; +import org.apache.ibatis.annotations.Mapper; + +/** + * 接入山东通app信息 + * + * @author generator generator@elink-cn.com + * @since v1.0.0 2022-11-02 + */ +@Mapper +public interface SdtAppInfoDao extends BaseDao { + +} \ No newline at end of file diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/entity/SdtAppInfoEntity.java b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/entity/SdtAppInfoEntity.java new file mode 100755 index 0000000000..a61e2e5e9f --- /dev/null +++ b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/entity/SdtAppInfoEntity.java @@ -0,0 +1,54 @@ +package com.epmet.entity; + +import com.baomidou.mybatisplus.annotation.TableName; + +import com.epmet.commons.mybatis.entity.BaseEpmetEntity; +import lombok.Data; +import lombok.EqualsAndHashCode; + +import java.util.Date; + +/** + * 接入山东通app信息 + * + * @author generator generator@elink-cn.com + * @since v1.0.0 2022-11-02 + */ +@Data +@EqualsAndHashCode(callSuper=false) +@TableName("sdt_app_info") +public class SdtAppInfoEntity extends BaseEpmetEntity { + + private static final long serialVersionUID = 1L; + + /** + * 客户ID + */ + private String customerId; + + /** + * 组织ID + */ + private String corpId; + + /** + * 秘钥 + */ + private String corpSecret; + + /** + * api地址 + */ + private String apiAddress; + + /** + * 接口调用凭证 + */ + private String accessToken; + + /** + * 何时过期 + */ + private Date expiresAt; + +} diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/SdtService.java b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/SdtService.java new file mode 100644 index 0000000000..ebc98e60ea --- /dev/null +++ b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/SdtService.java @@ -0,0 +1,35 @@ +package com.epmet.service; + +import com.zaxxer.hikari.util.FastList; +import lombok.Data; + +/** + * @ClassName SdtService + * @Description 山东通相关service + * @Author wangxianzhang + * @Date 2022/11/2 22:02 + */ +public interface SdtService { + + /** + * 获取accessToken的url地址 + * /cgi-bin/gettoken?corpid=id&corpsecret=secrect + */ + String API_GET_ACCESS_TOKEN_URL = "/cgi-bin/gettoken"; + + /** + * 刷新token + * @author wxz + * @date 2022/11/2 22:50 + */ + void refreshAccessToken(String customerId); + + /** + * 获取缓存的AccessToken + * @author wxz + * @date 2022/11/2 22:56 + * + * * @return String + */ + String getCachedAccessToken(String customerId); +} diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/impl/SdtServiceImpl.java b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/impl/SdtServiceImpl.java new file mode 100644 index 0000000000..3ae9a03bb4 --- /dev/null +++ b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/impl/SdtServiceImpl.java @@ -0,0 +1,156 @@ +package com.epmet.service.impl; + +import com.alibaba.fastjson.JSON; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.epmet.commons.tools.redis.RedisKeys; +import com.epmet.commons.tools.redis.RedisUtils; +import com.epmet.commons.tools.utils.HttpClientManager; +import com.epmet.commons.tools.utils.Result; +import com.epmet.dao.SdtAppInfoDao; +import com.epmet.entity.SdtAppInfoEntity; +import com.epmet.sdt.SdtGetAccessTokenResult; +import com.epmet.service.SdtService; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.*; + +/** + * @ClassName SdtServiceImpl + * @Description 山东通相关service + * @Author wangxianzhang + * @Date 2022/11/2 22:03 + */ +@Service +@Slf4j +public class SdtServiceImpl implements SdtService { + + @Autowired + private SdtAppInfoDao sdtAppInfoDao; + + @Autowired + private RedisUtils redisUtils; + + /** + * 刷新accessToken + * @author wxz + * @date 2022/11/2 22:53 + */ + @Override + public void refreshAccessToken(String customerId) { + List appInfos; + + if (StringUtils.isBlank(customerId)) { + // 没传递customerId,刷新所有 + appInfos = sdtAppInfoDao.selectList(new QueryWrapper<>()); + } else { + // 传递了customerId,只刷新该客户的 + LambdaQueryWrapper query = new LambdaQueryWrapper<>(); + query.eq(SdtAppInfoEntity::getCustomerId, customerId); + appInfos = sdtAppInfoDao.selectList(query); + } + + appInfos.stream().forEach(app -> { + Date now = new Date(); + String apiAddress = app.getApiAddress(); + String customerIdInner = app.getCustomerId(); + + HashMap params = new HashMap<>(); + params.put("corpid", app.getCorpId()); + params.put("corpsecret", app.getCorpSecret()); + Result stringResult = HttpClientManager.getInstance().sendGet(apiAddress + API_GET_ACCESS_TOKEN_URL, params); + if (!stringResult.success()) { + // 请求有问题 + log.error("【山东通】刷新accessToken出现异常:{}", stringResult.getMsg()); + return; + } + + SdtGetAccessTokenResult sdtResult = JSON.parseObject(stringResult.getMsg(), SdtGetAccessTokenResult.class); + if (!sdtResult.success()) { + log.error("【山东通】刷新accessToken失败:{}", sdtResult.getErrmsg()); + return; + } + + String access_token = sdtResult.getAccess_token(); + Long expires_in = sdtResult.getExpires_in(); + + SdtAppInfoEntity updateEntity = new SdtAppInfoEntity(); + updateEntity.setId(app.getId()); + updateEntity.setAccessToken(access_token); + updateEntity.setExpiresAt(getExpireAt(now, expires_in)); + sdtAppInfoDao.updateById(updateEntity); + + // token刷新了以后,要刷新缓存中的token,这样下次才能获取到最新的token + String key = RedisKeys.getOpenApiAccessTokenKey(customerIdInner); + + redisUtils.set(key, access_token, expires_in); + }); + } + + /** + * 计算过期时间 + * @author wxz + * @date 2022/11/2 23:22 + * * @param startTime + * @param expires_in + * * @return Date + */ + private Date getExpireAt(Date startTime, Long expires_in) { + Calendar calendar = Calendar.getInstance(); + calendar.setTime(startTime); + calendar.add(Calendar.SECOND, expires_in.intValue()); + return calendar.getTime(); + } + + /** + * 还有多少秒过期 + * @author wxz + * @date 2022/11/3 10:08 + * * @param expireAt + * * @return Long + */ + private Long getExpireIn(Date expireAt) { + return expireAt.getTime() - System.currentTimeMillis(); + } + + /** + * 获取缓存的AccessToken + * @author wxz + * @date 2022/11/2 22:56 + * + * * @return String + */ + @Override + public String getCachedAccessToken(String customerId) { + String key = RedisKeys.getOpenApiAccessTokenKey(customerId); + String accessToken = redisUtils.getString(key); + if (StringUtils.isNotBlank(accessToken)) { + return accessToken; + } + + // db中查询 + LambdaQueryWrapper query = new LambdaQueryWrapper<>(); + query.eq(SdtAppInfoEntity::getCustomerId, customerId); + SdtAppInfoEntity sdtAppInfoEntity = sdtAppInfoDao.selectOne(query); + + if (sdtAppInfoEntity == null) { + log.error("【查询山东通AccessToken】失败,原因:该客户id没有配置山东通相关信息"); + return null; + } + + accessToken = sdtAppInfoEntity.getAccessToken(); + Date expiresAt = sdtAppInfoEntity.getExpiresAt(); + + if (StringUtils.isNotBlank(accessToken)) { + redisUtils.set(key, accessToken, getExpireIn(expiresAt)); + return accessToken; + } else { + // 配置了appInfo,但是没有accessToken,刷新token,刷新缓存,然后重新从缓存中取 + refreshAccessToken(customerId); + return redisUtils.getString(key); + } + } +} diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/resources/db/migration/V0.0.19__sdt_create_corp_info.sql b/epmet-module/epmet-third/epmet-third-server/src/main/resources/db/migration/V0.0.19__sdt_create_corp_info.sql new file mode 100644 index 0000000000..a9cdb09fd9 --- /dev/null +++ b/epmet-module/epmet-third/epmet-third-server/src/main/resources/db/migration/V0.0.19__sdt_create_corp_info.sql @@ -0,0 +1,19 @@ +create table sdt_app_info +( + `ID` varchar(64) NOT NULL COMMENT '主键', + `CUSTOMER_ID` varchar(64) NOT NULL COMMENT '客户ID', + `CORPID` varchar(32) not null comment '组织ID', + `CORPSECRET` varchar(64) not null comment '秘钥', + `ACCESS_TOKEN` varchar(512) not null comment '接口调用凭证', + `API_ADDRESS` varchar(128) not null comment 'api地址', + `EXPIRES_AT` datetime not null comment '何时过期', + `DEL_FLAG` int(11) NOT NULL COMMENT '删除标识:0.未删除 1.已删除', + `REVISION` int(11) NOT NULL COMMENT '乐观锁', + `CREATED_BY` varchar(64) NOT NULL COMMENT '创建人', + `CREATED_TIME` datetime NOT NULL COMMENT '创建时间', + `UPDATED_BY` varchar(64) NOT NULL COMMENT '更新人', + `UPDATED_TIME` datetime NOT NULL COMMENT '更新时间', + PRIMARY KEY (`ID`) +) ENGINE = InnoDB + DEFAULT CHARSET = utf8mb4 COMMENT ='接入山东通app信息'; + diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/SdtAppInfoDao.xml b/epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/SdtAppInfoDao.xml new file mode 100755 index 0000000000..aa068e47f6 --- /dev/null +++ b/epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/SdtAppInfoDao.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file