🆕 #1290 小程序增加动态消息接口

This commit is contained in:
Binary Wang 2020-02-17 18:44:44 +08:00
parent f90f4540d7
commit 27d88a752e
4 changed files with 161 additions and 20 deletions

View File

@ -1,9 +1,7 @@
package cn.binarywang.wx.miniapp.api;
import cn.binarywang.wx.miniapp.bean.WxMaKefuMessage;
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import cn.binarywang.wx.miniapp.bean.WxMaTemplateMessage;
import cn.binarywang.wx.miniapp.bean.WxMaUniformMessage;
import cn.binarywang.wx.miniapp.bean.*;
import com.google.gson.JsonObject;
import me.chanjar.weixin.common.error.WxErrorException;
/**
@ -18,6 +16,8 @@ public interface WxMaMsgService {
String TEMPLATE_MSG_SEND_URL = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send";
String SUBSCRIBE_MSG_SEND_URL = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send";
String UNIFORM_MSG_SEND_URL = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send";
String ACTIVITY_ID_CREATE_URL = "https://api.weixin.qq.com/cgi-bin/message/wxopen/activityid/create";
String UPDATABLE_MSG_SEND_URL = "https://api.weixin.qq.com/cgi-bin/message/wxopen/updatablemsg/send";
/**
* <pre>
@ -25,6 +25,10 @@ public interface WxMaMsgService {
* 详情请见: <a href="https://developers.weixin.qq.com/miniprogram/dev/api-backend/customerServiceMessage.send.html">发送客服消息</a>
* 接口url格式https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=ACCESS_TOKEN
* </pre>
*
* @param message 客服消息
* @return .
* @throws WxErrorException .
*/
boolean sendKefuMsg(WxMaKefuMessage message) throws WxErrorException;
@ -53,13 +57,43 @@ public interface WxMaMsgService {
*/
void sendSubscribeMsg(WxMaSubscribeMessage subscribeMessage) throws WxErrorException;
/**
* <pre>
* 下发小程序和公众号统一的服务消息
* 详情请见: <a href="https://developers.weixin.qq.com/miniprogram/dev/api/open-api/uniform-message/sendUniformMessage.html">下发小程序和公众号统一的服务消息</a>
* 接口url格式https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=ACCESS_TOKEN
* </pre>
*
* @param uniformMessage 消息
* @throws WxErrorException .
*/
void sendUniformMsg(WxMaUniformMessage uniformMessage) throws WxErrorException;
/**
* <pre>
* 创建被分享动态消息的 activity_id.
* 动态消息: https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/share/updatable-message.html
*
* 文档地址https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/updatable-message/updatableMessage.createActivityId.html
* 接口地址GET https://api.weixin.qq.com/cgi-bin/message/wxopen/activityid/create?access_token=ACCESS_TOKEN
* </pre>
*
* @return .
* @throws WxErrorException .
*/
JsonObject createUpdatableMessageActivityId() throws WxErrorException;
/**
* <pre>
* 修改被分享的动态消息.
* 动态消息: https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/share/updatable-message.html
*
* 文档地址https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/updatable-message/updatableMessage.setUpdatableMsg.html
* 接口地址POST https://api.weixin.qq.com/cgi-bin/message/wxopen/activityid/create?access_token=ACCESS_TOKEN
* </pre>
*
* @param msg 动态消息
* @throws WxErrorException .
*/
void setUpdatableMsg(WxMaUpdatableMsg msg) throws WxErrorException;
}

View File

@ -2,11 +2,9 @@ package cn.binarywang.wx.miniapp.api.impl;
import cn.binarywang.wx.miniapp.api.WxMaMsgService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaKefuMessage;
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import cn.binarywang.wx.miniapp.bean.WxMaTemplateMessage;
import cn.binarywang.wx.miniapp.bean.WxMaUniformMessage;
import cn.binarywang.wx.miniapp.bean.*;
import cn.binarywang.wx.miniapp.constant.WxMaConstants;
import cn.binarywang.wx.miniapp.util.json.WxMaGsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import lombok.AllArgsConstructor;
@ -55,4 +53,15 @@ public class WxMaMsgServiceImpl implements WxMaMsgService {
}
}
@Override
public JsonObject createUpdatableMessageActivityId() throws WxErrorException {
final String responseContent = this.wxMaService.get(ACTIVITY_ID_CREATE_URL, null);
return JSON_PARSER.parse(responseContent).getAsJsonObject();
}
@Override
public void setUpdatableMsg(WxMaUpdatableMsg msg) throws WxErrorException {
this.wxMaService.post(UPDATABLE_MSG_SEND_URL, WxMaGsonBuilder.create().toJson(msg));
}
}

View File

