瀏覽代碼

转移群发消息相关接口到单独接口及其实现类中

master
Binary Wang 7 年之前
父節點
當前提交
92947b344a
共有 6 個文件被更改,包括 197 次插入140 次删除
  1. +95
    -0
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpMassMessageService.java
  2. +5
    -78
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java
  3. +58
    -0
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpMassMessageServiceImpl.java
  4. +6
    -30
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceAbstractImpl.java
  5. +32
    -31
      weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpMassMessageServiceImplTest.java
  6. +1
    -1
      weixin-java-mp/src/test/resources/testng.xml

+ 95
- 0
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpMassMessageService.java 查看文件

@@ -0,0 +1,95 @@
package me.chanjar.weixin.mp.api;

import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.bean.*;
import me.chanjar.weixin.mp.bean.result.WxMpMassSendResult;
import me.chanjar.weixin.mp.bean.result.WxMpMassUploadResult;

/**
* <pre>
* 群发消息服务类
* Created by Binary Wang on 2017-8-16.
* </pre>
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
public interface WxMpMassMessageService {
/**
* 上传群发用的图文消息
*/
String MEDIA_UPLOAD_NEWS_URL = "https://api.weixin.qq.com/cgi-bin/media/uploadnews";
/**
* 上传群发用的视频
*/
String MEDIA_UPLOAD_VIDEO_URL = "https://api.weixin.qq.com/cgi-bin/media/uploadvideo";
/**
* 分组群发消息
*/
String MESSAGE_MASS_SENDALL_URL = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall";
/**
* 按openId列表群发消息
*/
String MESSAGE_MASS_SEND_URL = "https://api.weixin.qq.com/cgi-bin/message/mass/send";
/**
* 群发消息预览接口
*/
String MESSAGE_MASS_PREVIEW_URL = "https://api.weixin.qq.com/cgi-bin/message/mass/preview";

/**
* <pre>
* 上传群发用的图文消息,上传后才能群发图文消息
*
* 详情请见: http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140549&token=&lang=zh_CN
* </pre>
*
* @see #massGroupMessageSend(WxMpMassTagMessage)
* @see #massOpenIdsMessageSend(WxMpMassOpenIdsMessage)
*/
WxMpMassUploadResult massNewsUpload(WxMpMassNews news) throws WxErrorException;

/**
* <pre>
* 上传群发用的视频,上传后才能群发视频消息
* 详情请见: http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140549&token=&lang=zh_CN
* </pre>
*
* @see #massGroupMessageSend(WxMpMassTagMessage)
* @see #massOpenIdsMessageSend(WxMpMassOpenIdsMessage)
*/
WxMpMassUploadResult massVideoUpload(WxMpMassVideo video) throws WxErrorException;

/**
* <pre>
* 分组群发消息
* 如果发送图文消息,必须先使用 {@link #massNewsUpload(WxMpMassNews)} 获得media_id,然后再发送
* 如果发送视频消息,必须先使用 {@link #massVideoUpload(WxMpMassVideo)} 获得media_id,然后再发送
* 详情请见: http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140549&token=&lang=zh_CN
* </pre>
*/
WxMpMassSendResult massGroupMessageSend(WxMpMassTagMessage message) throws WxErrorException;

/**
* <pre>
* 按openId列表群发消息
* 如果发送图文消息,必须先使用 {@link #massNewsUpload(WxMpMassNews)} 获得media_id,然后再发送
* 如果发送视频消息,必须先使用 {@link #massVideoUpload(WxMpMassVideo)} 获得media_id,然后再发送
* 详情请见: http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140549&token=&lang=zh_CN
* </pre>
*/
WxMpMassSendResult massOpenIdsMessageSend(WxMpMassOpenIdsMessage message) throws WxErrorException;

/**
* <pre>
* 群发消息预览接口
* 开发者可通过该接口发送消息给指定用户,在手机端查看消息的样式和排版。为了满足第三方平台开发者的需求,在保留对openID预览能力的同时,增加了对指定微信号发送预览的能力,但该能力每日调用次数有限制(100次),请勿滥用。
* 接口调用请求说明
* http请求方式: POST
* https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token=ACCESS_TOKEN
* 详情请见:http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140549&token=&lang=zh_CN
* </pre>
*
* @return wxMpMassSendResult
*/
WxMpMassSendResult massMessagePreview(WxMpMassPreviewMessage wxMpMassPreviewMessage) throws Exception;

}

+ 5
- 78
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java 查看文件

@@ -20,26 +20,6 @@ public interface WxMpService {
* 获得jsapi_ticket
*/
String GET_JSAPI_TICKET_URL = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi";
/**
* 上传群发用的图文消息
*/
String MEDIA_UPLOAD_NEWS_URL = "https://api.weixin.qq.com/cgi-bin/media/uploadnews";
/**
* 上传群发用的视频
*/
String MEDIA_UPLOAD_VIDEO_URL = "https://api.weixin.qq.com/cgi-bin/media/uploadvideo";
/**
* 分组群发消息
*/
String MESSAGE_MASS_SENDALL_URL = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall";
/**
* 按openId列表群发消息
*/
String MESSAGE_MASS_SEND_URL = "https://api.weixin.qq.com/cgi-bin/message/mass/send";
/**
* 群发消息预览接口
*/
String MESSAGE_MASS_PREVIEW_URL = "https://api.weixin.qq.com/cgi-bin/message/mass/preview";
/**
* 长链接转短链接接口
*/
@@ -141,63 +121,6 @@ public interface WxMpService {
*/
WxJsapiSignature createJsapiSignature(String url) throws WxErrorException;

/**
* <pre>
* 上传群发用的图文消息,上传后才能群发图文消息
*
* 详情请见: http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140549&token=&lang=zh_CN
* </pre>
*
* @see #massGroupMessageSend(me.chanjar.weixin.mp.bean.WxMpMassTagMessage)
* @see #massOpenIdsMessageSend(me.chanjar.weixin.mp.bean.WxMpMassOpenIdsMessage)
*/
WxMpMassUploadResult massNewsUpload(WxMpMassNews news) throws WxErrorException;

/**
* <pre>
* 上传群发用的视频,上传后才能群发视频消息
* 详情请见: http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140549&token=&lang=zh_CN
* </pre>
*
* @see #massGroupMessageSend(me.chanjar.weixin.mp.bean.WxMpMassTagMessage)
* @see #massOpenIdsMessageSend(me.chanjar.weixin.mp.bean.WxMpMassOpenIdsMessage)
*/
WxMpMassUploadResult massVideoUpload(WxMpMassVideo video) throws WxErrorException;

/**
* <pre>
* 分组群发消息
* 如果发送图文消息,必须先使用 {@link #massNewsUpload(me.chanjar.weixin.mp.bean.WxMpMassNews)} 获得media_id,然后再发送
* 如果发送视频消息,必须先使用 {@link #massVideoUpload(me.chanjar.weixin.mp.bean.WxMpMassVideo)} 获得media_id,然后再发送
* 详情请见: http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140549&token=&lang=zh_CN
* </pre>
*/
WxMpMassSendResult massGroupMessageSend(WxMpMassTagMessage message) throws WxErrorException;

/**
* <pre>
* 按openId列表群发消息
* 如果发送图文消息,必须先使用 {@link #massNewsUpload(me.chanjar.weixin.mp.bean.WxMpMassNews)} 获得media_id,然后再发送
* 如果发送视频消息,必须先使用 {@link #massVideoUpload(me.chanjar.weixin.mp.bean.WxMpMassVideo)} 获得media_id,然后再发送
* 详情请见: http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140549&token=&lang=zh_CN
* </pre>
*/
WxMpMassSendResult massOpenIdsMessageSend(WxMpMassOpenIdsMessage message) throws WxErrorException;

/**
* <pre>
* 群发消息预览接口
* 开发者可通过该接口发送消息给指定用户,在手机端查看消息的样式和排版。为了满足第三方平台开发者的需求,在保留对openID预览能力的同时,增加了对指定微信号发送预览的能力,但该能力每日调用次数有限制(100次),请勿滥用。
* 接口调用请求说明
* http请求方式: POST
* https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token=ACCESS_TOKEN
* 详情请见:http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140549&token=&lang=zh_CN
* </pre>
*
* @return wxMpMassSendResult
*/
WxMpMassSendResult massMessagePreview(WxMpMassPreviewMessage wxMpMassPreviewMessage) throws Exception;

/**
* <pre>
* 长链接转短链接接口
@@ -451,5 +374,9 @@ public interface WxMpService {
*/
RequestHttp getRequestHttp();


/**
* 返回群发消息相关接口方法的实现类对象,以方便调用其各个接口
* @return WxMpMassMessageService
*/
WxMpMassMessageService getMassMessageService();
}

+ 58
- 0
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpMassMessageServiceImpl.java 查看文件

@@ -0,0 +1,58 @@
package me.chanjar.weixin.mp.api.impl;

import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpMassMessageService;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.*;
import me.chanjar.weixin.mp.bean.result.WxMpMassSendResult;
import me.chanjar.weixin.mp.bean.result.WxMpMassUploadResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* <pre>
* 群发消息服务类
* Created by Binary Wang on 2017-8-16.
* </pre>
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
public class WxMpMassMessageServiceImpl implements WxMpMassMessageService {
protected final Logger log = LoggerFactory.getLogger(this.getClass());
private WxMpService wxMpService;

public WxMpMassMessageServiceImpl(WxMpService wxMpService) {
this.wxMpService = wxMpService;
}

@Override
public WxMpMassUploadResult massNewsUpload(WxMpMassNews news) throws WxErrorException {
String responseContent = this.wxMpService.post(MEDIA_UPLOAD_NEWS_URL, news.toJson());
return WxMpMassUploadResult.fromJson(responseContent);
}

@Override
public WxMpMassUploadResult massVideoUpload(WxMpMassVideo video) throws WxErrorException {
String responseContent = this.wxMpService.post(MEDIA_UPLOAD_VIDEO_URL, video.toJson());
return WxMpMassUploadResult.fromJson(responseContent);
}

@Override
public WxMpMassSendResult massGroupMessageSend(WxMpMassTagMessage message) throws WxErrorException {
String responseContent = this.wxMpService.post(WxMpMassMessageService.MESSAGE_MASS_SENDALL_URL, message.toJson());
return WxMpMassSendResult.fromJson(responseContent);
}

@Override
public WxMpMassSendResult massOpenIdsMessageSend(WxMpMassOpenIdsMessage message) throws WxErrorException {
String responseContent = this.wxMpService.post(MESSAGE_MASS_SEND_URL, message.toJson());
return WxMpMassSendResult.fromJson(responseContent);
}

@Override
public WxMpMassSendResult massMessagePreview(WxMpMassPreviewMessage wxMpMassPreviewMessage) throws Exception {
String responseContent = this.wxMpService.post(MESSAGE_MASS_PREVIEW_URL, wxMpMassPreviewMessage.toJson());
return WxMpMassSendResult.fromJson(responseContent);
}

}

+ 6
- 30
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceAbstractImpl.java 查看文件

@@ -43,6 +43,7 @@ public abstract class WxMpServiceAbstractImpl<H, P> implements WxMpService, Requ
private WxMpDeviceService deviceService = new WxMpDeviceServiceImpl(this);
private WxMpShakeService shakeService = new WxMpShakeServiceImpl(this);
private WxMpMemberCardService memberCardService = new WxMpMemberCardServiceImpl(this);
private WxMpMassMessageService massMessageService = new WxMpMassMessageServiceImpl(this);

private int retrySleepMillis = 1000;
private int maxRetryTimes = 5;
@@ -108,36 +109,6 @@ public abstract class WxMpServiceAbstractImpl<H, P> implements WxMpService, Requ
return getAccessToken(false);
}

