mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-10-29 09:38:19 +08:00
🎨 精简代码,移除无用过期接口代码
This commit is contained in:
@ -32,20 +32,6 @@ public interface WxMaMsgService {
|
||||
*/
|
||||
boolean sendKefuMsg(WxMaKefuMessage message) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 发送模板消息
|
||||
* 详情请见: <a href="https://developers.weixin.qq.com/miniprogram/dev/api-backend/templateMessage.send.html">发送模板消息</a>
|
||||
* 接口url格式:https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=ACCESS_TOKEN
|
||||
* 小程序模板消息接口将于2020年1月10日下线,开发者可使用订阅消息功能
|
||||
* </pre>
|
||||
*
|
||||
* @param templateMessage 模版消息
|
||||
* @throws WxErrorException .
|
||||
*/
|
||||
@Deprecated
|
||||
void sendTemplateMsg(WxMaTemplateMessage templateMessage) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 发送订阅消息
|
||||
|
||||
@ -229,13 +229,6 @@ public interface WxMaService extends WxService {
|
||||
*/
|
||||
WxMaQrcodeService getQrcodeService();
|
||||
|
||||
/**
|
||||
* 返回模板配置相关接口方法的实现类对象, 以方便调用其各个接口.
|
||||
*
|
||||
* @return WxMaTemplateService
|
||||
*/
|
||||
WxMaTemplateService getTemplateService();
|
||||
|
||||
/**
|
||||
* 返回订阅消息配置相关接口方法的实现类对象, 以方便调用其各个接口.
|
||||
*
|
||||
|
||||
@ -1,90 +0,0 @@
|
||||
package cn.binarywang.wx.miniapp.api;
|
||||
|
||||
import cn.binarywang.wx.miniapp.bean.template.WxMaTemplateAddResult;
|
||||
import cn.binarywang.wx.miniapp.bean.template.WxMaTemplateLibraryGetResult;
|
||||
import cn.binarywang.wx.miniapp.bean.template.WxMaTemplateLibraryListResult;
|
||||
import cn.binarywang.wx.miniapp.bean.template.WxMaTemplateListResult;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author IOMan(lewis.lynn1006@gmail.com)
|
||||
*/
|
||||
@Deprecated
|
||||
public interface WxMaTemplateService {
|
||||
/**
|
||||
* 获取小程序模板库标题列表.
|
||||
*/
|
||||
String TEMPLATE_LIBRARY_LIST_URL = "https://api.weixin.qq.com/cgi-bin/wxopen/template/library/list";
|
||||
|
||||
/**
|
||||
* 获取模板库某个模板标题下关键词库.
|
||||
*/
|
||||
String TEMPLATE_LIBRARY_KEYWORD_URL = "https://api.weixin.qq.com/cgi-bin/wxopen/template/library/get";
|
||||
|
||||
/**
|
||||
* 组合模板并添加至帐号下的个人模板库.
|
||||
*/
|
||||
String TEMPLATE_ADD_URL = "https://api.weixin.qq.com/cgi-bin/wxopen/template/add";
|
||||
|
||||
/**
|
||||
* 获取帐号下已存在的模板列表.
|
||||
*/
|
||||
String TEMPLATE_LIST_URL = "https://api.weixin.qq.com/cgi-bin/wxopen/template/list";
|
||||
|
||||
/**
|
||||
* 删除帐号下的某个模板.
|
||||
*/
|
||||
String TEMPLATE_DEL_URL = "https://api.weixin.qq.com/cgi-bin/wxopen/template/del";
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 获取小程序模板库标题列表
|
||||
*
|
||||
* 详情请见: <a href="https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1500465446_j4CgR&token=&lang=zh_CN">获取小程序模板库标题列表</a>
|
||||
* 接口url格式: https://api.weixin.qq.com/cgi-bin/wxopen/template/library/list?access_token=ACCESS_TOKEN
|
||||
* </pre>
|
||||
*/
|
||||
WxMaTemplateLibraryListResult findTemplateLibraryList(int offset, int count) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 获取模板库某个模板标题下关键词库
|
||||
*
|
||||
* 详情请见: <a href="https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1500465446_j4CgR&token=&lang=zh_CN">获取小程序模板库标题列表</a>
|
||||
* 接口url格式: https://api.weixin.qq.com/cgi-bin/wxopen/template/library/get?access_token=ACCESS_TOKEN
|
||||
* </pre>
|
||||
*/
|
||||
WxMaTemplateLibraryGetResult findTemplateLibraryKeywordList(String id) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 组合模板并添加至帐号下的个人模板库
|
||||
*
|
||||
* 详情请见: <a href="https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1500465446_j4CgR&token=&lang=zh_CN">获取小程序模板库标题列表</a>
|
||||
* 接口url格式: https://api.weixin.qq.com/cgi-bin/wxopen/template/add?access_token=ACCESS_TOKEN
|
||||
* </pre>
|
||||
*/
|
||||
WxMaTemplateAddResult addTemplate(String id, List<Integer> keywordIdList) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 获取帐号下已存在的模板列表
|
||||
*
|
||||
* 详情请见: <a href="https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1500465446_j4CgR&token=&lang=zh_CN">获取小程序模板库标题列表</a>
|
||||
* 接口url格式: https://api.weixin.qq.com/cgi-bin/wxopen/template/list?access_token=ACCESS_TOKEN
|
||||
* </pre>
|
||||
*/
|
||||
WxMaTemplateListResult findTemplateList(int offset, int count) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 删除帐号下的某个模板
|
||||
*
|
||||
* 详情请见: <a href="https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1500465446_j4CgR&token=&lang=zh_CN">获取小程序模板库标题列表</a>
|
||||
* 接口url格式: https://api.weixin.qq.com/cgi-bin/wxopen/template/list?access_token=ACCESS_TOKEN
|
||||
* </pre>
|
||||
*/
|
||||
boolean delTemplate(String templateId) throws WxErrorException;
|
||||
}
|
||||
@ -40,13 +40,11 @@ import java.util.concurrent.locks.Lock;
|
||||
@Slf4j
|
||||
public abstract class BaseWxMaServiceImpl<H, P> implements WxMaService, RequestHttp<H, P> {
|
||||
private Map<String, WxMaConfig> configMap;
|
||||
private WxMaConfig wxMaConfig;
|
||||
|
||||
private final WxMaMsgService kefuService = new WxMaMsgServiceImpl(this);
|
||||
private final WxMaMediaService materialService = new WxMaMediaServiceImpl(this);
|
||||
private final WxMaUserService userService = new WxMaUserServiceImpl(this);
|
||||
private final WxMaQrcodeService qrCodeService = new WxMaQrcodeServiceImpl(this);
|
||||
private final WxMaTemplateService templateService = new WxMaTemplateServiceImpl(this);
|
||||
private final WxMaAnalysisService analysisService = new WxMaAnalysisServiceImpl(this);
|
||||
private final WxMaCodeService codeService = new WxMaCodeServiceImpl(this);
|
||||
private final WxMaSettingService settingService = new WxMaSettingServiceImpl(this);
|
||||
@ -275,9 +273,9 @@ public abstract class BaseWxMaServiceImpl<H, P> implements WxMaService, RequestH
|
||||
/**
|
||||
* 设置当前的AccessToken
|
||||
*
|
||||
* @param resultContent
|
||||
* @return
|
||||
* @throws WxErrorException
|
||||
* @param resultContent 响应内容
|
||||
* @return access token
|
||||
* @throws WxErrorException 异常
|
||||
*/
|
||||
protected String extractAccessToken(String resultContent) throws WxErrorException {
|
||||
WxMaConfig config = this.getWxMaConfig();
|
||||
@ -399,11 +397,6 @@ public abstract class BaseWxMaServiceImpl<H, P> implements WxMaService, RequestH
|
||||
return this.qrCodeService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMaTemplateService getTemplateService() {
|
||||
return this.templateService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMaSubscribeService getSubscribeService() {
|
||||
return this.subscribeService;
|
||||
|
||||
@ -17,8 +17,7 @@ import me.chanjar.weixin.common.util.json.GsonParser;
|
||||
*/
|
||||
@AllArgsConstructor
|
||||
public class WxMaMsgServiceImpl implements WxMaMsgService {
|
||||
|
||||
private WxMaService wxMaService;
|
||||
private final WxMaService wxMaService;
|
||||
|
||||
@Override
|
||||
public boolean sendKefuMsg(WxMaKefuMessage message) throws WxErrorException {
|
||||
@ -26,15 +25,6 @@ public class WxMaMsgServiceImpl implements WxMaMsgService {
|
||||
return responseContent != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendTemplateMsg(WxMaTemplateMessage templateMessage) throws WxErrorException {
|
||||
String responseContent = this.wxMaService.post(TEMPLATE_MSG_SEND_URL, templateMessage.toJson());
|
||||
JsonObject jsonObject = GsonParser.parse(responseContent);
|
||||
if (jsonObject.get(WxMaConstants.ERRCODE).getAsInt() != 0) {
|
||||
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendSubscribeMsg(WxMaSubscribeMessage subscribeMessage) throws WxErrorException {
|
||||
String responseContent = this.wxMaService.post(SUBSCRIBE_MSG_SEND_URL, subscribeMessage.toJson());
|
||||
|
||||
@ -1,58 +0,0 @@
|
||||
package cn.binarywang.wx.miniapp.api.impl;
|
||||
|
||||
import cn.binarywang.wx.miniapp.api.WxMaService;
|
||||
import cn.binarywang.wx.miniapp.api.WxMaTemplateService;
|
||||
import cn.binarywang.wx.miniapp.bean.template.WxMaTemplateAddResult;
|
||||
import cn.binarywang.wx.miniapp.bean.template.WxMaTemplateLibraryGetResult;
|
||||
import cn.binarywang.wx.miniapp.bean.template.WxMaTemplateLibraryListResult;
|
||||
import cn.binarywang.wx.miniapp.bean.template.WxMaTemplateListResult;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import lombok.AllArgsConstructor;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Binary
|
||||
*/
|
||||
@AllArgsConstructor
|
||||
public class WxMaTemplateServiceImpl implements WxMaTemplateService {
|
||||
private WxMaService wxMaService;
|
||||
|
||||
@Override
|
||||
public WxMaTemplateLibraryListResult findTemplateLibraryList(int offset, int count) throws WxErrorException {
|
||||
Map<String, Integer> params = ImmutableMap.of("offset", offset, "count", count);
|
||||
String responseText = this.wxMaService.post(TEMPLATE_LIBRARY_LIST_URL, WxGsonBuilder.create().toJson(params));
|
||||
return WxMaTemplateLibraryListResult.fromJson(responseText);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMaTemplateLibraryGetResult findTemplateLibraryKeywordList(String id) throws WxErrorException {
|
||||
String responseText = this.wxMaService.post(TEMPLATE_LIBRARY_KEYWORD_URL,
|
||||
WxGsonBuilder.create().toJson(ImmutableMap.of("id", id)));
|
||||
return WxMaTemplateLibraryGetResult.fromJson(responseText);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMaTemplateAddResult addTemplate(String id, List<Integer> keywordIdList) throws WxErrorException {
|
||||
String responseText = this.wxMaService.post(TEMPLATE_ADD_URL,
|
||||
WxGsonBuilder.create().toJson(ImmutableMap.of("id", id, "keyword_id_list", keywordIdList.toArray())));
|
||||
return WxMaTemplateAddResult.fromJson(responseText);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMaTemplateListResult findTemplateList(int offset, int count) throws WxErrorException {
|
||||
Map<String, Integer> params = ImmutableMap.of("offset", offset, "count", count);
|
||||
String responseText = this.wxMaService.post(TEMPLATE_LIST_URL, WxGsonBuilder.create().toJson(params));
|
||||
return WxMaTemplateListResult.fromJson(responseText);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean delTemplate(String templateId) throws WxErrorException {
|
||||
Map<String, String> params = ImmutableMap.of("template_id", templateId);
|
||||
this.wxMaService.post(TEMPLATE_DEL_URL, WxGsonBuilder.create().toJson(params));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -1,101 +0,0 @@
|
||||
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.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
/**
|
||||
* 模板消息.
|
||||
* 参考 https://developers.weixin.qq.com/miniprogram/dev/api-backend/templateMessage.send.html
|
||||
*
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Builder
|
||||
public class WxMaTemplateMessage implements Serializable {
|
||||
private static final long serialVersionUID = 5063374783759519418L;
|
||||
|
||||
/**
|
||||
* 接收者(用户)的 openid.
|
||||
* <pre>
|
||||
* 参数:touser
|
||||
* 是否必填: 是
|
||||
* 描述: 接收者(用户)的 openid
|
||||
* </pre>
|
||||
*/
|
||||
private String toUser;
|
||||
|
||||
/**
|
||||
* 所需下发的模板消息的id.
|
||||
* <pre>
|
||||
* 参数:template_id
|
||||
* 是否必填: 是
|
||||
* 描述: 所需下发的模板消息的id
|
||||
* </pre>
|
||||
*/
|
||||
private String templateId;
|
||||
|
||||
/**
|
||||
* 点击模板卡片后的跳转页面,仅限本小程序内的页面.
|
||||
* <pre>
|
||||
* 参数:page
|
||||
* 是否必填: 否
|
||||
* 描述: 点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。
|
||||
* </pre>
|
||||
*/
|
||||
private String page;
|
||||
|
||||
/**
|
||||
* 表单提交场景下,为 submit 事件带上的 formId;支付场景下,为本次支付的 prepay_id.
|
||||
* <pre>
|
||||
* 参数:form_id
|
||||
* 是否必填: 是
|
||||
* 描述: 表单提交场景下,为 submit 事件带上的 formId;支付场景下,为本次支付的 prepay_id
|
||||
* </pre>
|
||||
*/
|
||||
private String formId;
|
||||
|
||||
/**
|
||||
* 模板内容,不填则下发空模板.
|
||||
* <pre>
|
||||
* 参数:data
|
||||
* 是否必填: 是
|
||||
* 描述: 模板内容,不填则下发空模板
|
||||
* </pre>
|
||||
*/
|
||||
private List<WxMaTemplateData> data;
|
||||
|
||||
/**
|
||||
* 模板需要放大的关键词,不填则默认无放大.
|
||||
* <pre>
|
||||
* 参数:emphasis_keyword
|
||||
* 是否必填: 否
|
||||
* 描述: 模板需要放大的关键词,不填则默认无放大
|
||||
* </pre>
|
||||
*/
|
||||
private String emphasisKeyword;
|
||||
|
||||
public WxMaTemplateMessage 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);
|
||||
}
|
||||
|
||||
}
|
||||
@ -12,10 +12,25 @@ import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
|
||||
*/
|
||||
public interface WxMaConfig {
|
||||
|
||||
/**
|
||||
* Gets access token.
|
||||
*
|
||||
* @return the access token
|
||||
*/
|
||||
String getAccessToken();
|
||||
|
||||
/**
|
||||
* Gets access token lock.
|
||||
*
|
||||
* @return the access token lock
|
||||
*/
|
||||
Lock getAccessTokenLock();
|
||||
|
||||
/**
|
||||
* Is access token expired boolean.
|
||||
*
|
||||
* @return the boolean
|
||||
*/
|
||||
boolean isAccessTokenExpired();
|
||||
|
||||
/**
|
||||
@ -38,10 +53,25 @@ public interface WxMaConfig {
|
||||
*/
|
||||
void updateAccessToken(String accessToken, int expiresInSeconds);
|
||||
|
||||
/**
|
||||
* Gets jsapi ticket.
|
||||
*
|
||||
* @return the jsapi ticket
|
||||
*/
|
||||
String getJsapiTicket();
|
||||
|
||||
/**
|
||||
* Gets jsapi ticket lock.
|
||||
*
|
||||
* @return the jsapi ticket lock
|
||||
*/
|
||||
Lock getJsapiTicketLock();
|
||||
|
||||
/**
|
||||
* Is jsapi ticket expired boolean.
|
||||
*
|
||||
* @return the boolean
|
||||
*/
|
||||
boolean isJsapiTicketExpired();
|
||||
|
||||
/**
|
||||
@ -59,11 +89,23 @@ public interface WxMaConfig {
|
||||
|
||||
/**
|
||||
* 卡券api_ticket.
|
||||
*
|
||||
* @return the card api ticket
|
||||
*/
|
||||
String getCardApiTicket();
|
||||
|
||||
/**
|
||||
* Gets card api ticket lock.
|
||||
*
|
||||
* @return the card api ticket lock
|
||||
*/
|
||||
Lock getCardApiTicketLock();
|
||||
|
||||
/**
|
||||
* Is card api ticket expired boolean.
|
||||
*
|
||||
* @return the boolean
|
||||
*/
|
||||
boolean isCardApiTicketExpired();
|
||||
|
||||
/**
|
||||
@ -74,44 +116,106 @@ public interface WxMaConfig {
|
||||
/**
|
||||
* 应该是线程安全的.
|
||||
*
|
||||
* @param apiTicket 新的卡券api ticket值
|
||||
* @param apiTicket 新的卡券api ticket值
|
||||
* @param expiresInSeconds 过期时间,以秒为单位
|
||||
*/
|
||||
void updateCardApiTicket(String apiTicket, int expiresInSeconds);
|
||||
|
||||
/**
|
||||
* Gets appid.
|
||||
*
|
||||
* @return the appid
|
||||
*/
|
||||
String getAppid();
|
||||
|
||||
/**
|
||||
* Gets secret.
|
||||
*
|
||||
* @return the secret
|
||||
*/
|
||||
String getSecret();
|
||||
|
||||
/**
|
||||
* Gets token.
|
||||
*
|
||||
* @return the token
|
||||
*/
|
||||
String getToken();
|
||||
|
||||
/**
|
||||
* Gets aes key.
|
||||
*
|
||||
* @return the aes key
|
||||
*/
|
||||
String getAesKey();
|
||||
|
||||
/**
|
||||
* Gets original id.
|
||||
*
|
||||
* @return the original id
|
||||
*/
|
||||
String getOriginalId();
|
||||
|
||||
/**
|
||||
* Gets cloud env.
|
||||
*
|
||||
* @return the cloud env
|
||||
*/
|
||||
String getCloudEnv();
|
||||
|
||||
/**
|
||||
* Gets msg data format.
|
||||
*
|
||||
* @return the msg data format
|
||||
*/
|
||||
String getMsgDataFormat();
|
||||
|
||||
/**
|
||||
* Gets expires time.
|
||||
*
|
||||
* @return the expires time
|
||||
*/
|
||||
long getExpiresTime();
|
||||
|
||||
/**
|
||||
* Gets http proxy host.
|
||||
*
|
||||
* @return the http proxy host
|
||||
*/
|
||||
String getHttpProxyHost();
|
||||
|
||||
/**
|
||||
* Gets http proxy port.
|
||||
*
|
||||
* @return the http proxy port
|
||||
*/
|
||||
int getHttpProxyPort();
|
||||
|
||||
/**
|
||||
* Gets http proxy username.
|
||||
*
|
||||
* @return the http proxy username
|
||||
*/
|
||||
String getHttpProxyUsername();
|
||||
|
||||
/**
|
||||
* Gets http proxy password.
|
||||
*
|
||||
* @return the http proxy password
|
||||
*/
|
||||
String getHttpProxyPassword();
|
||||
|
||||
/**
|
||||
* http client builder
|
||||
*
|
||||
* @return ApacheHttpClientBuilder
|
||||
* @return ApacheHttpClientBuilder apache http client builder
|
||||
*/
|
||||
ApacheHttpClientBuilder getApacheHttpClientBuilder();
|
||||
|
||||
/**
|
||||
* 是否自动刷新token
|
||||
*
|
||||
* @return the boolean
|
||||
*/
|
||||
boolean autoRefreshToken();
|
||||
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
package cn.binarywang.wx.miniapp.util.json;
|
||||
|
||||
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
|
||||
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;
|
||||
@ -19,7 +18,6 @@ public class WxMaGsonBuilder {
|
||||
|
||||
static {
|
||||
INSTANCE.disableHtmlEscaping();
|
||||
INSTANCE.registerTypeAdapter(WxMaTemplateMessage.class, new WxMaTemplateMessageGsonAdapter());
|
||||
INSTANCE.registerTypeAdapter(WxMaSubscribeMessage.class, new WxMaSubscribeMessageGsonAdapter());
|
||||
INSTANCE.registerTypeAdapter(WxMaUniformMessage.class, new WxMaUniformMessageGsonAdapter());
|
||||
INSTANCE.registerTypeAdapter(WxMaCodeCommitRequest.class, new WxMaCodeCommitRequestGsonAdapter());
|
||||
|
||||
@ -1,50 +0,0 @@
|
||||
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;
|
||||
|
||||
/**
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
*/
|
||||
public class WxMaTemplateMessageGsonAdapter implements JsonSerializer<WxMaTemplateMessage> {
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(WxMaTemplateMessage message, Type typeOfSrc, JsonSerializationContext context) {
|
||||
JsonObject messageJson = new JsonObject();
|
||||
messageJson.addProperty("touser", message.getToUser());
|
||||
messageJson.addProperty("template_id", message.getTemplateId());
|
||||
if (message.getPage() != null) {
|
||||
messageJson.addProperty("page", message.getPage());
|
||||
}
|
||||
|
||||
if (message.getFormId() != null) {
|
||||
messageJson.addProperty("form_id", message.getFormId());
|
||||
}
|
||||
|
||||
if (message.getEmphasisKeyword() != null) {
|
||||
messageJson.addProperty("emphasis_keyword", message.getEmphasisKeyword());
|
||||
}
|
||||
|
||||
JsonObject data = new JsonObject();
|
||||
messageJson.add("data", data);
|
||||
|
||||
if (message.getData() == null) {
|
||||
return messageJson;
|
||||
}
|
||||
|
||||
for (WxMaTemplateData datum : message.getData()) {
|
||||
JsonObject dataJson = new JsonObject();
|
||||
dataJson.addProperty("value", datum.getValue());
|
||||
data.add(datum.getName(), dataJson);
|
||||
}
|
||||
|
||||
return messageJson;
|
||||
}
|
||||
|
||||
}
|
||||
@ -40,28 +40,6 @@ public class WxMaMsgServiceImplTest {
|
||||
this.wxService.getMsgService().sendKefuMsg(message);
|
||||
}
|
||||
|
||||
@Test(invocationCount = 5, threadPoolSize = 3)
|
||||
public void testSendTemplateMsg() throws WxErrorException {
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
|
||||
TestConfig config = (TestConfig) this.wxService.getWxMaConfig();
|
||||
|
||||
WxMaTemplateMessage templateMessage = WxMaTemplateMessage.builder()
|
||||
.toUser(config.getOpenid())
|
||||
.formId("FORMID")
|
||||
.page("index")
|
||||
.data(Lists.newArrayList(
|
||||
new WxMaTemplateData("keyword1", "339208499"),
|
||||
new WxMaTemplateData("keyword2", dateFormat.format(new Date())),
|
||||
new WxMaTemplateData("keyword3", "粤海喜来登酒店"),
|
||||
new WxMaTemplateData("keyword4", "广州市天河区天河路208号")))
|
||||
.templateId(config.getTemplateId())
|
||||
.emphasisKeyword("keyword1.DATA")
|
||||
.build();
|
||||
//templateMessage.addData( new WxMaTemplateData("keyword1", "339208499", "#173177"));
|
||||
this.wxService.getMsgService().sendTemplateMsg(templateMessage);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testSendSubscribeMsg() throws WxErrorException {
|
||||
TestConfig config = (TestConfig) this.wxService.getWxMaConfig();
|
||||
|
||||
@ -1,32 +0,0 @@
|
||||
package cn.binarywang.wx.miniapp.bean;
|
||||
|
||||
import org.testng.annotations.*;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
import static org.testng.AssertJUnit.*;
|
||||
|
||||
/**
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
*/
|
||||
public class WxMaTemplateMessageTest {
|
||||
@Test
|
||||
public void testToJson() throws Exception {
|
||||
WxMaTemplateMessage tm = WxMaTemplateMessage.builder()
|
||||
.toUser("OPENID")
|
||||
//.color("aaaaa")
|
||||
.formId("FORMID")
|
||||
.page("index")
|
||||
.data(Lists.newArrayList(
|
||||
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();
|
||||
|
||||
assertEquals(tm.toJson(), "{\"touser\":\"OPENID\",\"template_id\":\"TEMPLATE_ID\",\"page\":\"index\",\"form_id\":\"FORMID\",\"emphasis_keyword\":\"keyword1.DATA\",\"data\":{\"keyword1\":{\"value\":\"339208499\",\"color\":\"#173177\"},\"keyword2\":{\"value\":\"2015年01月05日12:30\",\"color\":\"#173177\"},\"keyword3\":{\"value\":\"粤海喜来登酒店\",\"color\":\"#173177\"},\"keyword4\":{\"value\":\"广州市天河区天河路208号\",\"color\":\"#173177\"}}}");
|
||||
}
|
||||
|
||||
}
|
||||
@ -4,15 +4,12 @@ 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;
|
||||
import cn.binarywang.wx.miniapp.message.WxMaMessageHandler;
|
||||
import cn.binarywang.wx.miniapp.message.WxMaMessageRouter;
|
||||
import cn.binarywang.wx.miniapp.message.WxMaXmlOutMessage;
|
||||
import cn.binarywang.wx.miniapp.test.TestConfig;
|
||||
import com.google.common.collect.Lists;
|
||||
import me.chanjar.weixin.common.api.WxConsts;
|
||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
@ -97,22 +94,6 @@ public class WxMaDemoServer {
|
||||
}
|
||||
};
|
||||
|
||||
private static final WxMaMessageHandler templateMsgHandler = new WxMaMessageHandler() {
|
||||
@Override
|
||||
public WxMaXmlOutMessage handle(WxMaMessage wxMessage, Map<String, Object> context,
|
||||
WxMaService service, WxSessionManager sessionManager)
|
||||
throws WxErrorException {
|
||||
service.getMsgService().sendTemplateMsg(WxMaTemplateMessage.builder()
|
||||
.templateId(templateId).data(Lists.newArrayList(
|
||||
new WxMaTemplateData("keyword1", "339208499", "#173177")))
|
||||
.toUser(wxMessage.getFromUser())
|
||||
.formId("自己替换可用的formid")
|
||||
.build());
|
||||
return null;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
private static final WxMaMessageHandler customerServiceMessageHandler = new WxMaMessageHandler() {
|
||||
@Override
|
||||
public WxMaXmlOutMessage handle(WxMaMessage message, Map<String, Object> context, WxMaService service, WxSessionManager sessionManager) {
|
||||
@ -157,7 +138,6 @@ public class WxMaDemoServer {
|
||||
router = new WxMaMessageRouter(service);
|
||||
|
||||
router.rule().handler(logHandler).next()
|
||||
.rule().async(false).content("模板").handler(templateMsgHandler).end()
|
||||
.rule().async(false).content("文本").handler(textHandler).end()
|
||||
.rule().async(false).content("图片").handler(picHandler).end()
|
||||
.rule().async(false).content("二维码").handler(qrcodeHandler).end()
|
||||
|
||||
Reference in New Issue
Block a user