mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-04-05 17:38:05 +08:00
#253 修改企业号发送消息的messageSend方法,增加返回值,方便客户端进行自行处理
This commit is contained in:
parent
c7ffff0a9c
commit
aded340ac5
@ -8,10 +8,7 @@ import me.chanjar.weixin.common.session.WxSession;
|
|||||||
import me.chanjar.weixin.common.session.WxSessionManager;
|
import me.chanjar.weixin.common.session.WxSessionManager;
|
||||||
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
|
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
|
||||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||||
import me.chanjar.weixin.cp.bean.WxCpDepart;
|
import me.chanjar.weixin.cp.bean.*;
|
||||||
import me.chanjar.weixin.cp.bean.WxCpMessage;
|
|
||||||
import me.chanjar.weixin.cp.bean.WxCpTag;
|
|
||||||
import me.chanjar.weixin.cp.bean.WxCpUser;
|
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -141,7 +138,7 @@ public interface WxCpService {
|
|||||||
*
|
*
|
||||||
* @param message 要发送的消息对象
|
* @param message 要发送的消息对象
|
||||||
*/
|
*/
|
||||||
void messageSend(WxCpMessage message) throws WxErrorException;
|
WxCpMessageSendResult messageSend(WxCpMessage message) throws WxErrorException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <pre>
|
* <pre>
|
||||||
|
@ -17,10 +17,7 @@ import me.chanjar.weixin.common.util.http.*;
|
|||||||
import me.chanjar.weixin.common.util.json.GsonHelper;
|
import me.chanjar.weixin.common.util.json.GsonHelper;
|
||||||
import me.chanjar.weixin.cp.api.WxCpConfigStorage;
|
import me.chanjar.weixin.cp.api.WxCpConfigStorage;
|
||||||
import me.chanjar.weixin.cp.api.WxCpService;
|
import me.chanjar.weixin.cp.api.WxCpService;
|
||||||
import me.chanjar.weixin.cp.bean.WxCpDepart;
|
import me.chanjar.weixin.cp.bean.*;
|
||||||
import me.chanjar.weixin.cp.bean.WxCpMessage;
|
|
||||||
import me.chanjar.weixin.cp.bean.WxCpTag;
|
|
||||||
import me.chanjar.weixin.cp.bean.WxCpUser;
|
|
||||||
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
|
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@ -131,9 +128,9 @@ public abstract class AbstractWxCpServiceImpl<H, P> implements WxCpService, Requ
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void messageSend(WxCpMessage message) throws WxErrorException {
|
public WxCpMessageSendResult messageSend(WxCpMessage message) throws WxErrorException {
|
||||||
String url = "https://qyapi.weixin.qq.com/cgi-bin/message/send";
|
String url = "https://qyapi.weixin.qq.com/cgi-bin/message/send";
|
||||||
post(url, message.toJson());
|
return WxCpMessageSendResult.fromJson(this.post(url, message.toJson()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -0,0 +1,103 @@
|
|||||||
|
package me.chanjar.weixin.cp.bean;
|
||||||
|
|
||||||
|
import com.google.common.base.Splitter;
|
||||||
|
import com.google.gson.annotations.SerializedName;
|
||||||
|
import me.chanjar.weixin.common.util.ToStringUtils;
|
||||||
|
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <pre>
|
||||||
|
* 消息发送结果对象类
|
||||||
|
* Created by Binary Wang on 2017-6-22.
|
||||||
|
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||||
|
* </pre>
|
||||||
|
*/
|
||||||
|
public class WxCpMessageSendResult {
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return ToStringUtils.toSimpleString(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static WxCpMessageSendResult fromJson(String json) {
|
||||||
|
return WxCpGsonBuilder.INSTANCE.create().fromJson(json, WxCpMessageSendResult.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SerializedName("errcode")
|
||||||
|
private Integer errCode;
|
||||||
|
|
||||||
|
@SerializedName("errmsg")
|
||||||
|
private String errMsg;
|
||||||
|
|
||||||
|
@SerializedName("invaliduser")
|
||||||
|
private String invalidUser;
|
||||||
|
|
||||||
|
@SerializedName("invalidparty")
|
||||||
|
private String invalidParty;
|
||||||
|
|
||||||
|
@SerializedName("invalidtag")
|
||||||
|
private String invalidTag;
|
||||||
|
|
||||||
|
public Integer getErrCode() {
|
||||||
|
return this.errCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setErrCode(Integer errCode) {
|
||||||
|
this.errCode = errCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getErrMsg() {
|
||||||
|
return this.errMsg;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setErrMsg(String errMsg) {
|
||||||
|
this.errMsg = errMsg;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getInvalidUser() {
|
||||||
|
return this.invalidUser;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setInvalidUser(String invalidUser) {
|
||||||
|
this.invalidUser = invalidUser;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getInvalidParty() {
|
||||||
|
return this.invalidParty;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setInvalidParty(String invalidParty) {
|
||||||
|
this.invalidParty = invalidParty;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getInvalidTag() {
|
||||||
|
return this.invalidTag;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setInvalidTag(String invalidTag) {
|
||||||
|
this.invalidTag = invalidTag;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> getInvalidUserList() {
|
||||||
|
return this.content2List(this.invalidUser);
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<String> content2List(String content) {
|
||||||
|
if(StringUtils.isBlank(content)){
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
|
||||||
|
return Splitter.on("|").splitToList(content);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> getInvalidPartyList() {
|
||||||
|
return this.content2List(this.invalidParty);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> getInvalidTagList() {
|
||||||
|
return this.content2List(this.invalidTag);
|
||||||
|
}
|
||||||
|
}
|
@ -5,38 +5,58 @@ import me.chanjar.weixin.common.api.WxConsts;
|
|||||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||||
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
|
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
|
||||||
import me.chanjar.weixin.cp.bean.WxCpMessage;
|
import me.chanjar.weixin.cp.bean.WxCpMessage;
|
||||||
import org.testng.annotations.Guice;
|
import me.chanjar.weixin.cp.bean.WxCpMessageSendResult;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.*;
|
||||||
|
|
||||||
|
import static org.testng.Assert.*;
|
||||||
|
|
||||||
/***
|
/***
|
||||||
* 测试发送消息
|
* 测试发送消息
|
||||||
* @author Daniel Qian
|
* @author Daniel Qian
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@Test(groups = "customMessageAPI", dependsOnGroups = "baseAPI")
|
@Test(groups = "customMessageAPI")
|
||||||
@Guice(modules = ApiTestModule.class)
|
@Guice(modules = ApiTestModule.class)
|
||||||
public class WxCpMessageAPITest {
|
public class WxCpMessageAPITest {
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
protected WxCpServiceImpl wxService;
|
protected WxCpServiceImpl wxService;
|
||||||
|
private ApiTestModule.WxXmlCpInMemoryConfigStorage configStorage;
|
||||||
|
|
||||||
public void testSendCustomMessage() throws WxErrorException {
|
@BeforeTest
|
||||||
ApiTestModule.WxXmlCpInMemoryConfigStorage configStorage = (ApiTestModule.WxXmlCpInMemoryConfigStorage) this.wxService.getWxCpConfigStorage();
|
public void setup() {
|
||||||
WxCpMessage message1 = new WxCpMessage();
|
configStorage = (ApiTestModule.WxXmlCpInMemoryConfigStorage) this.wxService.getWxCpConfigStorage();
|
||||||
message1.setAgentId(configStorage.getAgentId());
|
}
|
||||||
message1.setMsgType(WxConsts.CUSTOM_MSG_TEXT);
|
|
||||||
message1.setToUser(configStorage.getUserId());
|
|
||||||
message1.setContent("欢迎欢迎,热烈欢迎\n换行测试\n超链接:<a href=\"http://www.baidu.com\">Hello World</a>");
|
|
||||||
this.wxService.messageSend(message1);
|
|
||||||
|
|
||||||
WxCpMessage message2 = WxCpMessage
|
public void testSendMessage() throws WxErrorException {
|
||||||
|
WxCpMessage message = new WxCpMessage();
|
||||||
|
message.setAgentId(configStorage.getAgentId());
|
||||||
|
message.setMsgType(WxConsts.CUSTOM_MSG_TEXT);
|
||||||
|
message.setToUser(configStorage.getUserId());
|
||||||
|
message.setContent("欢迎欢迎,热烈欢迎\n换行测试\n超链接:<a href=\"http://www.baidu.com\">Hello World</a>");
|
||||||
|
|
||||||
|
WxCpMessageSendResult messageSendResult = this.wxService.messageSend(message);
|
||||||
|
assertNotNull(messageSendResult);
|
||||||
|
System.out.println(messageSendResult);
|
||||||
|
System.out.println(messageSendResult.getInvalidPartyList());
|
||||||
|
System.out.println(messageSendResult.getInvalidUserList());
|
||||||
|
System.out.println(messageSendResult.getInvalidTagList());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testSendMessage1() throws WxErrorException {
|
||||||
|
WxCpMessage message = WxCpMessage
|
||||||
.TEXT()
|
.TEXT()
|
||||||
.agentId(configStorage.getAgentId())
|
.agentId(configStorage.getAgentId())
|
||||||
.toUser(configStorage.getUserId())
|
.toUser(configStorage.getUserId())
|
||||||
.content("欢迎欢迎,热烈欢迎\n换行测试\n超链接:<a href=\"http://www.baidu.com\">Hello World</a>")
|
.content("欢迎欢迎,热烈欢迎\n换行测试\n超链接:<a href=\"http://www.baidu.com\">Hello World</a>")
|
||||||
.build();
|
.build();
|
||||||
this.wxService.messageSend(message2);
|
|
||||||
|
WxCpMessageSendResult messageSendResult = this.wxService.messageSend(message);
|
||||||
|
assertNotNull(messageSendResult);
|
||||||
|
System.out.println(messageSendResult);
|
||||||
|
System.out.println(messageSendResult.getInvalidPartyList());
|
||||||
|
System.out.println(messageSendResult.getInvalidUserList());
|
||||||
|
System.out.println(messageSendResult.getInvalidTagList());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user