1.前置条件
企业微信appId
企业微信secret
企业微信绑定小程序
小程序的appId
2.发送示例
2.1 获取access_token(HTTPS-GET)
请求URL:
https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=corpid&corpsecret=corpsecret
示例入参:
corpid=wx161we55e1fe5e4fr
corpsecret=Rspav9pmjem3zve8fkjqe6yTtCs78Rwdabn6tNdU1g
2.2 发送
完整代码示例(建议异步)
Java
复制代码
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
private static final ThreadLocal synExecuteService = ThreadLocal.withInitial(() -> Executors.newFixedThreadPool(200));
imsServiceDomainConfigStr = imsServiceDomainConfigStr + "?access_token=" + accessToken.getToken();
String result = HUHttpUtil.sendJsonPostRequest(imsServiceDomainConfigStr, jsonParam, 0);
logger.info("开始发送微信企业号消息,结果:" + result);
} else {
logger.info("该医生" + doctorNo + "在企业号通讯录中不存在,终止发送企业号消息");
}
});
}
/**
* 发送GET请求
*
* @param requestUrl
* @param requestMethod
* @param outputStr
* @return
*/
public static JSONObject httpRequest(String requestUrl, String requestMethod, String outputStr) {
logger.info("[WeixinUtil.httpRequest]:请求入参:" + requestUrl);
String respStr = HUHttpUtil.sendGet(requestUrl, null);
logger.info("[WeixinUtil.httpRequest]:请求响应:" + respStr);
try {
return JSON.parseObject(respStr);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private List getWXQYUserList(JSONObject hResponse) {
List wXQYUserList = (List) hResponse.get("userlist");
List nmList = null;
WXQYUser wxQYUser = null;
JSONObject mm = null;
List wxQYUserList = new ArrayList();
for (JSONObject jsObject : wXQYUserList) {
if (jsObject.containsKey("userid")) {
if (jsObject.containsKey("extattr")) {
mm = jsObject.getJSONObject("extattr");
if (null != mm.get("attrs")) {
nmList = (List) mm.get("attrs");
for (JSONObject nmObject : nmList) {
if (nmObject.containsKey("name") && "hisid".equals(nmObject.getString("name"))
&& null != nmObject.getString("value") && !"".equals(nmObject.getString("value"))) {
wxQYUser = new WXQYUser();
wxQYUser.setUserid(jsObject.getString("userid"));
wxQYUser.setName(jsObject.getString("name"));
wxQYUser.setMobile(jsObject.getString("mobile"));
wxQYUser.setHisid(nmObject.getString("value"));
wxQYUserList.add(wxQYUser);
}
}
}
}
}
}
return wxQYUserList;
}
// 企业微信存储12345,数据库存储:医院ID+12345,随意这里做截取再去匹配
private String getDoctorNo(String doctorNo) {
if (doctorNo.startsWith("10000")) {
return doctorNo.substring(5, doctorNo.length());
}
if (doctorNo.startsWith("10001")) {
return doctorNo.substring(5, doctorNo.length());
}
return doctorNo;
}
Java
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class WXQYUser {
private String userid;
private String name;
private String mobile;
private String hisid;
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getHisid() {
return hisid;
}
public void setHisid(String hisid) {
this.hisid = hisid;
}
}