-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🆕 #2473 【小程序】接入小程序广告实现,实现创建数据源和回传数据两个接口
Co-authored-by: wywy12300 <[email protected]>
- Loading branch information
Showing
5 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaMarketingService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...ava-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaMarketingServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
...in-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/marketing/WxMaUserAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |