java 通知_发送微信通知 java 实现

/实现类

@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);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值