| | |
| | | |
| | | import javax.annotation.PostConstruct; |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.io.*; |
| | | import java.net.HttpURLConnection; |
| | | import java.net.URL; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.text.MessageFormat; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.Date; |
| | | import java.util.Map; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | |
| | | public String getAccessToken(String appId) throws Exception { |
| | | String accessToken = ""; |
| | | accessToken = wxXCXTempSend.stringRedisTemplate.boundValueOps("access_token:access_token:" + appId).get(); |
| | | if (appId.equals(APP_ID)) { |
| | | accessToken = validAccessToken(accessToken, appId, APP_SECRET); |
| | | } |
| | | return accessToken; |
| | | } |
| | | |
| | |
| | | String accessTokenUrl = ACCESS_TOKEN_URL + "&appid=" + appId + "&secret=" + appSecret; |
| | | String result = HttpClientUtil.httpGet(accessTokenUrl, null, null); |
| | | Map<String, Object> resultMap = JSON.parseObject(result, Map.class); |
| | | StringRedisTemplate redisTemplate = wxXCXTempSend.stringRedisTemplate; |
| | | if (resultMap.containsKey("access_token")) { |
| | | accessToken = resultMap.get("access_token").toString(); |
| | | redisTemplate.opsForValue().set("access_token:access_token:" + appId, accessToken); |
| | | } |
| | | HttpServletRequest request = ServletUtils.getRequest(); |
| | | String requestURI = request.getRequestURI(); |
| | | //加上时间戳 |
| | | String datetime = new SimpleDateFormat("yyyyMMdd").format(new Date()); |
| | | //这里是 Redis key的前缀,如: sys:tabieId:表名 如果不需要去掉表名也可以 |
| | | String key = MessageFormat.format("{0}:{1}:{2}", "access_token_request_incr",datetime,requestURI); |
| | | //查询 key 是否存在, 不存在返回 1 ,存在的话则自增加1 |
| | | redisTemplate.opsForValue().increment(key, 1); |
| | | |
| | | } catch (IOException ioe) { |
| | | log.error("小程序http请求异常"); |
| | | ioe.printStackTrace(); |
| | | } |
| | | return accessToken; |
| | | } |
| | | |
| | | /** |
| | | * 发布消息时重新设置access_token 防止过期 |
| | | * |
| | | * @param appId |
| | | * @param appSecret |
| | | * @throws Exception |
| | | */ |
| | | public void setAppAccessTokenToCache(String appId, String appSecret) throws Exception { |
| | | String accessToken = "0"; |
| | | try { |
| | | log.info("获取微信token参数:appid=" + appId + ",appSecret=" + appSecret); |
| | | String accessTokenUrl = ACCESS_TOKEN_URL + "&appid=" + appId + "&secret=" + appSecret; |
| | | String result = HttpClientUtil.httpGet(accessTokenUrl, null, null); |
| | | Map<String, Object> resultMap = JSON.parseObject(result, Map.class); |
| | | if (resultMap.containsKey("access_token")) { |
| | | accessToken = resultMap.get("access_token").toString(); |
| | | wxXCXTempSend.stringRedisTemplate.opsForValue().set("access_token:access_token:" + appId, accessToken, EXPIRE_TIME, TimeUnit.HOURS); |
| | | } |
| | | } catch (IOException ioe) { |
| | | log.error("小程序http请求异常"); |
| | | ioe.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | public String getAccessTokenValid(String appId, Integer index) throws Exception { |
| | | if (index % 1000 == 0) { |
| | | return getAccessToken(appId); |
| | | } else { |
| | | return wxXCXTempSend.stringRedisTemplate.boundValueOps("access_token:access_token:" + appId).get(); |
| | | } |
| | | |
| | | } |
| | | |
| | | public String getWsAccessToken() throws Exception { |