重构代码,将素材管理相关的接口移到单独一个类中管理

This commit is contained in:
BinaryWang
2016-07-21 12:35:45 +08:00
parent b033490560
commit 73bf3b0364
9 changed files with 454 additions and 410 deletions

View File

@@ -0,0 +1,182 @@
package me.chanjar.weixin.mp.api;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.bean.*;
import me.chanjar.weixin.mp.bean.result.*;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
/**
* Created by Binary Wang on 2016/7/21.
* 素材管理的相关接口,包括媒体管理的接口,
* 即以https://api.weixin.qq.com/cgi-bin/material
* 和 https://api.weixin.qq.com/cgi-bin/media开头的接口
*/
public interface WxMpMaterialService {
/**
* 新增临时素材
* @see #mediaUpload(String, String, InputStream)
* @param mediaType
* @param file
* @throws WxErrorException
*/
public WxMediaUploadResult mediaUpload(String mediaType, File file) throws WxErrorException;
/**
* <pre>
* 获取临时素材
* 本接口即为原“下载多媒体文件”接口。
* 根据微信文档视频文件下载不了会返回null
* 详情请见: <a href="http://mp.weixin.qq.com/wiki/9/677a85e3f3849af35de54bb5516c2521.html">获取临时素材</a>
* </pre>
* @param media_id
* @return 保存到本地的临时文件
* @throws WxErrorException
*/
public File mediaDownload(String media_id) throws WxErrorException;
/**
* <pre>
* 上传图文消息内的图片获取URL
* 详情请见http://mp.weixin.qq.com/wiki/15/40b6865b893947b764e2de8e4a1fb55f.html#.E4.B8.8A.E4.BC.A0.E5.9B.BE.E6.96.87.E6.B6.88.E6.81.AF.E5.86.85.E7.9A.84.E5.9B.BE.E7.89.87.E8.8E.B7.E5.8F.96URL.E3.80.90.E8.AE.A2.E9.98.85.E5.8F.B7.E4.B8.8E.E6.9C.8D.E5.8A.A1.E5.8F.B7.E8.AE.A4.E8.AF.81.E5.90.8E.E5.9D.87.E5.8F.AF.E7.94.A8.E3.80.91
* </pre>
* @param file
* @return WxMediaImgUploadResult 返回图片url
* @throws WxErrorException
*/
public WxMediaImgUploadResult mediaImgUpload(File file) throws WxErrorException;
/**
* <pre>
* 新增临时素材
* 本接口即为原“上传多媒体文件”接口。
*
* 上传的多媒体文件有格式和大小限制,如下:
* 图片image: 1M支持JPG格式
* 语音voice2M播放长度不超过60s支持AMR\MP3格式
* 视频video10MB支持MP4格式
* 缩略图thumb64KB支持JPG格式
*
* 详情请见: <a href="http://mp.weixin.qq.com/wiki/15/2d353966323806a202cd2deaafe8e557.html">新增临时素材</a>
* </pre>
* @param mediaType 媒体类型, 请看{@link me.chanjar.weixin.common.api.WxConsts}
* @param fileType 文件类型,请看{@link me.chanjar.weixin.common.api.WxConsts}
* @param inputStream 输入流
* @throws WxErrorException
*/
public WxMediaUploadResult mediaUpload(String mediaType, String fileType, InputStream inputStream) throws WxErrorException, IOException;
/**
* <pre>
* 上传非图文永久素材
*
* 上传的多媒体文件有格式和大小限制,如下:
* 图片image: 图片大小不超过2M支持bmp/png/jpeg/jpg/gif格式
* 语音voice语音大小不超过5M长度不超过60秒支持mp3/wma/wav/amr格式
* 视频video在上传视频素材时需要POST另一个表单id为description包含素材的描述信息内容格式为JSON
* 缩略图thumb文档未说明
*
* 详情请见: http://mp.weixin.qq.com/wiki/14/7e6c03263063f4813141c3e17dd4350a.html
* </pre>
* @param mediaType 媒体类型, 请看{@link me.chanjar.weixin.common.api.WxConsts}
* @param material 上传的素材, 请看{@link me.chanjar.weixin.mp.bean.WxMpMaterial}
*/
public WxMpMaterialUploadResult materialFileUpload(String mediaType, WxMpMaterial material) throws WxErrorException;
/**
* <pre>
* 上传永久图文素材
*
* 详情请见: http://mp.weixin.qq.com/wiki/14/7e6c03263063f4813141c3e17dd4350a.html
* </pre>
* @param news 上传的图文消息, 请看{@link me.chanjar.weixin.mp.bean.WxMpMaterialNews}
*/
public WxMpMaterialUploadResult materialNewsUpload(WxMpMaterialNews news) throws WxErrorException;
/**
* <pre>
* 下载声音或者图片永久素材
*
* 详情请见: http://mp.weixin.qq.com/wiki/4/b3546879f07623cb30df9ca0e420a5d0.html
* </pre>
* @param media_id 永久素材的id
*/
public InputStream materialImageOrVoiceDownload(String media_id) throws WxErrorException;
/**
* <pre>
* 获取视频永久素材的信息和下载地址
*
* 详情请见: http://mp.weixin.qq.com/wiki/4/b3546879f07623cb30df9ca0e420a5d0.html
* </pre>
* @param media_id 永久素材的id
*/
public WxMpMaterialVideoInfoResult materialVideoInfo(String media_id) throws WxErrorException;
/**
* <pre>
* 获取图文永久素材的信息
*
* 详情请见: http://mp.weixin.qq.com/wiki/4/b3546879f07623cb30df9ca0e420a5d0.html
* </pre>
* @param media_id 永久素材的id
*/
public WxMpMaterialNews materialNewsInfo(String media_id) throws WxErrorException;
/**
* <pre>
* 更新图文永久素材
*
* 详情请见: http://mp.weixin.qq.com/wiki/4/19a59cba020d506e767360ca1be29450.html
* </pre>
* @param wxMpMaterialArticleUpdate 用来更新图文素材的bean, 请看{@link me.chanjar.weixin.mp.bean.WxMpMaterialArticleUpdate}
*/
public boolean materialNewsUpdate(WxMpMaterialArticleUpdate wxMpMaterialArticleUpdate) throws WxErrorException;
/**
* <pre>
* 删除永久素材
*
* 详情请见: http://mp.weixin.qq.com/wiki/5/e66f61c303db51a6c0f90f46b15af5f5.html
* </pre>
* @param media_id 永久素材的id
*/
public boolean materialDelete(String media_id) throws WxErrorException;
/**
* <pre>
* 获取各类素材总数
*
* 详情请见: http://mp.weixin.qq.com/wiki/16/8cc64f8c189674b421bee3ed403993b8.html
* </pre>
*/
public WxMpMaterialCountResult materialCount() throws WxErrorException;
/**
* <pre>
* 分页获取图文素材列表
*
* 详情请见: http://mp.weixin.qq.com/wiki/12/2108cd7aafff7f388f41f37efa710204.html
* </pre>
* @param offset 从全部素材的该偏移位置开始返回0表示从第一个素材 返回
* @param count 返回素材的数量取值在1到20之间
*/
public WxMpMaterialNewsBatchGetResult materialNewsBatchGet(int offset, int count) throws WxErrorException;
/**
* <pre>
* 分页获取其他媒体素材列表
*
* 详情请见: http://mp.weixin.qq.com/wiki/12/2108cd7aafff7f388f41f37efa710204.html
* </pre>
* @param type 媒体类型, 请看{@link me.chanjar.weixin.common.api.WxConsts}
* @param offset 从全部素材的该偏移位置开始返回0表示从第一个素材 返回
* @param count 返回素材的数量取值在1到20之间
*/
public WxMpMaterialFileBatchGetResult materialFileBatchGet(String type, int offset, int count) throws WxErrorException;
}

