|
|
@@ -1,6 +1,8 @@ |
|
|
package com.iformall.service.wechat; |
|
|
package com.iformall.service.wechat; |
|
|
|
|
|
|
|
|
import com.iformall.common.FmHttpClientBuilder; |
|
|
import com.iformall.common.FmHttpClientBuilder; |
|
|
|
|
|
import com.iformall.domain.po.WxAuthorizerInfo; |
|
|
|
|
|
import com.iformall.domain.po.WxComponentVerifyTicket; |
|
|
import com.iformall.mapper.WxAuthorizerInfoMapper; |
|
|
import com.iformall.mapper.WxAuthorizerInfoMapper; |
|
|
import com.iformall.mapper.WxComponentVerifyTicketMapper; |
|
|
import com.iformall.mapper.WxComponentVerifyTicketMapper; |
|
|
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder; |
|
|
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder; |
|
|
@@ -9,10 +11,13 @@ import me.chanjar.weixin.open.bean.WxOpenAuthorizerAccessToken; |
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
import org.slf4j.Logger; |
|
|
import org.slf4j.Logger; |
|
|
import org.slf4j.LoggerFactory; |
|
|
import org.slf4j.LoggerFactory; |
|
|
|
|
|
import org.springframework.scheduling.annotation.Async; |
|
|
import redis.clients.jedis.Jedis; |
|
|
import redis.clients.jedis.Jedis; |
|
|
import redis.clients.jedis.JedisPool; |
|
|
import redis.clients.jedis.JedisPool; |
|
|
import redis.clients.util.Pool; |
|
|
import redis.clients.util.Pool; |
|
|
|
|
|
|
|
|
|
|
|
import java.util.Date; |
|
|
|
|
|
|
|
|
public class FmOpenInRedisDBConfigStorage extends WxOpenInMemoryConfigStorage { |
|
|
public class FmOpenInRedisDBConfigStorage extends WxOpenInMemoryConfigStorage { |
|
|
private final static String COMPONENT_VERIFY_TICKET_KEY = "wechat_component_verify_ticket:"; |
|
|
private final static String COMPONENT_VERIFY_TICKET_KEY = "wechat_component_verify_ticket:"; |
|
|
private final static String COMPONENT_ACCESS_TOKEN_KEY = "wechat_component_access_token:"; |
|
|
private final static String COMPONENT_ACCESS_TOKEN_KEY = "wechat_component_access_token:"; |
|
|
@@ -73,7 +78,27 @@ public class FmOpenInRedisDBConfigStorage extends WxOpenInMemoryConfigStorage { |
|
|
public String getComponentVerifyTicket() { |
|
|
public String getComponentVerifyTicket() { |
|
|
try (Jedis jedis = this.jedisPool.getResource()) { |
|
|
try (Jedis jedis = this.jedisPool.getResource()) { |
|
|
logger.info("getComponentVerifyTicket " + this.componentVerifyTicketKey); |
|
|
logger.info("getComponentVerifyTicket " + this.componentVerifyTicketKey); |
|
|
return jedis.get(this.componentVerifyTicketKey); |
|
|
|
|
|
|
|
|
String ticket = jedis.get(this.componentVerifyTicketKey); |
|
|
|
|
|
if(StringUtils.isBlank(ticket)) { |
|
|
|
|
|
String appId; |
|
|
|
|
|
String [] keys = componentAccessTokenKey.split(":"); |
|
|
|
|
|
if(StringUtils.isBlank(keyPrefix)) { |
|
|
|
|
|
appId = keys[1]; |
|
|
|
|
|
} else { |
|
|
|
|
|
appId = keys[2]; |
|
|
|
|
|
} |
|
|
|
|
|
WxComponentVerifyTicket wxComponentVerifyTicket = new WxComponentVerifyTicket(); |
|
|
|
|
|
wxComponentVerifyTicket.setComponentAppid(getComponentAppId()); |
|
|
|
|
|
try { |
|
|
|
|
|
wxComponentVerifyTicket = wxComponentVerifyTicketMapper.selectOne(wxComponentVerifyTicket); |
|
|
|
|
|
if(wxComponentVerifyTicket != null) { |
|
|
|
|
|
ticket = wxComponentVerifyTicket.getComponentVerifyTicket(); |
|
|
|
|
|
} |
|
|
|
|
|
}catch (Exception e) { |
|
|
|
|
|
logger.error(e.getMessage()); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return ticket; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@@ -110,18 +135,22 @@ public class FmOpenInRedisDBConfigStorage extends WxOpenInMemoryConfigStorage { |
|
|
public void updateComponentAccessTokent(String componentAccessToken, int expiresInSeconds) { |
|
|
public void updateComponentAccessTokent(String componentAccessToken, int expiresInSeconds) { |
|
|
try (Jedis jedis = this.jedisPool.getResource()) { |
|
|
try (Jedis jedis = this.jedisPool.getResource()) { |
|
|
jedis.setex(this.componentAccessTokenKey, expiresInSeconds - 200, componentAccessToken); |
|
|
jedis.setex(this.componentAccessTokenKey, expiresInSeconds - 200, componentAccessToken); |
|
|
/* |
|
|
|
|
|
WxComponentVerifyTicket ticket = new WxComponentVerifyTicket(); |
|
|
|
|
|
ticket.setComponentAppid(getComponentAppId()); |
|
|
|
|
|
ticket.setAccessToken(componentAccessToken); |
|
|
|
|
|
ticket.setAccessTokenExpire(new Date(Long.valueOf(System.currentTimeMillis() + (expiresInSeconds * 1000)))); |
|
|
|
|
|
int num = wxComponentVerifyTicketMapper.updateAccessToken(ticket); |
|
|
|
|
|
logger.info("updateComponentVerifyTicketAccessToken rows: " + num); |
|
|
|
|
|
*/ |
|
|
|
|
|
logger.info("updateComponentVerifyTicketAccessToken " + componentAccessToken); |
|
|
logger.info("updateComponentVerifyTicketAccessToken " + componentAccessToken); |
|
|
|
|
|
|
|
|
|
|
|
saveComponentVerifyTicket(componentAccessToken, expiresInSeconds); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Async |
|
|
|
|
|
public void saveComponentVerifyTicket(String componentAccessToken, int expiresInSeconds) { |
|
|
|
|
|
WxComponentVerifyTicket ticket = new WxComponentVerifyTicket(); |
|
|
|
|
|
ticket.setComponentAppid(getComponentAppId()); |
|
|
|
|
|
ticket.setAccessToken(componentAccessToken); |
|
|
|
|
|
ticket.setAccessTokenExpire(new Date(Long.valueOf(System.currentTimeMillis() + (expiresInSeconds * 1000)))); |
|
|
|
|
|
int num = wxComponentVerifyTicketMapper.updateAccessToken(ticket); |
|
|
|
|
|
logger.info("updateComponentVerifyTicketAccessToken rows: " + num); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
private String getKey(String prefix, String appId) { |
|
|
private String getKey(String prefix, String appId) { |
|
|
return prefix.endsWith(":") ? prefix.concat(appId) : prefix.concat(":").concat(appId); |
|
|
return prefix.endsWith(":") ? prefix.concat(appId) : prefix.concat(":").concat(appId); |
|
|
} |
|
|
} |
|
|
@@ -137,17 +166,20 @@ public class FmOpenInRedisDBConfigStorage extends WxOpenInMemoryConfigStorage { |
|
|
public void setAuthorizerRefreshToken(String appId, String authorizerRefreshToken) { |
|
|
public void setAuthorizerRefreshToken(String appId, String authorizerRefreshToken) { |
|
|
try (Jedis jedis = this.jedisPool.getResource()) { |
|
|
try (Jedis jedis = this.jedisPool.getResource()) { |
|
|
jedis.set(this.getKey(this.authorizerRefreshTokenKey, appId), authorizerRefreshToken); |
|
|
jedis.set(this.getKey(this.authorizerRefreshTokenKey, appId), authorizerRefreshToken); |
|
|
/* |
|
|
|
|
|
|
|
|
|
|
|
WxAuthorizerInfo authorizerInfo = new WxAuthorizerInfo(); |
|
|
|
|
|
authorizerInfo.setAuthorizerAppid(appId); |
|
|
|
|
|
authorizerInfo.setRefreshToken(authorizerRefreshToken); |
|
|
|
|
|
int num = wxAuthorizerInfoMapper.updateRefreshToken(authorizerInfo); |
|
|
|
|
|
*/ |
|
|
|
|
|
logger.info("setAuthorizerRefreshToken " + appId); |
|
|
logger.info("setAuthorizerRefreshToken " + appId); |
|
|
|
|
|
|
|
|
|
|
|
saveAuthorizerRefreshToken(appId, authorizerRefreshToken); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Async |
|
|
|
|
|
public void saveAuthorizerRefreshToken(String appId, String authorizerRefreshToken) { |
|
|
|
|
|
WxAuthorizerInfo authorizerInfo = new WxAuthorizerInfo(); |
|
|
|
|
|
authorizerInfo.setAuthorizerAppid(appId); |
|
|
|
|
|
authorizerInfo.setRefreshToken(authorizerRefreshToken); |
|
|
|
|
|
int num = wxAuthorizerInfoMapper.updateRefreshToken(authorizerInfo); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public String getAuthorizerAccessToken(String appId) { |
|
|
public String getAuthorizerAccessToken(String appId) { |
|
|
try (Jedis jedis = this.jedisPool.getResource()) { |
|
|
try (Jedis jedis = this.jedisPool.getResource()) { |
|
|
@@ -172,15 +204,20 @@ public class FmOpenInRedisDBConfigStorage extends WxOpenInMemoryConfigStorage { |
|
|
@Override |
|
|
@Override |
|
|
public void updateAuthorizerAccessToken(String appId, WxOpenAuthorizerAccessToken authorizerAccessToken) { |
|
|
public void updateAuthorizerAccessToken(String appId, WxOpenAuthorizerAccessToken authorizerAccessToken) { |
|
|
this.updateAuthorizerAccessToken(appId, authorizerAccessToken.getAuthorizerAccessToken(), authorizerAccessToken.getExpiresIn()); |
|
|
this.updateAuthorizerAccessToken(appId, authorizerAccessToken.getAuthorizerAccessToken(), authorizerAccessToken.getExpiresIn()); |
|
|
/* |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logger.info("updateAuthorizerAccessToken " + appId); |
|
|
|
|
|
|
|
|
|
|
|
dbUpdateAccessToken(appId, authorizerAccessToken); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Async |
|
|
|
|
|
public void dbUpdateAccessToken(String appId, WxOpenAuthorizerAccessToken authorizerAccessToken) { |
|
|
WxAuthorizerInfo authorizerInfo = new WxAuthorizerInfo(); |
|
|
WxAuthorizerInfo authorizerInfo = new WxAuthorizerInfo(); |
|
|
authorizerInfo.setAuthorizerAppid(appId); |
|
|
authorizerInfo.setAuthorizerAppid(appId); |
|
|
authorizerInfo.setAccessToken(authorizerAccessToken.getAuthorizerAccessToken()); |
|
|
authorizerInfo.setAccessToken(authorizerAccessToken.getAuthorizerAccessToken()); |
|
|
authorizerInfo.setAccessTokenExpire(new Date(Long.valueOf(System.currentTimeMillis() + (authorizerAccessToken.getExpiresIn() * 1000)))); |
|
|
authorizerInfo.setAccessTokenExpire(new Date(Long.valueOf(System.currentTimeMillis() + (authorizerAccessToken.getExpiresIn() * 1000)))); |
|
|
int num = wxAuthorizerInfoMapper.updateAccessToken(authorizerInfo); |
|
|
int num = wxAuthorizerInfoMapper.updateAccessToken(authorizerInfo); |
|
|
logger.info("updateAuthorizerAccessToken " + appId + " rows: " + num); |
|
|
logger.info("updateAuthorizerAccessToken " + appId + " rows: " + num); |
|
|
*/ |
|
|
|
|
|
logger.info("updateAuthorizerAccessToken " + appId); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
|