首页 技术 正文
技术 2022年11月22日
0 收藏 725 点赞 4,405 浏览 5777 个字

最近在开发activiti流程的时候有个需求:流程到达每个审批节点后,需要向该节点的审批人发送一个消息,提示有审批需要处理。

参考了一下微信的开发者文档和网络上的一些技术博客,现在记录一下。以便后续继续研究微信开发。【微信开发真的很好玩的】

首先,你需要注册一个企业微信账号:https://work.weixin.qq.com/wework_admin/register_wx?from=myhome

然后进入后台管理,创建一个应用

java访问微信接口发送消息

java访问微信接口发送消息

java访问微信接口发送消息

上面标红的信息,都是开发测试需要用到的,需要记录一下。

先贴代码:

WeChatData.java

package com.xfma.wx.test;/**
* 微信消息发送实体类
* @author PC-MXF
*
*/
public class WeChatData {
//发送微信消息的URLString sendMsgUrl="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=";
/**
* 成员账号
*/
private String touser;/**
* 消息类型
*/
private String msgtype;/**
* 企业用用的agentid
*/
private String agentid;/**
* 十几接收map类型数据
*/
private Object text;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 String getAgentid() {
return agentid;
}public void setAgentid(String agentid) {
this.agentid = agentid;
}public Object getText() {
return text;
}public void setText(Object text) {
this.text = text;
}}

  WeChatUrlData.java

package com.xfma.wx.test;/**
* 微信授权请求
* @author PC-MXF
*
*/
public class WeChatUrlData { /**
* 企业Id
*/
private String corpid; /**
* secret管理组的凭证密钥
*/
private String corpsecret; /**
* 获取ToKen的请求
*/
private String Get_Token_Url; /**
* 发送消息的请求
*/
private String SendMessage_Url;public String getCorpid() {
return corpid;
}public void setCorpid(String corpid) {
this.corpid = corpid;
}public String getCorpsecret() {
return corpsecret;
}public void setCorpsecret(String corpsecret) {
this.corpsecret = corpsecret;
}public String getGet_Token_Url() {
return Get_Token_Url;
}public void setGet_Token_Url(String corpid,String corpsecret) {
Get_Token_Url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid="+corpid+"&corpsecret="+corpsecret;
}public String getSendMessage_Url() {
SendMessage_Url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=";
return SendMessage_Url;
}public void setSendMessage_Url(String sendMessage_Url) {
SendMessage_Url = sendMessage_Url;
}}

  WeChatMsgSend.java

package com.xfma.wx.test;import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.LoggerFactory;import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;/**
* 微信发送消息
*
* @author PC-MXF
*
*/
public class WeChatMsgSend {private CloseableHttpClient httpClient;/**
* 用于提交登录数据
*/
private HttpPost httpPost;/**
* 用于获得登陆后页面
*/
private HttpGet httpGet;public static final String CONTENT_TYPE = "Content-Type";SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");private static Gson gson = new Gson();/**
* 微信授权请求,GET类型,获取授权响应,用于其他方法截取token
*
* @param Get_Token_Url
* @return String 授权响应内容
* @throws IOException
*/
protected String toAuth(String Get_Token_Url) throws IOException {
httpClient = HttpClients.createDefault();
httpGet = new HttpGet(Get_Token_Url);
CloseableHttpResponse response = httpClient.execute(httpGet);
String resp = "";try {
HttpEntity entity = response.getEntity();
resp = EntityUtils.toString(entity, "utf-8");
EntityUtils.consume(entity);
} catch (Exception e) {
e.getStackTrace();
} finally {
response.close();
}
LoggerFactory.getLogger(getClass()).info(" resp:{}", resp);
return resp;
}/**
* corpid应用组织编号 corpsecret应用秘钥 获取toAuth(String
* Get_Token_Url)返回结果中键值对中access_token键的值
*
* @param
*/
public String getToken(String corpid, String corpsecret) throws IOException {
WeChatMsgSend sw = new WeChatMsgSend();
WeChatUrlData uData = new WeChatUrlData();
uData.setGet_Token_Url(corpid, corpsecret);
String resp = sw.toAuth(uData.getGet_Token_Url());
System.out.println("resp=====:" + resp);
try {
Map<String, Object> map = gson.fromJson(resp, new TypeToken<Map<String, Object>>() {
}.getType());
return map.get("access_token").toString();
} catch (Exception e) {
e.getStackTrace();
return resp;
}
}/**
* 创建微信发送请求post数据 touser发送消息接收者 ,msgtype消息类型(文本/图片等), application_id应用编号。
* 本方法适用于text型微信消息,contentKey和contentValue只能组一对
*
* @param touser
* @param msgtype
* @param application_id
* @param contentKey
* @param contentValue
* @return
*/
public String createpostdata(String touser, String msgtype, int application_id, String contentKey,
String contentValue) {
WeChatData wcd = new WeChatData();
wcd.setTouser(touser);
wcd.setAgentid(application_id + "");
wcd.setMsgtype(msgtype);
Map<Object, Object> content = new HashMap<Object, Object>();
content.put(contentKey, contentValue);
wcd.setText(content);
return gson.toJson(wcd);
}/**
* @Title 创建微信发送请求post实体,charset消息编码 ,contentType消息体内容类型,
* url微信消息发送请求地址,data为post数据,token鉴权token
* @param charset
* @param contentType
* @param url
* @param data
* @param token
* @return
* @throws IOException
*/
public String post(String charset, String contentType, String url, String data, String token) throws IOException {
CloseableHttpClient httpclient = HttpClients.createDefault();
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();
}
LoggerFactory.getLogger(getClass()).info("call [{}], param:{}, resp:{}", url, data, resp);
return resp;
}
}

  Test.java

package com.xfma.wx.test;public class Test {public static void main(String[] args) {
WeChatMsgSend swx = new WeChatMsgSend();
try {
String token = swx.getToken("ww44a4ede4d3626a","w6tRMGfN5WSFarMmqdpkwBztPRy-HVXRRs76QXvFU");
String postdata = swx.createpostdata("jiangpin", "text", 1000009, "content","这是一条测试信息");
String resp = swx.post("utf-8", WeChatMsgSend.CONTENT_TYPE,(new WeChatUrlData()).getSendMessage_Url(), postdata, token);
System.out.println("获取到的token======>" + token);
System.out.println("请求数据======>" + postdata);
System.out.println("发送微信的响应数据======>" + resp);
}catch (Exception e) {
e.getStackTrace();
}
}
}

  

上一篇: Python锁
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,026
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,517
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,364
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,145
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,779
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,856