Skip to content

Commit 42e92ae

Browse files
committed
🐛 binarywang#3265【视频号】视频号线索[获取留资信息详情]接口,兼容新版本返回的更多详细字段
1 parent 47c55ef commit 42e92ae

File tree

5 files changed

+97
-17
lines changed

5 files changed

+97
-17
lines changed

Diff for: weixin-java-channel/src/main/java/me/chanjar/weixin/channel/api/impl/WxLeadComponentServiceImpl.java

+33-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package me.chanjar.weixin.channel.api.impl;
22

33

4+
import com.fasterxml.jackson.core.JsonProcessingException;
5+
import com.fasterxml.jackson.databind.JsonNode;
6+
import com.fasterxml.jackson.databind.ObjectMapper;
7+
import com.fasterxml.jackson.databind.node.ArrayNode;
8+
import com.fasterxml.jackson.databind.node.ObjectNode;
49
import lombok.RequiredArgsConstructor;
510
import lombok.extern.slf4j.Slf4j;
611
import me.chanjar.weixin.channel.api.WxLeadComponentService;
@@ -15,6 +20,7 @@
1520
import me.chanjar.weixin.channel.bean.lead.component.response.LeadInfoResponse;
1621
import me.chanjar.weixin.channel.util.ResponseUtils;
1722
import me.chanjar.weixin.common.error.WxErrorException;
23+
import org.apache.commons.lang3.ObjectUtils;
1824

1925
import static me.chanjar.weixin.channel.constant.WxChannelApiUrlConstants.LeadComponent.GET_LEADS_COMPONENT_ID;
2026
import static me.chanjar.weixin.channel.constant.WxChannelApiUrlConstants.LeadComponent.GET_LEADS_COMPONENT_PROMOTE_RECORD;
@@ -33,16 +39,19 @@ public class WxLeadComponentServiceImpl implements WxLeadComponentService {
3339

3440
/** 微信商店服务 */
3541
private final BaseWxChannelServiceImpl shopService;
42+
private final ObjectMapper objectMapper = new ObjectMapper();
3643
@Override
3744
public LeadInfoResponse getLeadsInfoByComponentId(GetLeadInfoByComponentRequest req) throws WxErrorException {
45+
req.setVersion(ObjectUtils.defaultIfNull(req.getVersion(), 1));
3846
String resJson = shopService.post(GET_LEADS_INFO_BY_COMPONENT_ID, req);
39-
return ResponseUtils.decode(resJson, LeadInfoResponse.class);
47+
return this.convertLeadInfoResponse(resJson);
4048
}
4149

4250
@Override
4351
public LeadInfoResponse getLeadsInfoByRequestId(GetLeadsInfoByRequestIdRequest req) throws WxErrorException {
52+
req.setVersion(ObjectUtils.defaultIfNull(req.getVersion(), 1));
4453
String resJson = shopService.post(GET_LEADS_INFO_BY_REQUEST_ID, req);
45-
return ResponseUtils.decode(resJson, LeadInfoResponse.class);
54+
return this.convertLeadInfoResponse(resJson);
4655
}
4756

4857
@Override
@@ -62,4 +71,26 @@ public GetLeadsComponentIdResponse getLeadsComponentId(GetLeadsComponentIdReques
6271
String resJson = shopService.post(GET_LEADS_COMPONENT_ID, req);
6372
return ResponseUtils.decode(resJson, GetLeadsComponentIdResponse.class);
6473
}
74+
75+
/**
76+
* 微信返回的数据中, user_data和leads_data均为字符串包裹的非标准JSON结构, 为方便业务使用避免踩坑此处做好解析
77+
*/
78+
private LeadInfoResponse convertLeadInfoResponse(String resJson) throws WxErrorException {
79+
try {
80+
ObjectNode rootNode = (ObjectNode) objectMapper.readTree(resJson);
81+
ArrayNode convertedUserDataArray = objectMapper.createArrayNode();
82+
for (JsonNode userDataEle : rootNode.get("user_data")) {
83+
ObjectNode userDataJsonNode = (ObjectNode) objectMapper.readTree(userDataEle.asText());
84+
ArrayNode leadsDataArray = (ArrayNode) objectMapper.readTree(userDataJsonNode.get("leads_data").asText());
85+
userDataJsonNode.set("leads_data", leadsDataArray);
86+
convertedUserDataArray.add(userDataJsonNode);
87+
}
88+
rootNode.set("user_data", convertedUserDataArray);
89+
String json = objectMapper.writeValueAsString(rootNode);
90+
return ResponseUtils.decode(json, LeadInfoResponse.class);
91+
} catch (JsonProcessingException e) {
92+
throw new WxErrorException(e);
93+
}
94+
}
95+
6596
}

Diff for: weixin-java-channel/src/main/java/me/chanjar/weixin/channel/bean/lead/component/request/GetLeadInfoByComponentRequest.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ public class GetLeadInfoByComponentRequest {
4343
private String lastBuffer;
4444

4545
/**
46-
* 接口版本号
46+
* 接口版本号,默认=1
47+
* =null和=1,微信返回的结构不一样,=1信息更全
4748
*/
4849
@JsonProperty("version")
49-
private int version;
50+
private Integer version;
5051

5152
}

Diff for: weixin-java-channel/src/main/java/me/chanjar/weixin/channel/bean/lead/component/request/GetLeadsInfoByRequestIdRequest.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ public class GetLeadsInfoByRequestIdRequest {
3131
private String lastBuffer;
3232

3333
/**
34-
* 接口版本号
34+
* 接口版本号,默认=1
35+
* =null和=1,微信返回的结构不一样,=1信息更全
3536
*/
3637
@JsonProperty("version")
37-
private int version;
38+
private Integer version;
3839

3940
}

Diff for: weixin-java-channel/src/main/java/me/chanjar/weixin/channel/bean/lead/component/response/LeadInfoResponse.java

+38-1
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,47 @@ public class LeadInfoResponse extends WxChannelBaseResponse {
4444
@NoArgsConstructor
4545
public static class UserData {
4646

47+
/**
48+
* 主播昵称
49+
*/
50+
@JsonProperty("anchor_nickname")
51+
private String anchorNickname;
52+
53+
/**
54+
* 直播开始时间
55+
*/
56+
@JsonProperty("live_start_time")
57+
private Long liveStartTime;
58+
59+
/**
60+
* 用户留资信息列表
61+
*/
62+
@JsonProperty("leads_data")
63+
private List<LeadsData> leadsData;
64+
65+
/**
66+
* 用户留资时间
67+
*/
68+
@JsonProperty("time")
69+
private Long time;
70+
71+
}
72+
73+
@Data
74+
@NoArgsConstructor
75+
public static class LeadsData {
76+
77+
/**
78+
* 表单名称
79+
*/
4780
@JsonProperty("title")
4881
private String title;
4982

83+
/**
84+
* 手机号,文本框,单选框时, 均为字符串
85+
* 仅当title=城市 时, 微信返回字符串数组, eg: ["北京市","北京市","东城区"]
86+
*/
5087
@JsonProperty("value")
51-
private String value;
88+
private Object value;
5289
}
5390
}

Diff for: weixin-java-channel/src/test/java/me/chanjar/weixin/channel/api/impl/WxLeadComponentServiceImplTest.java

+20-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package me.chanjar.weixin.channel.api.impl;
22

