Skip to content

Commit

Permalink
🐛 binarywang#3265【视频号】视频号线索[获取留资信息详情]接口,兼容新版本返回的更多详细字段
Browse files Browse the repository at this point in the history
  • Loading branch information
imyzt committed Apr 28, 2024
1 parent 47c55ef commit 42e92ae
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -15,6 +20,7 @@
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;
Expand All @@ -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
Expand All @@ -62,4 +71,26 @@ public GetLeadsComponentIdResponse getLeadsComponentId(GetLeadsComponentIdReques
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);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ public class GetLeadInfoByComponentRequest {
private String lastBuffer;

/**
* 接口版本号
* 接口版本号,默认=1
* =null和=1,微信返回的结构不一样,=1信息更全
*/
@JsonProperty("version")
private int version;
private Integer version;

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ public class GetLeadsInfoByRequestIdRequest {
private String lastBuffer;

/**
* 接口版本号
* 接口版本号,默认=1
* =null和=1,微信返回的结构不一样,=1信息更全
*/
@JsonProperty("version")
private int version;
private Integer version;

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -28,19 +30,24 @@
@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();
Expand All @@ -51,13 +58,14 @@ public void testGetLeadsInfoByComponentId() throws WxErrorException {
}

@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();
Expand All @@ -68,13 +76,14 @@ public void testGetLeadsInfoByRequestId() throws WxErrorException {
}

@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();
Expand All @@ -85,15 +94,16 @@ public void testGetLeadsRequestId() throws WxErrorException {
}

@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();
Expand All @@ -104,13 +114,13 @@ public void testGetLeadsComponentPromoteRecord() throws WxErrorException {
}

@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();
Expand Down

0 comments on commit 42e92ae

Please sign in to comment.