Skip to content

Commit a40a92b

Browse files
Copilotbinarywang
andcommitted
Add basic Mini Program customer service management interfaces
Co-authored-by: binarywang <[email protected]>
1 parent 8b752b0 commit a40a92b

File tree

10 files changed

+543
-0
lines changed

10 files changed

+543
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package cn.binarywang.wx.miniapp.api;
2+
3+
import cn.binarywang.wx.miniapp.bean.kefu.WxMaKfInfo;
4+
import cn.binarywang.wx.miniapp.bean.kefu.WxMaKfList;
5+
import cn.binarywang.wx.miniapp.bean.kefu.WxMaKfSession;
6+
import cn.binarywang.wx.miniapp.bean.kefu.WxMaKfSessionList;
7+
import cn.binarywang.wx.miniapp.bean.kefu.request.WxMaKfAccountRequest;
8+
import me.chanjar.weixin.common.error.WxErrorException;
9+
10+
/**
11+
* <pre>
12+
* 小程序客服管理接口.
13+
* 不同于 WxMaCustomserviceWorkService (企业微信客服绑定) 和 WxMaMsgService.sendKefuMsg (发送客服消息),
14+
* 此接口专门处理小程序客服账号管理、会话管理等功能。
15+
*
16+
* 注意:小程序客服管理接口与公众号客服管理接口在API端点和功能上有所不同。
17+
* </pre>
18+
*
19+
* @author <a href="https://github.com/binarywang">Binary Wang</a>
20+
*/
21+
public interface WxMaKefuService {
22+
23+
/**
24+
* <pre>
25+
* 获取客服基本信息
26+
* 详情请见:<a href="https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/customer-service/customerServiceMessage.getContactList.html">获取客服基本信息</a>
27+
* 接口url格式:https://api.weixin.qq.com/cgi-bin/customservice/getkflist?access_token=ACCESS_TOKEN
28+
* </pre>
29+
*
30+
* @return 客服列表
31+
* @throws WxErrorException 异常
32+
*/
33+
WxMaKfList kfList() throws WxErrorException;
34+
35+
/**
36+
* <pre>
37+
* 添加客服账号
38+
* 详情请见:<a href="https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/customer-service/customerServiceMessage.addKfAccount.html">添加客服账号</a>
39+
* 接口url格式:https://api.weixin.qq.com/customservice/kfaccount/add?access_token=ACCESS_TOKEN
40+
* </pre>
41+
*
42+
* @param request 客服账号信息
43+
* @return 是否成功
44+
* @throws WxErrorException 异常
45+
*/
46+
boolean kfAccountAdd(WxMaKfAccountRequest request) throws WxErrorException;
47+
48+
/**
49+
* <pre>
50+
* 修改客服账号
51+
* 详情请见:<a href="https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/customer-service/customerServiceMessage.updateKfAccount.html">修改客服账号</a>
52+
* 接口url格式:https://api.weixin.qq.com/customservice/kfaccount/update?access_token=ACCESS_TOKEN
53+
* </pre>
54+
*
55+
* @param request 客服账号信息
56+
* @return 是否成功
57+
* @throws WxErrorException 异常
58+
*/
59+
boolean kfAccountUpdate(WxMaKfAccountRequest request) throws WxErrorException;
60+
61+
/**
62+
* <pre>
63+
* 删除客服账号
64+
* 详情请见:<a href="https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/customer-service/customerServiceMessage.deleteKfAccount.html">删除客服账号</a>
65+
* 接口url格式:https://api.weixin.qq.com/customservice/kfaccount/del?access_token=ACCESS_TOKEN&kf_account=KFACCOUNT
66+
* </pre>
67+
*
68+
* @param kfAccount 客服账号
69+
* @return 是否成功
70+
* @throws WxErrorException 异常
71+
*/
72+
boolean kfAccountDel(String kfAccount) throws WxErrorException;
73+
74+
/**
75+
* <pre>
76+
* 创建会话
77+
* 详情请见:<a href="https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/customer-service/customerServiceMessage.createSession.html">创建会话</a>
78+
* 接口url格式:https://api.weixin.qq.com/customservice/kfsession/create?access_token=ACCESS_TOKEN
79+
* </pre>
80+
*
81+
* @param openid 用户openid
82+
* @param kfAccount 客服账号
83+
* @return 是否成功
84+
* @throws WxErrorException 异常
85+
*/
86+
boolean kfSessionCreate(String openid, String kfAccount) throws WxErrorException;
87+
88+
/**
89+
* <pre>
90+
* 关闭会话
91+
* 详情请见:<a href="https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/customer-service/customerServiceMessage.closeSession.html">关闭会话</a>
92+
* 接口url格式:https://api.weixin.qq.com/customservice/kfsession/close?access_token=ACCESS_TOKEN
93+
* </pre>
94+
*
95+
* @param openid 用户openid
96+
* @param kfAccount 客服账号
97+
* @return 是否成功
98+
* @throws WxErrorException 异常
99+
*/
100+
boolean kfSessionClose(String openid, String kfAccount) throws WxErrorException;
101+
102+
/**
103+
* <pre>
104+
* 获取客户的会话状态
105+
* 详情请见:<a href="https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/customer-service/customerServiceMessage.getSession.html">获取客户的会话状态</a>
106+
* 接口url格式:https://api.weixin.qq.com/customservice/kfsession/getsession?access_token=ACCESS_TOKEN&openid=OPENID
107+
* </pre>
108+
*
109+
* @param openid 用户openid
110+
* @return 会话信息
111+
* @throws WxErrorException 异常
112+
*/
113+
WxMaKfSession kfSessionGet(String openid) throws WxErrorException;
114+
115+
/**
116+
* <pre>
117+
* 获取客服的会话列表
118+
* 详情请见:<a href="https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/customer-service/customerServiceMessage.getSessionList.html">获取客服的会话列表</a>
119+
* 接口url格式:https://api.weixin.qq.com/customservice/kfsession/getsessionlist?access_token=ACCESS_TOKEN&kf_account=KFACCOUNT
120+
* </pre>
121+
*
122+
* @param kfAccount 客服账号
123+
* @return 会话列表
124+
* @throws WxErrorException 异常
125+
*/
126+
WxMaKfSessionList kfSessionList(String kfAccount) throws WxErrorException;
127+
128+
}

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaService.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,13 @@ WxMaApiResponse execute(
285285
*/
286286
WxMaCustomserviceWorkService getCustomserviceWorkService();
287287

288+
/**
289+
* 获取小程序客服管理服务。
290+
*
291+
* @return 客服管理服务对象WxMaKefuService
292+
*/
293+
WxMaKefuService getKefuService();
294+
288295
/**
289296
* 获取jsapi操作相关服务对象。
290297
*

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/BaseWxMaServiceImpl.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ public abstract class BaseWxMaServiceImpl<H, P> implements WxMaService, RequestH
113113
private final WxMaAnalysisService analysisService = new WxMaAnalysisServiceImpl(this);
114114
private final WxMaCodeService codeService = new WxMaCodeServiceImpl(this);
115115
private final WxMaCustomserviceWorkService customserviceWorkService = new WxMaCustomserviceWorkServiceImpl(this);
116+
private final WxMaKefuService maKefuService = new WxMaKefuServiceImpl(this);
116117
private final WxMaInternetService internetService = new WxMaInternetServiceImpl(this);
117118
private final WxMaSettingService settingService = new WxMaSettingServiceImpl(this);
118119
private final WxMaJsapiService jsapiService = new WxMaJsapiServiceImpl(this);
@@ -657,6 +658,11 @@ public WxMaCustomserviceWorkService getCustomserviceWorkService() {
657658
return this.customserviceWorkService;
658659
}
659660

661+
@Override
662+
public WxMaKefuService getKefuService() {
663+
return this.maKefuService;
664+
}
665+
660666
@Override
661667
public WxMaJsapiService getJsapiService() {
662668
return this.jsapiService;
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package cn.binarywang.wx.miniapp.api.impl;
2+
3+
import cn.binarywang.wx.miniapp.api.WxMaKefuService;
4+
import cn.binarywang.wx.miniapp.api.WxMaService;
5+
import cn.binarywang.wx.miniapp.bean.kefu.WxMaKfInfo;
6+
import cn.binarywang.wx.miniapp.bean.kefu.WxMaKfList;
7+
import cn.binarywang.wx.miniapp.bean.kefu.WxMaKfSession;
8+
import cn.binarywang.wx.miniapp.bean.kefu.WxMaKfSessionList;
9+
import cn.binarywang.wx.miniapp.bean.kefu.request.WxMaKfAccountRequest;
10+
import cn.binarywang.wx.miniapp.bean.kefu.request.WxMaKfSessionRequest;
11+
import lombok.RequiredArgsConstructor;
12+
import me.chanjar.weixin.common.error.WxErrorException;
13+
14+
/**
15+
* 小程序客服管理服务实现.
16+
*
17+
* @author <a href="https://github.com/binarywang">Binary Wang</a>
18+
*/
19+
@RequiredArgsConstructor
20+
public class WxMaKefuServiceImpl implements WxMaKefuService {
21+
22+
// 小程序客服管理接口URL
23+
private static final String KFLIST_GET_URL = "https://api.weixin.qq.com/cgi-bin/customservice/getkflist";
24+
private static final String KFACCOUNT_ADD_URL = "https://api.weixin.qq.com/customservice/kfaccount/add";
25+
private static final String KFACCOUNT_UPDATE_URL = "https://api.weixin.qq.com/customservice/kfaccount/update";
26+
private static final String KFACCOUNT_DEL_URL = "https://api.weixin.qq.com/customservice/kfaccount/del?kf_account=%s";
27+
private static final String KFSESSION_CREATE_URL = "https://api.weixin.qq.com/customservice/kfsession/create";
28+
private static final String KFSESSION_CLOSE_URL = "https://api.weixin.qq.com/customservice/kfsession/close";
29+
private static final String KFSESSION_GET_URL = "https://api.weixin.qq.com/customservice/kfsession/getsession?openid=%s";
30+
private static final String KFSESSION_LIST_URL = "https://api.weixin.qq.com/customservice/kfsession/getsessionlist?kf_account=%s";
31+
32+
private final WxMaService service;
33+
34+
@Override
35+
public WxMaKfList kfList() throws WxErrorException {
36+
String responseContent = this.service.get(KFLIST_GET_URL, null);
37+
return WxMaKfList.fromJson(responseContent);
38+
}
39+
40+
@Override
41+
public boolean kfAccountAdd(WxMaKfAccountRequest request) throws WxErrorException {
42+
String responseContent = this.service.post(KFACCOUNT_ADD_URL, request.toJson());
43+
return responseContent != null;
44+
}
45+
46+
@Override
47+
public boolean kfAccountUpdate(WxMaKfAccountRequest request) throws WxErrorException {
48+
String responseContent = this.service.post(KFACCOUNT_UPDATE_URL, request.toJson());
49+
return responseContent != null;
50+
}
51+
52+
@Override
53+
public boolean kfAccountDel(String kfAccount) throws WxErrorException {
54+
String url = String.format(KFACCOUNT_DEL_URL, kfAccount);
55+
String responseContent = this.service.get(url, null);
56+
return responseContent != null;
57+
}
58+
59+
@Override
60+
public boolean kfSessionCreate(String openid, String kfAccount) throws WxErrorException {
61+
WxMaKfSessionRequest request = WxMaKfSessionRequest.builder()
62+
.kfAccount(kfAccount)
63+
.openid(openid)
64+
.build();
65+
String responseContent = this.service.post(KFSESSION_CREATE_URL, request.toJson());
66+
return responseContent != null;
67+
}
68+
69+
@Override
70+
public boolean kfSessionClose(String openid, String kfAccount) throws WxErrorException {
71+
WxMaKfSessionRequest request = WxMaKfSessionRequest.builder()
72+
.kfAccount(kfAccount)
73+
.openid(openid)
74+
.build();
75+
String responseContent = this.service.post(KFSESSION_CLOSE_URL, request.toJson());
76+
return responseContent != null;
77+
}
78+
79+
@Override
80+
public WxMaKfSession kfSessionGet(String openid) throws WxErrorException {
81+
String url = String.format(KFSESSION_GET_URL, openid);
82+
String responseContent = this.service.get(url, null);
83+
return WxMaKfSession.fromJson(responseContent);
84+
}
85+
86+
@Override
87+
public WxMaKfSessionList kfSessionList(String kfAccount) throws WxErrorException {
88+
String url = String.format(KFSESSION_LIST_URL, kfAccount);
89+
String responseContent = this.service.get(url, null);
90+
return WxMaKfSessionList.fromJson(responseContent);
91+
}
92+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package cn.binarywang.wx.miniapp.bean.kefu;
2+
3+
import cn.binarywang.wx.miniapp.json.WxMaGsonBuilder;
4+
import com.google.gson.annotations.SerializedName;
5+
import lombok.AllArgsConstructor;
6+
import lombok.Builder;
7+
import lombok.Data;
8+
import lombok.NoArgsConstructor;
9+
10+
import java.io.Serializable;
11+
12+
/**
13+
* 小程序客服信息.
14+
*
15+
* @author <a href="https://github.com/binarywang">Binary Wang</a>
16+
*/
17+
@Data
18+
@Builder
19+
@NoArgsConstructor
20+
@AllArgsConstructor
21+
public class WxMaKfInfo implements Serializable {
22+
private static final long serialVersionUID = -7916302137791763175L;
23+
24+
/**
25+
* 客服账号.
26+
*/
27+
@SerializedName("kf_account")
28+
private String kfAccount;
29+
30+
/**
31+
* 客服昵称.
32+
*/
33+
@SerializedName("kf_nick")
34+
private String kfNick;
35+
36+
/**
37+
* 客服密码.
38+
*/
39+
@SerializedName("kf_id")
40+
private String kfId;
41+
42+
/**
43+
* 客服头像.
44+
*/
45+
@SerializedName("kf_headimgurl")
46+
private String kfHeadImgUrl;
47+
48+
/**
49+
* 如果客服帐号已绑定了客服人员微信号,则此处显示微信号.
50+
*/
51+
@SerializedName("kf_wx")
52+
private String kfWx;
53+
54+
/**
55+
* 如果客服帐号尚未绑定微信号,但是已经发起了一个绑定邀请,则此处显示绑定邀请的微信号.
56+
*/
57+
@SerializedName("invite_wx")
58+
private String inviteWx;
59+
60+
/**
61+
* 邀请的状态,有等待确认"waiting",被拒绝"rejected",过期"expired".
62+
*/
63+
@SerializedName("invite_expire_time")
64+
private Long inviteExpireTime;
65+
66+
/**
67+
* 邀请的过期时间,为unix时间戳.
68+
*/
69+
@SerializedName("invite_status")
70+
private String inviteStatus;
71+
72+
public static WxMaKfInfo fromJson(String json) {
73+
return WxMaGsonBuilder.create().fromJson(json, WxMaKfInfo.class);
74+
}
75+
76+
public String toJson() {
77+
return WxMaGsonBuilder.create().toJson(this);
78+
}
79+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package cn.binarywang.wx.miniapp.bean.kefu;
2+
3+
import cn.binarywang.wx.miniapp.json.WxMaGsonBuilder;
4+
import com.google.gson.annotations.SerializedName;
5+
import lombok.AllArgsConstructor;
6+
import lombok.Builder;
7+
import lombok.Data;
8+
import lombok.NoArgsConstructor;
9+
10+
import java.io.Serializable;
11+
import java.util.List;
12+
13+
/**
14+
* 小程序客服列表.
15+
*
16+
* @author <a href="https://github.com/binarywang">Binary Wang</a>
17+
*/
18+
@Data
19+
@Builder
20+
@NoArgsConstructor
21+
@AllArgsConstructor
22+
public class WxMaKfList implements Serializable {
23+
private static final long serialVersionUID = 6416633293297389972L;
24+
25+
@SerializedName("kf_list")
26+
private List<WxMaKfInfo> kfList;
27+
28+
public static WxMaKfList fromJson(String json) {
29+
return WxMaGsonBuilder.create().fromJson(json, WxMaKfList.class);
30+
}
31+
32+
public String toJson() {
33+
return WxMaGsonBuilder.create().toJson(this);
34+
}
35+
}

0 commit comments

Comments
 (0)