#978 增加微信营销相关接口

This commit is contained in:
007gzs
2019-03-15 17:09:53 +08:00
committed by Binary Wang
parent b33e1708c0
commit be33986ecd
12 changed files with 514 additions and 20 deletions

View File

@@ -0,0 +1,71 @@
package me.chanjar.weixin.mp.api;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.bean.marketing.WxMpAdLeadFilter;
import me.chanjar.weixin.mp.bean.marketing.WxMpAdLeadResult;
import me.chanjar.weixin.mp.bean.marketing.WxMpUserAction;
import me.chanjar.weixin.mp.bean.marketing.WxMpUserActionSet;
import java.io.IOException;
import java.util.Date;
import java.util.List;
/**
* <pre>
* 微信营销接口
* </pre>
*
* @author <a href="https://github.com/007gzs">007</a>
*/
public interface WxMpMarketingService {
String API_URL_PREFIX = "https://api.weixin.qq.com/marketing/";
/**
* <pre>
* 创建数据源
* 接口调用请求说明
* https://wximg.qq.com/wxp/pdftool/get.html?id=rkalQXDBM&pa=39
* </pre>
*
* @param type 用户行为源类型
* @param name 用户行为源名称 必填
* @param description 用户行为源描述,字段长度最小 1 字节,长度最大 128 字节
*/
long addUserActionSets(String type, String name, String description) throws WxErrorException;
/**
* <pre>
* 获取数据源信息
* </pre>
*
* @param userActionSetId 数据源唯一ID
*/
List<WxMpUserActionSet> getUserActionSets(Long userActionSetId) throws WxErrorException;
/**
* 回传数据
* 接口调用请求说明
* https://wximg.qq.com/wxp/pdftool/get.html?id=rkalQXDBM&pa=39
*
* @param actions 用户行为源类型
*/
void addUserAction(List<WxMpUserAction> actions) throws WxErrorException;
/**
* <pre>
* 获取朋友圈销售线索数据接口
* 接口调用请求说明
*
* http请求方式: POST
* http://api.weixin.qq.com/cgi-bin/media/voice/translatecontent?access_token=ACCESS_TOKEN&lfrom=xxx&lto=xxx
*
* </pre>
*
* @param beginDate 开始日期
* @param endDate 结束日期
* @param filtering 过滤条件
* @param page 页码,获取指定页数据
* @param page_size 一页获取的数据条数(1-100)
*/
WxMpAdLeadResult getAdLeads(Date beginDate, Date endDate, List<WxMpAdLeadFilter> filtering, Integer page, Integer page_size) throws WxErrorException, IOException;
}

View File

@@ -411,6 +411,13 @@ public interface WxMpService {
*/
WxMpMemberCardService getMemberCardService();
/**
* 返回营销相关接口方法的实现类对象,以方便调用其各个接口.
*
* @return WxMpMarketingService
*/
WxMpMarketingService getMarketingService();
/**
* 初始化http请求对象.
*/
@@ -473,4 +480,6 @@ public interface WxMpService {
void setMassMessageService(WxMpMassMessageService massMessageService);
void setAiOpenService(WxMpAiOpenService aiOpenService);
void setMarketingService(WxMpMarketingService marketingService);
}

View File

@@ -3,6 +3,7 @@ package me.chanjar.weixin.mp.api.impl;
import java.io.IOException;
import java.util.concurrent.locks.Lock;
import me.chanjar.weixin.mp.api.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -24,26 +25,6 @@ import me.chanjar.weixin.common.util.http.RequestHttp;
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
import me.chanjar.weixin.common.util.http.URIUtil;
import me.chanjar.weixin.mp.api.WxMpAiOpenService;
import me.chanjar.weixin.mp.api.WxMpCardService;
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpDataCubeService;
import me.chanjar.weixin.mp.api.WxMpDeviceService;
import me.chanjar.weixin.mp.api.WxMpKefuService;
import me.chanjar.weixin.mp.api.WxMpMassMessageService;
import me.chanjar.weixin.mp.api.WxMpMaterialService;
import me.chanjar.weixin.mp.api.WxMpMemberCardService;
import me.chanjar.weixin.mp.api.WxMpMenuService;
import me.chanjar.weixin.mp.api.WxMpQrcodeService;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.WxMpShakeService;
import me.chanjar.weixin.mp.api.WxMpStoreService;
import me.chanjar.weixin.mp.api.WxMpSubscribeMsgService;
import me.chanjar.weixin.mp.api.WxMpTemplateMsgService;
import me.chanjar.weixin.mp.api.WxMpUserBlacklistService;
import me.chanjar.weixin.mp.api.WxMpUserService;
import me.chanjar.weixin.mp.api.WxMpUserTagService;
import me.chanjar.weixin.mp.api.WxMpWifiService;
import me.chanjar.weixin.mp.bean.WxMpSemanticQuery;
import me.chanjar.weixin.mp.bean.result.WxMpCurrentAutoReplyInfo;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
@@ -81,6 +62,7 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH
private WxMpMassMessageService massMessageService = new WxMpMassMessageServiceImpl(this);
private WxMpAiOpenService aiOpenService = new WxMpAiOpenServiceImpl(this);
private WxMpWifiService wifiService = new WxMpWifiServiceImpl(this);
private WxMpMarketingService marketingService = new WxMpMarketingServiceImpl(this);
private int retrySleepMillis = 1000;
private int maxRetryTimes = 5;
@@ -545,4 +527,14 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH
public WxMpWifiService getWifiService() {
return this.wifiService;
}
@Override
public WxMpMarketingService getMarketingService() {
return this.marketingService;
}
@Override
public void setMarketingService(WxMpMarketingService marketingService) {
this.marketingService = marketingService;
}
}

