11package me .chanjar .weixin .mp .api .impl ;
22
33import java .io .File ;
4+ import java .util .Date ;
45
6+ import com .google .gson .JsonObject ;
7+ import me .chanjar .weixin .common .bean .result .WxError ;
58import me .chanjar .weixin .common .exception .WxErrorException ;
69import me .chanjar .weixin .common .util .http .MediaUploadRequestExecutor ;
710import me .chanjar .weixin .common .util .http .SimpleGetRequestExecutor ;
1013import me .chanjar .weixin .mp .api .WxMpService ;
1114import me .chanjar .weixin .mp .bean .kefu .request .WxMpKfAccountRequest ;
1215import me .chanjar .weixin .mp .bean .kefu .request .WxMpKfSessionRequest ;
13- import me .chanjar .weixin .mp .bean .kefu .result .WxMpKfList ;
14- import me .chanjar .weixin .mp .bean .kefu .result .WxMpKfOnlineList ;
15- import me .chanjar .weixin .mp .bean .kefu .result .WxMpKfSessionGetResult ;
16- import me .chanjar .weixin .mp .bean .kefu .result .WxMpKfSessionList ;
17- import me .chanjar .weixin .mp .bean .kefu .result .WxMpKfSessionWaitCaseList ;
16+ import me .chanjar .weixin .mp .bean .kefu .result .*;
1817
1918/**
2019 *
2120 * @author Binary Wang
2221 *
2322 */
2423public class WxMpKefuServiceImpl implements WxMpKefuService {
24+ public static final String API_URL_PREFIX = "https://api.weixin.qq.com/customservice" ;
2525 private WxMpService wxMpService ;
2626
2727 public WxMpKefuServiceImpl (WxMpService wxMpService ) {
@@ -30,15 +30,15 @@ public WxMpKefuServiceImpl(WxMpService wxMpService) {
3030
3131 @ Override
3232 public WxMpKfList kfList () throws WxErrorException {
33- String url = "https://api.weixin.qq.com/cgi-bin/customservice /getkflist" ;
33+ String url = API_URL_PREFIX + " /getkflist" ;
3434 String responseContent = this .wxMpService
3535 .execute (new SimpleGetRequestExecutor (), url , null );
3636 return WxMpKfList .fromJson (responseContent );
3737 }
3838
3939 @ Override
4040 public WxMpKfOnlineList kfOnlineList () throws WxErrorException {
41- String url = "https://api.weixin.qq.com/cgi-bin/customservice /getonlinekflist" ;
41+ String url = API_URL_PREFIX + " /getonlinekflist" ;
4242 String responseContent = this .wxMpService
4343 .execute (new SimpleGetRequestExecutor (), url , null );
4444 return WxMpKfOnlineList .fromJson (responseContent );
@@ -47,7 +47,7 @@ public WxMpKfOnlineList kfOnlineList() throws WxErrorException {
4747 @ Override
4848 public boolean kfAccountAdd (WxMpKfAccountRequest request )
4949 throws WxErrorException {
50- String url = "https://api.weixin.qq.com/customservice /kfaccount/add" ;
50+ String url = API_URL_PREFIX + " /kfaccount/add" ;
5151 this .wxMpService .execute (new SimplePostRequestExecutor (), url ,
5252 request .toJson ());
5353 return true ;
@@ -56,15 +56,15 @@ public boolean kfAccountAdd(WxMpKfAccountRequest request)
5656 @ Override
5757 public boolean kfAccountUpdate (WxMpKfAccountRequest request )
5858 throws WxErrorException {
59- String url = "https://api.weixin.qq.com/customservice /kfaccount/update" ;
59+ String url = API_URL_PREFIX + " /kfaccount/update" ;
6060 this .wxMpService .execute (new SimplePostRequestExecutor (), url ,
6161 request .toJson ());
6262 return true ;
6363 }
6464
6565 @ Override
6666 public boolean kfAccountInviteWorker (WxMpKfAccountRequest request ) throws WxErrorException {
67- String url = "https://api.weixin.qq.com/customservice /kfaccount/inviteworker" ;
67+ String url = API_URL_PREFIX + " /kfaccount/inviteworker" ;
6868 this .wxMpService .execute (new SimplePostRequestExecutor (), url ,
6969 request .toJson ());
7070 return true ;
@@ -73,16 +73,14 @@ public boolean kfAccountInviteWorker(WxMpKfAccountRequest request) throws WxErro
7373 @ Override
7474 public boolean kfAccountUploadHeadImg (String kfAccount , File imgFile )
7575 throws WxErrorException {
76- String url = "https://api.weixin.qq.com/customservice/kfaccount/uploadheadimg?kf_account="
77- + kfAccount ;
76+ String url = API_URL_PREFIX + "/kfaccount/uploadheadimg?kf_account=" + kfAccount ;
7877 this .wxMpService .execute (new MediaUploadRequestExecutor (), url , imgFile );
7978 return true ;
8079 }
8180
8281 @ Override
8382 public boolean kfAccountDel (String kfAccount ) throws WxErrorException {
84- String url = "https://api.weixin.qq.com/customservice/kfaccount/del?kf_account="
85- + kfAccount ;
83+ String url = API_URL_PREFIX + "/kfaccount/del?kf_account=" + kfAccount ;
8684 this .wxMpService .execute (new SimpleGetRequestExecutor (), url , null );
8785 return true ;
8886 }
@@ -91,7 +89,7 @@ public boolean kfAccountDel(String kfAccount) throws WxErrorException {
9189 public boolean kfSessionCreate (String openid , String kfAccount )
9290 throws WxErrorException {
9391 WxMpKfSessionRequest request = new WxMpKfSessionRequest (kfAccount , openid );
94- String url = "https://api.weixin.qq.com/customservice /kfsession/create" ;
92+ String url = API_URL_PREFIX + " /kfsession/create" ;
9593 this .wxMpService .execute (new SimplePostRequestExecutor (), url ,
9694 request .toJson ());
9795 return true ;
@@ -101,7 +99,7 @@ public boolean kfSessionCreate(String openid, String kfAccount)
10199 public boolean kfSessionClose (String openid , String kfAccount )
102100 throws WxErrorException {
103101 WxMpKfSessionRequest request = new WxMpKfSessionRequest (kfAccount , openid );
104- String url = "https://api.weixin.qq.com/customservice /kfsession/close" ;
102+ String url = API_URL_PREFIX + " /kfsession/close" ;
105103 this .wxMpService .execute (new SimplePostRequestExecutor (), url ,
106104 request .toJson ());
107105 return true ;
@@ -110,8 +108,7 @@ public boolean kfSessionClose(String openid, String kfAccount)
110108 @ Override
111109 public WxMpKfSessionGetResult kfSessionGet (String openid )
112110 throws WxErrorException {
113- String url = "https://api.weixin.qq.com/customservice/kfsession/getsession?openid="
114- + openid ;
111+ String url = API_URL_PREFIX + "/kfsession/getsession?openid=" + openid ;
115112 String responseContent = this .wxMpService
116113 .execute (new SimpleGetRequestExecutor (), url , null );
117114 return WxMpKfSessionGetResult .fromJson (responseContent );
@@ -120,8 +117,7 @@ public WxMpKfSessionGetResult kfSessionGet(String openid)
120117 @ Override
121118 public WxMpKfSessionList kfSessionList (String kfAccount )
122119 throws WxErrorException {
123- String url = "https://api.weixin.qq.com/customservice/kfsession/getsessionlist?kf_account="
124- + kfAccount ;
120+ String url = API_URL_PREFIX + "/kfsession/getsessionlist?kf_account=" + kfAccount ;
125121 String responseContent = this .wxMpService
126122 .execute (new SimpleGetRequestExecutor (), url , null );
127123 return WxMpKfSessionList .fromJson (responseContent );
@@ -130,10 +126,28 @@ public WxMpKfSessionList kfSessionList(String kfAccount)
130126 @ Override
131127 public WxMpKfSessionWaitCaseList kfSessionGetWaitCase ()
132128 throws WxErrorException {
133- String url = "https://api.weixin.qq.com/customservice /kfsession/getwaitcase" ;
129+ String url = API_URL_PREFIX + " /kfsession/getwaitcase" ;
134130 String responseContent = this .wxMpService
135131 .execute (new SimpleGetRequestExecutor (), url , null );
136132 return WxMpKfSessionWaitCaseList .fromJson (responseContent );
137133 }
138134
135+ @ Override
136+ public WxMpKfMsgList kfMsgList (Date startTime , Date endTime , Integer msgId , Integer number ) throws WxErrorException {
137+ if (startTime .after (endTime )){
138+ throw new WxErrorException (WxError .newBuilder ().setErrorMsg ("起始时间不能晚于结束时间!" ).build ());
139+ }
140+
141+ String url = API_URL_PREFIX + "/msgrecord/getmsglist" ;
142+
143+ JsonObject param = new JsonObject ();
144+ param .addProperty ("starttime" , startTime .getTime () / 1000 ); //starttime 起始时间,unix时间戳
145+ param .addProperty ("endtime" , endTime .getTime () / 1000 ); //endtime 结束时间,unix时间戳,每次查询时段不能超过24小时
146+ param .addProperty ("msgid" , msgId ); //msgid 消息id顺序从小到大,从1开始
147+ param .addProperty ("number" , number ); //number 每次获取条数,最多10000条
148+
149+ String responseContent = this .wxMpService .execute (new SimplePostRequestExecutor (), url , param .toString ());
150+ return WxMpKfMsgList .fromJson (responseContent );
151+ }
152+
139153}
0 commit comments