3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
35
import com.google.inject.Inject;
46
import me.chanjar.weixin.channel.api.WxChannelService;
57
import me.chanjar.weixin.channel.bean.lead.component.request.GetLeadInfoByComponentRequest;
@@ -28,19 +30,24 @@
2830
@Guice(modules = ApiTestModule.class)
2931
public class WxLeadComponentServiceImplTest {
3032

33+
private static final String LEADS_COMPONENT_ID = "123";
34+
private static final String REQUEST_ID = "123";
35+
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
3136
@Inject
3237
private WxChannelService channelService;
3338

3439
@Test
35-
public void testGetLeadsInfoByComponentId() throws WxErrorException {
40+
public void testGetLeadsInfoByComponentId() throws WxErrorException, JsonProcessingException {
3641
String lastBuffer = null;
3742
for (; ; ) {
3843
GetLeadInfoByComponentRequest req = new GetLeadInfoByComponentRequest();
3944
req.setStartTime(Instant.now().minus(1, ChronoUnit.DAYS).getEpochSecond());
4045
req.setEndTime(Instant.now().getEpochSecond());
41-
req.setLeadsComponentId("123");
46+
req.setLeadsComponentId(LEADS_COMPONENT_ID);
4247
req.setLastBuffer(lastBuffer);
48+
req.setVersion(1);
4349
LeadInfoResponse response = channelService.getLeadComponentService().getLeadsInfoByComponentId(req);
50+
System.out.println(OBJECT_MAPPER.writeValueAsString(response));
4451
assertNotNull(response);
4552
assertTrue(response.isSuccess());
4653
lastBuffer = response.getLastBuffer();
@@ -51,13 +58,14 @@ public void testGetLeadsInfoByComponentId() throws WxErrorException {
5158
}
5259

5360
@Test
54-
public void testGetLeadsInfoByRequestId() throws WxErrorException {
61+
public void testGetLeadsInfoByRequestId() throws WxErrorException, JsonProcessingException {
5562
String lastBuffer = null;
5663
for (; ; ) {
5764
GetLeadsInfoByRequestIdRequest req = new GetLeadsInfoByRequestIdRequest();
5865
req.setLastBuffer(lastBuffer);
59-
req.setRequestId("123");
66+
req.setRequestId(REQUEST_ID);
6067
LeadInfoResponse response = channelService.getLeadComponentService().getLeadsInfoByRequestId(req);
68+
System.out.println(OBJECT_MAPPER.writeValueAsString(response));
6169
assertNotNull(response);
6270
assertTrue(response.isSuccess());
6371
lastBuffer = response.getLastBuffer();
@@ -68,13 +76,14 @@ public void testGetLeadsInfoByRequestId() throws WxErrorException {
6876
}
6977

7078
@Test
71-
public void testGetLeadsRequestId() throws WxErrorException {
79+
public void testGetLeadsRequestId() throws WxErrorException, JsonProcessingException {
7280
String lastBuffer = null;
7381
for (; ; ) {
7482
GetLeadsRequestIdRequest req = new GetLeadsRequestIdRequest();
7583
req.setLastBuffer(lastBuffer);
76-
req.setLeadsComponentId("123");
84+
req.setLeadsComponentId(LEADS_COMPONENT_ID);
7785
GetLeadsRequestIdResponse response = channelService.getLeadComponentService().getLeadsRequestId(req);
86+
System.out.println(OBJECT_MAPPER.writeValueAsString(response));
7887
assertNotNull(response);
7988
assertTrue(response.isSuccess());
8089
lastBuffer = response.getLastBuffer();
@@ -85,15 +94,16 @@ public void testGetLeadsRequestId() throws WxErrorException {
8594
}
8695

8796
@Test
88-
public void testGetLeadsComponentPromoteRecord() throws WxErrorException {
97+
public void testGetLeadsComponentPromoteRecord() throws WxErrorException, JsonProcessingException {
8998
String lastBuffer = null;
9099
for (; ; ) {
91100
GetLeadsComponentPromoteRecordRequest req = new GetLeadsComponentPromoteRecordRequest();
92101
req.setStartTime(Instant.now().minus(1, ChronoUnit.DAYS).getEpochSecond());
93102
req.setEndTime(Instant.now().getEpochSecond());
94-
req.setLeadsComponentId("123");
103+
req.setLeadsComponentId(LEADS_COMPONENT_ID);
95104
req.setLastBuffer(lastBuffer);
96105
GetLeadsComponentPromoteRecordResponse response = channelService.getLeadComponentService().getLeadsComponentPromoteRecord(req);
106+
System.out.println(OBJECT_MAPPER.writeValueAsString(response));
97107
assertNotNull(response);
98108
assertTrue(response.isSuccess());
99109
lastBuffer = response.getLastBuffer();
@@ -104,13 +114,13 @@ public void testGetLeadsComponentPromoteRecord() throws WxErrorException {
104114
}
105115

106116
@Test
107-
public void testGetLeadsComponentId() throws WxErrorException {
117+
public void testGetLeadsComponentId() throws WxErrorException, JsonProcessingException {
108118
String lastBuffer = null;
109119
for (; ; ) {
110120
GetLeadsComponentIdRequest req = new GetLeadsComponentIdRequest();
111121
req.setLastBuffer(lastBuffer);
112122
GetLeadsComponentIdResponse response = channelService.getLeadComponentService().getLeadsComponentId(req);
113-
System.out.println(response);
123+
System.out.println(OBJECT_MAPPER.writeValueAsString(response));
114124
assertNotNull(response);
115125
assertTrue(response.isSuccess());
116126
lastBuffer = response.getLastBuffer();

0 commit comments

Comments
 (0)