diff --git a/zc-business/src/main/java/com/zc/business/service/impl/MsmServiceImpl.java b/zc-business/src/main/java/com/zc/business/service/impl/MsmServiceImpl.java index e2c3fe23..fb0d2131 100644 --- a/zc-business/src/main/java/com/zc/business/service/impl/MsmServiceImpl.java +++ b/zc-business/src/main/java/com/zc/business/service/impl/MsmServiceImpl.java @@ -138,10 +138,10 @@ public class MsmServiceImpl implements IMsmService { String expiresIn = jsonObject.getString("expires_in");//token有效秒数 redisCache.setCacheObject(WEIXINTUISONTOKEN, accessToken, Integer.parseInt(expiresIn), TimeUnit.SECONDS);//把token存入到redis中,并设置过期时间(时间来自于微信接口返回) } - JSONArray userListOpenid = msmService.getUserListOpenid(accessToken); + JSONArray userListOpenid = msmService.getUserListOpenid(accessToken).getJSONObject("data").getJSONArray("openid"); CountDownLatch latch = new CountDownLatch(userListOpenid.size()); for (Object openid : userListOpenid) { - String token = accessToken; + String token = redisCache.getCacheObject(WEIXINTUISONTOKEN); executor.execute(() -> { // 封装要发送的json Map map = new HashMap(); @@ -190,7 +190,7 @@ public class MsmServiceImpl implements IMsmService { } //获取微信公众号全部关注人的openid - public static JSONArray getUserListOpenid(String accessToken) { + public static com.alibaba.fastjson.JSONObject getUserListOpenid(String accessToken) { String url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=" + accessToken; com.alibaba.fastjson.JSONObject jsonObj = null; try { @@ -206,11 +206,19 @@ public class MsmServiceImpl implements IMsmService { String userOpenid = new String(jsonBytes, "UTF-8"); jsonObj = com.alibaba.fastjson.JSONObject.parseObject(userOpenid); is.close(); + if(jsonObj.getInteger("errcode")!=null&&"40001".equals(jsonObj.getInteger("errcode").toString())){//token失效 + RedisCache redisCache = SpringUtils.getBean(RedisCache.class); + com.alibaba.fastjson.JSONObject jsonObject =getAccessToken(WEIXINAPPID, WEIXINAPPSECRET); + accessToken = jsonObject.getString("access_token");//获取到了access_token + String expiresIn = jsonObject.getString("expires_in");//token有效秒数 + redisCache.setCacheObject(WEIXINTUISONTOKEN, accessToken, Integer.parseInt(expiresIn), TimeUnit.SECONDS);//把token存入到redis中,并设置过期时间(时间来自于微信接口返回) + jsonObj = getUserListOpenid(accessToken); + } } catch (Exception e) { e.printStackTrace(); } - com.alibaba.fastjson.JSONObject data = jsonObj.getJSONObject("data"); - return data.getJSONArray("openid"); + return jsonObj; +// return data.getJSONArray("openid"); } //整合推送 @Override