| | |
| | | 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; |
| | |
| | | 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; |
| | | } |
| | | |
| | | /** |
| | |
| | | * @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 = redisService.getCacheObject(ACCESS_TOKEN_OFFICE_CACHE_KEY); |
| | | String accessToken = redisCache.getCacheObject(ACCESS_TOKEN_OFFICE_CACHE_KEY); |
| | | if (StringUtils.hasLength(accessToken)) { |
| | | return accessToken; |
| | | } |
| | |
| | | // 抛出错误 |
| | | throw new WxException(accessTokenRespBody.getErrorCode() + ":" + accessTokenRespBody.getErrorMsg()); |
| | | } |
| | | redisService.setCacheObject(ACCESS_TOKEN_OFFICE_CACHE_KEY, accessTokenRespBody.getAccessToken(), 7200L, TimeUnit.SECONDS); |
| | | redisCache.setCacheObject(ACCESS_TOKEN_OFFICE_CACHE_KEY, accessTokenRespBody.getAccessToken(), 7200, TimeUnit.SECONDS); |
| | | return accessTokenRespBody.getAccessToken(); |
| | | } |
| | | |