diff --git a/epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/dto/result/AuthorizationInfoResultDTO.java b/epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/dto/result/AuthorizationInfoResultDTO.java index 1d20dc3aac..ce31106941 100644 --- a/epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/dto/result/AuthorizationInfoResultDTO.java +++ b/epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/dto/result/AuthorizationInfoResultDTO.java @@ -29,7 +29,7 @@ public class AuthorizationInfoResultDTO implements Serializable { /** * authorizer_access_token 的有效期(在授权的公众号/小程序具备API权限时,才有此返回值),单位:秒 */ - private String expires_in; + private Integer expires_in; /** * 刷新令牌(在授权的公众号具备API权限时,才有此返回值),刷新令牌主要用于第三方平台获取和刷新已授权用户的 authorizer_access_token。 diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/constant/ThirdRedisKeyConstant.java b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/constant/ThirdRedisKeyConstant.java index 43e1e19f52..13fa20c226 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/constant/ThirdRedisKeyConstant.java +++ b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/constant/ThirdRedisKeyConstant.java @@ -34,7 +34,7 @@ public interface ThirdRedisKeyConstant { /** * 第三方 和 授权方交互使用的 */ - String AUTHORIZER_REFRESH_TOKEN_REDIS_KEY = "epmet:wechartthird:authorizerrefreshtoken"; + String AUTHORIZER_REFRESH_TOKEN_REDIS_KEY = "epmet:wechartthird:authorizerrefreshtoken:"; /** * auth_code 授权码 diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/dao/AuthCodeDao.java b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/dao/AuthCodeDao.java index 53bf395ea1..f4e9eaf698 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/dao/AuthCodeDao.java +++ b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/dao/AuthCodeDao.java @@ -55,5 +55,12 @@ public interface AuthCodeDao extends BaseDao { * @author zxc */ void updateAppId(@Param("customerId")String customerId,@Param("clientType")String clientType,@Param("authAppId")String authAppId); - + + /** + * @Description 逻辑删除客户下的授权码,保持一个授权码有用 + * @param customerId + * @param clientType + * @author zxc + */ + void deleteCustomerAuthCode(@Param("customerId")String customerId,@Param("clientType")String clientType); } \ No newline at end of file diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/redis/RedisThird.java b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/redis/RedisThird.java index 7cda9307d6..9317ce418e 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/redis/RedisThird.java +++ b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/redis/RedisThird.java @@ -1,5 +1,6 @@ package com.epmet.redis; +import cn.hutool.core.bean.BeanUtil; import com.epmet.commons.tools.redis.RedisUtils; import com.epmet.constant.ThirdRedisKeyConstant; import com.epmet.dto.form.AuthCodeFormDTO; @@ -10,6 +11,10 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; +import java.util.Map; + +import static com.epmet.commons.tools.redis.RedisUtils.NOT_EXPIRE; + /** * @Author zxc * @CreateTime 2020/7/7 9:33 @@ -74,17 +79,19 @@ public class RedisThird { * @author zxc */ public void setAuthorizerRefreshToken(AuthorizationInfoFormDTO authInfoDTO){ - String key = ThirdRedisKeyConstant.AUTHORIZER_REFRESH_TOKEN_REDIS_KEY + ThirdRedisKeyConstant.COLON + authInfoDTO.getCustomerId()+ThirdRedisKeyConstant.COLON+authInfoDTO.getClientType(); - redisUtils.set(key,authInfoDTO,-1); + Map map = BeanUtil.beanToMap(authInfoDTO, false, true); + String key = ThirdRedisKeyConstant.AUTHORIZER_REFRESH_TOKEN_REDIS_KEY + authInfoDTO.getCustomerId()+ThirdRedisKeyConstant.COLON+authInfoDTO.getClientType(); + redisUtils.hMSet(key, map,NOT_EXPIRE); } /** * @Description 获取刷新 - * @param key = epmet:wechartthird:authorizerrefreshtoken:customerId:clientType 前缀+客户ID+客户端类型 + * @param key = epmet:wechartthird:authorizerrefreshtoken:custome rId:clientType 前缀+客户ID+客户端类型 * @author zxc */ - public String getAuthorizerRefreshToken(String key){ - return redisTemplate.opsForValue().get(ThirdRedisKeyConstant.AUTHORIZER_REFRESH_TOKEN_REDIS_KEY+key).toString(); + public Map getAuthorizerRefreshToken(String key){ + Map result = redisUtils.hGetAll(ThirdRedisKeyConstant.AUTHORIZER_REFRESH_TOKEN_REDIS_KEY + key); + return result; } /** diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/impl/ComponentVerifyTicketServiceImpl.java b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/impl/ComponentVerifyTicketServiceImpl.java index dc71a4fff9..30517d5cb7 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/impl/ComponentVerifyTicketServiceImpl.java +++ b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/impl/ComponentVerifyTicketServiceImpl.java @@ -139,13 +139,16 @@ public class ComponentVerifyTicketServiceImpl implements ComponentVerifyTicketSe break; case ModuleConstant.AUTHORIZED: //授权成功 // 更改customer_mp 授权信息,appId,并绑定 - String authAppId = result.get(ModuleConstant.APP_ID); +// String authAppId = result.get(ModuleConstant.APP_ID); String authCode = result.get(ModuleConstant.AUTHORIZATION_CODE_HUMP); + log.info("=============================="+authCode); AuthCodeResultDTO authCodeResultDTO = authCodeDao.selectCustomerIdByAuthCode(authCode); String clientType = authCodeResultDTO.getClientType(); String customerId = authCodeResultDTO.getCustomerId(); + Map authorizerRefreshToken = redisThird.getAuthorizerRefreshToken(customerId + ThirdRedisKeyConstant.COLON + clientType); + String authAppId = authorizerRefreshToken.get("authorizerAppid").toString(); this.updateCustomerMpAppIdAndCreateOpenPlatform(customerId,authAppId,clientType); - this.authInfoByAuthCode(authCode, customerId,clientType); +// this.authInfoByAuthCode(authCode, customerId,clientType); authCodeDao.updateAppId(customerId,clientType,authAppId); this.saveAuthAccountInfo(customerId,authAppId,clientType); // customerMpDao.updateAuthorizationFlag(authAppId); @@ -269,7 +272,7 @@ public class ComponentVerifyTicketServiceImpl implements ComponentVerifyTicketSe authorizationInfoResultDTO = mapToEntity(map, AuthorizationInfoResultDTO.class); String authAppId = authorizationInfoResultDTO.getAuthorizer_appid(); log.info("授权信息:"+map); - String expiresIn = authorizationInfoResultDTO.getExpires_in(); + String expiresIn = authorizationInfoResultDTO.getExpires_in().toString(); Date expiresInTime = this.countExpirationTime(expiresIn); //授权信息分为两张表,基础信息authorization_info,授权列表func_info // 1. 基础信息 @@ -288,7 +291,7 @@ public class ComponentVerifyTicketServiceImpl implements ComponentVerifyTicketSe List funcInfos = new ArrayList<>(); List func_info = authorizationInfoResultDTO.getFunc_info(); log.info("权限列表信息:"+func_info); - func_info.forEach(func -> { + /*func_info.forEach(func -> { func.forEach((key,value) -> { FuncInfoFormDTO fu = new FuncInfoFormDTO(); fu.setFuncscopeCategory((String) key); @@ -298,10 +301,10 @@ public class ComponentVerifyTicketServiceImpl implements ComponentVerifyTicketSe fu.setCustomerId(customerId); funcInfos.add(fu); }); - }); - //先逻辑删除,在插入 - funcInfoDao.updateOldFuncInfo(customerId,authAppId); - funcInfoDao.insertFuncInfo(funcInfos); + });*/ + // todo 先逻辑删除,在插入 + /*funcInfoDao.updateOldFuncInfo(customerId,authAppId); + funcInfoDao.insertFuncInfo(funcInfos);*/ // 授权信息放入缓存 redisThird.setAuthInfo(authorizationInfoResultDTO,customerId,clientType); //authorizer_refresh_token 放入缓存 @@ -375,6 +378,7 @@ public class ComponentVerifyTicketServiceImpl implements ComponentVerifyTicketSe String client = authCodeAndTime.getClientType(); String expiresIn = authCodeAndTime.getExpiresIn(); String customerId = tokenDto.getCustomerId(); + customerId = "f530774b31e0609a3c7f0f83794cda0c"; Date expiresInTime = this.countExpirationTime(expiresIn); if (StringUtils.isBlank(customerId)||StringUtils.isBlank(client)||StringUtils.isBlank(authCode)||StringUtils.isBlank(expiresIn)){ log.info("客户ID = "+customerId+", 客户端类型为 = "+client+", 授权码为 = "+authCode+", 有效期 = "+expiresIn); @@ -387,9 +391,11 @@ public class ComponentVerifyTicketServiceImpl implements ComponentVerifyTicketSe //授权方AppId formDTO.setCustomerId(customerId); log.info(formDTO.toString()); + authCodeDao.deleteCustomerAuthCode(customerId,client); authCodeDao.insertRedirectAuthCode(formDTO); //authCode存缓存 redisThird.setAuthCode(formDTO); + this.authInfoByAuthCode(authCode, customerId,client); log.info("回调结束"); } @@ -496,14 +502,15 @@ public class ComponentVerifyTicketServiceImpl implements ComponentVerifyTicketSe log.info("开始创建开放平台账号并绑定"); Integer authCount = customerMpDao.selectAuthCount(customerId); String openPlatformId = null; - String authorizerRefreshToken = redisThird.getAuthorizerRefreshToken(customerId + ThirdRedisKeyConstant.COLON + clientType); + Map authorizerRefreshToken = redisThird.getAuthorizerRefreshToken(customerId + ThirdRedisKeyConstant.COLON + clientType); + String authorizerAccessToken = authorizerRefreshToken.get("authorizerAccessToken").toString(); if (authCount==NumConstant.ZERO){ log.info("未查询到该客户授权信息,先创建开放平台账号,再绑定"); //没有任何一个小程序/公众号授权,【先创建,再绑定】 JSONObject jsonObject = new JSONObject(); - jsonObject.put(ModuleConstant.APP_ID,authAppId); + jsonObject.put(ModuleConstant.LOW_APP_ID,authAppId); // 此处的 access_token 为 【authorizer_access_token】 - String data = HttpClientManager.getInstance().sendPostByJSON(WxMaCodeConstant.API_CREATE_OPEN + authorizerRefreshToken, JSON.toJSONString(jsonObject)).getData(); + String data = HttpClientManager.getInstance().sendPostByJSON(WxMaCodeConstant.API_CREATE_OPEN + authorizerAccessToken, JSON.toJSONString(jsonObject)).getData(); Map map = JSON.parseObject(data, Map.class); CreateOpenResultDTO createOpen = new CreateOpenResultDTO(); createOpen.setErrCode(Integer.valueOf(map.get(ModuleConstant.ERR_CODE))); diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/wxapi/constant/WxMaCodeConstant.java b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/wxapi/constant/WxMaCodeConstant.java index 43da294db4..e98f7a4701 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/wxapi/constant/WxMaCodeConstant.java +++ b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/wxapi/constant/WxMaCodeConstant.java @@ -16,7 +16,7 @@ public interface WxMaCodeConstant { /** * 使用授权码获取授权信息请求地址 */ - String API_QUERY_AUTH_URL = "https://api.weixin.qq.com/cgi-bin/component/api_query_auth"; + String API_QUERY_AUTH_URL = "https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token="; /** * 获取令牌请求地址 @@ -41,7 +41,8 @@ public interface WxMaCodeConstant { * pre_auth_code:预授权码 * redirect_uri:回调url(获取授权码) */ - String API_AUTH_REGISTER_URL = "https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=%s&pre_auth_code=%s&redirect_uri=%s"; +// String API_AUTH_REGISTER_URL = "https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=%s&pre_auth_code=%s&redirect_uri=%s"; + String API_AUTH_REGISTER_URL = "https://mp.weixin.qq.com/safe/bindcomponent?action=bindcomponent&auth_type=3&no_scan=1&component_appid=%s&pre_auth_code=%s&redirect_uri=%s#wechat_redirect"; /** * 创建开放平台帐号并绑定公众号/小程序 diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/AuthCodeDao.xml b/epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/AuthCodeDao.xml index 81440df943..b9b9ad4e2e 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/AuthCodeDao.xml +++ b/epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/AuthCodeDao.xml @@ -42,4 +42,14 @@ AND auth_code = #{authCode} + + + UPDATE auth_code + SET del_flag = 1 + where + del_flag = 0 + AND customer_id = #{customerId} + AND client_type = #{clientType} + + \ No newline at end of file diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/AuthorizationInfoDao.xml b/epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/AuthorizationInfoDao.xml index ea012b7f0a..406a9fd06c 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/AuthorizationInfoDao.xml +++ b/epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/AuthorizationInfoDao.xml @@ -14,7 +14,7 @@ #{authorizerAccessToken}, #{expiresInTime}, #{authorizerRefreshToken}, - #{clientType} + #{clientType}, #{delFlag}, #{createdBy}, NOW(), diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/CustomerMpDao.xml b/epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/CustomerMpDao.xml index 245761becf..4612ca8c88 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/CustomerMpDao.xml +++ b/epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/CustomerMpDao.xml @@ -27,7 +27,7 @@ WHERE del_flag = '0' AND customer_id = #{customerId} - AND authorization_flag = 0 + AND authorization_flag = 1