@@ -257,9 +257,8 @@ public class WxMpServiceImpl implements WxMpService { | |||
@Override | |||
public String templateSend(WxMpTemplateMessage templateMessage) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/cgi-bin/message/template/send"; | |||
String responseContent = execute(new SimplePostRequestExecutor(), url, templateMessage.toJson()); | |||
JsonElement tmpJsonElement = JSON_PARSER.parse(responseContent); | |||
final JsonObject jsonObject = tmpJsonElement.getAsJsonObject(); | |||
String responseContent = this.post(url, templateMessage.toJson()); | |||
final JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject(); | |||
if (jsonObject.get("errcode").getAsInt() == 0){ | |||
return jsonObject.get("msgid").getAsString(); | |||
} | |||
@@ -7,9 +7,6 @@ import java.io.Serializable; | |||
*/ | |||
public class WxMpTemplateData implements Serializable { | |||
/** | |||
* | |||
*/ | |||
private static final long serialVersionUID = 6301835292940277870L; | |||
private String name; | |||
private String value; | |||
@@ -48,6 +45,7 @@ public class WxMpTemplateData implements Serializable { | |||
public String getColor() { | |||
return this.color; | |||
} | |||
public void setColor(String color) { | |||
this.color = color; | |||
} | |||
@@ -1,17 +1,14 @@ | |||
package me.chanjar.weixin.mp.bean; | |||
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | |||
import java.io.Serializable; | |||
import java.util.ArrayList; | |||
import java.util.List; | |||
public class WxMpTemplateMessage implements Serializable { | |||
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | |||
/** | |||
* | |||
*/ | |||
public class WxMpTemplateMessage implements Serializable { | |||
private static final long serialVersionUID = 5063374783759519418L; | |||
private String toUser; | |||
private String templateId; | |||
private String url; | |||
@@ -65,4 +62,61 @@ public class WxMpTemplateMessage implements Serializable { | |||
public String toJson() { | |||
return WxMpGsonBuilder.INSTANCE.create().toJson(this); | |||
} | |||
public static WxMpTemplateMessageBuilder builder() { | |||
return new WxMpTemplateMessageBuilder(); | |||
} | |||
public static class WxMpTemplateMessageBuilder { | |||
private String toUser; | |||
private String templateId; | |||
private String url; | |||
private String topColor; | |||
private List<WxMpTemplateData> data = new ArrayList<>(); | |||
public WxMpTemplateMessageBuilder toUser(String toUser) { | |||
this.toUser = toUser; | |||
return this; | |||
} | |||
public WxMpTemplateMessageBuilder templateId(String templateId) { | |||
this.templateId = templateId; | |||
return this; | |||
} | |||
public WxMpTemplateMessageBuilder url(String url) { | |||
this.url = url; | |||
return this; | |||
} | |||
public WxMpTemplateMessageBuilder topColor(String topColor) { | |||
this.topColor = topColor; | |||
return this; | |||
} | |||
public WxMpTemplateMessageBuilder data(List<WxMpTemplateData> data) { | |||
this.data = data; | |||
return this; | |||
} | |||
public WxMpTemplateMessageBuilder from(WxMpTemplateMessage origin) { | |||
this.toUser(origin.toUser); | |||
this.templateId(origin.templateId); | |||
this.url(origin.url); | |||
this.topColor(origin.topColor); | |||
this.data(origin.data); | |||
return this; | |||
} | |||
public WxMpTemplateMessage build() { | |||
WxMpTemplateMessage m = new WxMpTemplateMessage(); | |||
m.toUser = this.toUser; | |||
m.templateId = this.templateId; | |||
m.url = this.url; | |||
m.topColor = this.topColor; | |||
m.data = this.data; | |||
return m; | |||
} | |||
} | |||
} |
@@ -3,15 +3,12 @@ package me.chanjar.weixin.mp.api; | |||
import java.io.IOException; | |||
import java.io.InputStream; | |||
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl; | |||
import org.apache.commons.lang3.builder.ToStringBuilder; | |||
import com.google.inject.Binder; | |||
import com.google.inject.Module; | |||
import com.thoughtworks.xstream.XStream; | |||
import com.thoughtworks.xstream.annotations.XStreamAlias; | |||
import me.chanjar.weixin.common.util.xml.XStreamInitializer; | |||
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl; | |||
public class ApiTestModule implements Module { | |||
@@ -19,8 +16,8 @@ public class ApiTestModule implements Module { | |||
public void configure(Binder binder) { | |||
try (InputStream is1 = ClassLoader | |||
.getSystemResourceAsStream("test-config.xml")) { | |||
WxXmlMpInMemoryConfigStorage config = fromXml( | |||
WxXmlMpInMemoryConfigStorage.class, is1); | |||
WxXmlMpInMemoryConfigStorage config = this | |||
.fromXml(WxXmlMpInMemoryConfigStorage.class, is1); | |||
WxMpServiceImpl wxService = new WxMpServiceImpl(); | |||
wxService.setWxMpConfigStorage(config); | |||
@@ -32,50 +29,11 @@ public class ApiTestModule implements Module { | |||
} | |||
@SuppressWarnings("unchecked") | |||
public static <T> T fromXml(Class<T> clazz, InputStream is) { | |||
private <T> T fromXml(Class<T> clazz, InputStream is) { | |||
XStream xstream = XStreamInitializer.getInstance(); | |||
xstream.alias("xml", clazz); | |||
xstream.processAnnotations(clazz); | |||
return (T) xstream.fromXML(is); | |||
} | |||
@XStreamAlias("xml") | |||
public static class WxXmlMpInMemoryConfigStorage | |||
extends WxMpInMemoryConfigStorage { | |||
private String openid; | |||
private String kfAccount; | |||
private String qrconnectRedirectUrl; | |||
public String getOpenid() { | |||
return this.openid; | |||
} | |||
public void setOpenid(String openid) { | |||
this.openid = openid; | |||
} | |||
@Override | |||
public String toString() { | |||
return ToStringBuilder.reflectionToString(this); | |||
} | |||
public String getKfAccount() { | |||
return this.kfAccount; | |||
} | |||
public void setKfAccount(String kfAccount) { | |||
this.kfAccount = kfAccount; | |||
} | |||
public String getQrconnectRedirectUrl() { | |||
return this.qrconnectRedirectUrl; | |||
} | |||
public void setQrconnectRedirectUrl(String qrconnectRedirectUrl) { | |||
this.qrconnectRedirectUrl = qrconnectRedirectUrl; | |||
} | |||
} | |||
} |
@@ -36,7 +36,7 @@ public class WxMpMassMessageAPITest { | |||
@Test | |||
public void testTextMassOpenIdsMessageSend() throws WxErrorException { | |||
// 发送群发消息 | |||
ApiTestModule.WxXmlMpInMemoryConfigStorage configProvider = (ApiTestModule.WxXmlMpInMemoryConfigStorage) this.wxService | |||
WxXmlMpInMemoryConfigStorage configProvider = (WxXmlMpInMemoryConfigStorage) this.wxService | |||
.getWxMpConfigStorage(); | |||
WxMpMassOpenIdsMessage massMessage = new WxMpMassOpenIdsMessage(); | |||
massMessage.setMsgType(WxConsts.MASS_MSG_TEXT); | |||
@@ -53,7 +53,7 @@ public class WxMpMassMessageAPITest { | |||
public void testMediaMassOpenIdsMessageSend(String massMsgType, | |||
String mediaId) throws WxErrorException { | |||
// 发送群发消息 | |||
ApiTestModule.WxXmlMpInMemoryConfigStorage configProvider = (ApiTestModule.WxXmlMpInMemoryConfigStorage) this.wxService | |||
WxXmlMpInMemoryConfigStorage configProvider = (WxXmlMpInMemoryConfigStorage) this.wxService | |||
.getWxMpConfigStorage(); | |||
WxMpMassOpenIdsMessage massMessage = new WxMpMassOpenIdsMessage(); | |||
massMessage.setMsgType(massMsgType); | |||
@@ -0,0 +1,57 @@ | |||
/** | |||
* Copyright(c) 2011-2016 by UCredit Inc. | |||
* All Rights Reserved | |||
*/ | |||
package me.chanjar.weixin.mp.api; | |||
import org.apache.commons.lang3.builder.ToStringBuilder; | |||
import com.thoughtworks.xstream.annotations.XStreamAlias; | |||
@XStreamAlias("xml") | |||
public class WxXmlMpInMemoryConfigStorage | |||
extends WxMpInMemoryConfigStorage { | |||
private String openid; | |||
private String kfAccount; | |||
private String qrconnectRedirectUrl; | |||
private String templateId; | |||
public String getOpenid() { | |||
return this.openid; | |||
} | |||
public void setOpenid(String openid) { | |||
this.openid = openid; | |||
} | |||
@Override | |||
public String toString() { | |||
return ToStringBuilder.reflectionToString(this); | |||
} | |||
public String getKfAccount() { | |||
return this.kfAccount; | |||
} | |||
public void setKfAccount(String kfAccount) { | |||
this.kfAccount = kfAccount; | |||
} | |||
public String getQrconnectRedirectUrl() { | |||
return this.qrconnectRedirectUrl; | |||
} | |||
public void setQrconnectRedirectUrl(String qrconnectRedirectUrl) { | |||
this.qrconnectRedirectUrl = qrconnectRedirectUrl; | |||
} | |||
public String getTemplateId() { | |||
return this.templateId; | |||
} | |||
public void setTemplateId(String templateId) { | |||
this.templateId = templateId; | |||
} | |||
} |
@@ -3,6 +3,7 @@ package me.chanjar.weixin.mp.api.impl; | |||
import com.google.inject.Inject; | |||
import me.chanjar.weixin.common.exception.WxErrorException; | |||
import me.chanjar.weixin.mp.api.ApiTestModule; | |||
import me.chanjar.weixin.mp.api.WxXmlMpInMemoryConfigStorage; | |||
import me.chanjar.weixin.mp.bean.WxMpGroup; | |||
import org.testng.Assert; | |||
import org.testng.annotations.Guice; | |||
@@ -48,13 +49,13 @@ public class WxMpGroupServiceImplTest { | |||
} | |||
public void testGroupQueryUserGroup() throws WxErrorException { | |||
ApiTestModule.WxXmlMpInMemoryConfigStorage configStorage = (ApiTestModule.WxXmlMpInMemoryConfigStorage) this.wxService.getWxMpConfigStorage(); | |||
WxXmlMpInMemoryConfigStorage configStorage = (WxXmlMpInMemoryConfigStorage) this.wxService.getWxMpConfigStorage(); | |||
long groupid = this.wxService.getGroupService().userGetGroup(configStorage.getOpenid()); | |||
Assert.assertTrue(groupid != -1l); | |||
} | |||
public void testGroupMoveUser() throws WxErrorException { | |||
ApiTestModule.WxXmlMpInMemoryConfigStorage configStorage = (ApiTestModule.WxXmlMpInMemoryConfigStorage) this.wxService.getWxMpConfigStorage(); | |||
WxXmlMpInMemoryConfigStorage configStorage = (WxXmlMpInMemoryConfigStorage) this.wxService.getWxMpConfigStorage(); | |||
this.wxService.getGroupService().userUpdateGroup(configStorage.getOpenid(), this.wxService.getGroupService().groupGet().get(3).getId()); | |||
} | |||
@@ -14,7 +14,7 @@ import com.google.inject.Inject; | |||
import me.chanjar.weixin.common.api.WxConsts; | |||
import me.chanjar.weixin.common.exception.WxErrorException; | |||
import me.chanjar.weixin.mp.api.ApiTestModule; | |||
import me.chanjar.weixin.mp.api.ApiTestModule.WxXmlMpInMemoryConfigStorage; | |||
import me.chanjar.weixin.mp.api.WxXmlMpInMemoryConfigStorage; | |||
import me.chanjar.weixin.mp.bean.WxMpCustomMessage; | |||
import me.chanjar.weixin.mp.bean.kefu.request.WxMpKfAccountRequest; | |||
import me.chanjar.weixin.mp.bean.kefu.result.WxMpKfInfo; | |||
@@ -38,7 +38,7 @@ public class WxMpKefuServiceImplTest { | |||
protected WxMpServiceImpl wxService; | |||
public void testSendCustomMessage() throws WxErrorException { | |||
ApiTestModule.WxXmlMpInMemoryConfigStorage configStorage = (ApiTestModule.WxXmlMpInMemoryConfigStorage) this.wxService | |||
WxXmlMpInMemoryConfigStorage configStorage = (WxXmlMpInMemoryConfigStorage) this.wxService | |||
.getWxMpConfigStorage(); | |||
WxMpCustomMessage message = new WxMpCustomMessage(); | |||
message.setMsgType(WxConsts.CUSTOM_MSG_TEXT); | |||
@@ -50,7 +50,7 @@ public class WxMpKefuServiceImplTest { | |||
} | |||
public void testSendCustomMessageWithKfAccount() throws WxErrorException { | |||
ApiTestModule.WxXmlMpInMemoryConfigStorage configStorage = (ApiTestModule.WxXmlMpInMemoryConfigStorage) this.wxService | |||
WxXmlMpInMemoryConfigStorage configStorage = (WxXmlMpInMemoryConfigStorage) this.wxService | |||
.getWxMpConfigStorage(); | |||
WxMpCustomMessage message = new WxMpCustomMessage(); | |||
message.setMsgType(WxConsts.CUSTOM_MSG_TEXT); | |||
@@ -7,6 +7,7 @@ import com.google.inject.Inject; | |||
import me.chanjar.weixin.common.exception.WxErrorException; | |||
import me.chanjar.weixin.mp.api.ApiTestModule; | |||
import me.chanjar.weixin.mp.api.WxXmlMpInMemoryConfigStorage; | |||
import me.chanjar.weixin.mp.bean.pay.WxRedpackResult; | |||
import me.chanjar.weixin.mp.bean.pay.WxSendRedpackRequest; | |||
import me.chanjar.weixin.mp.bean.pay.WxUnifiedOrderRequest; | |||
@@ -71,7 +72,7 @@ public class WxMpPayServiceImplTest { | |||
request.setClientIp("aaa"); | |||
request.setMchBillno("aaaa"); | |||
request | |||
.setReOpenid(((ApiTestModule.WxXmlMpInMemoryConfigStorage) this.wxService.getWxMpConfigStorage()).getOpenid()); | |||
.setReOpenid(((WxXmlMpInMemoryConfigStorage) this.wxService.getWxMpConfigStorage()).getOpenid()); | |||
WxRedpackResult redpackResult = this.wxService.getPayService().sendRedpack(request); | |||
System.err.println(redpackResult); | |||
} | |||
@@ -1,5 +1,8 @@ | |||
package me.chanjar.weixin.mp.api.impl; | |||
import java.text.SimpleDateFormat; | |||
import java.util.Date; | |||
import org.testng.Assert; | |||
import org.testng.annotations.Guice; | |||
import org.testng.annotations.Test; | |||
@@ -7,8 +10,11 @@ import org.testng.annotations.Test; | |||
import com.google.inject.Inject; | |||
import me.chanjar.weixin.common.api.WxConsts; | |||
import me.chanjar.weixin.common.exception.WxErrorException; | |||
import me.chanjar.weixin.mp.api.ApiTestModule; | |||
import me.chanjar.weixin.mp.api.ApiTestModule.WxXmlMpInMemoryConfigStorage; | |||
import me.chanjar.weixin.mp.api.WxXmlMpInMemoryConfigStorage; | |||
import me.chanjar.weixin.mp.bean.WxMpTemplateData; | |||
import me.chanjar.weixin.mp.bean.WxMpTemplateMessage; | |||
@Test | |||
@Guice(modules = ApiTestModule.class) | |||
@@ -82,9 +88,18 @@ public class WxMpServiceImplTest { | |||
Assert.fail("Not yet implemented"); | |||
} | |||
@Test | |||
public void testTemplateSend() { | |||
Assert.fail("Not yet implemented"); | |||
@Test(invocationCount = 100, threadPoolSize = 30) | |||
public void testTemplateSend() throws WxErrorException { | |||
SimpleDateFormat dateFormat = new SimpleDateFormat( | |||
"yyyy-MM-dd HH:mm:ss.SSS"); | |||
WxXmlMpInMemoryConfigStorage configStorage = (WxXmlMpInMemoryConfigStorage) this.wxService | |||
.getWxMpConfigStorage(); | |||
WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder() | |||
.toUser(configStorage.getOpenid()) | |||
.templateId(configStorage.getTemplateId()).build(); | |||
templateMessage.addWxMpTemplateData( | |||
new WxMpTemplateData("first", dateFormat.format(new Date()))); | |||
this.wxService.templateSend(templateMessage); | |||
} | |||
@Test | |||
@@ -1,6 +1,7 @@ | |||
package me.chanjar.weixin.mp.api.impl; | |||
import me.chanjar.weixin.mp.api.ApiTestModule; | |||
import me.chanjar.weixin.mp.api.WxXmlMpInMemoryConfigStorage; | |||
import me.chanjar.weixin.mp.bean.result.WxMpUserBlacklistGetResult; | |||
import org.testng.Assert; | |||
import org.testng.annotations.Guice; | |||
@@ -21,7 +22,7 @@ public class WxMpUserBlacklistServiceImplTest { | |||
@Test | |||
public void testGetBlacklist() throws Exception { | |||
ApiTestModule.WxXmlMpInMemoryConfigStorage configStorage = (ApiTestModule.WxXmlMpInMemoryConfigStorage) this.wxService | |||
WxXmlMpInMemoryConfigStorage configStorage = (WxXmlMpInMemoryConfigStorage) this.wxService | |||
.getWxMpConfigStorage(); | |||
WxMpUserBlacklistGetResult wxMpUserBlacklistGetResult = this.wxService.getBlackListService().getBlacklist(configStorage.getOpenid()); | |||
Assert.assertNotNull(wxMpUserBlacklistGetResult); | |||
@@ -33,7 +34,7 @@ public class WxMpUserBlacklistServiceImplTest { | |||
@Test | |||
public void testPushToBlacklist() throws Exception { | |||
ApiTestModule.WxXmlMpInMemoryConfigStorage configStorage = (ApiTestModule.WxXmlMpInMemoryConfigStorage) this.wxService | |||
WxXmlMpInMemoryConfigStorage configStorage = (WxXmlMpInMemoryConfigStorage) this.wxService | |||
.getWxMpConfigStorage(); | |||
List<String> openidList = new ArrayList<>(); | |||
openidList.add(configStorage.getOpenid()); | |||
@@ -42,7 +43,7 @@ public class WxMpUserBlacklistServiceImplTest { | |||
@Test | |||
public void testPullFromBlacklist() throws Exception { | |||
ApiTestModule.WxXmlMpInMemoryConfigStorage configStorage = (ApiTestModule.WxXmlMpInMemoryConfigStorage) this.wxService | |||
WxXmlMpInMemoryConfigStorage configStorage = (WxXmlMpInMemoryConfigStorage) this.wxService | |||
.getWxMpConfigStorage(); | |||
List<String> openidList = new ArrayList<>(); | |||
openidList.add(configStorage.getOpenid()); | |||
@@ -8,6 +8,7 @@ import com.google.inject.Inject; | |||
import me.chanjar.weixin.common.exception.WxErrorException; | |||
import me.chanjar.weixin.mp.api.ApiTestModule; | |||
import me.chanjar.weixin.mp.api.WxXmlMpInMemoryConfigStorage; | |||
import me.chanjar.weixin.mp.bean.result.WxMpUser; | |||
import me.chanjar.weixin.mp.bean.result.WxMpUserList; | |||
@@ -25,12 +26,12 @@ public class WxMpUserServiceImplTest { | |||
protected WxMpServiceImpl wxService; | |||
public void testUserUpdateRemark() throws WxErrorException { | |||
ApiTestModule.WxXmlMpInMemoryConfigStorage configProvider = (ApiTestModule.WxXmlMpInMemoryConfigStorage) this.wxService.getWxMpConfigStorage(); | |||
WxXmlMpInMemoryConfigStorage configProvider = (WxXmlMpInMemoryConfigStorage) this.wxService.getWxMpConfigStorage(); | |||
this.wxService.getUserService().userUpdateRemark(configProvider.getOpenid(), "测试备注名"); | |||
} | |||
public void testUserInfo() throws WxErrorException { | |||
ApiTestModule.WxXmlMpInMemoryConfigStorage configProvider = (ApiTestModule.WxXmlMpInMemoryConfigStorage) this.wxService.getWxMpConfigStorage(); | |||
WxXmlMpInMemoryConfigStorage configProvider = (WxXmlMpInMemoryConfigStorage) this.wxService.getWxMpConfigStorage(); | |||
WxMpUser user = this.wxService.getUserService().userInfo(configProvider.getOpenid(), null); | |||
Assert.assertNotNull(user); | |||
System.out.println(user); | |||
@@ -2,6 +2,7 @@ package me.chanjar.weixin.mp.api.impl; | |||
import com.google.inject.Inject; | |||
import me.chanjar.weixin.mp.api.ApiTestModule; | |||
import me.chanjar.weixin.mp.api.WxXmlMpInMemoryConfigStorage; | |||
import me.chanjar.weixin.mp.bean.tag.WxTagListUser; | |||
import me.chanjar.weixin.mp.bean.tag.WxUserTag; | |||
import org.testng.Assert; | |||
@@ -63,7 +64,7 @@ public class WxMpUserTagServiceImplTest { | |||
@Test | |||
public void testBatchUntagging() throws Exception { | |||
String[] openids = new String[]{((ApiTestModule.WxXmlMpInMemoryConfigStorage) this.wxService.getWxMpConfigStorage()).getOpenid()}; | |||
String[] openids = new String[]{((WxXmlMpInMemoryConfigStorage) this.wxService.getWxMpConfigStorage()).getOpenid()}; | |||
boolean res = this.wxService.getUserTagService().batchUntagging(this.tagId, openids); | |||
System.out.println(res); | |||
Assert.assertTrue(res); | |||
@@ -72,7 +73,7 @@ public class WxMpUserTagServiceImplTest { | |||
@Test | |||
public void testUserTagList() throws Exception { | |||
List<Integer> res = this.wxService.getUserTagService().userTagList( | |||
((ApiTestModule.WxXmlMpInMemoryConfigStorage) this.wxService.getWxMpConfigStorage()).getOpenid()); | |||
((WxXmlMpInMemoryConfigStorage) this.wxService.getWxMpConfigStorage()).getOpenid()); | |||
System.out.println(res); | |||
Assert.assertNotNull(res); | |||
} | |||
@@ -6,6 +6,7 @@ | |||
<accessToken>可以不填写</accessToken> | |||
<expiresTime>可以不填写</expiresTime> | |||
<openid>某个加你公众号的用户的openId</openid> | |||
<templateId>模版消息的模版ID</templateId> | |||
<oauth2redirectUri>网页授权获取用户信息回调地址</oauth2redirectUri> | |||
<qrconnectRedirectUrl>网页应用授权登陆回调地址</qrconnectRedirectUrl> | |||
<kfAccount>完整客服账号,格式为:账号前缀@公众号微信号</kfAccount> | |||