1
0
mirror of synced 2025-12-20 15:48:01 +08:00

#436 添加一次性订阅消息接口

This commit is contained in:
Mklaus
2018-01-23 10:23:29 +08:00
committed by Binary Wang
parent be50ea009c
commit f2b05480b5
14 changed files with 336 additions and 2 deletions

View File

@@ -0,0 +1,48 @@
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.WxMpService;
import me.chanjar.weixin.mp.api.test.ApiTestModule;
import me.chanjar.weixin.mp.api.test.TestConfigStorage;
import me.chanjar.weixin.mp.bean.subscribe.WxMpSubscribeMessage;
import org.testng.Assert;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
/**
* @author Mklaus
* @date 2018-01-22 下午2:02
*/
@Guice(modules = ApiTestModule.class)
public class WxMpSubscribeMsgServiceImplTest {
@Inject
protected WxMpService wxService;
@Test
public void testSendSubscribeMessage() throws WxErrorException {
TestConfigStorage configStorage = (TestConfigStorage) this.wxService
.getWxMpConfigStorage();
WxMpSubscribeMessage message = WxMpSubscribeMessage.builder()
.title("weixin test")
.toUser(configStorage.getOpenid())
.scene("1000")
.contentColor("#FF0000")
.contentValue("Send subscribe message test")
.build();
try {
boolean send = this.wxService.getSubscribeMsgService().sendSubscribeMessage(message);
Assert.assertTrue(send);
} catch (WxErrorException e) {
// 当用户没有授权,获取之前的授权已使用。微信会返回错误代码 {"errcode":43101,"errmsg":"user refuse to accept the msg hint: [xxxxxxxxxxx]"}
if (e.getError().getErrorCode() != 43101) {
throw e;
}
}
}
}

View File

@@ -0,0 +1,46 @@
package me.chanjar.weixin.mp.bean.subscribe;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.testng.annotations.Test;
import static org.testng.AssertJUnit.assertEquals;
/**
* @author Mklaus
* @date 2018-01-22 下午1:41
*/
public class WxMpSubscribeMessageTest {
@Test
public void testToJson() {
String actual = "{" +
"\"touser\":\"OPENID\"," +
"\"template_id\":\"TEMPLATE_ID\"," +
"\"url\":\"URL\"," +
"\"miniprogram\":{" +
"\"appid\":\"xiaochengxuappid12345\"," +
"\"pagepath\":\"index?foo=bar\"" +
"}," +
"\"scene\":\"SCENE\"," +
"\"title\":\"TITLE\"," +
"\"data\":{" +
"\"content\":{" +
"\"value\":\"VALUE\"," +
"\"color\":\"COLOR\"" +
"}" +
"}" +
"}";
WxMpSubscribeMessage message = WxMpSubscribeMessage.builder()
.toUser("OPENID")
.templateId("TEMPLATE_ID")
.url("URL")
.miniProgram(new WxMpTemplateMessage.MiniProgram("xiaochengxuappid12345", "index?foo=bar"))
.scene("SCENE")
.title("TITLE")
.contentValue("VALUE")
.contentColor("COLOR")
.build();
assertEquals(message.toJson(), actual);
}
}

View File

@@ -22,7 +22,7 @@ class WxMpDemoInMemoryConfigStorage extends WxMpInMemoryConfigStorage {
@Override
public String toString() {
return "SimpleWxConfigProvider [appId=" + this.appId + ", secret=" + this.secret + ", accessToken=" + this.accessToken
+ ", expiresTime=" + this.expiresTime + ", token=" + this.token + ", aesKey=" + this.aesKey + "]";
+ ", expiresTime=" + this.expiresTime + ", token=" + this.token + ", aesKey=" + this.aesKey + ", templateId=" + this.templateId + "]";
}
}