View File

@@ -110,157 +110,6 @@ public interface WxMpService {
*/
public WxJsapiSignature createJsapiSignature(String url) throws WxErrorException;
/**
* <pre>
* 新增临时素材
* 本接口即为原“上传多媒体文件”接口。
*
* 上传的多媒体文件有格式和大小限制,如下:
* 图片image: 1M支持JPG格式
* 语音voice2M播放长度不超过60s支持AMR\MP3格式
* 视频video10MB支持MP4格式
* 缩略图thumb64KB支持JPG格式
*
* 详情请见: <a href="http://mp.weixin.qq.com/wiki/15/2d353966323806a202cd2deaafe8e557.html">新增临时素材</a>
* </pre>
* @param mediaType 媒体类型, 请看{@link me.chanjar.weixin.common.api.WxConsts}
* @param fileType 文件类型,请看{@link me.chanjar.weixin.common.api.WxConsts}
* @param inputStream 输入流
* @throws WxErrorException
*/
public WxMediaUploadResult mediaUpload(String mediaType, String fileType, InputStream inputStream) throws WxErrorException, IOException;
/**
* <pre>
* 上传非图文永久素材
*
* 上传的多媒体文件有格式和大小限制,如下:
* 图片image: 图片大小不超过2M支持bmp/png/jpeg/jpg/gif格式
* 语音voice语音大小不超过5M长度不超过60秒支持mp3/wma/wav/amr格式
* 视频video在上传视频素材时需要POST另一个表单id为description包含素材的描述信息内容格式为JSON
* 缩略图thumb文档未说明
*
* 详情请见: http://mp.weixin.qq.com/wiki/14/7e6c03263063f4813141c3e17dd4350a.html
* </pre>
* @param mediaType 媒体类型, 请看{@link me.chanjar.weixin.common.api.WxConsts}
* @param material 上传的素材, 请看{@link me.chanjar.weixin.mp.bean.WxMpMaterial}
*/
public WxMpMaterialUploadResult materialFileUpload(String mediaType, WxMpMaterial material) throws WxErrorException;
/**
* <pre>
* 上传永久图文素材
*
* 详情请见: http://mp.weixin.qq.com/wiki/14/7e6c03263063f4813141c3e17dd4350a.html
* </pre>
* @param news 上传的图文消息, 请看{@link me.chanjar.weixin.mp.bean.WxMpMaterialNews}
*/
public WxMpMaterialUploadResult materialNewsUpload(WxMpMaterialNews news) throws WxErrorException;
/**
* <pre>
* 下载声音或者图片永久素材
*
* 详情请见: http://mp.weixin.qq.com/wiki/4/b3546879f07623cb30df9ca0e420a5d0.html
* </pre>
* @param media_id 永久素材的id
*/
public InputStream materialImageOrVoiceDownload(String media_id) throws WxErrorException;
/**
* <pre>
* 获取视频永久素材的信息和下载地址
*
* 详情请见: http://mp.weixin.qq.com/wiki/4/b3546879f07623cb30df9ca0e420a5d0.html
* </pre>
* @param media_id 永久素材的id
*/
public WxMpMaterialVideoInfoResult materialVideoInfo(String media_id) throws WxErrorException;
/**
* <pre>
* 获取图文永久素材的信息
*
* 详情请见: http://mp.weixin.qq.com/wiki/4/b3546879f07623cb30df9ca0e420a5d0.html
* </pre>
* @param media_id 永久素材的id
*/
public WxMpMaterialNews materialNewsInfo(String media_id) throws WxErrorException;
/**
* <pre>
* 更新图文永久素材
*
* 详情请见: http://mp.weixin.qq.com/wiki/4/19a59cba020d506e767360ca1be29450.html
* </pre>
* @param wxMpMaterialArticleUpdate 用来更新图文素材的bean, 请看{@link me.chanjar.weixin.mp.bean.WxMpMaterialArticleUpdate}
*/
public boolean materialNewsUpdate(WxMpMaterialArticleUpdate wxMpMaterialArticleUpdate) throws WxErrorException;
/**
* <pre>
* 删除永久素材
*
* 详情请见: http://mp.weixin.qq.com/wiki/5/e66f61c303db51a6c0f90f46b15af5f5.html
* </pre>
* @param media_id 永久素材的id
*/
public boolean materialDelete(String media_id) throws WxErrorException;
/**
* <pre>
* 获取各类素材总数
*
* 详情请见: http://mp.weixin.qq.com/wiki/16/8cc64f8c189674b421bee3ed403993b8.html
* </pre>
*/
public WxMpMaterialCountResult materialCount() throws WxErrorException;
/**
* <pre>
* 分页获取图文素材列表
*
* 详情请见: http://mp.weixin.qq.com/wiki/12/2108cd7aafff7f388f41f37efa710204.html
* </pre>
* @param offset 从全部素材的该偏移位置开始返回0表示从第一个素材 返回
* @param count 返回素材的数量取值在1到20之间
*/
public WxMpMaterialNewsBatchGetResult materialNewsBatchGet(int offset, int count) throws WxErrorException;
/**
* <pre>
* 分页获取其他媒体素材列表
*
* 详情请见: http://mp.weixin.qq.com/wiki/12/2108cd7aafff7f388f41f37efa710204.html
* </pre>
* @param type 媒体类型, 请看{@link me.chanjar.weixin.common.api.WxConsts}
* @param offset 从全部素材的该偏移位置开始返回0表示从第一个素材 返回
* @param count 返回素材的数量取值在1到20之间
*/
public WxMpMaterialFileBatchGetResult materialFileBatchGet(String type, int offset, int count) throws WxErrorException;
/**
* 新增临时素材
* @see #mediaUpload(String, String, InputStream)
* @param mediaType
* @param file
* @throws WxErrorException
*/
public WxMediaUploadResult mediaUpload(String mediaType, File file) throws WxErrorException;
/**
* <pre>
* 获取临时素材
* 本接口即为原“下载多媒体文件”接口。
* 根据微信文档视频文件下载不了会返回null
* 详情请见: <a href="http://mp.weixin.qq.com/wiki/9/677a85e3f3849af35de54bb5516c2521.html">获取临时素材</a>
* </pre>
* @param media_id
* @return 保存到本地的临时文件
* @throws WxErrorException
*/
public File mediaDownload(String media_id) throws WxErrorException;
/**
* <pre>
* 发送客服消息
@@ -881,17 +730,6 @@ public interface WxMpService {
*/
public WxMpMassSendResult massMessagePreview(WxMpMassPreviewMessage wxMpMassPreviewMessage) throws Exception;
/**
* <pre>
* 上传图文消息内的图片获取URL
* 详情请见http://mp.weixin.qq.com/wiki/15/40b6865b893947b764e2de8e4a1fb55f.html#.E4.B8.8A.E4.BC.A0.E5.9B.BE.E6.96.87.E6.B6.88.E6.81.AF.E5.86.85.E7.9A.84.E5.9B.BE.E7.89.87.E8.8E.B7.E5.8F.96URL.E3.80.90.E8.AE.A2.E9.98.85.E5.8F.B7.E4.B8.8E.E6.9C.8D.E5.8A.A1.E5.8F.B7.E8.AE.A4.E8.AF.81.E5.90.8E.E5.9D.87.E5.8F.AF.E7.94.A8.E3.80.91
* </pre>
* @param file
* @return WxMediaImgUploadResult 返回图片url
* @throws WxErrorException
*/
public WxMediaImgUploadResult mediaImgUpload(File file) throws WxErrorException;
/**
* <pre>
* 设置所属行业
@@ -920,5 +758,16 @@ public interface WxMpService {
* @return WxMpKefuService
*/
WxMpKefuService getKefuService();
/**
* 返回客服接口方法实现类,以方便调用个其各种接口
* @return WxMpMaterialService
*/
WxMpMaterialService getMaterialService();
/**
* 获取WxMpConfigStorage 对象
* @return WxMpConfigStorage
*/
WxMpConfigStorage getWxMpConfigStorage();
}

