| | |
| | | package com.ruoyi.system.utils.wx.tools; |
| | | |
| | | |
| | | import com.ruoyi.common.core.redis.RedisCache; |
| | | import com.ruoyi.common.redis.service.RedisService; |
| | | import com.ruoyi.system.utils.wx.body.resp.AccessTokenRespBody; |
| | | import com.ruoyi.system.utils.wx.body.resp.Code2SessionRespBody; |
| | |
| | | @Slf4j |
| | | public class WxAppletTools { |
| | | private final static String ACCESSTOKEN_CACHE_KEY = "accessToken"; |
| | | private final static String ACCESS_TOKEN_OFFICE_CACHE_KEY = "accessTokenOffice"; |
| | | /** |
| | | * 请求参数 |
| | | * 属性 类型 默认值 必填 说明 |
| | |
| | | public static String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}"; |
| | | private WeixinProperties wxConfig; |
| | | private RestTemplate wxRestTemplate; |
| | | private RedisService redisService; |
| | | private RedisCache redisCache; |
| | | |
| | | public WxAppletTools(RestTemplate wxRestTemplate, WeixinProperties wxConfig, RedisService redisService) { |
| | | public WxAppletTools(RestTemplate wxRestTemplate, WeixinProperties wxConfig, RedisCache redisCache) { |
| | | this.wxRestTemplate = wxRestTemplate; |
| | | this.wxConfig = wxConfig; |
| | | this.redisService = redisService; |
| | | this.redisCache = redisCache; |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | public Code2SessionRespBody getOpenIdByCode2session(Code2SessionResqBody resqBody) { |
| | | long start = System.currentTimeMillis(); |
| | | String requestUrl = MessageFormat.format(CODE_2_SESSION_URL, wxConfig.getAppId(), wxConfig.getSecretId(), resqBody.getJsCode()); |
| | | String requestUrl = MessageFormat.format(CODE_2_SESSION_URL, wxConfig.getOfficeAppId(), wxConfig.getOfficeSecretId(), resqBody.getJsCode()); |
| | | long end = System.currentTimeMillis(); |
| | | log.info("code换取sessionKey时间:{}", (end - start)); |
| | | String respBody = wxRestTemplate.getForEntity(requestUrl, String.class).getBody(); |
| | |
| | | * @return |
| | | */ |
| | | public String getAccessToken() { |
| | | String accessToken = redisService.getCacheObject(ACCESSTOKEN_CACHE_KEY); |
| | | String accessToken = redisCache.getCacheObject(ACCESSTOKEN_CACHE_KEY); |
| | | if (StringUtils.hasLength(accessToken)) { |
| | | return accessToken; |
| | | } |
| | |
| | | // 抛出错误 |
| | | throw new WxException(accessTokenRespBody.getErrorCode() + ":" + accessTokenRespBody.getErrorMsg()); |
| | | } |
| | | redisService.setCacheObject(ACCESSTOKEN_CACHE_KEY, accessTokenRespBody.getAccessToken(), 7200L, TimeUnit.SECONDS); |
| | | redisCache.setCacheObject(ACCESSTOKEN_CACHE_KEY, accessTokenRespBody.getAccessToken(), 7200, TimeUnit.SECONDS); |
| | | return accessTokenRespBody.getAccessToken(); |
| | | } |
| | | |
| | | /** |
| | | * @return |
| | | */ |
| | | public String getAccessTokenOffice() { |
| | | String accessToken = redisCache.getCacheObject(ACCESS_TOKEN_OFFICE_CACHE_KEY); |
| | | if (StringUtils.hasLength(accessToken)) { |
| | | return accessToken; |
| | | } |
| | | String requestUrl = MessageFormat.format(ACCESS_TOKEN_URL, wxConfig.getOfficeAppId(), wxConfig.getOfficeSecretId()); |
| | | String respBody = wxRestTemplate.getForEntity(requestUrl, String.class).getBody(); |
| | | AccessTokenRespBody accessTokenRespBody = WxJsonUtils.parseObject(respBody, AccessTokenRespBody.class); |
| | | // 判断有误异常 |
| | | if (StringUtils.hasLength(accessTokenRespBody.getErrorMsg())) { |
| | | // 抛出错误 |
| | | throw new WxException(accessTokenRespBody.getErrorCode() + ":" + accessTokenRespBody.getErrorMsg()); |
| | | } |
| | | redisCache.setCacheObject(ACCESS_TOKEN_OFFICE_CACHE_KEY, accessTokenRespBody.getAccessToken(), 7200, TimeUnit.SECONDS); |
| | | return accessTokenRespBody.getAccessToken(); |
| | | } |
| | | |