...
|
...
|
@@ -24,31 +24,9 @@ public class MiniAppAccessTokenServiceImpl implements IMiniAppAccessTokenService |
|
|
|
|
|
private final static Logger logger = LoggerFactory.getLogger(MiniAppAccessTokenServiceImpl.class);
|
|
|
|
|
|
@Autowired
|
|
|
private PushRedisService pushRedisService;
|
|
|
|
|
|
@Value("${mini.accessToken.get:http://api.yoho.cn?method=wechat.token.getmini}")
|
|
|
private String MINI_ACCESSTOKEN_GET;
|
|
|
|
|
|
private AccessToken getTokenByAccount(String miniappType) {
|
|
|
|
|
|
// 1 代表新与力的 小程序
|
|
|
// 0 有货的小程序
|
|
|
if (!"1".equals(miniappType)){
|
|
|
miniappType = "0";
|
|
|
}
|
|
|
|
|
|
String accessTokenKey = KeyBuilder.getMiniAppAccesTokenKey(miniappType);
|
|
|
AccessToken accessToken = getAccessTokenFromUrl(accessTokenKey,miniappType);
|
|
|
try {
|
|
|
long expireTime = accessToken.getExpiresIn() - 1800;
|
|
|
pushRedisService.value_setWithExpired(accessTokenKey, CacheHelper.value2String(accessToken), expireTime, TimeUnit.SECONDS);
|
|
|
} catch (Exception e) {
|
|
|
logger.warn("set token to cache fail! e {}", e);
|
|
|
}
|
|
|
return accessToken;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取AccesToken
|
|
|
* @param miniappType
|
...
|
...
|
@@ -62,28 +40,18 @@ public class MiniAppAccessTokenServiceImpl implements IMiniAppAccessTokenService |
|
|
miniappType = "0";
|
|
|
}
|
|
|
|
|
|
String accessTokenKey = KeyBuilder.getMiniAppAccesTokenKey(miniappType);
|
|
|
AccessToken accessToken = getAccessTokenFromUrl(miniappType);
|
|
|
|
|
|
String result = pushRedisService.value_get(accessTokenKey);
|
|
|
|
|
|
if (StringUtils.isNotEmpty(result)){
|
|
|
AccessToken accessToken = CacheHelper.string2Value(result, AccessToken.class);
|
|
|
if (accessToken != null){
|
|
|
return accessToken;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return getTokenByAccount(miniappType);
|
|
|
return accessToken;
|
|
|
|
|
|
}
|
|
|
|
|
|
public AccessToken getAccessTokenFromUrl(String accessTokenKey, String miniappType) {
|
|
|
public AccessToken getAccessTokenFromUrl(String miniappType) {
|
|
|
String url = MINI_ACCESSTOKEN_GET+"&miniappType="+miniappType;
|
|
|
JSONObject res = HttpClientUtil.doGet(url, "UTF-8");
|
|
|
logger.info("getAccessTokenFromUrl is {}, result is {}", url, JSONObject.toJSONString(res));
|
|
|
if (null != res && res.getIntValue("code") == 200) {
|
|
|
AccessToken accessToken = new AccessToken(res.getString("data"), 1800);
|
|
|
pushRedisService.value_setWithExpired(accessTokenKey, accessToken.getToken() + "," + 1800, 1800, TimeUnit.SECONDS);
|
|
|
return accessToken;
|
|
|
}
|
|
|
return null;
|
...
|
...
|
|