Commit 4f6b2f35a35da4a974eec0a389cf5dbaf21396f8
1 parent
be64f45181
Exists in
master
解决access_token替换时间差保证access_token长期有效
Showing 1 changed file with 9 additions and 6 deletions
talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/WeiXinUtil.java
View file @
4f6b2f3
| ... | ... | @@ -82,7 +82,7 @@ |
| 82 | 82 | * @param template_id 公众号消息模板id |
| 83 | 83 | * @param dataMap 推送内容消息主题显示相关map |
| 84 | 84 | */ |
| 85 | - public static Integer SendWeChatMsg(String openid, String template_id, Map<String,Object> dataMap, Map<String,Object> mapInfo) { | |
| 85 | + public static Integer SendWeChatMsg(String openid, String template_id, Map<String,Object> dataMap, Map<String,Object> mapInfo)throws Exception { | |
| 86 | 86 | //整体参数map |
| 87 | 87 | Map<String, Object> paramMap = new HashMap<String, Object>(); |
| 88 | 88 | paramMap.put("touser", openid); |
| 89 | 89 | |
| 90 | 90 | |
| 91 | 91 | |
| 92 | 92 | |
| 93 | 93 | |
| ... | ... | @@ -101,19 +101,22 @@ |
| 101 | 101 | paramMap.put("miniprogram", miniprogram); |
| 102 | 102 | paramMap.put("data", dataMap); |
| 103 | 103 | // 接口地址 |
| 104 | -// boolean TokenValid=true; | |
| 105 | - while (true) { | |
| 104 | + //处理access_token失效报40001 | |
| 105 | + Integer code=null; | |
| 106 | + for(int i=0;i<3;i++) { | |
| 106 | 107 | String sendMsgApi = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + AccessTokenServlet.accessToken; |
| 107 | 108 | String result = repeatDoGetPost(sendMsgApi, "POST", paramMap); |
| 108 | 109 | if (StringUtil.isEmpty(result)) { |
| 109 | - return null; | |
| 110 | + break; | |
| 110 | 111 | } |
| 111 | 112 | Map<String, Object> resultMap = JSON.parseObject(result, HashMap.class); |
| 112 | - Integer code=Integer.parseInt(resultMap.get("errcode").toString()); | |
| 113 | + code=Integer.parseInt(resultMap.get("errcode").toString()); | |
| 113 | 114 | if(!code.equals(40001)){ |
| 114 | - return code; | |
| 115 | + break; | |
| 115 | 116 | } |
| 117 | + Thread.sleep(1000 * 1);//解决access_token替换时间差保证access_token长期有效 | |
| 116 | 118 | } |
| 119 | + return code; | |
| 117 | 120 | } |
| 118 | 121 | |
| 119 | 122 | /** |