1:获取access_token
WxUtil.getAccessToken()
2:发送消息
String resp=WxUtil.sendTextMesg("@all", "友情提示:没点餐的快点餐啦!(今天不用上班就忽略吧)", access_token);
3:结果处理
JSONObject respJson = JSONObject.parseObject(resp); if (respJson.containsKey("errcode") && respJson.getIntValue("errcode") == 40014) { // // 表示access_token已过期,重新获取 log.info("access_token过期"); WxUtil.setAccessToken(null); }
//工具类
@Slf4j public class WxUtil { private static String accessToken = null; // 企业号的全局唯一票据 private static Date createTime = null; // 获取accessToken的时间,每100分钟重新获取一次 private static final String CORPID = ""; // 企业微信企业ID private static final int AGENTID = ; ; // 企业微信应用ID private static final String SECRET = ""; // 企业微信应用认证密钥 private static final String getAccessTokenUrl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"; // 获取accessToken地址 private static final String sendMsgUrl = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="; // 发送消息接口 public static String getAccessToken() { String resp; if (accessToken == null) { // 获取accessToken try { resp = HttpUtil.toGet(getAccessTokenUrl + "?corpid=" + CORPID + "&corpsecret=" + SECRET); JSONObject json = JSONObject.parseObject(resp); if (json.containsKey("access_token") && json.getIntValue("errcode") == 0) { accessToken = json.getString("access_token"); createTime = new Date(); } else { log.info( "获取accessToken失败;errcode=" + json.getIntValue("errcode") + ",errmsg=" + json.getString("errmsg")); } } catch (Exception e) { // TODO Auto-generated catch block log.error("获取accessToken异常", e); } } else { // 判断是否获取时间是否超过100分钟,是的话重新获取accessToken Date now = new Date(); long second = DateUtils.diffDateByMilliSeconds(now, createTime); long minute = second / 1000 / 60; if (minute >= 100) { Map<String, Object> map = new HashMap<>(); map.put("corpid", CORPID); map.put("corpsecret", SECRET); try { resp = HttpUtil.toGet(getAccessTokenUrl + "?corpid=" + CORPID + "&corpsecret=" + SECRET); JSONObject json = JSONObject.parseObject(resp); if (json.containsKey("access_token") && json.getIntValue("errcode") == 0) { accessToken = json.getString("access_token"); createTime = new Date(); } else { log.info( "获取accessToken失败;errcode=" + json.getIntValue("errcode") + ",errmsg=" + json.getString("errmsg")); } } catch (Exception e) { // TODO Auto-generated catch block log.error("获取accessToken异常", e); } } else { log.info("accessToken仍在有效期,直接返回:" + accessToken); } } return accessToken; } public static String sendTextMesg(String toUser, String content, String token) { String postData = createPostData(toUser, "text", AGENTID, "content", content); String response = ""; try { response = HttpUtil.postWxMsg("utf-8", "Content-Type", sendMsgUrl, postData, token); } catch (IOException e) { log.error("发送文本信息异常", e); } log.info("获取到的token======>" + token); log.info("请求数据======>" + postData); log.info("发送微信的响应数据======>" + response); return response; } public static String createPostData( String touser, String msgtype, int agent_id, String contentKey, String contentValue) { weChatJson wcd = new weChatJson(); wcd.setTouser(touser); wcd.setAgentid(agent_id); wcd.setMsgtype(msgtype); Map<Object, Object> map = new HashMap<Object, Object>(); map.put(contentKey, contentValue); wcd.setText(map); return JSONObject.toJSONString(wcd); } public static String setAccessToken(String param) { accessToken = param; return accessToken; }
}
//实体类
public class weChatJson { String touser; String msgtype; int agentid; Object text; // 实际接收Map类型数据 public String getTouser() { return touser; } public void setTouser(String touser) { this.touser = touser; } public String getMsgtype() { return msgtype; } public void setMsgtype(String msgtype) { this.msgtype = msgtype; } public int getAgentid() { return agentid; } public void setAgentid(int agentid) { this.agentid = agentid; } public Object getText() { return text; } public void setText(Object text) { this.text = text; } }
//POST方法
public static String postWxMsg( String charset, String contentType, String url, String data, String token) throws IOException { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url + token); httpPost.setHeader("Content-Type", contentType); httpPost.setEntity(new StringEntity(data, charset)); CloseableHttpResponse response = httpclient.execute(httpPost); String resp; try { HttpEntity entity = response.getEntity(); resp = EntityUtils.toString(entity, charset); EntityUtils.consume(entity); } finally { response.close(); } log.info("call [{}], param:{}, resp:{}", url, data, resp); return resp; }