@ -0,0 +1,76 @@
package cn.binarywang.wx.miniapp.bean;
import com.google.gson.annotations.SerializedName;
import lombok.Data;
import lombok.experimental.Accessors;
import java.io.Serializable;
import java.util.List;
/**
* 动态消息.
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
* @date 2020-02-17
*/
@Data
@Accessors(chain = true)
public class WxMaUpdatableMsg implements Serializable {
private static final long serialVersionUID = 6231957192034798165L;
/**
* 动态消息的 ID通过 updatableMessage.createActivityId 接口获取
*/
@SerializedName("activity_id")
private String activityId;
/**
* 动态消息修改后的状态
* 0 未开始
* 1 已开始
*/
@SerializedName("target_state")
private Integer targetState;
/**
* 动态消息对应的模板信息
*/
@SerializedName("template_info")
private TemplateInfo templateInfo;
@Data
@Accessors(chain = true)
public static class TemplateInfo implements Serializable {
private static final long serialVersionUID = -9218473401759062841L;
/**
* 模板中需要修改的参数
*/
@SerializedName("parameter_list")
private List<Parameter> parameterList;
}
@Data
@Accessors(chain = true)
public static class Parameter implements Serializable {
private static final long serialVersionUID = 7444716050341038046L;
/**
* 要修改的参数名
* <pre>
* 合法值
* member_count target_state = 0 时必填文字内容模板中 member_count 的值
* room_limit target_state = 0 时必填文字内容模板中 room_limit 的值
* path target_state = 1 时必填点击进入启动小程序时使用的路径对于小游戏没有页面的概念可以用于传递查询字符串query "?foo=bar"
* version_type target_state = 1 时必填点击进入启动小程序时使用的版本
* 有效参数值为develop开发版trial体验版release正式版
* </pre>
*/
private String name;
/**
* 修改后的参数值
*/
private String value;
}
}

View File

@ -1,17 +1,20 @@
package cn.binarywang.wx.miniapp.api.impl;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.*;
import cn.binarywang.wx.miniapp.test.ApiTestModule;
import cn.binarywang.wx.miniapp.test.TestConfig;
import com.google.common.collect.Lists;
import com.google.gson.JsonObject;
import com.google.inject.Inject;
import me.chanjar.weixin.common.error.WxErrorException;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
import java.text.SimpleDateFormat;
import java.util.Date;
import cn.binarywang.wx.miniapp.bean.*;
import org.testng.annotations.*;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.test.ApiTestModule;
import cn.binarywang.wx.miniapp.test.TestConfig;
import com.google.common.collect.Lists;
import com.google.inject.Inject;
import me.chanjar.weixin.common.error.WxErrorException;
import static org.assertj.core.api.Assertions.assertThat;
/**
* 测试消息相关接口
@ -25,7 +28,8 @@ public class WxMaMsgServiceImplTest {
@Inject
private WxMaService wxService;
public void testSendKefuMessage() throws WxErrorException {
@Test
public void testSendKefuMsg() throws WxErrorException {
TestConfig config = (TestConfig) this.wxService.getWxMaConfig();
WxMaKefuMessage message = WxMaKefuMessage.newTextBuilder()
.toUser(config.getOpenid())
@ -74,7 +78,7 @@ public class WxMaMsgServiceImplTest {
@Test
public void testSendUniformMsg() throws WxErrorException {
TestConfig config = (TestConfig) this.wxService.getWxMaConfig();
WxMaUniformMessage message = WxMaUniformMessage.builder()
WxMaUniformMessage message = WxMaUniformMessage.builder()
.isMpTemplateMsg(false)
.toUser(config.getOpenid())
.page("page/page/index")
@ -89,4 +93,22 @@ public class WxMaMsgServiceImplTest {
this.wxService.getMsgService().sendUniformMsg(message);
}
@Test
public void testCreateUpdatableMessageActivityId() throws WxErrorException {
final JsonObject jsonObject = this.wxService.getMsgService().createUpdatableMessageActivityId();
assertThat(jsonObject).isNotNull();
assertThat(jsonObject.get("activity_id")).isNotNull();
System.out.println(jsonObject.get("activity_id"));
assertThat(jsonObject.get("expiration_time")).isNotNull();
}
@Test
public void testSetUpdatableMsg() throws WxErrorException {
this.wxService.getMsgService().setUpdatableMsg(new WxMaUpdatableMsg()
.setActivityId("1048_4f61uDloWPZl9pAs1dGx07vDiHKZ7FwJ0suohS1iMH5z8zhFktYk4nRqqBY~")
.setTargetState(1)
.setTemplateInfo(new WxMaUpdatableMsg.TemplateInfo()
.setParameterList(Lists.newArrayList(new WxMaUpdatableMsg.Parameter().setName("member_count").setValue("1")))));
}
}