/实现类 @Service public class WeChatServiceImpl implements IWeChatService { @Override public WeChatSendMsgResult sendMsg(String paramJson) { try { String url = MessageFormat.format(WeChatConstant.SEND_MESSAGE, WeChatUtil.getToken()); String result = HttpUtil.doJsonPost(url, paramJson, null); return JSONObject.parseObject(result, WeChatSendMsgResult.class); } catch (Exception e) { return new WeChatSendMsgResult(-2020, "消息通知发送到微信异常"); } } @Override public WeChatSendMsgResult sendTextMsg(String touser, String toparty, String totag, String content) { try { WeChatText text = new WeChatText(); text.setContent(content); WeChatTextMsg textMsg = new WeChatTextMsg(); textMsg.setText(text); textMsg.setMsgtype("text"); textMsg.setAgentid(WeChatUtil.agentId); textMsg.setTouser(touser); textMsg.setToparty(toparty); textMsg.setTotag(totag); return sendMsg(JSONObject.toJSONString(textMsg)); } catch (Exception e) { return new WeChatSendMsgResult(-2021, "组装微信消息通知异常"); } } @Override public WeChatSendMsgResult sendImageMsg(String touser, String toparty, String totag, String mediaId) { try { WeChatImage image = new WeChatImage(); image.setMedia_id(mediaId); WeChatImageMsg imageMsg = new WeChatImageMsg(); imageMsg.setImage(image); imageMsg.setMsgtype("image"); imageMsg.setAgentid(WeChatUtil.agentId); imageMsg.setTouser(touser); imageMsg.setToparty(toparty); imageMsg.setTotag(totag); return sendMsg(JSONObject.toJSONString(imageMsg)); } catch (Exception e) { return new WeChatSendMsgResult(-2021, "组装微信消息通知异常"); } } @Override public WeChatSendMsgResult sendVoiceMsg(String touser, String toparty, String totag, String mediaId) { try { WeChatVoice voice = new WeChatVoice(); voice.setMedia_id(mediaId); WeChatVoiceMsg voiceMsg = new WeChatVoiceMsg(); voiceMsg.setVoice(voice); voiceMsg.setMsgtype("voice"); voiceMsg.setAgentid(WeChatUtil.agentId); voiceMsg.setTouser(touser); voiceMsg.setToparty(toparty); voiceMsg.setTotag(totag); return sendMsg(JSONObject.toJSONString(voiceMsg)); } catch (Exception e) { return new WeChatSendMsgResult(-2021, "组装微信消息通知异常"); } } @Override public WeChatSendMsgResult sendVideoMsg(String touser, String toparty, String totag, String description, String mediaId, String title) { try { WeChatVideo video = new WeChatVideo(); video.setDescription(description); video.setMedia_id(mediaId); video.setTitle(title); WeChatVideoMsg videoMsg = new WeChatVideoMsg(); videoMsg.setVideo(video); videoMsg.setMsgtype("video"); videoMsg.setAgentid(WeChatUtil.agentId); videoMsg.setTouser(touser); videoMsg.setToparty(toparty); videoMsg.setTotag(totag); return sendMsg(JSONObject.toJSONString(videoMsg)); } catch (Exception e) { return new WeChatSendMsgResult(-2021, "组装微信消息通知异常"); } } @Override public WeChatSendMsgResult sendFileMsg(String touser, String toparty, String totag, String mediaId) { try { WeChatFile file = new WeChatFile(); file.setMedia_id(mediaId); WeChatFileMsg fileMsg = new WeChatFileMsg(); fileMsg.setFile(file); fileMsg.setMsgtype("file"); fileMsg.setAgentid(WeChatUtil.agentId); fileMsg.setTouser(touser); fileMsg.setToparty(toparty); fileMsg.setTotag(totag); return sendMsg(JSONObject.toJSONString(fileMsg)); } catch (Exception e) { return new WeChatSendMsgResult(-2021, "组装微信消息通知异常"); } } @Override public WeChatSendMsgResult sendTextCardMsg(String touser, String toparty, String totag, String btnTxt, String description, String title, String url) { try { WeChatTextCard textCard = new WeChatTextCard(); textCard.setBtntxt(btnTxt); textCard.setDescription(description); textCard.setTitle(title); textCard.setUrl(url); WeChatTextCardMsg textCartMsg = new WeChatTextCardMsg(); textCartMsg.setTextcard(textCard); textCartMsg.setMsgtype("textcard"); textCartMsg.setAgentid(WeChatUtil.agentId); textCartMsg.setTouser(touser); textCartMsg.setToparty(toparty); textCartMsg.setTotag(totag); return sendMsg(JSONObject.toJSONString(textCartMsg)); } catch (Exception e) { return new WeChatSendMsgResult(-2021, "组装微信消息通知异常"); } } @Override public WeChatSendMsgResult sendNewsMsg(String touser, String toparty, String totag, WeChatNews news) { try { WeChatNewsMsg newsMsg = new WeChatNewsMsg(); newsMsg.setNews(news); newsMsg.setMsgtype("news"); newsMsg.setAgentid(WeChatUtil.agentId); newsMsg.setTouser(touser); newsMsg.setToparty(toparty); newsMsg.setTotag(totag); return sendMsg(JSONObject.toJSONString(newsMsg)); } catch (Exception e) { return new WeChatSendMsgResult(-2021, "组装微信消息通知异常"); } } @Override public WeChatSendMsgResult sendMpNewsMsg(String touser, String toparty, String totag, WeChatMpNews mpnews) { try { WeChatMpNewsMsg mpNewsMsg = new WeChatMpNewsMsg(); mpNewsMsg.setMpnews(mpnews); mpNewsMsg.setMsgtype("news"); mpNewsMsg.setAgentid(WeChatUtil.agentId); mpNewsMsg.setTouser(touser); mpNewsMsg.setToparty(toparty); mpNewsMsg.setTotag(totag); return sendMsg(JSONObject.toJSONString(mpNewsMsg)); } catch (Exception e) { return new WeChatSendMsgResult(-2021, "组装微信消息通知异常"); } } } //工具类====== @Component public class WeChatUtil { public static String corpid ="xxxxxxxx"; public static String agentId ="xxxxxxxx"; public static String secret ="xxxxxxxxxxx"; public static String accessToken =""; public static long createTime = 0; public static String getToken(){ if("".equals(accessToken)){ getToken(corpid,secret); } else{ if(DateUtil.now().getTime() - createTime > 7000000l ){ getToken(corpid,secret); } } return accessToken; } public static void getToken(String corpid, String corpsecret ){ String url = MessageFormat.format(WeChatConstant.GET_TOKEN,corpid,corpsecret); String result = HttpUtil.doGet(url,null); WeChatAccessTokenResult res = JSONObject.parseObject(result, WeChatAccessTokenResult.class); if("0".equals(String.valueOf(res.getErrcode()))){ accessToken = res.getAccess_token(); createTime = DateUtil.now().getTime(); } } } //常量===== public interface WeChatConstant { String GET_TOKEN="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={0}&corpsecret={1}"; String SEND_MESSAGE="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={0}"; } //实体类 public class WeChatAccessTokenResult { private Integer errcode; private String errmsg; private String access_token; private int expires_in; }
//调用接口
package org.springblade.resource.service; import org.springblade.resource.entity.WeChatSendMsgResult; import org.springblade.resource.entity.wechat.WeChatMpNews; import org.springblade.resource.entity.wechat.WeChatNews; public interface IWeChatService { WeChatSendMsgResult sendMsg(String paramJson); WeChatSendMsgResult sendTextMsg(String touser, String toparty, String totag, String content); WeChatSendMsgResult sendImageMsg(String touser, String toparty, String totag, String mediaId); WeChatSendMsgResult sendVoiceMsg(String touser, String toparty, String totag, String mediaId); WeChatSendMsgResult sendVideoMsg(String touser, String toparty, String totag, String description, String mediaId, String title); WeChatSendMsgResult sendFileMsg(String touser, String toparty, String totag, String mediaId); WeChatSendMsgResult sendTextCardMsg(String touser, String toparty, String totag, String btnTxt, String description, String title, String url); WeChatSendMsgResult sendNewsMsg(String touser, String toparty, String totag, WeChatNews news); WeChatSendMsgResult sendMpNewsMsg(String touser, String toparty, String totag, WeChatMpNews mpnews); }