发送微信通知 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); }
发送微信通知 java 实现的更多相关文章
- 发送微信通知 java
//实现类@Service public class WeChatServiceImpl implements IWeChatService { @Override public WeChatSend ...
- 微信支付java
直接上代码: 1.支付配置PayCommonUtil import com.legendshop.payment.tenpay.util.MD5Util; import com.legendshop. ...
- 微信支付java版V3验证数据合法性
[TOC] 1. 微信支付java版V3验证数据合法性 概要:使用微信支付接口时,微信会返回或回调给商户XML数据,开发者需要验证微信返回的数据是否合法. 特别提醒:商户系统对于支付结果通知的内容一定 ...
- 微信小程序-统一下单、微信支付(Java后台)
1.首先分享 微信统一下单接口: https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1 微信接口 签名 对比网址: https: ...
- app微信支付-java服务端接口 支付-查询-退款
个人不怎么看得懂微信的文档,看了很多前辈的写法,终于调通了,在这里做一下记录. 首先来定义各种处理类(微信支付不需要特殊jar包,很多处理需要自己封装,当然也可以自己写完打个jar包) 参数要用jdo ...
- APP微信支付Java后台的实现(springmvc)
第一次做微信支付,阅读完开发文档后,下了个官方demo,摸索了好久,期间也出现了好多问题,终于是实现生成预支付订单以及支付成功后接收微信服务器通知,不多说了,直接上代码: 一.工具类 Constant ...
- 全网最全的Windows下Anaconda2 / Anaconda3里Python语言实现定时发送微信消息给好友或群里(图文详解)
不多说,直接上干货! 缘由: (1)最近看到情侣零点送祝福,感觉还是很浪漫的事情,相信有很多人熬夜为了给爱的人送上零点祝福,但是有时等着等着就睡着了或者时间并不是卡的那么准就有点强迫症了,这是也许程序 ...
- C#的Xamarin开发小米盒子应用并以WCF实现微信通知
对于熟悉C#语言的开发人员而言,用Xamarin开发Android应用也是一个不错的选择.小米盒子是Android系统.当然也就能够使用Xamarin来开发.首选来看效果图. watermark/2/ ...
- [企业微信通知系列]Jenkins发布后自动通知
一.前言 最近使用Jenkins进行自动化部署,但是部署后,并没有相应的通知,虽然有邮件发送通知,但是发现邮件会受限于接收方的接收设置,导致不能及时看到相关的发布内容.而由于公司使用的是企业微信,因此 ...
随机推荐
- 10万级etl批量作业自动化调度工具Taskctl之轻量级Web应用版
什么是批量作业: 批量处理是银行业整个信息后台最为重要的技术形态,也是银行核心信息资产数据的分享.传输.演化的重要技术手段.有调查指出,全球70%的数据是经过批量处理得以再次使用,可见批量处理在整个信 ...
- 在VMware下创建windows server 2008虚拟机
1.创建新的虚拟机 打开VMware软件,点击主页内创建新的虚拟机 2.进入新建虚拟机向导 点击典型,点击下一步 3.在下一步中单击稍后安装操作系统 点击下一步 4.选择操作系统类型 客户机操作系统选 ...
- V-指令,细节补充
v-for遍历对象的时候 v-for=" (item) in person" 这里item是person对象里面的value值 而v-for=" (item,key) i ...
- 「刷题笔记」AC自动机
自动AC机 Keywords Research 板子题,同luoguP3808,不过是多测. 然后多测不清空,\(MLE\)两行泪. 板子放一下 #include<bits/stdc++.h&g ...
- 单体->集群->模块化->分布式微服务
开头语: 每篇一段开头语,在技术的道路中寻找文采的乐趣.(如果随笔中都是大白话勿喷,兄弟姐妹们) 单体项目 单体项目适用于小型开发,或自己来进行小项目的测试和使用. 单体项目的缺憾 多人开发项目所出现 ...
- Prometheus 使用之 node exporter
本文使用的 Prometheus 版本为 2.22.0,node exporter 版本为 1.0.1:部署在 Linux 服务器Prometheus 是开源的监控报警系统和时序列数据库 (TSDB) ...
- 树莓CM3开机连接WIFI
两年没玩树莓派了,最近拿了一块CM3,发现启动后不能连接WIFI,网上的文章都是针对3B的,修改/etc/network/interfaces文件和/etc/wpa_supplicant/wpa_su ...
- Synchronized和ReentTrantLock二者区别
写在前面 Java 提供了两种锁机制来控制多个线程对共享资源的互斥访问,第一个是 JVM 实现的 synchronized,而另一个是 JDK 实现的 ReentrantLock.这两种锁以及后面提到 ...
- tensorflow 小记——如何对张量做任意行求和,得到新tensor(一种方法:列表生成式)
希望实现图片上的功能 import tensorflow as tfa = tf.range(10,dtype=float)b = aa = tf.reshape(a,[-1,1])a = tf.ti ...
- 蒲公英 · JELLY技术周刊 Vol.33: 前端基础课堂开课啦~
蒲公英 · JELLY技术周刊 Vol.33 页面文件太大?图片过大了吧:页面加载白屏?很有可能是字体文件还没加载完:页面加载时间过长?多半是主进程被阻塞--该怎么办呢?快来小葵,咳咳,「蒲公英」前端 ...