Skip to content

Commit cf78cd5

Browse files
forfunsbinarywang
authored andcommitted
#698 企业微信增加OA数据接口
1 parent 2dfcd6a commit cf78cd5

File tree

12 files changed

+686
-6
lines changed

12 files changed

+686
-6
lines changed

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpChatService.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public interface WxCpChatService {
2828
*/
2929
String chatCreate(String name, String owner, List<String> users, String chatId) throws WxErrorException;
3030

31+
/**
32+
* chatCreate 同名方法
33+
*/
34+
String create(String name, String owner, List<String> users, String chatId) throws WxErrorException;
35+
3136
/**
3237
* 修改群聊会话.
3338
*
@@ -40,6 +45,11 @@ public interface WxCpChatService {
4045
*/
4146
void chatUpdate(String chatId, String name, String owner, List<String> usersToAdd, List<String> usersToDelete) throws WxErrorException;
4247

48+
/**
49+
* chatUpdate 同名方法
50+
*/
51+
void update(String chatId, String name, String owner, List<String> usersToAdd, List<String> usersToDelete) throws WxErrorException;
52+
4353
/**
4454
* 获取群聊会话.
4555
*
@@ -49,6 +59,11 @@ public interface WxCpChatService {
4959
*/
5060
WxCpChat chatGet(String chatId) throws WxErrorException;
5161

62+
/**
63+
* chatGet 同名方法
64+
*/
65+
WxCpChat get(String chatId) throws WxErrorException;
66+
5267
/**
5368
* 应用支持推送文本、图片、视频、文件、图文等类型.
5469
* 请求方式: POST(HTTPS)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package me.chanjar.weixin.cp.api;
2+
3+
import me.chanjar.weixin.common.error.WxErrorException;
4+
import me.chanjar.weixin.cp.bean.WxCpApprovalDataResult;
5+
import me.chanjar.weixin.cp.bean.WxCpCheckinData;
6+
import me.chanjar.weixin.cp.bean.WxCpCheckinOption;
7+
import me.chanjar.weixin.cp.bean.WxCpDialRecord;
8+
9+
import java.util.Date;
10+
import java.util.List;
11+
12+
/**
13+
* @author Element
14+
* @Package me.chanjar.weixin.cp.api
15+
* @date 2019-04-06 10:52
16+
* @Description: <pre>
17+
* 企业微信OA相关接口
18+
*
19+
* </pre>
20+
*/
21+
public interface WxCpOAService {
22+
23+
/**
24+
* <pre>
25+
* 获取打卡数据
26+
* API doc : https://work.weixin.qq.com/api/doc#90000/90135/90262
27+
* </pre>
28+
*
29+
* @param openCheckinDataType 打卡类型。1:上下班打卡;2:外出打卡;3:全部打卡
30+
* @param starttime 获取打卡记录的开始时间
31+
* @param endtime 获取打卡记录的结束时间
32+
* @param userIdList 需要获取打卡记录的用户列表
33+
*/
34+
List<WxCpCheckinData> getCheckinData(Integer openCheckinDataType, Date starttime, Date endtime, List<String> userIdList) throws WxErrorException;
35+
36+
/**
37+
* <pre>
38+
* 获取打卡规则
39+
* API doc : https://work.weixin.qq.com/api/doc#90000/90135/90263
40+
* </pre>
41+
*
42+
* @param datetime 需要获取规则的当天日期
43+
* @param userIdList 需要获取打卡规则的用户列表
44+
* @return
45+
* @throws WxErrorException
46+
*/
47+
List<WxCpCheckinOption> getCheckinOption(Date datetime, List<String> userIdList) throws WxErrorException;
48+
49+
/**
50+
* <pre>
51+
* 获取审批数据
52+
* 通过本接口来获取公司一段时间内的审批记录。一次拉取调用最多拉取10000个审批记录,可以通过多次拉取的方式来满足需求,但调用频率不可超过600次/分。
53+
* API doc : https://work.weixin.qq.com/api/doc#90000/90135/91530
54+
* </pre>
55+
*
56+
* @param starttime 获取审批记录的开始时间
57+
* @param endtime 获取审批记录的结束时间
58+
* @param nextSpnum 第一个拉取的审批单号,不填从该时间段的第一个审批单拉取
59+
* @return
60+
* @throws WxErrorException
61+
*/
62+
WxCpApprovalDataResult getApprovalData(Date starttime, Date endtime, Long nextSpnum) throws WxErrorException;
63+
64+
List<WxCpDialRecord> getDialRecord(Date starttime, Date endtime, Integer offset, Integer limit) throws WxErrorException;
65+
66+
}

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,8 @@ public interface WxCpService {
303303

304304
WxCpAgentService getAgentService();
305305

306+
WxCpOAService getOAService();
307+
306308
/**
307309
* http请求对象
308310
*/

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/BaseWxCpServiceImpl.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public abstract class BaseWxCpServiceImpl<H, P> implements WxCpService, RequestH
4545
private WxCpOAuth2Service oauth2Service = new WxCpOAuth2ServiceImpl(this);
4646
private WxCpTagService tagService = new WxCpTagServiceImpl(this);
4747
private WxCpAgentService agentService = new WxCpAgentServiceImpl(this);
48+
private WxCpOAService oaService = new WxCpOAServiceImpl(this);
4849

4950
/**
5051
* 全局的是否正在刷新access token的锁
@@ -386,6 +387,11 @@ public WxCpChatService getChatService() {
386387
return chatService;
387388
}
388389

390+
@Override
391+
public WxCpOAService getOAService() {
392+
return oaService;
393+
}
394+
389395
@Override
390396
public RequestHttp<?, ?> getRequestHttp() {
391397
return this;

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpChatServiceImpl.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
package me.chanjar.weixin.cp.api.impl;
22

3-
import java.util.HashMap;
4-
import java.util.List;
5-
import java.util.Map;
6-
7-
import org.apache.commons.lang3.StringUtils;
8-
93
import com.google.gson.JsonParser;
104
import me.chanjar.weixin.common.error.WxErrorException;
115
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
@@ -14,6 +8,11 @@
148
import me.chanjar.weixin.cp.bean.WxCpAppChatMessage;
159
import me.chanjar.weixin.cp.bean.WxCpChat;
1610
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
11+
import org.apache.commons.lang3.StringUtils;
12+
13+
import java.util.HashMap;
14+
import java.util.List;
15+
import java.util.Map;
1716

1817
/**
1918
* 群聊服务实现.
@@ -52,6 +51,11 @@ public String chatCreate(String name, String owner, List<String> users, String c
5251
return new JsonParser().parse(result).getAsJsonObject().get("chatid").getAsString();
5352
}
5453

54+
@Override
55+
public String create(String name, String owner, List<String> users, String chatId) throws WxErrorException {
56+
return chatCreate(name, owner, users, chatId);
57+
}
58+
5559
@Override
5660
public void chatUpdate(String chatId, String name, String owner, List<String> usersToAdd, List<String> usersToDelete)
5761
throws WxErrorException {
@@ -75,13 +79,23 @@ public void chatUpdate(String chatId, String name, String owner, List<String> us
7579
this.cpService.post(APPCHAT_UPDATE, WxGsonBuilder.create().toJson(data));
7680
}
7781

82+
@Override
83+
public void update(String chatId, String name, String owner, List<String> usersToAdd, List<String> usersToDelete) throws WxErrorException {
84+
chatUpdate(chatId, name, owner, usersToAdd, usersToDelete);
85+
}
86+
7887
@Override
7988
public WxCpChat chatGet(String chatId) throws WxErrorException {
8089
String result = this.cpService.get(APPCHAT_GET_CHATID + chatId, null);
8190
return WxCpGsonBuilder.create()
8291
.fromJson(JSON_PARSER.parse(result).getAsJsonObject().getAsJsonObject("chat_info").toString(), WxCpChat.class);
8392
}
8493

94+
@Override
95+
public WxCpChat get(String chatId) throws WxErrorException {
96+
return chatGet(chatId);
97+
}
98+
8599
@Override
86100
public void sendMsg(WxCpAppChatMessage message) throws WxErrorException {
87101
this.cpService.post("https://qyapi.weixin.qq.com/cgi-bin/appchat/send", message.toJson());
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
package me.chanjar.weixin.cp.api.impl;
2+
3+
import com.google.gson.JsonArray;
4+
import com.google.gson.JsonElement;
5+
import com.google.gson.JsonObject;
6+
import com.google.gson.JsonParser;
7+
import com.google.gson.reflect.TypeToken;
8+
import me.chanjar.weixin.common.error.WxErrorException;
9+
import me.chanjar.weixin.cp.api.WxCpOAService;
10+
import me.chanjar.weixin.cp.api.WxCpService;
11+
import me.chanjar.weixin.cp.bean.WxCpApprovalDataResult;
12+
import me.chanjar.weixin.cp.bean.WxCpCheckinData;
13+
import me.chanjar.weixin.cp.bean.WxCpCheckinOption;
14+
import me.chanjar.weixin.cp.bean.WxCpDialRecord;
15+
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
16+
17+
import java.util.Date;
18+
import java.util.List;
19+
20+
/**
21+
* @author Element
22+
* @Package me.chanjar.weixin.cp.api.impl
23+
* @date 2019-04-06 11:20
24+
* @Description: TODO
25+
*/
26+
public class WxCpOAServiceImpl implements WxCpOAService {
27+
28+
private WxCpService mainService;
29+
30+
public WxCpOAServiceImpl(WxCpService mainService) {
31+
this.mainService = mainService;
32+
}
33+
34+
@Override
35+
public List<WxCpCheckinData> getCheckinData(Integer openCheckinDataType, Date starttime, Date endtime, List<String> userIdList) throws WxErrorException {
36+
37+
if (starttime == null || endtime == null) {
38+
throw new RuntimeException("starttime and endtime can't be null");
39+
}
40+
41+
if (userIdList == null || userIdList.size() > 100) {
42+
throw new RuntimeException("用户列表不能为空,不超过100个,若用户超过100个,请分批获取");
43+
}
44+
45+
long endtimestamp = endtime.getTime() / 1000L;
46+
long starttimestamp = starttime.getTime() / 1000L;
47+
48+
if (endtimestamp - starttimestamp < 0 || endtimestamp - starttimestamp >= 30 * 24 * 60 * 60) {
49+
throw new RuntimeException("获取记录时间跨度不超过一个月");
50+
}
51+
52+
String url = "https://qyapi.weixin.qq.com/cgi-bin/checkin/getcheckindata";
53+
54+
JsonObject jsonObject = new JsonObject();
55+
JsonArray jsonArray = new JsonArray();
56+
57+
jsonObject.addProperty("opencheckindatatype", openCheckinDataType);
58+
jsonObject.addProperty("starttime", starttimestamp);
59+
jsonObject.addProperty("endtime", endtimestamp);
60+
61+
for (String userid : userIdList) {
62+
jsonArray.add(userid);
63+
}
64+
65+
jsonObject.add("useridlist", jsonArray);
66+
67+
String responseContent = this.mainService.post(url, jsonObject.toString());
68+
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
69+
return WxCpGsonBuilder.create()
70+
.fromJson(
71+
tmpJsonElement.getAsJsonObject().get("checkindata"),
72+
new TypeToken<List<WxCpCheckinData>>() {
73+
}.getType()
74+
);
75+
}
76+
77+
@Override
78+
public List<WxCpCheckinOption> getCheckinOption(Date datetime, List<String> userIdList) throws WxErrorException {
79+
String url = "https://qyapi.weixin.qq.com/cgi-bin/checkin/getcheckinoption";
80+
if (datetime == null) {
81+
throw new RuntimeException("datetime can't be null");
82+
}
83+
84+
if (userIdList == null || userIdList.size() > 100) {
85+
throw new RuntimeException("用户列表不能为空,不超过100个,若用户超过100个,请分批获取");
86+
}
87+
88+
JsonArray jsonArray = new JsonArray();
89+
for (String userid : userIdList) {
90+
jsonArray.add(userid);
91+
}
92+
93+
JsonObject jsonObject = new JsonObject();
94+
jsonObject.addProperty("datetime", datetime.getTime() / 1000L);
95+
jsonObject.add("useridlist", jsonArray);
96+
97+
String responseContent = this.mainService.post(url, jsonObject.toString());
98+
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
99+
100+
return WxCpGsonBuilder.create()
101+
.fromJson(
102+
tmpJsonElement.getAsJsonObject().get("info"),
103+
new TypeToken<List<WxCpCheckinOption>>() {
104+
}.getType()
105+
);
106+
}
107+
108+
@Override
109+
public WxCpApprovalDataResult getApprovalData(Date starttime, Date endtime, Long nextSpnum) throws WxErrorException {
110+
111+
String url = "https://qyapi.weixin.qq.com/cgi-bin/corp/getapprovaldata";
112+
JsonObject jsonObject = new JsonObject();
113+
jsonObject.addProperty("starttime", starttime.getTime() / 1000L);
114+
jsonObject.addProperty("endtime", endtime.getTime() / 1000L);
115+
if (nextSpnum != null) {
116+
jsonObject.addProperty("next_spnum", nextSpnum);
117+
}
118+
119+
String responseContent = this.mainService.post(url, jsonObject.toString());
120+
return WxCpGsonBuilder.create().fromJson(responseContent, WxCpApprovalDataResult.class);
121+
}
122+
123+
@Override
124+
public List<WxCpDialRecord> getDialRecord(Date starttime, Date endtime, Integer offset, Integer limit) throws WxErrorException {
125+
126+
String url = "https://qyapi.weixin.qq.com/cgi-bin/dial/get_dial_record";
127+
JsonObject jsonObject = new JsonObject();
128+
129+
if (offset == null) {
130+
offset = 0;
131+
}
132+
133+
if (limit == null || limit <= 0) {
134+
limit = 100;
135+
}
136+
137+
jsonObject.addProperty("offset", offset);
138+
jsonObject.addProperty("limit", limit);
139+
140+
if (starttime != null && endtime != null) {
141+
142+
long endtimestamp = endtime.getTime() / 1000L;
143+
long starttimestamp = starttime.getTime() / 1000L;
144+
145+
if (endtimestamp - starttimestamp < 0 || endtimestamp - starttimestamp >= 30 * 24 * 60 * 60) {
146+
throw new RuntimeException("受限于网络传输,起止时间的最大跨度为30天,如超过30天,则以结束时间为基准向前取30天进行查询");
147+
}
148+
149+
jsonObject.addProperty("start_time", starttimestamp);
150+
jsonObject.addProperty("end_time", endtimestamp);
151+
152+
153+
}
154+
155+
String responseContent = this.mainService.post(url, jsonObject.toString());
156+
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
157+
158+
return WxCpGsonBuilder.create()
159+
.fromJson(
160+
tmpJsonElement.getAsJsonObject().get("record"),
161+
new TypeToken<List<WxCpDialRecord>>() {
162+
}.getType()
163+
);
164+
}
165+
}

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpServiceApacheHttpClientImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import me.chanjar.weixin.common.util.http.HttpType;
99
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
1010
import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder;
11+
import me.chanjar.weixin.cp.api.WxCpOAService;
1112
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
1213
import org.apache.http.HttpHost;
1314
import org.apache.http.client.config.RequestConfig;

0 commit comments

Comments
 (0)