View File

@@ -0,0 +1,92 @@
package me.chanjar.weixin.mp.api.impl;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpMarketingService;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.marketing.WxMpAdLeadFilter;
import me.chanjar.weixin.mp.bean.marketing.WxMpAdLeadResult;
import me.chanjar.weixin.mp.bean.marketing.WxMpUserAction;
import me.chanjar.weixin.mp.bean.marketing.WxMpUserActionSet;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.List;
/**
* @author <a href="https://github.com/007gzs">007</a>
*/
public class WxMpMarketingServiceImpl implements WxMpMarketingService {
protected final Logger log = LoggerFactory.getLogger(this.getClass());
private WxMpService wxMpService;
public WxMpMarketingServiceImpl(WxMpService wxMpService) {
this.wxMpService = wxMpService;
}
@Override
public long addUserActionSets(String type, String name, String description) throws WxErrorException {
String url = API_URL_PREFIX + "user_action_sets/add?version=v1.0";
JsonObject json = new JsonObject();
json.addProperty("type", type);
json.addProperty("name", name);
json.addProperty("description", description);
String responseContent = wxMpService.post(url, json.toString());
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
return tmpJsonElement.getAsJsonObject().get("data").getAsJsonObject().get("user_action_set_id").getAsLong();
}
@Override
public List<WxMpUserActionSet> getUserActionSets(Long userActionSetId) throws WxErrorException {
String url = API_URL_PREFIX + "user_action_sets/get";
String responseContent = wxMpService.get(url, "version=v1.0&user_action_set_id=" + userActionSetId);
return WxMpUserActionSet.fromJson(responseContent);
}
@Override
public void addUserAction(List<WxMpUserAction> actions) throws WxErrorException {
String url = API_URL_PREFIX + "user_actions/add?version=v1.0";
JsonArray json = new JsonArray();
for (WxMpUserAction action : actions) {
json.add(action.toJsonObject());
}
wxMpService.post(url, json.toString());
}
@Override
public WxMpAdLeadResult getAdLeads(Date beginDate, Date endDate, List<WxMpAdLeadFilter> filtering, Integer page, Integer page_size) throws WxErrorException, IOException {
Date today = new Date();
if (beginDate == null) {
beginDate = today;
}
if (endDate == null) {
endDate = today;
}
String url = API_URL_PREFIX + "wechat_ad_leads/get";
String params = "version=v1.0";
JsonObject dateRange = new JsonObject();
dateRange.addProperty("begin_date", DateFormatUtils.format(beginDate, "yyyy-MM-dd"));
dateRange.addProperty("end_date", DateFormatUtils.format(endDate, "yyyy-MM-dd"));
params += "&date_range=" + URLEncoder.encode(dateRange.toString(), StandardCharsets.UTF_8.name());
params += "&page=" + page;
params += "&page_size=" + page_size;
if (filtering != null) {
JsonArray filterJson = new JsonArray();
for (WxMpAdLeadFilter filter : filtering) {
filterJson.add(filter.toJsonObject());
}
params += "&filtering=" + URLEncoder.encode(filterJson.toString(), StandardCharsets.UTF_8.name());
;
}
String responseContent = wxMpService.get(url, params);
return WxMpAdLeadResult.fromJson(responseContent);
}
}