mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-11-01 00:46:54 +08:00 
			
		
		
		
	🆕 #2473 【小程序】接入小程序广告实现,实现创建数据源和回传数据两个接口
Co-authored-by: wywy12300 <gaoyong@chebada.com>
This commit is contained in:
		| @@ -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; | ||||
| } | ||||
| @@ -469,4 +469,11 @@ public interface WxMaService extends WxService { | ||||
|    * @return WxMaDeviceSubscribeService plugin service | ||||
|    */ | ||||
|   WxMaDeviceSubscribeService getDeviceSubscribeService(); | ||||
|  | ||||
|   /** | ||||
|    * 返回小程序广告接入相关接口服务对象 | ||||
|    * | ||||
|    * @return WxMaDeviceSubscribeService plugin service | ||||
|    */ | ||||
|   WxMaMarketingService getMarketingService(); | ||||
| } | ||||
|   | ||||
| @@ -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; | ||||
| @@ -577,4 +578,7 @@ public abstract class BaseWxMaServiceImpl<H, P> implements WxMaService, RequestH | ||||
|  | ||||
|   @Override | ||||
|   public WxMaDeviceSubscribeService getDeviceSubscribeService(){ return this.deviceSubscribeService; } | ||||
|  | ||||
|   @Override | ||||
|   public WxMaMarketingService getMarketingService() {return  this.marketingService;  } | ||||
| } | ||||
|   | ||||
| @@ -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)); | ||||
|   } | ||||
| } | ||||
| @@ -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(); | ||||
|   } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 184759547
					184759547