Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

weixin-java-miniapp微信小程序广告接入 #2473

Merged
merged 1 commit into from
Dec 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package cn.binarywang.wx.miniapp.api;

import cn.binarywang.wx.miniapp.bean.marketing.WxMaUserAction;
import me.chanjar.weixin.common.error.WxErrorException;
import java.util.List;

/**
*
* @Description :微信营销接口
* @author <a href="https://github.com/184759547">184759547</a>
* @since : 2021/12/28
*/
public interface WxMaMarketingService {
/**
* <pre>
* 创建数据源.
* 接口调用请求说明
* https://ad.weixin.qq.com/guide/457
* </pre>
*
* @param type 用户行为源类型
* @param name 用户行为源名称 必填
* @param description 用户行为源描述,字段长度最小 1 字节,长度最大 128 字节
*/
long addUserActionSets(String type, String name, String description) throws WxErrorException;

/**
* 回传数据.
* 接口调用请求说明
* https://ad.weixin.qq.com/guide/457
*
* @param actions 用户行为源类型
*/
String addUserAction(List<WxMaUserAction> actions, Long userActionSetId) throws WxErrorException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -469,4 +469,11 @@ public interface WxMaService extends WxService {
* @return WxMaDeviceSubscribeService plugin service
*/
WxMaDeviceSubscribeService getDeviceSubscribeService();

/**
* 返回小程序广告接入相关接口服务对象
*
* @return WxMaDeviceSubscribeService plugin service
*/
WxMaMarketingService getMarketingService();
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public abstract class BaseWxMaServiceImpl<H, P> implements WxMaService, RequestH
private final WxMaLinkService linkService = new WxMaLinkServiceImpl(this);
private final WxMaReimburseInvoiceService reimburseInvoiceService = new WxMaReimburseInvoiceServiceImpl(this);
private final WxMaDeviceSubscribeService deviceSubscribeService = new WxMaDeviceSubscribeServiceImpl(this);
private final WxMaMarketingService marketingService = new WxMaMarketingServiceImpl(this);
private Map<String, WxMaConfig> configMap;
private int retrySleepMillis = 1000;
private int maxRetryTimes = 5;
Expand Down Expand Up @@ -577,4 +578,7 @@ public WxMaReimburseInvoiceService getReimburseInvoiceService() {

@Override
public WxMaDeviceSubscribeService getDeviceSubscribeService(){ return this.deviceSubscribeService; }

@Override
public WxMaMarketingService getMarketingService() {return this.marketingService; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package cn.binarywang.wx.miniapp.api.impl;

import cn.binarywang.wx.miniapp.api.WxMaMarketingService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.marketing.WxMaUserAction;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.json.GsonParser;

import java.util.List;

/**
* @author <a href="https://github.com/184759547">184759547</a>
* @Description :微信营销接口
* @since : 2021/12/28
*/
@Slf4j
@RequiredArgsConstructor
public class WxMaMarketingServiceImpl implements WxMaMarketingService {
private final WxMaService wxMaService;
private final String USER_ACTION_SETS_ADD = "https://api.weixin.qq.com/marketing/user_action_sets/add?version=v1.0";
private final String USER_ACTIONS_ADD = "https://api.weixin.qq.com/marketing/user_actions/add?version=v1.0";

@Override
public long addUserActionSets(String type, String name, String description) throws WxErrorException {
JsonObject json = new JsonObject();
json.addProperty("type", type);
json.addProperty("name", name);
json.addProperty("description", description);
String responseContent = wxMaService.post(USER_ACTION_SETS_ADD, json.toString());
JsonObject tmpJson = GsonParser.parse(responseContent);
return tmpJson.get("data").getAsJsonObject().get("user_action_set_id").getAsLong();
}

@Override
public String addUserAction(List<WxMaUserAction> actions, Long userActionSetId) throws WxErrorException {
return wxMaService.post(USER_ACTIONS_ADD, WxMaUserAction.listToJson(actions, userActionSetId));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package cn.binarywang.wx.miniapp.bean.marketing;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import sun.rmi.runtime.Log;

import java.io.Serializable;
import java.util.List;

/**
* @Description :微信营销接口
* @author <a href="https://github.com/184759547">184759547</a>
* @since : 2021/12/28
*/

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class WxMaUserAction implements Serializable {
private static final long serialVersionUID = 7482378011709983616L;
private String url;
private Integer actionTime;
private String actionType;
private String leadsType;
private String clickId;
private Integer actionParam;

private JsonObject toJsonObject() {
JsonObject json = new JsonObject();
json.addProperty("url", this.url);
json.addProperty("action_time", this.actionTime);
json.addProperty("action_type", this.actionType);

if (this.clickId != null) {
JsonObject traceJson = new JsonObject();
traceJson.addProperty("click_id", this.clickId);
json.add("trace", traceJson);
}

if (this.actionParam != null) {
JsonObject actionParamJson = new JsonObject();
actionParamJson.addProperty("value", actionParam);
if (this.leadsType != null) {
actionParamJson.addProperty("leads_type", leadsType);
}
json.add("action_param", actionParamJson);
}

return json;
}

/**
* list对象转换为json字符串
*
* @param actions .
* @return .
*/
public static String listToJson(List<WxMaUserAction> actions, Long userActionSetId) {
JsonArray array = new JsonArray();
for (WxMaUserAction action : actions) {
array.add(action.toJsonObject());
}

JsonObject result = new JsonObject();
result.addProperty("user_action_set_id", userActionSetId);
result.add("actions", array);
return result.toString();
}
}