View File

@@ -18,6 +18,7 @@ import java.util.SortedMap;
import java.util.TreeMap;
import java.util.UUID;
import me.chanjar.weixin.mp.api.impl.WxMpMaterialServiceImpl;
import org.apache.http.Consts;
import org.apache.http.HttpHost;
import org.apache.http.client.ClientProtocolException;
@@ -136,6 +137,8 @@ public class WxMpServiceImpl implements WxMpService {
protected WxMpKefuService kefuService = new WxMpKefuServiceImpl(this);
protected WxMpMaterialService materialService = new WxMpMaterialServiceImpl(this);
protected CloseableHttpClient httpClient;
protected HttpHost httpProxy;
@@ -309,119 +312,6 @@ public class WxMpServiceImpl implements WxMpService {
}
}
@Override
public WxMediaUploadResult mediaUpload(String mediaType, String fileType, InputStream inputStream) throws WxErrorException, IOException {
return mediaUpload(mediaType, FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), fileType));
}
@Override
public WxMediaUploadResult mediaUpload(String mediaType, File file) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/media/upload?type=" + mediaType;
return execute(new MediaUploadRequestExecutor(), url, file);
}
@Override
public File mediaDownload(String media_id) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/media/get";
return execute(new MediaDownloadRequestExecutor(this.wxMpConfigStorage.getTmpDirFile()), url, "media_id=" + media_id);
}
@Override
public WxMpMaterialUploadResult materialFileUpload(String mediaType, WxMpMaterial material) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/material/add_material?type=" + mediaType;
return execute(new MaterialUploadRequestExecutor(), url, material);
}
@Override
public WxMpMaterialUploadResult materialNewsUpload(WxMpMaterialNews news) throws WxErrorException {
if (news == null || news.isEmpty()) {
throw new IllegalArgumentException("news is empty!");
}
String url = "https://api.weixin.qq.com/cgi-bin/material/add_news";
String responseContent = post(url, news.toJson());
return WxMpMaterialUploadResult.fromJson(responseContent);
}
@Override
public InputStream materialImageOrVoiceDownload(String media_id) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/material/get_material";
return execute(new MaterialVoiceAndImageDownloadRequestExecutor(this.wxMpConfigStorage.getTmpDirFile()), url, media_id);
}
@Override
public WxMpMaterialVideoInfoResult materialVideoInfo(String media_id) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/material/get_material";
return execute(new MaterialVideoInfoRequestExecutor(), url, media_id);
}
@Override
public WxMpMaterialNews materialNewsInfo(String media_id) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/material/get_material";
return execute(new MaterialNewsInfoRequestExecutor(), url, media_id);
}
@Override
public boolean materialNewsUpdate(WxMpMaterialArticleUpdate wxMpMaterialArticleUpdate) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/material/update_news";
String responseText = post(url, wxMpMaterialArticleUpdate.toJson());
WxError wxError = WxError.fromJson(responseText);
if (wxError.getErrorCode() == 0) {
return true;
} else {
throw new WxErrorException(wxError);
}
}
@Override
public boolean materialDelete(String media_id) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/material/del_material";
return execute(new MaterialDeleteRequestExecutor(), url, media_id);
}
@Override
public WxMpMaterialCountResult materialCount() throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/material/get_materialcount";
String responseText = get(url, null);
WxError wxError = WxError.fromJson(responseText);
if (wxError.getErrorCode() == 0) {
return WxMpGsonBuilder.create().fromJson(responseText, WxMpMaterialCountResult.class);
} else {
throw new WxErrorException(wxError);
}
}
@Override
public WxMpMaterialNewsBatchGetResult materialNewsBatchGet(int offset, int count) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material";
Map<String, Object> params = new HashMap<>();
params.put("type", WxConsts.MATERIAL_NEWS);
params.put("offset", offset);
params.put("count", count);
String responseText = post(url, WxGsonBuilder.create().toJson(params));
WxError wxError = WxError.fromJson(responseText);
if (wxError.getErrorCode() == 0) {
return WxMpGsonBuilder.create().fromJson(responseText, WxMpMaterialNewsBatchGetResult.class);
} else {
throw new WxErrorException(wxError);
}
}
@Override
public WxMpMaterialFileBatchGetResult materialFileBatchGet(String type, int offset, int count) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material";
Map<String, Object> params = new HashMap<>();
params.put("type", type);
params.put("offset", offset);
params.put("count", count);
String responseText = post(url, WxGsonBuilder.create().toJson(params));
WxError wxError = WxError.fromJson(responseText);
if (wxError.getErrorCode() == 0) {
return WxMpGsonBuilder.create().fromJson(responseText, WxMpMaterialFileBatchGetResult.class);
} else {
throw new WxErrorException(wxError);
}
}
@Override
public WxMpMassUploadResult massNewsUpload(WxMpMassNews news) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/media/uploadnews";
@@ -877,6 +767,7 @@ public class WxMpServiceImpl implements WxMpService {
this.httpClient = apacheHttpClientBuilder.build();
}
@Override
public WxMpConfigStorage getWxMpConfigStorage() {
return this.wxMpConfigStorage;
}
@@ -1406,12 +1297,6 @@ public class WxMpServiceImpl implements WxMpService {
return WxMpMassSendResult.fromJson(responseContent);
}
@Override
public WxMediaImgUploadResult mediaImgUpload(File file) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/media/uploadimg";
return execute(new MediaImgUploadRequestExecutor(), url, file);
}
@Override
public String setIndustry(WxMpIndustry wxMpIndustry) throws WxErrorException {
if (null == wxMpIndustry.getPrimaryIndustry() || null == wxMpIndustry.getPrimaryIndustry().getId()
@@ -1433,5 +1318,10 @@ public class WxMpServiceImpl implements WxMpService {
public WxMpKefuService getKefuService() {
return this.kefuService;
}
@Override
public WxMpMaterialService getMaterialService() {
return this.materialService;
}
}

View File

@@ -0,0 +1,155 @@
package me.chanjar.weixin.mp.api.impl;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.fs.FileUtils;
import me.chanjar.weixin.common.util.http.MediaDownloadRequestExecutor;
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import me.chanjar.weixin.mp.api.WxMpMaterialService;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.*;
import me.chanjar.weixin.mp.bean.result.*;
import me.chanjar.weixin.mp.util.http.*;
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
/**
* Created by Binary Wang on 2016/7/21.
*/
public class WxMpMaterialServiceImpl implements WxMpMaterialService {
private WxMpService wxMpService;
public WxMpMaterialServiceImpl(WxMpService wxMpService) {
this.wxMpService = wxMpService;
}
@Override
public WxMediaUploadResult mediaUpload(String mediaType, String fileType, InputStream inputStream) throws WxErrorException, IOException {
return this.mediaUpload(mediaType, FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), fileType));
}
@Override
public WxMediaUploadResult mediaUpload(String mediaType, File file) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/media/upload?type=" + mediaType;
return this.wxMpService.execute(new MediaUploadRequestExecutor(), url, file);
}
@Override
public File mediaDownload(String media_id) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/media/get";
return this.wxMpService.execute(new MediaDownloadRequestExecutor(this.wxMpService.getWxMpConfigStorage().getTmpDirFile()), url, "media_id=" + media_id);
}
@Override
public WxMpMaterialUploadResult materialFileUpload(String mediaType, WxMpMaterial material) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/material/add_material?type=" + mediaType;
return this.wxMpService.execute(new MaterialUploadRequestExecutor(), url, material);
}
@Override
public WxMpMaterialUploadResult materialNewsUpload(WxMpMaterialNews news) throws WxErrorException {
if (news == null || news.isEmpty()) {
throw new IllegalArgumentException("news is empty!");
}
String url = "https://api.weixin.qq.com/cgi-bin/material/add_news";
String responseContent = this.wxMpService.post(url, news.toJson());
return WxMpMaterialUploadResult.fromJson(responseContent);
}
@Override
public InputStream materialImageOrVoiceDownload(String media_id) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/material/get_material";
return this.wxMpService.execute(new MaterialVoiceAndImageDownloadRequestExecutor(this.wxMpService.getWxMpConfigStorage().getTmpDirFile()), url, media_id);
}
@Override
public WxMpMaterialVideoInfoResult materialVideoInfo(String media_id) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/material/get_material";
return this.wxMpService.execute(new MaterialVideoInfoRequestExecutor(), url, media_id);
}
@Override
public WxMpMaterialNews materialNewsInfo(String media_id) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/material/get_material";
return this.wxMpService.execute(new MaterialNewsInfoRequestExecutor(), url, media_id);
}
@Override
public boolean materialNewsUpdate(WxMpMaterialArticleUpdate wxMpMaterialArticleUpdate) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/material/update_news";
String responseText = this.wxMpService.post(url, wxMpMaterialArticleUpdate.toJson());
WxError wxError = WxError.fromJson(responseText);
if (wxError.getErrorCode() == 0) {
return true;
} else {
throw new WxErrorException(wxError);
}
}
@Override
public boolean materialDelete(String media_id) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/material/del_material";
return this.wxMpService.execute(new MaterialDeleteRequestExecutor(), url, media_id);
}
@Override
public WxMpMaterialCountResult materialCount() throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/material/get_materialcount";
String responseText = this.wxMpService.get(url, null);
WxError wxError = WxError.fromJson(responseText);
if (wxError.getErrorCode() == 0) {
return WxMpGsonBuilder.create().fromJson(responseText, WxMpMaterialCountResult.class);
} else {
throw new WxErrorException(wxError);
}
}
@Override
public WxMpMaterialNewsBatchGetResult materialNewsBatchGet(int offset, int count) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material";
Map<String, Object> params = new HashMap<>();
params.put("type", WxConsts.MATERIAL_NEWS);
params.put("offset", offset);
params.put("count", count);
String responseText = this.wxMpService.post(url, WxGsonBuilder.create().toJson(params));
WxError wxError = WxError.fromJson(responseText);
if (wxError.getErrorCode() == 0) {
return WxMpGsonBuilder.create().fromJson(responseText, WxMpMaterialNewsBatchGetResult.class);
} else {
throw new WxErrorException(wxError);
}
}
@Override
public WxMpMaterialFileBatchGetResult materialFileBatchGet(String type, int offset, int count) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material";
Map<String, Object> params = new HashMap<>();
params.put("type", type);
params.put("offset", offset);
params.put("count", count);
String responseText = this.wxMpService.post(url, WxGsonBuilder.create().toJson(params));
WxError wxError = WxError.fromJson(responseText);
if (wxError.getErrorCode() == 0) {
return WxMpGsonBuilder.create().fromJson(responseText, WxMpMaterialFileBatchGetResult.class);
} else {
throw new WxErrorException(wxError);
}
}
@Override
public WxMediaImgUploadResult mediaImgUpload(File file) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/media/uploadimg";
return this.wxMpService.execute(new MediaImgUploadRequestExecutor(), url, file);
}
}