Skip to content

Commit

Permalink
转移群发消息相关接口到单独接口及其实现类中
Browse files Browse the repository at this point in the history
  • Loading branch information
binarywang committed Sep 2, 2017
1 parent e6a3f60 commit 2a7c977
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 140 deletions.
Original file line number Diff line number Diff line change
@@ -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;

}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
/**
* 长链接转短链接接口
*/
Expand Down Expand Up @@ -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>
* 长链接转短链接接口
Expand Down Expand Up @@ -451,5 +374,9 @@ public interface WxMpService {
*/
RequestHttp getRequestHttp();


/**
* 返回群发消息相关接口方法的实现类对象,以方便调用其各个接口
* @return WxMpMassMessageService
*/
WxMpMassMessageService getMassMessageService();
}
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -108,36 +109,6 @@ public String getAccessToken() throws WxErrorException {
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();
Expand Down Expand Up @@ -415,4 +386,9 @@ public WxMpMemberCardService getMemberCardService() {
public RequestHttp getRequestHttp() {
return this;
}

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

0 comments on commit 2a7c977

Please sign in to comment.