@Override
public WxMpMassUploadResult massNewsUpload(WxMpMassNews news) throws WxErrorException {
String responseContent = this.post(WxMpService.MEDIA_UPLOAD_NEWS_URL, news.toJson());
return WxMpMassUploadResult.fromJson(responseContent);
}

@Override
public WxMpMassUploadResult massVideoUpload(WxMpMassVideo video) throws WxErrorException {
String responseContent = this.post(WxMpService.MEDIA_UPLOAD_VIDEO_URL, video.toJson());
return WxMpMassUploadResult.fromJson(responseContent);
}

@Override
public WxMpMassSendResult massGroupMessageSend(WxMpMassTagMessage message) throws WxErrorException {
String responseContent = this.post(WxMpService.MESSAGE_MASS_SENDALL_URL, message.toJson());
return WxMpMassSendResult.fromJson(responseContent);
}

@Override
public WxMpMassSendResult massOpenIdsMessageSend(WxMpMassOpenIdsMessage message) throws WxErrorException {
String responseContent = this.post(WxMpService.MESSAGE_MASS_SEND_URL, message.toJson());
return WxMpMassSendResult.fromJson(responseContent);
}

@Override
public WxMpMassSendResult massMessagePreview(WxMpMassPreviewMessage wxMpMassPreviewMessage) throws Exception {
String responseContent = this.post(WxMpService.MESSAGE_MASS_PREVIEW_URL, wxMpMassPreviewMessage.toJson());
return WxMpMassSendResult.fromJson(responseContent);
}

