mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-04-24 08:57:16 +08:00
issue #24 模板消息接口
This commit is contained in:
parent
46e66d333a
commit
a69c972261
@ -88,6 +88,7 @@ public class WxConsts {
|
||||
public static final String EVT_PIC_PHOTO_OR_ALBUM = "pic_photo_or_album";
|
||||
public static final String EVT_PIC_WEIXIN = "pic_weixin";
|
||||
public static final String EVT_LOCATION_SELECT = "location_select";
|
||||
public static final String EVT_TEMPLATESENDJOBFINISH = "TEMPLATESENDJOBFINISH";
|
||||
|
||||
///////////////////////
|
||||
// 上传多媒体文件的类型
|
||||
|
@ -306,7 +306,17 @@ public interface WxMpService {
|
||||
* @throws WxErrorException
|
||||
*/
|
||||
public String shortUrl(String long_url) throws WxErrorException;
|
||||
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 发送模板消息
|
||||
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=模板消息接口
|
||||
* </pre>
|
||||
* @param templateMessage
|
||||
* @throws WxErrorException
|
||||
*/
|
||||
public void templateSend(WxMpTemplateMessage templateMessage) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* 注入 {@link WxMpConfigStorage} 的实现
|
||||
* @param wxConfigProvider
|
||||
|
@ -31,6 +31,7 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.StringReader;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
@ -272,7 +273,12 @@ public class WxMpServiceImpl implements WxMpService {
|
||||
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
|
||||
return tmpJsonElement.getAsJsonObject().get("short_url").getAsString();
|
||||
}
|
||||
|
||||
|
||||
public void templateSend(WxMpTemplateMessage templateMessage) throws WxErrorException {
|
||||
String url = "https://api.weixin.qq.com/cgi-bin/message/template/send";
|
||||
execute(new SimplePostRequestExecutor(), url, templateMessage.toJson());
|
||||
}
|
||||
|
||||
/**
|
||||
* 向微信端发送请求,在这里执行的策略是当发生access_token过期时才去刷新,然后重新执行请求,而不是全局定时请求
|
||||
* @param executor
|
||||
|
@ -0,0 +1,49 @@
|
||||
package me.chanjar.weixin.mp.bean;
|
||||
|
||||
/**
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
public class WxMpTemplateData {
|
||||
|
||||
private String name;
|
||||
private String value;
|
||||
private String color;
|
||||
|
||||
public WxMpTemplateData() {
|
||||
}
|
||||
|
||||
public WxMpTemplateData(String name, String value) {
|
||||
this.name = name;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public WxMpTemplateData(String name, String value, String color) {
|
||||
this.name = name;
|
||||
this.value = value;
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getColor() {
|
||||
return color;
|
||||
}
|
||||
public void setColor(String color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
package me.chanjar.weixin.mp.bean;
|
||||
|
||||
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by qianjia on 14/11/1.
|
||||
*/
|
||||
public class WxMpTemplateMessage {
|
||||
|
||||
private String toUser;
|
||||
private String templateId;
|
||||
private String url;
|
||||
private String topColor;
|
||||
private List<WxMpTemplateData> datas = new ArrayList<WxMpTemplateData>();
|
||||
|
||||
public String getToUser() {
|
||||
return toUser;
|
||||
}
|
||||
|
||||
public void setToUser(String toUser) {
|
||||
this.toUser = toUser;
|
||||
}
|
||||
|
||||
public String getTemplateId() {
|
||||
return templateId;
|
||||
}
|
||||
|
||||
public void setTemplateId(String templateId) {
|
||||
this.templateId = templateId;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public String getTopColor() {
|
||||
return topColor;
|
||||
}
|
||||
|
||||
public void setTopColor(String topColor) {
|
||||
this.topColor = topColor;
|
||||
}
|
||||
|
||||
public List<WxMpTemplateData> getDatas() {
|
||||
return datas;
|
||||
}
|
||||
|
||||
public void setDatas(List<WxMpTemplateData> datas) {
|
||||
this.datas = datas;
|
||||
}
|
||||
|
||||
public String toJson() {
|
||||
return WxMpGsonBuilder.INSTANCE.create().toJson(this);
|
||||
}
|
||||
}
|
@ -22,6 +22,7 @@ public class WxMpGsonBuilder {
|
||||
INSTANCE.registerTypeAdapter(WxMpMassSendResult.class, new WxMpMassSendResultAdapter());
|
||||
INSTANCE.registerTypeAdapter(WxMpMassUploadResult.class, new WxMpMassUploadResultAdapter());
|
||||
INSTANCE.registerTypeAdapter(WxMpQrCodeTicket.class, new WxQrCodeTicketAdapter());
|
||||
INSTANCE.registerTypeAdapter(WxMpTemplateMessage.class, new WxMpTemplateMessageGsonAdapter());
|
||||
}
|
||||
|
||||
public static Gson create() {
|
||||
|
@ -0,0 +1,50 @@
|
||||
/*
|
||||
* KINGSTAR MEDIA SOLUTIONS Co.,LTD. Copyright c 2005-2013. All rights reserved.
|
||||
*
|
||||
* This source code is the property of KINGSTAR MEDIA SOLUTIONS LTD. It is intended
|
||||
* only for the use of KINGSTAR MEDIA application development. Reengineering, reproduction
|
||||
* arose from modification of the original source, or other redistribution of this source
|
||||
* is not permitted without written permission of the KINGSTAR MEDIA SOLUTIONS LTD.
|
||||
*/
|
||||
package me.chanjar.weixin.mp.util.json;
|
||||
|
||||
import com.google.gson.*;
|
||||
import me.chanjar.weixin.common.api.WxConsts;
|
||||
import me.chanjar.weixin.mp.bean.WxMpCustomMessage;
|
||||
import me.chanjar.weixin.mp.bean.WxMpTemplateData;
|
||||
import me.chanjar.weixin.mp.bean.WxMpTemplateMessage;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
/**
|
||||
* @author qianjia
|
||||
*/
|
||||
public class WxMpTemplateMessageGsonAdapter implements JsonSerializer<WxMpTemplateMessage> {
|
||||
|
||||
public JsonElement serialize(WxMpTemplateMessage message, Type typeOfSrc, JsonSerializationContext context) {
|
||||
JsonObject messageJson = new JsonObject();
|
||||
messageJson.addProperty("touser", message.getToUser());
|
||||
messageJson.addProperty("template_id", message.getTemplateId());
|
||||
if (message.getUrl() != null) {
|
||||
messageJson.addProperty("url", message.getUrl());
|
||||
}
|
||||
if (message.getTopColor() != null) {
|
||||
messageJson.addProperty("topcolor", message.getTopColor());
|
||||
}
|
||||
|
||||
JsonObject datas = new JsonObject();
|
||||
messageJson.add("data", datas);
|
||||
|
||||
for (WxMpTemplateData data : message.getDatas()) {
|
||||
JsonObject dataJson = new JsonObject();
|
||||
dataJson.addProperty("value", data.getValue());
|
||||
if (data.getColor() != null) {
|
||||
dataJson.addProperty("color", data.getColor());
|
||||
}
|
||||
datas.add(data.getName(), dataJson);
|
||||
}
|
||||
|
||||
return messageJson;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user