1
0
mirror of synced 2025-12-18 13:58:11 +08:00

🐛 #3265【视频号】视频号线索[获取留资信息详情]接口兼容新版本返回的更多详细字段

This commit is contained in:
杨镇涛
2024-05-09 00:29:04 +08:00
committed by GitHub
parent 3caeecc219
commit a8655b9c4c
5 changed files with 97 additions and 17 deletions

View File

@@ -1,6 +1,11 @@
package me.chanjar.weixin.channel.api.impl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.channel.api.WxLeadComponentService;
@@ -15,6 +20,7 @@ import me.chanjar.weixin.channel.bean.lead.component.response.GetLeadsRequestIdR
import me.chanjar.weixin.channel.bean.lead.component.response.LeadInfoResponse;
import me.chanjar.weixin.channel.util.ResponseUtils;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.commons.lang3.ObjectUtils;
import static me.chanjar.weixin.channel.constant.WxChannelApiUrlConstants.LeadComponent.GET_LEADS_COMPONENT_ID;
import static me.chanjar.weixin.channel.constant.WxChannelApiUrlConstants.LeadComponent.GET_LEADS_COMPONENT_PROMOTE_RECORD;
@@ -33,16 +39,19 @@ public class WxLeadComponentServiceImpl implements WxLeadComponentService {
/** 微信商店服务 */
private final BaseWxChannelServiceImpl shopService;
private final ObjectMapper objectMapper = new ObjectMapper();
@Override
public LeadInfoResponse getLeadsInfoByComponentId(GetLeadInfoByComponentRequest req) throws WxErrorException {
req.setVersion(ObjectUtils.defaultIfNull(req.getVersion(), 1));
String resJson = shopService.post(GET_LEADS_INFO_BY_COMPONENT_ID, req);
return ResponseUtils.decode(resJson, LeadInfoResponse.class);
return this.convertLeadInfoResponse(resJson);
}
@Override
public LeadInfoResponse getLeadsInfoByRequestId(GetLeadsInfoByRequestIdRequest req) throws WxErrorException {
req.setVersion(ObjectUtils.defaultIfNull(req.getVersion(), 1));
String resJson = shopService.post(GET_LEADS_INFO_BY_REQUEST_ID, req);
return ResponseUtils.decode(resJson, LeadInfoResponse.class);
return this.convertLeadInfoResponse(resJson);
}
@Override
@@ -62,4 +71,26 @@ public class WxLeadComponentServiceImpl implements WxLeadComponentService {
String resJson = shopService.post(GET_LEADS_COMPONENT_ID, req);
return ResponseUtils.decode(resJson, GetLeadsComponentIdResponse.class);
}
/**
* 微信返回的数据中, user_data和leads_data均为字符串包裹的非标准JSON结构, 为方便业务使用避免踩坑此处做好解析
*/
private LeadInfoResponse convertLeadInfoResponse(String resJson) throws WxErrorException {
try {
ObjectNode rootNode = (ObjectNode) objectMapper.readTree(resJson);
ArrayNode convertedUserDataArray = objectMapper.createArrayNode();
for (JsonNode userDataEle : rootNode.get("user_data")) {
ObjectNode userDataJsonNode = (ObjectNode) objectMapper.readTree(userDataEle.asText());
ArrayNode leadsDataArray = (ArrayNode) objectMapper.readTree(userDataJsonNode.get("leads_data").asText());
userDataJsonNode.set("leads_data", leadsDataArray);
convertedUserDataArray.add(userDataJsonNode);
}
rootNode.set("user_data", convertedUserDataArray);
String json = objectMapper.writeValueAsString(rootNode);
return ResponseUtils.decode(json, LeadInfoResponse.class);
} catch (JsonProcessingException e) {
throw new WxErrorException(e);
}
}
}

View File

@@ -43,9 +43,10 @@ public class GetLeadInfoByComponentRequest {
private String lastBuffer;
/**
* 接口版本号
* 接口版本号,默认=1
* =null和=1,微信返回的结构不一样,=1信息更全
*/
@JsonProperty("version")
private int version;
private Integer version;
}

View File

@@ -31,9 +31,10 @@ public class GetLeadsInfoByRequestIdRequest {
private String lastBuffer;
/**
* 接口版本号
* 接口版本号,默认=1
* =null和=1,微信返回的结构不一样,=1信息更全
*/
@JsonProperty("version")
private int version;
private Integer version;
}

View File

@@ -44,10 +44,47 @@ public class LeadInfoResponse extends WxChannelBaseResponse {
@NoArgsConstructor
public static class UserData {
/**
* 主播昵称
*/
@JsonProperty("anchor_nickname")
private String anchorNickname;
/**
* 直播开始时间
*/
@JsonProperty("live_start_time")
private Long liveStartTime;
/**
* 用户留资信息列表
*/
@JsonProperty("leads_data")
private List<LeadsData> leadsData;
/**
* 用户留资时间
*/
@JsonProperty("time")
private Long time;
}
@Data
@NoArgsConstructor
public static class LeadsData {
/**
* 表单名称
*/
@JsonProperty("title")
private String title;
/**
* 手机号,文本框,单选框时, 均为字符串
* 仅当title=城市 时, 微信返回字符串数组, eg: ["北京市","北京市","东城区"]
*/
@JsonProperty("value")
private String value;
private Object value;
}
}

