Skip to content

Commit f50612b

Browse files
Copilotbinarywang
andcommitted
Complete Mini Program customer service management implementation with tests and documentation
Co-authored-by: binarywang <[email protected]>
1 parent a40a92b commit f50612b

File tree

3 files changed

+224
-0
lines changed

3 files changed

+224
-0
lines changed

MINIAPP_KEFU_SERVICE.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# WeChat Mini Program Customer Service Management
2+
3+
This document describes the new customer service management functionality added to the WxJava Mini Program SDK.
4+
5+
## Overview
6+
7+
Previously, the mini program module only had:
8+
- `WxMaCustomserviceWorkService` - For binding mini programs to enterprise WeChat customer service
9+
- `WxMaMsgService.sendKefuMsg()` - For sending customer service messages
10+
11+
The new `WxMaKefuService` adds comprehensive customer service management capabilities:
12+
13+
## Features
14+
15+
### Customer Service Account Management
16+
- `kfList()` - Get list of customer service accounts
17+
- `kfAccountAdd()` - Add new customer service account
18+
- `kfAccountUpdate()` - Update customer service account
19+
- `kfAccountDel()` - Delete customer service account
20+
21+
### Session Management
22+
- `kfSessionCreate()` - Create customer service session
23+
- `kfSessionClose()` - Close customer service session
24+
- `kfSessionGet()` - Get customer session status
25+
- `kfSessionList()` - Get customer service session list
26+
27+
## Usage Example
28+
29+
```java
30+
// Get the customer service management service
31+
WxMaKefuService kefuService = wxMaService.getKefuService();
32+
33+
// Add a new customer service account
34+
WxMaKfAccountRequest request = WxMaKfAccountRequest.builder()
35+
.kfAccount("service001@example")
36+
.kfNick("Customer Service 001")
37+
.kfPwd("password123")
38+
.build();
39+
boolean result = kefuService.kfAccountAdd(request);
40+
41+
// Create a session between user and customer service
42+
boolean sessionResult = kefuService.kfSessionCreate("user_openid", "service001@example");
43+
44+
// Get customer service list
45+
WxMaKfList kfList = kefuService.kfList();
46+
```
47+
48+
## Bean Classes
49+
50+
### Request Objects
51+
- `WxMaKfAccountRequest` - For customer service account operations
52+
- `WxMaKfSessionRequest` - For session operations
53+
54+
### Response Objects
55+
- `WxMaKfInfo` - Customer service account information
56+
- `WxMaKfList` - List of customer service accounts
57+
- `WxMaKfSession` - Session information
58+
- `WxMaKfSessionList` - List of sessions
59+
60+
## API Endpoints
61+
62+
The service uses the following WeChat Mini Program API endpoints:
63+
- `https://api.weixin.qq.com/cgi-bin/customservice/getkflist` - Get customer service list
64+
- `https://api.weixin.qq.com/customservice/kfaccount/add` - Add customer service account
65+
- `https://api.weixin.qq.com/customservice/kfaccount/update` - Update customer service account
66+
- `https://api.weixin.qq.com/customservice/kfaccount/del` - Delete customer service account
67+
- `https://api.weixin.qq.com/customservice/kfsession/create` - Create session
68+
- `https://api.weixin.qq.com/customservice/kfsession/close` - Close session
69+
- `https://api.weixin.qq.com/customservice/kfsession/getsession` - Get session
70+
- `https://api.weixin.qq.com/customservice/kfsession/getsessionlist` - Get session list
71+
72+
## Integration
73+
74+
The service is automatically available through the main `WxMaService` interface:
75+
76+
```java
77+
WxMaKefuService kefuService = wxMaService.getKefuService();
78+
```
79+
80+
This fills the gap mentioned in the original issue and provides full customer service management capabilities for WeChat Mini Programs.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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.WxMaKfList;
6+
import cn.binarywang.wx.miniapp.bean.kefu.request.WxMaKfAccountRequest;
7+
import me.chanjar.weixin.common.error.WxErrorException;
8+
import org.testng.Assert;
9+
import org.testng.annotations.Test;
10+
11+
import static org.mockito.ArgumentMatchers.*;
12+
import static org.mockito.Mockito.mock;
13+
import static org.mockito.Mockito.when;
14+
15+
/**
16+
* 小程序客服管理服务测试.
17+
*
18+
* @author <a href="https://github.com/binarywang">Binary Wang</a>
19+
*/
20+
public class WxMaKefuServiceImplTest {
21+
22+
@Test
23+
public void testKfList() throws WxErrorException {
24+
WxMaService service = mock(WxMaService.class);
25+
when(service.get(anyString(), any())).thenReturn("{\"kf_list\":[]}");
26+
27+
WxMaKefuService kefuService = new WxMaKefuServiceImpl(service);
28+
WxMaKfList result = kefuService.kfList();
29+
30+
Assert.assertNotNull(result);
31+
Assert.assertNotNull(result.getKfList());
32+
Assert.assertEquals(result.getKfList().size(), 0);
33+
}
34+
35+
@Test
36+
public void testKfAccountAdd() throws WxErrorException {
37+
WxMaService service = mock(WxMaService.class);
38+
when(service.post(anyString(), anyString())).thenReturn("{\"errcode\":0}");
39+
40+
WxMaKefuService kefuService = new WxMaKefuServiceImpl(service);
41+
WxMaKfAccountRequest request = WxMaKfAccountRequest.builder()
42+
.kfAccount("test@kfaccount")
43+
.kfNick("测试客服")
44+
.kfPwd("password")
45+
.build();
46+
47+
boolean result = kefuService.kfAccountAdd(request);
48+
Assert.assertTrue(result);
49+
}
50+
51+
@Test
52+
public void testKfSessionCreate() throws WxErrorException {
53+
WxMaService service = mock(WxMaService.class);
54+
when(service.post(anyString(), anyString())).thenReturn("{\"errcode\":0}");
55+
56+
WxMaKefuService kefuService = new WxMaKefuServiceImpl(service);
57+
boolean result = kefuService.kfSessionCreate("test_openid", "test@kfaccount");
58+
Assert.assertTrue(result);
59+
}
60+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package cn.binarywang.wx.miniapp.demo;
2+
3+
import cn.binarywang.wx.miniapp.api.WxMaService;
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+
* 小程序客服管理功能使用示例.
12+
*
13+
* @author <a href="https://github.com/binarywang">Binary Wang</a>
14+
*/
15+
public class WxMaKefuServiceDemo {
16+
17+
private final WxMaService wxMaService;
18+
19+
public WxMaKefuServiceDemo(WxMaService wxMaService) {
20+
this.wxMaService = wxMaService;
21+
}
22+
23+
/**
24+
* 演示客服账号管理功能
25+
*/
26+
public void demonstrateCustomerServiceManagement() throws WxErrorException {
27+
// 1. 获取客服列表
28+
WxMaKfList kfList = wxMaService.getKefuService().kfList();
29+
System.out.println("当前客服数量: " + kfList.getKfList().size());
30+
31+
// 2. 添加新客服账号
32+
WxMaKfAccountRequest addRequest = WxMaKfAccountRequest.builder()
33+
.kfAccount("service001@example")
34+
.kfNick("客服001")
35+
.kfPwd("password123")
36+
.build();
37+
38+
boolean addResult = wxMaService.getKefuService().kfAccountAdd(addRequest);
39+
System.out.println("添加客服账号结果: " + addResult);
40+
41+
// 3. 更新客服账号信息
42+
WxMaKfAccountRequest updateRequest = WxMaKfAccountRequest.builder()
43+
.kfAccount("service001@example")
44+
.kfNick("高级客服001")
45+
.build();
46+
47+
boolean updateResult = wxMaService.getKefuService().kfAccountUpdate(updateRequest);
48+
System.out.println("更新客服账号结果: " + updateResult);
49+
}
50+
51+
/**
52+
* 演示客服会话管理功能
53+
*/
54+
public void demonstrateSessionManagement() throws WxErrorException {
55+
String userOpenid = "user_openid_example";
56+
String kfAccount = "service001@example";
57+
58+
// 1. 创建客服会话
59+
boolean createResult = wxMaService.getKefuService().kfSessionCreate(userOpenid, kfAccount);
60+
System.out.println("创建会话结果: " + createResult);
61+
62+
// 2. 获取用户会话状态
63+
WxMaKfSession session = wxMaService.getKefuService().kfSessionGet(userOpenid);
64+
System.out.println("用户当前会话客服: " + session.getKfAccount());
65+
66+
// 3. 获取客服的会话列表
67+
WxMaKfSessionList sessionList = wxMaService.getKefuService().kfSessionList(kfAccount);
68+
System.out.println("客服当前会话数量: " + sessionList.getSessionList().size());
69+
70+
// 4. 关闭客服会话
71+
boolean closeResult = wxMaService.getKefuService().kfSessionClose(userOpenid, kfAccount);
72+
System.out.println("关闭会话结果: " + closeResult);
73+
}
74+
75+
/**
76+
* 演示客服账号删除功能
77+
*/
78+
public void demonstrateAccountDeletion() throws WxErrorException {
79+
String kfAccount = "service001@example";
80+
81+
boolean deleteResult = wxMaService.getKefuService().kfAccountDel(kfAccount);
82+
System.out.println("删除客服账号结果: " + deleteResult);
83+
}
84+
}

0 commit comments

Comments
 (0)