#760 小程序增加统一服务消息接口
This commit is contained in:
@@ -1,17 +1,20 @@
|
||||
package cn.binarywang.wx.miniapp.api.impl;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
import org.testng.annotations.*;
|
||||
|
||||
import cn.binarywang.wx.miniapp.api.WxMaService;
|
||||
import cn.binarywang.wx.miniapp.bean.WxMaKefuMessage;
|
||||
import cn.binarywang.wx.miniapp.bean.WxMaTemplateData;
|
||||
import cn.binarywang.wx.miniapp.bean.WxMaTemplateMessage;
|
||||
import cn.binarywang.wx.miniapp.bean.WxMaUniformMessage;
|
||||
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 org.testng.annotations.*;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 测试消息相关接口
|
||||
@@ -45,15 +48,33 @@ public class WxMaMsgServiceImplTest {
|
||||
.formId("FORMID")
|
||||
.page("index")
|
||||
.data(Lists.newArrayList(
|
||||
new WxMaTemplateMessage.Data("keyword1", "339208499", "#173177"),
|
||||
new WxMaTemplateMessage.Data("keyword2", dateFormat.format(new Date()), "#173177"),
|
||||
new WxMaTemplateMessage.Data("keyword3", "粤海喜来登酒店", "#173177"),
|
||||
new WxMaTemplateMessage.Data("keyword4", "广州市天河区天河路208号", "#173177")))
|
||||
new WxMaTemplateData("keyword1", "339208499", "#173177"),
|
||||
new WxMaTemplateData("keyword2", dateFormat.format(new Date()), "#173177"),
|
||||
new WxMaTemplateData("keyword3", "粤海喜来登酒店", "#173177"),
|
||||
new WxMaTemplateData("keyword4", "广州市天河区天河路208号", "#173177")))
|
||||
.templateId(config.getTemplateId())
|
||||
.emphasisKeyword("keyword1.DATA")
|
||||
.build();
|
||||
//templateMessage.addData( new WxMaTemplateMessage.Data("keyword1", "339208499", "#173177"));
|
||||
//templateMessage.addData( new WxMaTemplateData("keyword1", "339208499", "#173177"));
|
||||
this.wxService.getMsgService().sendTemplateMsg(templateMessage);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSendUniformMsg() throws WxErrorException {
|
||||
TestConfig config = (TestConfig) this.wxService.getWxMaConfig();
|
||||
WxMaUniformMessage message = WxMaUniformMessage.builder()
|
||||
.isMpTemplateMsg(false)
|
||||
.toUser(config.getOpenid())
|
||||
.page("page/page/index")
|
||||
.templateId("TEMPLATE_ID")
|
||||
.formId("FORMID")
|
||||
.emphasisKeyword("keyword1.DATA")
|
||||
.build();
|
||||
message.addData(new WxMaTemplateData("keyword1", "339208499"))
|
||||
.addData(new WxMaTemplateData("keyword2", "2015年01月05日 12:30"))
|
||||
.addData(new WxMaTemplateData("keyword3", "腾讯微信总部"))
|
||||
.addData(new WxMaTemplateData("keyword4", "广州市海珠区新港中路397号"));
|
||||
|
||||
this.wxService.getMsgService().sendUniformMsg(message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
package cn.binarywang.wx.miniapp.bean;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import org.testng.annotations.Test;
|
||||
import org.testng.annotations.*;
|
||||
|
||||
import static org.testng.AssertJUnit.assertEquals;
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
import static org.testng.AssertJUnit.*;
|
||||
|
||||
/**
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
@@ -17,10 +18,10 @@ public class WxMaTemplateMessageTest {
|
||||
.formId("FORMID")
|
||||
.page("index")
|
||||
.data(Lists.newArrayList(
|
||||
new WxMaTemplateMessage.Data("keyword1", "339208499", "#173177"),
|
||||
new WxMaTemplateMessage.Data("keyword2", "2015年01月05日12:30", "#173177"),
|
||||
new WxMaTemplateMessage.Data("keyword3", "粤海喜来登酒店", "#173177"),
|
||||
new WxMaTemplateMessage.Data("keyword4", "广州市天河区天河路208号", "#173177")))
|
||||
new WxMaTemplateData("keyword1", "339208499", "#173177"),
|
||||
new WxMaTemplateData("keyword2", "2015年01月05日12:30", "#173177"),
|
||||
new WxMaTemplateData("keyword3", "粤海喜来登酒店", "#173177"),
|
||||
new WxMaTemplateData("keyword4", "广州市天河区天河路208号", "#173177")))
|
||||
.templateId("TEMPLATE_ID")
|
||||
.emphasisKeyword("keyword1.DATA")
|
||||
.build();
|
||||
|
||||
@@ -1,9 +1,20 @@
|
||||
package cn.binarywang.wx.miniapp.demo;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
import org.eclipse.jetty.server.Server;
|
||||
import org.eclipse.jetty.servlet.ServletHandler;
|
||||
import org.eclipse.jetty.servlet.ServletHolder;
|
||||
|
||||
import cn.binarywang.wx.miniapp.api.WxMaService;
|
||||
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
|
||||
import cn.binarywang.wx.miniapp.bean.WxMaKefuMessage;
|
||||
import cn.binarywang.wx.miniapp.bean.WxMaMessage;
|
||||
import cn.binarywang.wx.miniapp.bean.WxMaTemplateData;
|
||||
import cn.binarywang.wx.miniapp.bean.WxMaTemplateMessage;
|
||||
import cn.binarywang.wx.miniapp.config.WxMaConfig;
|
||||
import cn.binarywang.wx.miniapp.constant.WxMaConstants;
|
||||
@@ -14,15 +25,6 @@ import com.google.common.collect.Lists;
|
||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.session.WxSessionManager;
|
||||
import org.eclipse.jetty.server.Server;
|
||||
import org.eclipse.jetty.servlet.ServletHandler;
|
||||
import org.eclipse.jetty.servlet.ServletHolder;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
/**
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
@@ -96,7 +98,7 @@ public class WxMaDemoServer {
|
||||
throws WxErrorException {
|
||||
service.getMsgService().sendTemplateMsg(WxMaTemplateMessage.builder()
|
||||
.templateId(templateId).data(Lists.newArrayList(
|
||||
new WxMaTemplateMessage.Data("keyword1", "339208499", "#173177")))
|
||||
new WxMaTemplateData("keyword1", "339208499", "#173177")))
|
||||
.toUser(wxMessage.getFromUser())
|
||||
.formId("自己替换可用的formid")
|
||||
.build());
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
package cn.binarywang.wx.miniapp.util.json;
|
||||
|
||||
import org.testng.annotations.*;
|
||||
|
||||
import cn.binarywang.wx.miniapp.bean.WxMaTemplateData;
|
||||
import cn.binarywang.wx.miniapp.bean.WxMaUniformMessage;
|
||||
import com.google.gson.JsonParser;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
*
|
||||
* Created by Binary Wang on 2018/9/23.
|
||||
* </pre>
|
||||
*
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
*/
|
||||
public class WxMaUniformMessageGsonAdapterTest {
|
||||
|
||||
@Test
|
||||
public void testSerialize_mp() {
|
||||
WxMaUniformMessage message = WxMaUniformMessage.builder()
|
||||
.isMpTemplateMsg(true)
|
||||
.toUser("OPENID")
|
||||
.appid("APPID")
|
||||
.templateId("TEMPLATE_ID")
|
||||
.url("http://weixin.qq.com/download")
|
||||
.miniProgram(new WxMaUniformMessage.MiniProgram("xiaochengxuappid12345", "index?foo=bar", false))
|
||||
.build();
|
||||
message.addData(new WxMaTemplateData("first", "恭喜你购买成功!", "#173177"))
|
||||
.addData(new WxMaTemplateData("keyword1", "巧克力", "#173177"))
|
||||
.addData(new WxMaTemplateData("keyword2", "39.8元", "#173177"))
|
||||
.addData(new WxMaTemplateData("keyword3", "2014年9月22日", "#173177"))
|
||||
.addData(new WxMaTemplateData("remark", "欢迎再次购买!", "#173177"));
|
||||
|
||||
assertThat(message.toJson()).isEqualTo(new JsonParser().parse("{\n" +
|
||||
" \"touser\":\"OPENID\",\n" +
|
||||
" \"mp_template_msg\":{\n" +
|
||||
" \"appid\":\"APPID\",\n" +
|
||||
" \"template_id\":\"TEMPLATE_ID\",\n" +
|
||||
" \"url\":\"http://weixin.qq.com/download\",\n" +
|
||||
" \"miniprogram\":{\n" +
|
||||
" \"appid\":\"xiaochengxuappid12345\",\n" +
|
||||
" \"pagepath\":\"index?foo=bar\"\n" +
|
||||
" },\n" +
|
||||
" \"data\":{\n" +
|
||||
" \"first\":{\n" +
|
||||
" \"value\":\"恭喜你购买成功!\",\n" +
|
||||
" \"color\":\"#173177\"\n" +
|
||||
" },\n" +
|
||||
" \"keyword1\":{\n" +
|
||||
" \"value\":\"巧克力\",\n" +
|
||||
" \"color\":\"#173177\"\n" +
|
||||
" },\n" +
|
||||
" \"keyword2\":{\n" +
|
||||
" \"value\":\"39.8元\",\n" +
|
||||
" \"color\":\"#173177\"\n" +
|
||||
" },\n" +
|
||||
" \"keyword3\":{\n" +
|
||||
" \"value\":\"2014年9月22日\",\n" +
|
||||
" \"color\":\"#173177\"\n" +
|
||||
" },\n" +
|
||||
" \"remark\":{\n" +
|
||||
" \"value\":\"欢迎再次购买!\",\n" +
|
||||
" \"color\":\"#173177\"\n" +
|
||||
" }\n" +
|
||||
" }\n" +
|
||||
" }\n" +
|
||||
"}").getAsJsonObject().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSerialize_ma() {
|
||||
WxMaUniformMessage message = WxMaUniformMessage.builder()
|
||||
.isMpTemplateMsg(false)
|
||||
.toUser("OPENID")
|
||||
.page("page/page/index")
|
||||
.templateId("TEMPLATE_ID")
|
||||
.formId("FORMID")
|
||||
.emphasisKeyword("keyword1.DATA")
|
||||
.build();
|
||||
message.addData(new WxMaTemplateData("keyword1", "339208499"))
|
||||
.addData(new WxMaTemplateData("keyword2", "2015年01月05日 12:30"))
|
||||
.addData(new WxMaTemplateData("keyword3", "腾讯微信总部"))
|
||||
.addData(new WxMaTemplateData("keyword4", "广州市海珠区新港中路397号"));
|
||||
|
||||
assertThat(message.toJson()).isEqualTo(new JsonParser().parse("{\n" +
|
||||
" \"touser\":\"OPENID\",\n" +
|
||||
" \"weapp_template_msg\":{\n" +
|
||||
" \"template_id\":\"TEMPLATE_ID\",\n" +
|
||||
" \"page\":\"page/page/index\",\n" +
|
||||
" \"form_id\":\"FORMID\",\n" +
|
||||
" \"data\":{\n" +
|
||||
" \"keyword1\":{\n" +
|
||||
" \"value\":\"339208499\"\n" +
|
||||
" },\n" +
|
||||
" \"keyword2\":{\n" +
|
||||
" \"value\":\"2015年01月05日 12:30\"\n" +
|
||||
" },\n" +
|
||||
" \"keyword3\":{\n" +
|
||||
" \"value\":\"腾讯微信总部\"\n" +
|
||||
" },\n" +
|
||||
" \"keyword4\":{\n" +
|
||||
" \"value\":\"广州市海珠区新港中路397号\"\n" +
|
||||
" }\n" +
|
||||
" },\n" +
|
||||
" \"emphasis_keyword\":\"keyword1.DATA\"\n" +
|
||||
" }\n" +
|
||||
"}").getAsJsonObject().toString());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user