View File

@@ -1,5 +1,7 @@
package me.chanjar.weixin.channel.api.impl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.inject.Inject;
import me.chanjar.weixin.channel.api.WxChannelService;
import me.chanjar.weixin.channel.bean.lead.component.request.GetLeadInfoByComponentRequest;
@@ -28,19 +30,24 @@ import static org.testng.Assert.assertTrue;
@Guice(modules = ApiTestModule.class)
public class WxLeadComponentServiceImplTest {
private static final String LEADS_COMPONENT_ID = "123";
private static final String REQUEST_ID = "123";
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
@Inject
private WxChannelService channelService;
@Test
public void testGetLeadsInfoByComponentId() throws WxErrorException {
public void testGetLeadsInfoByComponentId() throws WxErrorException, JsonProcessingException {
String lastBuffer = null;
for (; ; ) {
GetLeadInfoByComponentRequest req = new GetLeadInfoByComponentRequest();
req.setStartTime(Instant.now().minus(1, ChronoUnit.DAYS).getEpochSecond());
req.setEndTime(Instant.now().getEpochSecond());
req.setLeadsComponentId("123");
req.setLeadsComponentId(LEADS_COMPONENT_ID);
req.setLastBuffer(lastBuffer);
req.setVersion(1);
LeadInfoResponse response = channelService.getLeadComponentService().getLeadsInfoByComponentId(req);
System.out.println(OBJECT_MAPPER.writeValueAsString(response));
assertNotNull(response);
assertTrue(response.isSuccess());
lastBuffer = response.getLastBuffer();
@@ -51,13 +58,14 @@ public class WxLeadComponentServiceImplTest {
}
@Test
public void testGetLeadsInfoByRequestId() throws WxErrorException {
public void testGetLeadsInfoByRequestId() throws WxErrorException, JsonProcessingException {
String lastBuffer = null;
for (; ; ) {
GetLeadsInfoByRequestIdRequest req = new GetLeadsInfoByRequestIdRequest();
req.setLastBuffer(lastBuffer);
req.setRequestId("123");
req.setRequestId(REQUEST_ID);
LeadInfoResponse response = channelService.getLeadComponentService().getLeadsInfoByRequestId(req);
System.out.println(OBJECT_MAPPER.writeValueAsString(response));
assertNotNull(response);
assertTrue(response.isSuccess());
lastBuffer = response.getLastBuffer();
@@ -68,13 +76,14 @@ public class WxLeadComponentServiceImplTest {
}
@Test
public void testGetLeadsRequestId() throws WxErrorException {
public void testGetLeadsRequestId() throws WxErrorException, JsonProcessingException {
String lastBuffer = null;
for (; ; ) {
GetLeadsRequestIdRequest req = new GetLeadsRequestIdRequest();
req.setLastBuffer(lastBuffer);
req.setLeadsComponentId("123");
req.setLeadsComponentId(LEADS_COMPONENT_ID);
GetLeadsRequestIdResponse response = channelService.getLeadComponentService().getLeadsRequestId(req);
System.out.println(OBJECT_MAPPER.writeValueAsString(response));
assertNotNull(response);
assertTrue(response.isSuccess());
lastBuffer = response.getLastBuffer();
@@ -85,15 +94,16 @@ public class WxLeadComponentServiceImplTest {
}
@Test
public void testGetLeadsComponentPromoteRecord() throws WxErrorException {
public void testGetLeadsComponentPromoteRecord() throws WxErrorException, JsonProcessingException {
String lastBuffer = null;
for (; ; ) {
GetLeadsComponentPromoteRecordRequest req = new GetLeadsComponentPromoteRecordRequest();
req.setStartTime(Instant.now().minus(1, ChronoUnit.DAYS).getEpochSecond());
req.setEndTime(Instant.now().getEpochSecond());
req.setLeadsComponentId("123");
req.setLeadsComponentId(LEADS_COMPONENT_ID);
req.setLastBuffer(lastBuffer);
GetLeadsComponentPromoteRecordResponse response = channelService.getLeadComponentService().getLeadsComponentPromoteRecord(req);
System.out.println(OBJECT_MAPPER.writeValueAsString(response));
assertNotNull(response);
assertTrue(response.isSuccess());
lastBuffer = response.getLastBuffer();
@@ -104,13 +114,13 @@ public class WxLeadComponentServiceImplTest {
}
@Test
public void testGetLeadsComponentId() throws WxErrorException {
public void testGetLeadsComponentId() throws WxErrorException, JsonProcessingException {
String lastBuffer = null;
for (; ; ) {
GetLeadsComponentIdRequest req = new GetLeadsComponentIdRequest();
req.setLastBuffer(lastBuffer);
GetLeadsComponentIdResponse response = channelService.getLeadComponentService().getLeadsComponentId(req);
System.out.println(response);
System.out.println(OBJECT_MAPPER.writeValueAsString(response));
assertNotNull(response);
assertTrue(response.isSuccess());
lastBuffer = response.getLastBuffer();