| @@ -2,6 +2,7 @@ package cn.binarywang.wx.miniapp.api; | |||||
| import cn.binarywang.wx.miniapp.bean.WxMaKefuMessage; | import cn.binarywang.wx.miniapp.bean.WxMaKefuMessage; | ||||
| import cn.binarywang.wx.miniapp.bean.WxMaTemplateMessage; | import cn.binarywang.wx.miniapp.bean.WxMaTemplateMessage; | ||||
| import cn.binarywang.wx.miniapp.bean.WxMaUniformMessage; | |||||
| import me.chanjar.weixin.common.error.WxErrorException; | import me.chanjar.weixin.common.error.WxErrorException; | ||||
| /** | /** | ||||
| @@ -14,6 +15,7 @@ import me.chanjar.weixin.common.error.WxErrorException; | |||||
| public interface WxMaMsgService { | public interface WxMaMsgService { | ||||
| String KEFU_MESSAGE_SEND_URL = "https://api.weixin.qq.com/cgi-bin/message/custom/send"; | String KEFU_MESSAGE_SEND_URL = "https://api.weixin.qq.com/cgi-bin/message/custom/send"; | ||||
| String TEMPLATE_MSG_SEND_URL = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send"; | String TEMPLATE_MSG_SEND_URL = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send"; | ||||
| String UNIFORM_MSG_SEND_URL = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send"; | |||||
| /** | /** | ||||
| * <pre> | * <pre> | ||||
| @@ -32,4 +34,14 @@ public interface WxMaMsgService { | |||||
| * </pre> | * </pre> | ||||
| */ | */ | ||||
| void sendTemplateMsg(WxMaTemplateMessage templateMessage) throws WxErrorException; | void sendTemplateMsg(WxMaTemplateMessage templateMessage) 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> | |||||
| */ | |||||
| void sendUniformMsg(WxMaUniformMessage uniformMessage) throws WxErrorException; | |||||
| } | } | ||||
| @@ -4,6 +4,7 @@ import cn.binarywang.wx.miniapp.api.WxMaMsgService; | |||||
| import cn.binarywang.wx.miniapp.api.WxMaService; | import cn.binarywang.wx.miniapp.api.WxMaService; | ||||
| import cn.binarywang.wx.miniapp.bean.WxMaKefuMessage; | import cn.binarywang.wx.miniapp.bean.WxMaKefuMessage; | ||||
| import cn.binarywang.wx.miniapp.bean.WxMaTemplateMessage; | import cn.binarywang.wx.miniapp.bean.WxMaTemplateMessage; | ||||
| import cn.binarywang.wx.miniapp.bean.WxMaUniformMessage; | |||||
| import cn.binarywang.wx.miniapp.constant.WxMaConstants; | import cn.binarywang.wx.miniapp.constant.WxMaConstants; | ||||
| import com.google.gson.JsonObject; | import com.google.gson.JsonObject; | ||||
| import com.google.gson.JsonParser; | import com.google.gson.JsonParser; | ||||
| @@ -36,4 +37,13 @@ public class WxMaMsgServiceImpl implements WxMaMsgService { | |||||
| } | } | ||||
| } | } | ||||
| @Override | |||||
| public void sendUniformMsg(WxMaUniformMessage uniformMessage) throws WxErrorException { | |||||
| String responseContent = this.wxMaService.post(UNIFORM_MSG_SEND_URL, uniformMessage.toJson()); | |||||
| JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject(); | |||||
| if (jsonObject.get(WxMaConstants.ERRCODE).getAsInt() != 0) { | |||||
| throw new WxErrorException(WxError.fromJson(responseContent)); | |||||
| } | |||||
| } | |||||
| } | } | ||||
| @@ -0,0 +1,32 @@ | |||||
| package cn.binarywang.wx.miniapp.bean; | |||||
| import lombok.Data; | |||||
| import lombok.NoArgsConstructor; | |||||
| /** | |||||
| * <pre> | |||||
| * | |||||
| * Created by Binary Wang on 2018/9/23. | |||||
| * </pre> | |||||
| * | |||||
| * @author <a href="https://github.com/binarywang">Binary Wang</a> | |||||
| */ | |||||
| @Data | |||||
| @NoArgsConstructor | |||||
| public class WxMaTemplateData { | |||||
| private String name; | |||||
| private String value; | |||||
| private String color; | |||||
| public WxMaTemplateData(String name, String value) { | |||||
| this.name = name; | |||||
| this.value = value; | |||||
| } | |||||
| public WxMaTemplateData(String name, String value, String color) { | |||||
| this.name = name; | |||||
| this.value = value; | |||||
| this.color = color; | |||||
| } | |||||
| } | |||||
| @@ -73,7 +73,7 @@ public class WxMaTemplateMessage implements Serializable { | |||||
| * 描述: 模板内容,不填则下发空模板 | * 描述: 模板内容,不填则下发空模板 | ||||
| * </pre> | * </pre> | ||||
| */ | */ | ||||
| private List<Data> data; | |||||
| private List<WxMaTemplateData> data; | |||||
| /** | /** | ||||
| * 模板内容字体的颜色,不填默认黑色. | * 模板内容字体的颜色,不填默认黑色. | ||||
| @@ -95,7 +95,7 @@ public class WxMaTemplateMessage implements Serializable { | |||||
| */ | */ | ||||
| private String emphasisKeyword; | private String emphasisKeyword; | ||||
| public WxMaTemplateMessage addData(Data datum) { | |||||
| public WxMaTemplateMessage addData(WxMaTemplateData datum) { | |||||
| if (this.data == null) { | if (this.data == null) { | ||||
| this.data = new ArrayList<>(); | this.data = new ArrayList<>(); | ||||
| } | } | ||||
| @@ -108,24 +108,4 @@ public class WxMaTemplateMessage implements Serializable { | |||||
| return WxMaGsonBuilder.create().toJson(this); | return WxMaGsonBuilder.create().toJson(this); | ||||
| } | } | ||||
| @lombok.Data | |||||
| @NoArgsConstructor | |||||
| public static class Data { | |||||
| private String name; | |||||
| private String value; | |||||
| private String color; | |||||
| public Data(String name, String value) { | |||||
| this.name = name; | |||||
| this.value = value; | |||||
| } | |||||
| public Data(String name, String value, String color) { | |||||
| this.name = name; | |||||
| this.value = value; | |||||
| this.color = color; | |||||
| } | |||||
| } | |||||
| } | } | ||||
| @@ -0,0 +1,108 @@ | |||||
| package cn.binarywang.wx.miniapp.bean; | |||||
| import java.io.Serializable; | |||||
| import java.util.ArrayList; | |||||
| import java.util.List; | |||||
| import cn.binarywang.wx.miniapp.util.json.WxMaGsonBuilder; | |||||
| import lombok.AllArgsConstructor; | |||||
| import lombok.Builder; | |||||
| import lombok.Data; | |||||
| import lombok.Getter; | |||||
| import lombok.NoArgsConstructor; | |||||
| import lombok.Setter; | |||||
| /** | |||||
| * 模板消息. | |||||
| * 参考 https://mp.weixin.qq.com/debug/wxadoc/dev/api/notice.html#接口说明 模板消息部分 | |||||
| * | |||||
| * @author <a href="https://github.com/binarywang">Binary Wang</a> | |||||
| */ | |||||
| @Getter | |||||
| @Setter | |||||
| @NoArgsConstructor | |||||
| @AllArgsConstructor | |||||
| @Builder | |||||
| public class WxMaUniformMessage implements Serializable { | |||||
| private static final long serialVersionUID = 5063374783759519418L; | |||||
| /** | |||||
| * 是否发送公众号模版消息,否则发送小程序模版消息. | |||||
| */ | |||||
| private boolean isMpTemplateMsg; | |||||
| /** | |||||
| * 用户openid. | |||||
| * 可以是小程序的openid,也可以是mp_template_msg.appid对应的公众号的openid | |||||
| */ | |||||
| private String toUser; | |||||
| /** | |||||
| * 公众号appid,要求与小程序有绑定且同主体. | |||||
| */ | |||||
| private String appid; | |||||
| /** | |||||
| * 公众号或小程序模板ID. | |||||
| */ | |||||
| private String templateId; | |||||
| /** | |||||
| * 公众号模板消息所要跳转的url. | |||||
| */ | |||||
| private String url; | |||||
| /** | |||||
| * 小程序页面路径. | |||||
| */ | |||||
| private String page; | |||||
| /** | |||||
| * 小程序模板消息formid. | |||||
| */ | |||||
| private String formId; | |||||
| /** | |||||
| * 公众号模板消息所要跳转的小程序,小程序的必须与公众号具有绑定关系. | |||||
| */ | |||||
| private MiniProgram miniProgram; | |||||
| /** | |||||
| * 小程序模板数据. | |||||
| */ | |||||
| private List<WxMaTemplateData> data; | |||||
| /** | |||||
| * 模板需要放大的关键词,不填则默认无放大. | |||||
| */ | |||||
| private String emphasisKeyword; | |||||
| public WxMaUniformMessage addData(WxMaTemplateData datum) { | |||||
| if (this.data == null) { | |||||
| this.data = new ArrayList<>(); | |||||
| } | |||||
| this.data.add(datum); | |||||
| return this; | |||||
| } | |||||
| public String toJson() { | |||||
| return WxMaGsonBuilder.create().toJson(this); | |||||
| } | |||||
| @Data | |||||
| @NoArgsConstructor | |||||
| @AllArgsConstructor | |||||
| public static class MiniProgram implements Serializable { | |||||
| private static final long serialVersionUID = -7945254706501974849L; | |||||
| private String appid; | |||||
| private String pagePath; | |||||
| /** | |||||
| * 是否使用path,否则使用pagepath. | |||||
| * 加入此字段是基于微信官方接口变化多端的考虑 | |||||
| */ | |||||
| private boolean usePath = false; | |||||
| } | |||||
| } | |||||
| @@ -1,6 +1,7 @@ | |||||
| package cn.binarywang.wx.miniapp.util.json; | package cn.binarywang.wx.miniapp.util.json; | ||||
| import cn.binarywang.wx.miniapp.bean.WxMaTemplateMessage; | import cn.binarywang.wx.miniapp.bean.WxMaTemplateMessage; | ||||
| import cn.binarywang.wx.miniapp.bean.WxMaUniformMessage; | |||||
| import cn.binarywang.wx.miniapp.bean.analysis.WxMaRetainInfo; | import cn.binarywang.wx.miniapp.bean.analysis.WxMaRetainInfo; | ||||
| import cn.binarywang.wx.miniapp.bean.analysis.WxMaUserPortrait; | import cn.binarywang.wx.miniapp.bean.analysis.WxMaUserPortrait; | ||||
| import cn.binarywang.wx.miniapp.bean.analysis.WxMaVisitDistribution; | import cn.binarywang.wx.miniapp.bean.analysis.WxMaVisitDistribution; | ||||
| @@ -18,6 +19,7 @@ public class WxMaGsonBuilder { | |||||
| static { | static { | ||||
| INSTANCE.disableHtmlEscaping(); | INSTANCE.disableHtmlEscaping(); | ||||
| INSTANCE.registerTypeAdapter(WxMaTemplateMessage.class, new WxMaTemplateMessageGsonAdapter()); | INSTANCE.registerTypeAdapter(WxMaTemplateMessage.class, new WxMaTemplateMessageGsonAdapter()); | ||||
| INSTANCE.registerTypeAdapter(WxMaUniformMessage.class, new WxMaUniformMessageGsonAdapter()); | |||||
| INSTANCE.registerTypeAdapter(WxMaCodeCommitRequest.class, new WxMaCodeCommitRequestGsonAdapter()); | INSTANCE.registerTypeAdapter(WxMaCodeCommitRequest.class, new WxMaCodeCommitRequestGsonAdapter()); | ||||
| INSTANCE.registerTypeAdapter(WxMaCodeVersionDistribution.class, new WxMaCodeVersionDistributionGsonAdapter()); | INSTANCE.registerTypeAdapter(WxMaCodeVersionDistribution.class, new WxMaCodeVersionDistributionGsonAdapter()); | ||||
| INSTANCE.registerTypeAdapter(WxMaVisitDistribution.class, new WxMaVisitDistributionGsonAdapter()); | INSTANCE.registerTypeAdapter(WxMaVisitDistribution.class, new WxMaVisitDistributionGsonAdapter()); | ||||
| @@ -1,13 +1,14 @@ | |||||
| package cn.binarywang.wx.miniapp.util.json; | package cn.binarywang.wx.miniapp.util.json; | ||||
| import java.lang.reflect.Type; | |||||
| import cn.binarywang.wx.miniapp.bean.WxMaTemplateData; | |||||
| import cn.binarywang.wx.miniapp.bean.WxMaTemplateMessage; | import cn.binarywang.wx.miniapp.bean.WxMaTemplateMessage; | ||||
| import com.google.gson.JsonElement; | import com.google.gson.JsonElement; | ||||
| import com.google.gson.JsonObject; | import com.google.gson.JsonObject; | ||||
| import com.google.gson.JsonSerializationContext; | import com.google.gson.JsonSerializationContext; | ||||
| import com.google.gson.JsonSerializer; | import com.google.gson.JsonSerializer; | ||||
| import java.lang.reflect.Type; | |||||
| /** | /** | ||||
| * @author <a href="https://github.com/binarywang">Binary Wang</a> | * @author <a href="https://github.com/binarywang">Binary Wang</a> | ||||
| */ | */ | ||||
| @@ -26,10 +27,6 @@ public class WxMaTemplateMessageGsonAdapter implements JsonSerializer<WxMaTempla | |||||
| messageJson.addProperty("form_id", message.getFormId()); | messageJson.addProperty("form_id", message.getFormId()); | ||||
| } | } | ||||
| if (message.getPage() != null) { | |||||
| messageJson.addProperty("page", message.getPage()); | |||||
| } | |||||
| if (message.getColor() != null) { | if (message.getColor() != null) { | ||||
| messageJson.addProperty("color", message.getColor()); | messageJson.addProperty("color", message.getColor()); | ||||
| } | } | ||||
| @@ -45,7 +42,7 @@ public class WxMaTemplateMessageGsonAdapter implements JsonSerializer<WxMaTempla | |||||
| return messageJson; | return messageJson; | ||||
| } | } | ||||
| for (WxMaTemplateMessage.Data datum : message.getData()) { | |||||
| for (WxMaTemplateData datum : message.getData()) { | |||||
| JsonObject dataJson = new JsonObject(); | JsonObject dataJson = new JsonObject(); | ||||
| dataJson.addProperty("value", datum.getValue()); | dataJson.addProperty("value", datum.getValue()); | ||||
| if (datum.getColor() != null) { | if (datum.getColor() != null) { | ||||
| @@ -0,0 +1,98 @@ | |||||
| package cn.binarywang.wx.miniapp.util.json; | |||||
| import java.lang.reflect.Type; | |||||
| import cn.binarywang.wx.miniapp.bean.WxMaTemplateData; | |||||
| import cn.binarywang.wx.miniapp.bean.WxMaUniformMessage; | |||||
| import com.google.gson.JsonElement; | |||||
| import com.google.gson.JsonObject; | |||||
| import com.google.gson.JsonSerializationContext; | |||||
| import com.google.gson.JsonSerializer; | |||||
| /** | |||||
| * @author <a href="https://github.com/binarywang">Binary Wang</a> | |||||
| */ | |||||
| public class WxMaUniformMessageGsonAdapter implements JsonSerializer<WxMaUniformMessage> { | |||||
| @Override | |||||
| public JsonElement serialize(WxMaUniformMessage message, Type typeOfSrc, JsonSerializationContext context) { | |||||
| JsonObject messageJson = new JsonObject(); | |||||
| messageJson.addProperty("touser", message.getToUser()); | |||||
| if (message.isMpTemplateMsg()) { | |||||
| JsonObject msg = new JsonObject(); | |||||
| if (message.getAppid() != null) { | |||||
| msg.addProperty("appid", message.getAppid()); | |||||
| } | |||||
| msg.addProperty("template_id", message.getTemplateId()); | |||||
| if (message.getUrl() != null) { | |||||
| msg.addProperty("url", message.getUrl()); | |||||
| } | |||||
| final WxMaUniformMessage.MiniProgram miniProgram = message.getMiniProgram(); | |||||
| if (miniProgram != null) { | |||||
| JsonObject miniProgramJson = new JsonObject(); | |||||
| miniProgramJson.addProperty("appid", miniProgram.getAppid()); | |||||
| if (miniProgram.isUsePath()) { | |||||
| miniProgramJson.addProperty("path", miniProgram.getPagePath()); | |||||
| } else { | |||||
| miniProgramJson.addProperty("pagepath", miniProgram.getPagePath()); | |||||
| } | |||||
| msg.add("miniprogram", miniProgramJson); | |||||
| } | |||||
| if (message.getData() != null) { | |||||
| JsonObject data = new JsonObject(); | |||||
| for (WxMaTemplateData templateData : message.getData()) { | |||||
| JsonObject dataJson = new JsonObject(); | |||||
| dataJson.addProperty("value", templateData.getValue()); | |||||
| if (templateData.getColor() != null) { | |||||
| dataJson.addProperty("color", templateData.getColor()); | |||||
| } | |||||
| data.add(templateData.getName(), dataJson); | |||||
| } | |||||
| msg.add("data", data); | |||||
| } | |||||
| messageJson.add("mp_template_msg", msg); | |||||
| return messageJson; | |||||
| } | |||||
| //小程序模版消息 | |||||
| JsonObject msg = new JsonObject(); | |||||
| msg.addProperty("template_id", message.getTemplateId()); | |||||
| if (message.getPage() != null) { | |||||
| msg.addProperty("page", message.getPage()); | |||||
| } | |||||
| if (message.getFormId() != null) { | |||||
| msg.addProperty("form_id", message.getFormId()); | |||||
| } | |||||
| JsonObject data = new JsonObject(); | |||||
| msg.add("data", data); | |||||
| if (message.getData() != null) { | |||||
| for (WxMaTemplateData templateData : message.getData()) { | |||||
| JsonObject dataJson = new JsonObject(); | |||||
| dataJson.addProperty("value", templateData.getValue()); | |||||
| if (templateData.getColor() != null) { | |||||
| dataJson.addProperty("color", templateData.getColor()); | |||||
| } | |||||
| data.add(templateData.getName(), dataJson); | |||||
| } | |||||
| } | |||||
| if (message.getEmphasisKeyword() != null) { | |||||
| msg.addProperty("emphasis_keyword", message.getEmphasisKeyword()); | |||||
| } | |||||
| messageJson.add("weapp_template_msg", msg); | |||||
| return messageJson; | |||||
| } | |||||
| } | |||||
| @@ -1,17 +1,20 @@ | |||||
| package cn.binarywang.wx.miniapp.api.impl; | 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.api.WxMaService; | ||||
| import cn.binarywang.wx.miniapp.bean.WxMaKefuMessage; | 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.WxMaTemplateMessage; | ||||
| import cn.binarywang.wx.miniapp.bean.WxMaUniformMessage; | |||||
| import cn.binarywang.wx.miniapp.test.ApiTestModule; | import cn.binarywang.wx.miniapp.test.ApiTestModule; | ||||
| import cn.binarywang.wx.miniapp.test.TestConfig; | import cn.binarywang.wx.miniapp.test.TestConfig; | ||||
| import com.google.common.collect.Lists; | import com.google.common.collect.Lists; | ||||
| import com.google.inject.Inject; | import com.google.inject.Inject; | ||||
| import me.chanjar.weixin.common.error.WxErrorException; | 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") | .formId("FORMID") | ||||
| .page("index") | .page("index") | ||||
| .data(Lists.newArrayList( | .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()) | .templateId(config.getTemplateId()) | ||||
| .emphasisKeyword("keyword1.DATA") | .emphasisKeyword("keyword1.DATA") | ||||
| .build(); | .build(); | ||||
| //templateMessage.addData( new WxMaTemplateMessage.Data("keyword1", "339208499", "#173177")); | |||||
| //templateMessage.addData( new WxMaTemplateData("keyword1", "339208499", "#173177")); | |||||
| this.wxService.getMsgService().sendTemplateMsg(templateMessage); | 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; | package cn.binarywang.wx.miniapp.bean; | ||||
| import org.testng.annotations.*; | |||||
| import com.google.common.collect.Lists; | import com.google.common.collect.Lists; | ||||
| import org.testng.annotations.Test; | |||||
| import static org.testng.AssertJUnit.assertEquals; | |||||
| import static org.testng.AssertJUnit.*; | |||||
| /** | /** | ||||
| * @author <a href="https://github.com/binarywang">Binary Wang</a> | * @author <a href="https://github.com/binarywang">Binary Wang</a> | ||||
| @@ -17,10 +18,10 @@ public class WxMaTemplateMessageTest { | |||||
| .formId("FORMID") | .formId("FORMID") | ||||
| .page("index") | .page("index") | ||||
| .data(Lists.newArrayList( | .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") | .templateId("TEMPLATE_ID") | ||||
| .emphasisKeyword("keyword1.DATA") | .emphasisKeyword("keyword1.DATA") | ||||
| .build(); | .build(); | ||||
| @@ -1,9 +1,20 @@ | |||||
| package cn.binarywang.wx.miniapp.demo; | 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.WxMaService; | ||||
| import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl; | import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl; | ||||
| import cn.binarywang.wx.miniapp.bean.WxMaKefuMessage; | import cn.binarywang.wx.miniapp.bean.WxMaKefuMessage; | ||||
| import cn.binarywang.wx.miniapp.bean.WxMaMessage; | 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.bean.WxMaTemplateMessage; | ||||
| import cn.binarywang.wx.miniapp.config.WxMaConfig; | import cn.binarywang.wx.miniapp.config.WxMaConfig; | ||||
| import cn.binarywang.wx.miniapp.constant.WxMaConstants; | 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.bean.result.WxMediaUploadResult; | ||||
| import me.chanjar.weixin.common.error.WxErrorException; | import me.chanjar.weixin.common.error.WxErrorException; | ||||
| import me.chanjar.weixin.common.session.WxSessionManager; | 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> | * @author <a href="https://github.com/binarywang">Binary Wang</a> | ||||
| @@ -96,7 +98,7 @@ public class WxMaDemoServer { | |||||
| throws WxErrorException { | throws WxErrorException { | ||||
| service.getMsgService().sendTemplateMsg(WxMaTemplateMessage.builder() | service.getMsgService().sendTemplateMsg(WxMaTemplateMessage.builder() | ||||
| .templateId(templateId).data(Lists.newArrayList( | .templateId(templateId).data(Lists.newArrayList( | ||||
| new WxMaTemplateMessage.Data("keyword1", "339208499", "#173177"))) | |||||
| new WxMaTemplateData("keyword1", "339208499", "#173177"))) | |||||
| .toUser(wxMessage.getFromUser()) | .toUser(wxMessage.getFromUser()) | ||||
| .formId("自己替换可用的formid") | .formId("自己替换可用的formid") | ||||
| .build()); | .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()); | |||||
| } | |||||
| } | |||||