1 package com.foxinmy.weixin4j.qy.api;
2
3 import java.util.List;
4
5 import com.alibaba.fastjson.JSON;
6 import com.alibaba.fastjson.JSONObject;
7 import com.foxinmy.weixin4j.exception.WeixinException;
8 import com.foxinmy.weixin4j.http.weixin.ApiResult;
9 import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
10 import com.foxinmy.weixin4j.model.Token;
11 import com.foxinmy.weixin4j.qy.model.Party;
12 import com.foxinmy.weixin4j.token.TokenManager;
13
14
15
16
17
18
19
20
21
22
23
24 public class PartyApi extends QyApi {
25 private final TokenManager tokenManager;
26
27 public PartyApi(TokenManager tokenManager) {
28 this.tokenManager = tokenManager;
29 }
30
31
32
33
34
35
36
37
38
39
40
41 public int createParty(Party party) throws WeixinException {
42 String department_create_uri = getRequestUri("department_create_uri");
43 JSONObject obj = (JSONObject) JSON.toJSON(party);
44 if (party.getParentId() < 1) {
45 obj.remove("parentid");
46 }
47 if (party.getId() < 1) {
48 obj.remove("id");
49 }
50 Token token = tokenManager.getCache();
51 WeixinResponse response = weixinExecutor.post(
52 String.format(department_create_uri, token.getAccessToken()),
53 obj.toJSONString());
54 return response.getAsJson().getIntValue("id");
55 }
56
57
58
59
60
61
62
63
64
65
66
67 public ApiResult updateParty(Party party) throws WeixinException {
68 if (party.getId() < 1) {
69 throw new WeixinException("department id must gt 1");
70 }
71 String department_update_uri = getRequestUri("department_update_uri");
72 JSONObject obj = (JSONObject) JSON.toJSON(party);
73 if (party.getParentId() < 1) {
74 obj.remove("parentid");
75 }
76 if (party.getOrder() < 0) {
77 obj.remove("order");
78 }
79 Token token = tokenManager.getCache();
80 WeixinResponse response = weixinExecutor.post(
81 String.format(department_update_uri, token.getAccessToken()),
82 obj.toJSONString());
83 return response.getAsResult();
84 }
85
86
87
88
89
90
91
92
93
94
95
96 public List<Party> listParty(int partId) throws WeixinException {
97 String department_list_uri = getRequestUri("department_list_uri");
98 if (partId > 0) {
99 department_list_uri += String.format("&id=%d", partId);
100 }
101 Token token = tokenManager.getCache();
102 WeixinResponse response = weixinExecutor.get(String.format(
103 department_list_uri, token.getAccessToken()));
104 return JSON.parseArray(response.getAsJson().getString("department"),
105 Party.class);
106 }
107
108
109
110
111
112
113
114
115
116
117 public ApiResult deleteParty(int partId) throws WeixinException {
118 String department_delete_uri = getRequestUri("department_delete_uri");
119 Token token = tokenManager.getCache();
120 WeixinResponse response = weixinExecutor.get(String.format(
121 department_delete_uri, token.getAccessToken(), partId));
122 return response.getAsResult();
123 }
124 }