Skip to content

Commit

Permalink
#760 小程序增加统一服务消息接口
Browse files Browse the repository at this point in the history
  • Loading branch information
binarywang committed Sep 23, 2018
1 parent b8e9d9d commit e237f0b
Show file tree
Hide file tree
Showing 12 changed files with 429 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import cn.binarywang.wx.miniapp.bean.WxMaKefuMessage;
import cn.binarywang.wx.miniapp.bean.WxMaTemplateMessage;
import cn.binarywang.wx.miniapp.bean.WxMaUniformMessage;
import me.chanjar.weixin.common.error.WxErrorException;

/**
Expand All @@ -14,6 +15,7 @@
public interface WxMaMsgService {
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 UNIFORM_MSG_SEND_URL = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send";

/**
* <pre>
Expand All @@ -32,4 +34,14 @@ public interface WxMaMsgService {
* </pre>
*/
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaKefuMessage;
import cn.binarywang.wx.miniapp.bean.WxMaTemplateMessage;
import cn.binarywang.wx.miniapp.bean.WxMaUniformMessage;
import cn.binarywang.wx.miniapp.constant.WxMaConstants;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
Expand Down Expand Up @@ -36,4 +37,13 @@ public void sendTemplateMsg(WxMaTemplateMessage templateMessage) throws WxErrorE
}
}

@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));
}
}

}
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public class WxMaTemplateMessage implements Serializable {
* 描述: 模板内容,不填则下发空模板
* </pre>
*/
private List<Data> data;
private List<WxMaTemplateData> data;

/**
* 模板内容字体的颜色,不填默认黑色.
Expand All @@ -95,7 +95,7 @@ public class WxMaTemplateMessage implements Serializable {
*/
private String emphasisKeyword;

public WxMaTemplateMessage addData(Data datum) {
public WxMaTemplateMessage addData(WxMaTemplateData datum) {
if (this.data == null) {
this.data = new ArrayList<>();
}
Expand All @@ -108,24 +108,4 @@ public String toJson() {
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;
}

}

}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cn.binarywang.wx.miniapp.util.json;

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.WxMaUserPortrait;
import cn.binarywang.wx.miniapp.bean.analysis.WxMaVisitDistribution;
Expand All @@ -18,6 +19,7 @@ public class WxMaGsonBuilder {
static {
INSTANCE.disableHtmlEscaping();
INSTANCE.registerTypeAdapter(WxMaTemplateMessage.class, new WxMaTemplateMessageGsonAdapter());
INSTANCE.registerTypeAdapter(WxMaUniformMessage.class, new WxMaUniformMessageGsonAdapter());
INSTANCE.registerTypeAdapter(WxMaCodeCommitRequest.class, new WxMaCodeCommitRequestGsonAdapter());
INSTANCE.registerTypeAdapter(WxMaCodeVersionDistribution.class, new WxMaCodeVersionDistributionGsonAdapter());
INSTANCE.registerTypeAdapter(WxMaVisitDistribution.class, new WxMaVisitDistributionGsonAdapter());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
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 com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

import java.lang.reflect.Type;

/**
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
Expand All @@ -26,10 +27,6 @@ public JsonElement serialize(WxMaTemplateMessage message, Type typeOfSrc, JsonSe
messageJson.addProperty("form_id", message.getFormId());
}

if (message.getPage() != null) {
messageJson.addProperty("page", message.getPage());
}

if (message.getColor() != null) {
messageJson.addProperty("color", message.getColor());
}
Expand All @@ -45,7 +42,7 @@ public JsonElement serialize(WxMaTemplateMessage message, Type typeOfSrc, JsonSe
return messageJson;
}

for (WxMaTemplateMessage.Data datum : message.getData()) {
for (WxMaTemplateData datum : message.getData()) {
JsonObject dataJson = new JsonObject();
dataJson.addProperty("value", datum.getValue());
if (datum.getColor() != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}

}
Loading

0 comments on commit e237f0b

Please sign in to comment.