mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2026-02-17 21:56:28 +08:00
#425 小程序客服消息新增图文链接消息支持
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
package cn.binarywang.wx.miniapp.bean;
|
||||
|
||||
import cn.binarywang.wx.miniapp.builder.ImageBuilder;
|
||||
import cn.binarywang.wx.miniapp.builder.LinkBuilder;
|
||||
import cn.binarywang.wx.miniapp.builder.TextBuilder;
|
||||
import cn.binarywang.wx.miniapp.util.json.WxMaGsonBuilder;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
@@ -16,13 +20,59 @@ import java.io.Serializable;
|
||||
public class WxMaKefuMessage implements Serializable {
|
||||
private static final long serialVersionUID = -9196732086954365246L;
|
||||
|
||||
@SerializedName("touser")
|
||||
private String toUser;
|
||||
|
||||
@SerializedName("msgtype")
|
||||
private String msgType;
|
||||
private String content;
|
||||
private String mediaId;
|
||||
private String thumbMediaId;
|
||||
private String title;
|
||||
private String description;
|
||||
|
||||
@SerializedName("text")
|
||||
private KfText text;
|
||||
|
||||
@SerializedName("image")
|
||||
private KfImage image;
|
||||
|
||||
@SerializedName("link")
|
||||
private KfLink link;
|
||||
|
||||
@SerializedName("miniprogrampage")
|
||||
private KfMaPage maPage;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public static class KfText {
|
||||
private String content;
|
||||
}
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public static class KfImage {
|
||||
@SerializedName("media_id")
|
||||
private String mediaId;
|
||||
}
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
public static class KfLink {
|
||||
private String title;
|
||||
private String description;
|
||||
private String url;
|
||||
|
||||
@SerializedName("thumb_url")
|
||||
private String thumbUrl;
|
||||
}
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
public static class KfMaPage {
|
||||
private String title;
|
||||
|
||||
@SerializedName("pagepath")
|
||||
private String pagePath;
|
||||
|
||||
@SerializedName("thumb_media_id")
|
||||
private String thumbMediaId;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得文本消息builder.
|
||||
@@ -38,8 +88,15 @@ public class WxMaKefuMessage implements Serializable {
|
||||
return new ImageBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得图文链接消息builder.
|
||||
*/
|
||||
public static LinkBuilder newLinkBuilder() {
|
||||
return new LinkBuilder();
|
||||
}
|
||||
|
||||
public String toJson() {
|
||||
return WxMaGsonBuilder.create().toJson(this);
|
||||
return new GsonBuilder().create().toJson(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -31,8 +31,9 @@ public class WxMaWxcodeLimit extends AbstractWxMaQrcodeWrapper implements Serial
|
||||
return WxMaGsonBuilder.create().fromJson(json, WxMaWxcodeLimit.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return super.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@ import cn.binarywang.wx.miniapp.bean.WxMaKefuMessage;
|
||||
import cn.binarywang.wx.miniapp.constant.WxMaConstants;
|
||||
|
||||
/**
|
||||
* 图片消息builder.
|
||||
*
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
*/
|
||||
public final class ImageBuilder extends BaseBuilder<ImageBuilder> {
|
||||
@@ -21,7 +23,7 @@ public final class ImageBuilder extends BaseBuilder<ImageBuilder> {
|
||||
@Override
|
||||
public WxMaKefuMessage build() {
|
||||
WxMaKefuMessage m = super.build();
|
||||
m.setMediaId(this.mediaId);
|
||||
m.setImage(new WxMaKefuMessage.KfImage(this.mediaId));
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
package cn.binarywang.wx.miniapp.builder;
|
||||
|
||||
import cn.binarywang.wx.miniapp.bean.WxMaKefuMessage;
|
||||
import cn.binarywang.wx.miniapp.constant.WxMaConstants;
|
||||
|
||||
/**
|
||||
* 图文链接builder
|
||||
*
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
*/
|
||||
public class LinkBuilder extends BaseBuilder<LinkBuilder> {
|
||||
private String title;
|
||||
private String description;
|
||||
private String url;
|
||||
private String thumbUrl;
|
||||
|
||||
public LinkBuilder() {
|
||||
this.msgType = WxMaConstants.KefuMsgType.IMAGE;
|
||||
}
|
||||
|
||||
public LinkBuilder title(String title) {
|
||||
this.title = title;
|
||||
return this;
|
||||
}
|
||||
|
||||
public LinkBuilder description(String description) {
|
||||
this.description = description;
|
||||
return this;
|
||||
}
|
||||
|
||||
public LinkBuilder url(String url) {
|
||||
this.url = url;
|
||||
return this;
|
||||
}
|
||||
|
||||
public LinkBuilder thumbUrl(String thumbUrl) {
|
||||
this.thumbUrl = thumbUrl;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMaKefuMessage build() {
|
||||
WxMaKefuMessage m = super.build();
|
||||
m.setLink(WxMaKefuMessage.KfLink.builder().title(this.title)
|
||||
.description(this.description)
|
||||
.url(this.url)
|
||||
.thumbUrl(this.thumbUrl)
|
||||
.build()
|
||||
);
|
||||
return m;
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,8 @@ import cn.binarywang.wx.miniapp.bean.WxMaKefuMessage;
|
||||
import cn.binarywang.wx.miniapp.constant.WxMaConstants;
|
||||
|
||||
/**
|
||||
* 文本消息builder.
|
||||
*
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
*/
|
||||
public final class TextBuilder extends BaseBuilder<TextBuilder> {
|
||||
@@ -21,7 +23,7 @@ public final class TextBuilder extends BaseBuilder<TextBuilder> {
|
||||
@Override
|
||||
public WxMaKefuMessage build() {
|
||||
WxMaKefuMessage m = super.build();
|
||||
m.setContent(this.content);
|
||||
m.setText(new WxMaKefuMessage.KfText(this.content));
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,29 +2,29 @@ package cn.binarywang.wx.miniapp.constant;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 小程序常量
|
||||
* 小程序常量.
|
||||
* </pre>
|
||||
*
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
*/
|
||||
public class WxMaConstants {
|
||||
/**
|
||||
* 微信接口返回的参数errcode
|
||||
* 微信接口返回的参数errcode.
|
||||
*/
|
||||
public static final String ERRCODE = "errcode";
|
||||
|
||||
/**
|
||||
* 素材类型
|
||||
* 素材类型.
|
||||
*/
|
||||
public static class MediaType {
|
||||
/**
|
||||
* 图片
|
||||
* 图片.
|
||||
*/
|
||||
public static final String IMAGE = "image";
|
||||
}
|
||||
|
||||
/**
|
||||
* 消息格式
|
||||
* 消息格式.
|
||||
*/
|
||||
public static class MsgDataFormat {
|
||||
public static final String XML = "XML";
|
||||
@@ -32,32 +32,40 @@ public class WxMaConstants {
|
||||
}
|
||||
|
||||
/**
|
||||
* 客服消息的消息类型
|
||||
* 客服消息的消息类型.
|
||||
*/
|
||||
public static class KefuMsgType {
|
||||
/**
|
||||
* 文本消息
|
||||
* 文本消息.
|
||||
*/
|
||||
public static final String TEXT = "text";
|
||||
/**
|
||||
* 图片消息
|
||||
* 图片消息.
|
||||
*/
|
||||
public static final String IMAGE = "image";
|
||||
/**
|
||||
* 图文链接.
|
||||
*/
|
||||
public static final String LINK = "link";
|
||||
/**
|
||||
* 小程序卡片消息.
|
||||
*/
|
||||
public static final String MA_PAGE = "miniprogrampage";
|
||||
}
|
||||
|
||||
public static final class ErrorCode {
|
||||
/**
|
||||
* 40001 获取access_token时AppSecret错误,或者access_token无效
|
||||
* 40001 获取access_token时AppSecret错误,或者access_token无效.
|
||||
*/
|
||||
public static final int ERR_40001 = 40001;
|
||||
|
||||
/**
|
||||
* 42001 access_token超时
|
||||
* 42001 access_token超时.
|
||||
*/
|
||||
public static final int ERR_42001 = 42001;
|
||||
|
||||
/**
|
||||
* 40014 不合法的access_token,请开发者认真比对access_token的有效性(如是否过期)
|
||||
* 40014 不合法的access_token,请开发者认真比对access_token的有效性(如是否过期).
|
||||
*/
|
||||
public static final int ERR_40014 = 40014;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package cn.binarywang.wx.miniapp.util.json;
|
||||
|
||||
import cn.binarywang.wx.miniapp.bean.WxMaKefuMessage;
|
||||
import cn.binarywang.wx.miniapp.bean.WxMaTemplateMessage;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
@@ -13,7 +12,6 @@ public class WxMaGsonBuilder {
|
||||
|
||||
static {
|
||||
INSTANCE.disableHtmlEscaping();
|
||||
INSTANCE.registerTypeAdapter(WxMaKefuMessage.class, new WxMaKefuMessageGsonAdapter());
|
||||
INSTANCE.registerTypeAdapter(WxMaTemplateMessage.class, new WxMaTemplateMessageGsonAdapter());
|
||||
}
|
||||
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
package cn.binarywang.wx.miniapp.util.json;
|
||||
|
||||
import cn.binarywang.wx.miniapp.bean.WxMaKefuMessage;
|
||||
import cn.binarywang.wx.miniapp.constant.WxMaConstants;
|
||||
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>
|
||||
*/
|
||||
public class WxMaKefuMessageGsonAdapter implements JsonSerializer<WxMaKefuMessage> {
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(WxMaKefuMessage message, Type typeOfSrc, JsonSerializationContext context) {
|
||||
JsonObject messageJson = new JsonObject();
|
||||
messageJson.addProperty("touser", message.getToUser());
|
||||
messageJson.addProperty("msgtype", message.getMsgType());
|
||||
|
||||
if (WxMaConstants.KefuMsgType.TEXT.equals(message.getMsgType())) {
|
||||
JsonObject text = new JsonObject();
|
||||
text.addProperty("content", message.getContent());
|
||||
messageJson.add("text", text);
|
||||
}
|
||||
|
||||
if (WxMaConstants.KefuMsgType.IMAGE.equals(message.getMsgType())) {
|
||||
JsonObject image = new JsonObject();
|
||||
image.addProperty("media_id", message.getMediaId());
|
||||
messageJson.add("image", image);
|
||||
}
|
||||
|
||||
return messageJson;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user