1 Maven依赖
<!-- 阿里JSON解析器 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.76</version>
</dependency>
<!--hutool工具包-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.5.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
2 Spring Boot配置
wechat:
#企业Id
corpid: ww2d09828b5265f310
#应用私钥
corpsecret: gNHceiU7ePJjWdhJi_g-48fZ3SbPo8qTpUZMQs38su4
#应用ID
agentId: 1000002
#获取token地址
accessTokenUrl: https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=CORPID&corpsecret=CORPSECRET
#发送消息地址
sendMessageUrl: https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=
#获取用户id地址
getUseridUrl: https://qyapi.weixin.qq.com/cgi-bin/user/getuserid?access_token=
#获取访问用户身份
getUserInfoUrl: https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=ACCESS_TOKEN&code=CODE
#获取企业的jsapi_ticket
getJsapiTicketUrl: https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=ACCESS_TOKEN
#获取部门列表地址(企业微信api地址)
getDepartmentListUrl: https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=ACCESS_TOKEN
#获取部门成员详情地址(企业微信api地址)
getUserListUrl: https://qyapi.weixin.qq.com/cgi-bin/user/list
3 WeChatMessageService
企业微信消息推送实现类。
package com.message;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.*;
import com.alibaba.fastjson.*;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.net.ssl.HttpsURLConnection;
import java.io.*;
import java.net.URL;
import java.util.*;
/**
* 企业微信消息推送实现类
*/
@Data
@Component
public class WeChatMessageService {
/**
* 企业Id
*/
@Value("${wechat.corpid}")
private String corpid;
/**
* 应用私钥
*/
@Value("${wechat.corpsecret}")
private String corpsecret;
/**
* 获取访问权限码(access_token)URL GET请求
*/
@Value("${wechat.accessTokenUrl}")
private String accessTokenUrl;
/**
* 发送消息URL POST请求
*/
@Value("${wechat.sendMessageUrl}")
private String sendMessageUrl;
/**
* 获取企业微信用户userid POST请求
*/
@Value("${wechat.getUseridUrl}")
private String getUseridUrl;
/**
* token
*/
private String token;
/**
* 获取部门列表地址 get请求
*/
@Value("${wechat.getDepartmentListUrl}")
private String getDepartmentListUrl;
/**
* 获取部门成员详情地址 get请求
*/
@Value("${wechat.getUserListUrl}")
private String getUserListUrl;
//应用ID
@Value("${wechat.agentId}")
private String agentId;
public String getAccessToken() {
//获取token
String url = accessTokenUrl.replaceAll("CORPID", corpid).replaceAll("CORPSECRET", corpsecret);
String result = HttpUtil.get(url);
JSONObject jsonObject = JSONObject.parseObject(result);
return jsonObject.getString("access_token");
}
/**
* 根据电话号码获取userId
*
* @param token
* @param employeePhone
* @return
*/
public String getUserId(String token, String employeePhone) {
StringBuffer sb = new StringBuffer();
sb.append("{");
sb.append("\"mobile\":" + "\"" + employeePhone + "\"");
sb.append("}");
String json = sb.toString();
String result = "";
String url = getUseridUrl + token;
try {
HttpsURLConnection http = getPostHttp(url, "");
OutputStream os = http.getOutputStream();
os.write(json.getBytes("UTF-8"));
InputStream is = http.getInputStream();
int size = is.available();
byte[] jsonBytes = new byte[size];
is.read(jsonBytes);
result = new String(jsonBytes, "UTF-8");
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
JSONObject jsonObject = JSONObject.parseObject(result);
return jsonObject.getString("userid");
}
public static HttpsURLConnection getPostHttp(String action, String token) throws Exception {
URL url = null;
HttpsURLConnection http = null;
try {
url = new URL(action);
http = (HttpsURLConnection) url.openConnection();
http.setRequestMethod("POST");
http.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
if (StrUtil.isNotBlank(token)) {
http.setRequestProperty("token", token);
}
http.setDoOutput(true);
http.setDoInput(true);
//连接超时30秒
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");
//读取超时30秒
System.setProperty("sun.net.client.defaultReadTimeout", "30000");
http.connect();
} catch (Exception e) {
e.printStackTrace();
}
return http;
}
/**
* 发送微信消息
*
* @param token
* @param json
* @return
*/
public String sendMessage(String token, String json) {
//请求链接
String action = sendMessageUrl + token;
try {
return HttpRequest.post(action)
.body(json)
.execute().body();
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
/**
* 获取微信所有用户
*
* @return
*/
public List<JSONObject> getWechatAllUser() {
// 获取token
String token = getAccessToken();
String template = getUserListUrl + "?access_token={}&department_id=1&fetch_child=1";
String str = StrUtil.format(template, token);
String userResult = HttpUtil.get(str);
JSONObject jsonUser = JSON.parseObject(userResult);
// 返回结果
List<JSONObject> resultJsonList = new ArrayList<>();
if ("0".equals(jsonUser.getString("errcode"))) {
String userListStr = jsonUser.getString("userlist");
JSONArray userList = JSON.parseArray(userListStr);
resultJsonList = JSONObject.parseArray(userList.toJSONString(userList.size()), JSONObject.class);
return resultJsonList;
} else {
return resultJsonList;
}
}
}
4 调试代码
/**
* 发送文本消息
*
* @return
*/
@GetMapping("/sendText")
public String sendText() {
//初始化token
String token = weChatMessageService.getAccessToken();
String phone = "15573967198";
//得到userId
String userId = weChatMessageService.getUserId(token, phone);
//构造消息体
StringBuffer sb = new StringBuffer();
String content = userId + ",你好";
sb.append("{");
sb.append("\"touser\":" + "\"" + userId + "\",");
sb.append("\"msgtype\":" + "\"" + "text" + "\",");
sb.append("\"agentid\":" + "\"" + weChatMessageService.getAgentId() + "\",");
sb.append("\"text\":" + "{");
sb.append("\"content\":" + "\"" + content + "\"},");
sb.append("\"safe\":\"0\"");
sb.append("}");
//发送消息
return weChatMessageService.sendMessage(token, sb.toString());
}
5 调试结果
注:
企业微信进行消息推送的配置请查看以下博客。
企业微信 消息推送使用(新建应用、发送消息)https://blog.csdn.net/qq_38974638/article/details/113246970