/实现类
@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); }

  

发送微信通知 java 实现的更多相关文章

  1. 发送微信通知 java

    //实现类@Service public class WeChatServiceImpl implements IWeChatService { @Override public WeChatSend ...

  2. 微信支付java

    直接上代码: 1.支付配置PayCommonUtil import com.legendshop.payment.tenpay.util.MD5Util; import com.legendshop. ...

  3. 微信支付java版V3验证数据合法性

    [TOC] 1. 微信支付java版V3验证数据合法性 概要:使用微信支付接口时,微信会返回或回调给商户XML数据,开发者需要验证微信返回的数据是否合法. 特别提醒:商户系统对于支付结果通知的内容一定 ...

  4. 微信小程序-统一下单、微信支付(Java后台)

    1.首先分享 微信统一下单接口: https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1   微信接口 签名 对比网址: https: ...

  5. app微信支付-java服务端接口 支付-查询-退款

    个人不怎么看得懂微信的文档,看了很多前辈的写法,终于调通了,在这里做一下记录. 首先来定义各种处理类(微信支付不需要特殊jar包,很多处理需要自己封装,当然也可以自己写完打个jar包) 参数要用jdo ...

  6. APP微信支付Java后台的实现(springmvc)

    第一次做微信支付,阅读完开发文档后,下了个官方demo,摸索了好久,期间也出现了好多问题,终于是实现生成预支付订单以及支付成功后接收微信服务器通知,不多说了,直接上代码: 一.工具类 Constant ...

  7. 全网最全的Windows下Anaconda2 / Anaconda3里Python语言实现定时发送微信消息给好友或群里(图文详解)

    不多说,直接上干货! 缘由: (1)最近看到情侣零点送祝福,感觉还是很浪漫的事情,相信有很多人熬夜为了给爱的人送上零点祝福,但是有时等着等着就睡着了或者时间并不是卡的那么准就有点强迫症了,这是也许程序 ...

  8. C#的Xamarin开发小米盒子应用并以WCF实现微信通知

    对于熟悉C#语言的开发人员而言,用Xamarin开发Android应用也是一个不错的选择.小米盒子是Android系统.当然也就能够使用Xamarin来开发.首选来看效果图. watermark/2/ ...

  9. [企业微信通知系列]Jenkins发布后自动通知

    一.前言 最近使用Jenkins进行自动化部署,但是部署后,并没有相应的通知,虽然有邮件发送通知,但是发现邮件会受限于接收方的接收设置,导致不能及时看到相关的发布内容.而由于公司使用的是企业微信,因此 ...

随机推荐

  1. Android中Application的意义及使用方法

    首先,在一个Android程序中,有且只有一个Application对象,在程序启动的时候,首先执行Application的onCreate方法,这是一个Android应用的入口,在开发中,我们常常自 ...

  2. CoProcessFunction实战三部曲之三:定时器和侧输出

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  3. 转:Python考核试题及答案

    Python测试(总分:120) 选择题(每题2分,共20分) 1.下列哪个语句在Python中是非法的? (B) A.x = y = z = 1 B.x = (y = z + 1) C.x, y = ...

  4. PyQt(Python+Qt)学习随笔:树型部件QTreeWidget中的项编辑方法editTriggers、editItem和openPersistentEditor作用及对比分析

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 在树型部件QTreeWidget中,有三种方法触发进行项数据的编辑:editTriggers触发编辑 ...

  5. PyQt学习随笔:Model/View中视图数据项编辑变动实时获取变动数据的方法

    对于Model/View中视图的数据编辑后怎么能实时获取编辑的数据变动位置和变动情况查阅了一些资料,终于基本弄明白必须重写Model的setData方法才能截获.setData方法是视图中各种角色数据 ...

  6. 百度前端技术学院-基础-day1

    2020.9.14 今天我开始在百度前端技术学院学习基础课程. 先立一个Flag,希望我能在30天之内学完前四十天的课程,后续课程再一天一节. 第一天的内容主要是提供了很多基础学习的网页,比如W3sc ...

  7. AtCoder Grand Contest 017 (VP)

    contest link Official Editorial 比赛体验--之前做题的时候感觉 AtCoder 挺快的,现在打了VP之后发现还是会挂的--而且不是加载缓慢或者载不出来,直接给你一个无法 ...

  8. 九、git学习之——git基本命令全总结

    初始化一个Git仓库,使用git init命令. 添加文件到Git仓库,分两步: git add <file>,注意,可反复多次使用,添加多个文件: 使用命令git commit,完成. ...

  9. DBeaver连接MySQ报错

    遇错情况:第一次使用DBaver连接MySQL遇到以下问题: 报错信息:Public Key Retrieval is not allowed 截图如下: 解决方案步骤: 一.已有连接的情况:F4或者 ...

  10. STL—— 容器(vector)数据插入insert()方法 的返回值

    vector 容器下的 insert() 方法拥有返回值,由于insert() 方法拥有4种重载函数,他的返回值不尽相同. 第一种,插入单个元素后的返回值: 1 #include <iostre ...