@Override
public String shortUrl(String long_url) throws WxErrorException {
JsonObject o = new JsonObject();
@@ -415,4 +386,9 @@ public abstract class WxMpServiceAbstractImpl<H, P> implements WxMpService, Requ
public RequestHttp getRequestHttp() {
return this;
}

@Override
public WxMpMassMessageService getMassMessageService() {
return this.massMessageService;
}
}

weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/WxMpMassMessageAPITest.java → weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpMassMessageServiceImplTest.java 查看文件

@@ -1,9 +1,10 @@
package me.chanjar.weixin.mp.api;
package me.chanjar.weixin.mp.api.impl;

import com.google.inject.Inject;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
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.api.test.TestConstants;
@@ -13,21 +14,21 @@ import me.chanjar.weixin.mp.bean.WxMpMassTagMessage;
import me.chanjar.weixin.mp.bean.WxMpMassVideo;
import me.chanjar.weixin.mp.bean.result.WxMpMassSendResult;
import me.chanjar.weixin.mp.bean.result.WxMpMassUploadResult;
import org.testng.*;
import org.testng.annotations.*;

import java.io.IOException;
import java.io.InputStream;

import static org.testng.Assert.*;

/**
* 测试群发消息
*
* @author chanjarster
*/
@Test(groups = "massAPI", dependsOnGroups = {"baseAPI", "mediaAPI", "groupAPI"})
@Test
@Guice(modules = ApiTestModule.class)
public class WxMpMassMessageAPITest {

public class WxMpMassMessageServiceImplTest {
@Inject
protected WxMpService wxService;

@@ -41,10 +42,10 @@ public class WxMpMassMessageAPITest {
massMessage.setContent("测试群发消息\n欢迎欢迎,热烈欢迎\n换行测试\n超链接:<a href=\"http://www.baidu.com\">Hello World</a>");
massMessage.getToUsers().add(configProvider.getOpenid());

WxMpMassSendResult massResult = this.wxService
WxMpMassSendResult massResult = this.wxService.getMassMessageService()
.massOpenIdsMessageSend(massMessage);
Assert.assertNotNull(massResult);
Assert.assertNotNull(massResult.getMsgId());
assertNotNull(massResult);
assertNotNull(massResult.getMsgId());
}

@Test(dataProvider = "massMessages")
@@ -57,10 +58,10 @@ public class WxMpMassMessageAPITest {
massMessage.setMediaId(mediaId);
massMessage.getToUsers().add(configProvider.getOpenid());

WxMpMassSendResult massResult = this.wxService
WxMpMassSendResult massResult = this.wxService.getMassMessageService()
.massOpenIdsMessageSend(massMessage);
Assert.assertNotNull(massResult);
Assert.assertNotNull(massResult.getMsgId());
assertNotNull(massResult);
assertNotNull(massResult.getMsgId());
}

@Test
@@ -71,10 +72,10 @@ public class WxMpMassMessageAPITest {
massMessage
.setTagId(this.wxService.getUserTagService().tagGet().get(0).getId());

WxMpMassSendResult massResult = this.wxService
WxMpMassSendResult massResult = this.wxService.getMassMessageService()
.massGroupMessageSend(massMessage);
Assert.assertNotNull(massResult);
Assert.assertNotNull(massResult.getMsgId());
assertNotNull(massResult);
assertNotNull(massResult.getMsgId());
}

@Test(dataProvider = "massMessages")
@@ -85,10 +86,10 @@ public class WxMpMassMessageAPITest {
massMessage.setMediaId(mediaId);
massMessage.setTagId(this.wxService.getUserTagService().tagGet().get(0).getId());

WxMpMassSendResult massResult = this.wxService
WxMpMassSendResult massResult = this.wxService.getMassMessageService()
.massGroupMessageSend(massMessage);
Assert.assertNotNull(massResult);
Assert.assertNotNull(massResult.getMsgId());
assertNotNull(massResult);
assertNotNull(massResult.getMsgId());
}

@DataProvider
@@ -103,17 +104,17 @@ public class WxMpMassMessageAPITest {
// 上传视频到媒体库
WxMediaUploadResult uploadMediaRes = this.wxService.getMaterialService()
.mediaUpload(WxConsts.MEDIA_VIDEO, TestConstants.FILE_MP4, inputStream);
Assert.assertNotNull(uploadMediaRes);
Assert.assertNotNull(uploadMediaRes.getMediaId());
assertNotNull(uploadMediaRes);
assertNotNull(uploadMediaRes.getMediaId());

// 把视频变成可被群发的媒体
WxMpMassVideo video = new WxMpMassVideo();
video.setTitle("测试标题");
video.setDescription("测试描述");
video.setMediaId(uploadMediaRes.getMediaId());
WxMpMassUploadResult uploadResult = this.wxService.massVideoUpload(video);
Assert.assertNotNull(uploadResult);
Assert.assertNotNull(uploadResult.getMediaId());
WxMpMassUploadResult uploadResult = this.wxService.getMassMessageService().massVideoUpload(video);
assertNotNull(uploadResult);
assertNotNull(uploadResult.getMediaId());
messages[0] = new Object[]{WxConsts.MASS_MSG_VIDEO, uploadResult.getMediaId()};
}

@@ -124,8 +125,8 @@ public class WxMpMassMessageAPITest {
.getSystemResourceAsStream("mm.jpeg")) {
WxMediaUploadResult uploadMediaRes = this.wxService.getMaterialService()
.mediaUpload(WxConsts.MEDIA_IMAGE, TestConstants.FILE_JPG, inputStream);
Assert.assertNotNull(uploadMediaRes);
Assert.assertNotNull(uploadMediaRes.getMediaId());
assertNotNull(uploadMediaRes);
assertNotNull(uploadMediaRes.getMediaId());
messages[1] = new Object[]{WxConsts.MASS_MSG_IMAGE, uploadMediaRes.getMediaId()};
}

@@ -136,8 +137,8 @@ public class WxMpMassMessageAPITest {
.getSystemResourceAsStream("mm.mp3")) {
WxMediaUploadResult uploadMediaRes = this.wxService.getMaterialService()
.mediaUpload(WxConsts.MEDIA_VOICE, TestConstants.FILE_MP3, inputStream);
Assert.assertNotNull(uploadMediaRes);
Assert.assertNotNull(uploadMediaRes.getMediaId());
assertNotNull(uploadMediaRes);
assertNotNull(uploadMediaRes.getMediaId());
messages[2] = new Object[]{WxConsts.MASS_MSG_VOICE, uploadMediaRes.getMediaId()};
}

@@ -149,8 +150,8 @@ public class WxMpMassMessageAPITest {
// 上传照片到媒体库
WxMediaUploadResult uploadMediaRes = this.wxService.getMaterialService()
.mediaUpload(WxConsts.MEDIA_IMAGE, TestConstants.FILE_JPG, inputStream);
Assert.assertNotNull(uploadMediaRes);
Assert.assertNotNull(uploadMediaRes.getMediaId());
assertNotNull(uploadMediaRes);
assertNotNull(uploadMediaRes.getMediaId());

// 上传图文消息
WxMpMassNews news = new WxMpMassNews();
@@ -170,10 +171,10 @@ public class WxMpMassMessageAPITest {
article2.setDigest("摘要2");
news.addArticle(article2);

WxMpMassUploadResult massUploadResult = this.wxService
WxMpMassUploadResult massUploadResult = this.wxService.getMassMessageService()
.massNewsUpload(news);
Assert.assertNotNull(massUploadResult);
Assert.assertNotNull(uploadMediaRes.getMediaId());
assertNotNull(massUploadResult);
assertNotNull(uploadMediaRes.getMediaId());
messages[3] = new Object[]{WxConsts.MASS_MSG_NEWS, massUploadResult.getMediaId()};
}


+ 1
- 1
weixin-java-mp/src/test/resources/testng.xml 查看文件

@@ -5,7 +5,7 @@
<classes>
<class name="me.chanjar.weixin.mp.api.WxMpBusyRetryTest"/>
<class name="me.chanjar.weixin.mp.api.WxMpBaseAPITest"/>
<class name="me.chanjar.weixin.mp.api.WxMpMassMessageAPITest"/>
<class name="me.chanjar.weixin.mp.api.impl.WxMpMassMessageServiceImplTest"/>
<class name="me.chanjar.weixin.mp.api.impl.WxMpUserServiceImplTest"/>
<class name="me.chanjar.weixin.mp.api.impl.WxMpQrcodeServiceImplTest"/>
<class name="me.chanjar.weixin.mp.api.WxMpShortUrlAPITest"/>


Loading…
取消
儲存