Skip to content

Commit b17041e

Browse files
committed
发送客服消息接口转移到客服专用service中,使用时需要加入getKefuService()
1 parent 21f1497 commit b17041e

File tree

7 files changed

+115
-101
lines changed

7 files changed

+115
-101
lines changed

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpKefuService.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
package me.chanjar.weixin.mp.api;
22

3-
import me.chanjar.weixin.common.exception.WxErrorException;
4-
import me.chanjar.weixin.mp.bean.kefu.request.WxMpKfAccountRequest;
5-
import me.chanjar.weixin.mp.bean.kefu.result.*;
6-
73
import java.io.File;
84
import java.util.Date;
95

6+
import me.chanjar.weixin.common.exception.WxErrorException;
7+
import me.chanjar.weixin.mp.bean.WxMpCustomMessage;
8+
import me.chanjar.weixin.mp.bean.kefu.request.WxMpKfAccountRequest;
9+
import me.chanjar.weixin.mp.bean.kefu.result.WxMpKfList;
10+
import me.chanjar.weixin.mp.bean.kefu.result.WxMpKfMsgList;
11+
import me.chanjar.weixin.mp.bean.kefu.result.WxMpKfOnlineList;
12+
import me.chanjar.weixin.mp.bean.kefu.result.WxMpKfSessionGetResult;
13+
import me.chanjar.weixin.mp.bean.kefu.result.WxMpKfSessionList;
14+
import me.chanjar.weixin.mp.bean.kefu.result.WxMpKfSessionWaitCaseList;
15+
1016
/**
1117
* 客服接口 ,
1218
* 命名采用kefu拼音的原因是:
@@ -17,6 +23,14 @@
1723
*/
1824
public interface WxMpKefuService {
1925

26+
/**
27+
* <pre>
28+
* 发送客服消息
29+
* 详情请见: <a href="http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140547&token=&lang=zh_CN">发送客服消息</a>
30+
* </pre>
31+
*/
32+
boolean customMessageSend(WxMpCustomMessage message) throws WxErrorException;
33+
2034
//*******************客服管理接口***********************//
2135

2236
/**

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import me.chanjar.weixin.common.bean.WxJsapiSignature;
44
import me.chanjar.weixin.common.exception.WxErrorException;
55
import me.chanjar.weixin.common.util.http.RequestExecutor;
6-
import me.chanjar.weixin.mp.bean.WxMpCustomMessage;
76
import me.chanjar.weixin.mp.bean.WxMpIndustry;
87
import me.chanjar.weixin.mp.bean.WxMpMassGroupMessage;
98
import me.chanjar.weixin.mp.bean.WxMpMassNews;
@@ -82,14 +81,6 @@ public interface WxMpService {
8281
*/
8382
WxJsapiSignature createJsapiSignature(String url) throws WxErrorException;
8483

85-
/**
86-
* <pre>
87-
* 发送客服消息
88-
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=发送客服消息
89-
* </pre>
90-
*/
91-
void customMessageSend(WxMpCustomMessage message) throws WxErrorException;
92-
9384
/**
9485
* <pre>
9586
* 上传群发用的图文消息,上传后才能群发图文消息

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpKefuServiceImpl.java

Lines changed: 60 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@
33
import java.io.File;
44
import java.util.Date;
55

6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
69
import com.google.gson.JsonObject;
710

811
import me.chanjar.weixin.common.bean.result.WxError;
12+
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
913
import me.chanjar.weixin.common.exception.WxErrorException;
1014
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
11-
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
12-
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
1315
import me.chanjar.weixin.mp.api.WxMpKefuService;
1416
import me.chanjar.weixin.mp.api.WxMpService;
17+
import me.chanjar.weixin.mp.bean.WxMpCustomMessage;
1518
import me.chanjar.weixin.mp.bean.kefu.request.WxMpKfAccountRequest;
1619
import me.chanjar.weixin.mp.bean.kefu.request.WxMpKfSessionRequest;
1720
import me.chanjar.weixin.mp.bean.kefu.result.WxMpKfList;
@@ -27,67 +30,90 @@
2730
*
2831
*/
2932
public class WxMpKefuServiceImpl implements WxMpKefuService {
33+
protected final Logger log = LoggerFactory
34+
.getLogger(WxMpKefuServiceImpl.class);
3035
private static final String API_URL_PREFIX = "https://api.weixin.qq.com/customservice";
3136
private WxMpService wxMpService;
3237

3338
public WxMpKefuServiceImpl(WxMpService wxMpService) {
3439
this.wxMpService = wxMpService;
3540
}
3641

42+
@Override
43+
public boolean customMessageSend(WxMpCustomMessage message)
44+
throws WxErrorException {
45+
String url = "https://api.weixin.qq.com/cgi-bin/message/custom/send";
46+
String responseContent = this.wxMpService.post(url, message.toJson());
47+
this.log.debug("\nurl:{}\nparams:{}\nresponse:{}", url, message.toJson(),
48+
responseContent);
49+
return true;
50+
}
51+
3752
@Override
3853
public WxMpKfList kfList() throws WxErrorException {
39-
String url = "https://api.weixin.qq.com/cgi-bin/customservice/getkflist";
40-
String responseContent = this.wxMpService
41-
.execute(new SimpleGetRequestExecutor(), url, null);
54+
String url = API_URL_PREFIX + "/getkflist";
55+
String responseContent = this.wxMpService.get(url, null);
56+
this.log.debug("\nurl:{}\nparams:{}\nresponse:{}", url, null,
57+
responseContent);
4258
return WxMpKfList.fromJson(responseContent);
4359
}
4460

4561
@Override
4662
public WxMpKfOnlineList kfOnlineList() throws WxErrorException {
47-
String url = "https://api.weixin.qq.com/cgi-bin/customservice/getonlinekflist";
48-
String responseContent = this.wxMpService
49-
.execute(new SimpleGetRequestExecutor(), url, null);
63+
String url = API_URL_PREFIX + "/getonlinekflist";
64+
String responseContent = this.wxMpService.get(url, null);
65+
this.log.debug("\nurl:{}\nparams:{}\nresponse:{}", url, null,
66+
responseContent);
5067
return WxMpKfOnlineList.fromJson(responseContent);
5168
}
5269

5370
@Override
5471
public boolean kfAccountAdd(WxMpKfAccountRequest request)
5572
throws WxErrorException {
5673
String url = API_URL_PREFIX + "/kfaccount/add";
57-
this.wxMpService.execute(new SimplePostRequestExecutor(), url,
58-
request.toJson());
74+
String responseContent = this.wxMpService.post(url, request.toJson());
75+
this.log.debug("\nurl:{}\nparams:{}\nresponse:{}", url, request.toJson(),
76+
responseContent);
5977
return true;
6078
}
6179

6280
@Override
6381
public boolean kfAccountUpdate(WxMpKfAccountRequest request)
6482
throws WxErrorException {
6583
String url = API_URL_PREFIX + "/kfaccount/update";
66-
this.wxMpService.execute(new SimplePostRequestExecutor(), url,
67-
request.toJson());
84+
String responseContent = this.wxMpService.post(url, request.toJson());
85+
this.log.debug("\nurl:{}\nparams:{}\nresponse:{}", url, request.toJson(),
86+
responseContent);
6887
return true;
6988
}
7089

7190
@Override
7291
public boolean kfAccountInviteWorker(WxMpKfAccountRequest request) throws WxErrorException {
7392
String url = API_URL_PREFIX + "/kfaccount/inviteworker";
74-
this.wxMpService.execute(new SimplePostRequestExecutor(), url,
75-
request.toJson());
93+
String responseContent = this.wxMpService.post(url, request.toJson());
94+
this.log.debug("\nurl:{}\nparams:{}\nresponse:{}", url, request.toJson(),
95+
responseContent);
7696
return true;
7797
}
7898

7999
@Override
80100
public boolean kfAccountUploadHeadImg(String kfAccount, File imgFile)
81101
throws WxErrorException {
82102
String url = API_URL_PREFIX + "/kfaccount/uploadheadimg?kf_account=" + kfAccount;
83-
this.wxMpService.execute(new MediaUploadRequestExecutor(), url, imgFile);
103+
WxMediaUploadResult responseContent = this.wxMpService
104+
.execute(new MediaUploadRequestExecutor(), url, imgFile);
105+
this.log.debug("\nurl:{}\nparams:{}&file:{}\nresponse:{}", url, kfAccount,
106+
imgFile.getAbsolutePath(),
107+
responseContent);
84108
return true;
85109
}
86110

87111
@Override
88112
public boolean kfAccountDel(String kfAccount) throws WxErrorException {
89113
String url = API_URL_PREFIX + "/kfaccount/del?kf_account=" + kfAccount;
90-
this.wxMpService.execute(new SimpleGetRequestExecutor(), url, null);
114+
String responseContent = this.wxMpService.get(url, null);
115+
this.log.debug("\nurl:{}\nparams:{}\nresponse:{}", url, null,
116+
responseContent);
91117
return true;
92118
}
93119

@@ -96,8 +122,9 @@ public boolean kfSessionCreate(String openid, String kfAccount)
96122
throws WxErrorException {
97123
WxMpKfSessionRequest request = new WxMpKfSessionRequest(kfAccount, openid);
98124
String url = API_URL_PREFIX + "/kfsession/create";
99-
this.wxMpService.execute(new SimplePostRequestExecutor(), url,
100-
request.toJson());
125+
String responseContent = this.wxMpService.post(url, request.toJson());
126+
this.log.debug("\nurl:{}\nparams:{}\nresponse:{}", url, request.toJson(),
127+
responseContent);
101128
return true;
102129
}
103130

@@ -106,35 +133,39 @@ public boolean kfSessionClose(String openid, String kfAccount)
106133
throws WxErrorException {
107134
WxMpKfSessionRequest request = new WxMpKfSessionRequest(kfAccount, openid);
108135
String url = API_URL_PREFIX + "/kfsession/close";
109-
this.wxMpService.execute(new SimplePostRequestExecutor(), url,
110-
request.toJson());
136+
String responseContent = this.wxMpService.post(url, request.toJson());
137+
this.log.debug("\nurl:{}\nparams:{}\nresponse:{}", url, request.toJson(),
138+
responseContent);
111139
return true;
112140
}
113141

114142
@Override
115143
public WxMpKfSessionGetResult kfSessionGet(String openid)
116144
throws WxErrorException {
117145
String url = API_URL_PREFIX + "/kfsession/getsession?openid=" + openid;
118-
String responseContent = this.wxMpService
119-
.execute(new SimpleGetRequestExecutor(), url, null);
146+
String responseContent = this.wxMpService.get(url, null);
147+
this.log.debug("\nurl:{}\nparams:{}\nresponse:{}", url, null,
148+
responseContent);
120149
return WxMpKfSessionGetResult.fromJson(responseContent);
121150
}
122151

123152
@Override
124153
public WxMpKfSessionList kfSessionList(String kfAccount)
125154
throws WxErrorException {
126155
String url = API_URL_PREFIX + "/kfsession/getsessionlist?kf_account=" + kfAccount;
127-
String responseContent = this.wxMpService
128-
.execute(new SimpleGetRequestExecutor(), url, null);
156+
String responseContent = this.wxMpService.get(url, null);
157+
this.log.debug("\nurl:{}\nparams:{}\nresponse:{}", url, null,
158+
responseContent);
129159
return WxMpKfSessionList.fromJson(responseContent);
130160
}
131161

132162
@Override
133163
public WxMpKfSessionWaitCaseList kfSessionGetWaitCase()
134164
throws WxErrorException {
135165
String url = API_URL_PREFIX + "/kfsession/getwaitcase";
136-
String responseContent = this.wxMpService
137-
.execute(new SimpleGetRequestExecutor(), url, null);
166+
String responseContent = this.wxMpService.get(url, null);
167+
this.log.debug("\nurl:{}\nparams:{}\nresponse:{}", url, null,
168+
responseContent);
138169
return WxMpKfSessionWaitCaseList.fromJson(responseContent);
139170
}
140171

@@ -156,7 +187,9 @@ public WxMpKfMsgList kfMsgList(Date startTime, Date endTime, Long msgId, Integer
156187
param.addProperty("msgid", msgId); //msgid 消息id顺序从小到大,从1开始
157188
param.addProperty("number", number); //number 每次获取条数,最多10000条
158189

159-
String responseContent = this.wxMpService.execute(new SimplePostRequestExecutor(), url, param.toString());
190+
String responseContent = this.wxMpService.post(url, param.toString());
191+
this.log.debug("\nurl:{}\nparams:{}\nresponse:{}", url, param.toString(),
192+
responseContent);
160193
return WxMpKfMsgList.fromJson(responseContent);
161194
}
162195

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceImpl.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import me.chanjar.weixin.mp.api.WxMpService;
4545
import me.chanjar.weixin.mp.api.WxMpUserService;
4646
import me.chanjar.weixin.mp.api.WxMpUserTagService;
47-
import me.chanjar.weixin.mp.bean.WxMpCustomMessage;
4847
import me.chanjar.weixin.mp.bean.WxMpIndustry;
4948
import me.chanjar.weixin.mp.bean.WxMpMassGroupMessage;
5049
import me.chanjar.weixin.mp.bean.WxMpMassNews;
@@ -203,12 +202,6 @@ public WxJsapiSignature createJsapiSignature(String url) throws WxErrorException
203202
return jsapiSignature;
204203
}
205204

206-
@Override
207-
public void customMessageSend(WxMpCustomMessage message) throws WxErrorException {
208-
String url = "https://api.weixin.qq.com/cgi-bin/message/custom/send";
209-
execute(new SimplePostRequestExecutor(), url, message.toJson());
210-
}
211-
212205
@Override
213206
public WxMpMassUploadResult massNewsUpload(WxMpMassNews news) throws WxErrorException {
214207
String url = "https://api.weixin.qq.com/cgi-bin/media/uploadnews";

weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/WxMpCustomMessageAPITest.java

Lines changed: 0 additions & 44 deletions
This file was deleted.

weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpKefuServiceImplTest.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111

1212
import com.google.inject.Inject;
1313

14+
import me.chanjar.weixin.common.api.WxConsts;
1415
import me.chanjar.weixin.common.exception.WxErrorException;
1516
import me.chanjar.weixin.mp.api.ApiTestModule;
1617
import me.chanjar.weixin.mp.api.ApiTestModule.WxXmlMpInMemoryConfigStorage;
18+
import me.chanjar.weixin.mp.bean.WxMpCustomMessage;
1719
import me.chanjar.weixin.mp.bean.kefu.request.WxMpKfAccountRequest;
1820
import me.chanjar.weixin.mp.bean.kefu.result.WxMpKfInfo;
1921
import me.chanjar.weixin.mp.bean.kefu.result.WxMpKfList;
@@ -35,6 +37,31 @@ public class WxMpKefuServiceImplTest {
3537
@Inject
3638
protected WxMpServiceImpl wxService;
3739

40+
public void testSendCustomMessage() throws WxErrorException {
41+
ApiTestModule.WxXmlMpInMemoryConfigStorage configStorage = (ApiTestModule.WxXmlMpInMemoryConfigStorage) this.wxService
42+
.getWxMpConfigStorage();
43+
WxMpCustomMessage message = new WxMpCustomMessage();
44+
message.setMsgType(WxConsts.CUSTOM_MSG_TEXT);
45+
message.setToUser(configStorage.getOpenId());
46+
message.setContent(
47+
"欢迎欢迎,热烈欢迎\n换行测试\n超链接:<a href=\"http://www.baidu.com\">Hello World</a>");
48+
49+
this.wxService.getKefuService().customMessageSend(message);
50+
}
51+
52+
public void testSendCustomMessageWithKfAccount() throws WxErrorException {
53+
ApiTestModule.WxXmlMpInMemoryConfigStorage configStorage = (ApiTestModule.WxXmlMpInMemoryConfigStorage) this.wxService
54+
.getWxMpConfigStorage();
55+
WxMpCustomMessage message = new WxMpCustomMessage();
56+
message.setMsgType(WxConsts.CUSTOM_MSG_TEXT);
57+
message.setToUser(configStorage.getOpenId());
58+
message.setKfAccount(configStorage.getKfAccount());
59+
message.setContent(
60+
"欢迎欢迎,热烈欢迎\n换行测试\n超链接:<a href=\"http://www.baidu.com\">Hello World</a>");
61+
62+
this.wxService.getKefuService().customMessageSend(message);
63+
}
64+
3865
public void testKfList() throws WxErrorException {
3966
WxMpKfList kfList = this.wxService.getKefuService().kfList();
4067
Assert.assertNotNull(kfList);
@@ -123,7 +150,7 @@ public void testKfSessionClose(String kfAccount, String openid)
123150
}
124151

125152
@Test(dataProvider = "getKfAccountAndOpenid")
126-
public void testKfSessionGet(String kfAccount,
153+
public void testKfSessionGet(@SuppressWarnings("unused") String kfAccount,
127154
String openid) throws WxErrorException {
128155
WxMpKfSessionGetResult result = this.wxService.getKefuService()
129156
.kfSessionGet(openid);

0 commit comments

Comments
 (0)