mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-04-05 17:38:05 +08:00
#195 抽取素材管理请求URL到常量类中
This commit is contained in:
parent
22344ebe2c
commit
554fd08fb8
@ -16,6 +16,16 @@ import java.io.InputStream;
|
||||
* </pre>
|
||||
*/
|
||||
public interface WxMpMaterialService {
|
||||
String MEDIA_GET_URL = "https://api.weixin.qq.com/cgi-bin/media/get";
|
||||
String MEDIA_UPLOAD_URL = "https://api.weixin.qq.com/cgi-bin/media/upload?type=%s";
|
||||
String IMG_UPLOAD_URL = "https://api.weixin.qq.com/cgi-bin/media/uploadimg";
|
||||
String MATERIAL_ADD_URL = "https://api.weixin.qq.com/cgi-bin/material/add_material?type=%s";
|
||||
String NEWS_ADD_URL = "https://api.weixin.qq.com/cgi-bin/material/add_news";
|
||||
String MATERIAL_GET_URL = "https://api.weixin.qq.com/cgi-bin/material/get_material";
|
||||
String NEWS_UPDATE_URL = "https://api.weixin.qq.com/cgi-bin/material/update_news";
|
||||
String MATERIAL_DEL_URL = "https://api.weixin.qq.com/cgi-bin/material/del_material";
|
||||
String MATERIAL_GET_COUNT_URL = "https://api.weixin.qq.com/cgi-bin/material/get_materialcount";
|
||||
String MATERIAL_BATCHGET_URL = "https://api.weixin.qq.com/cgi-bin/material/batchget_material";
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
@ -73,11 +83,11 @@ public interface WxMpMaterialService {
|
||||
* 接口url格式:https://api.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id=MEDIA_ID
|
||||
* </pre>
|
||||
*
|
||||
* @param media_id
|
||||
* @param mediaId 媒体文件Id
|
||||
* @return 保存到本地的临时文件
|
||||
* @throws WxErrorException
|
||||
*/
|
||||
File mediaDownload(String media_id) throws WxErrorException;
|
||||
File mediaDownload(String mediaId) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
|
@ -25,8 +25,7 @@ import java.util.UUID;
|
||||
* Created by Binary Wang on 2016/7/21.
|
||||
*/
|
||||
public class WxMpMaterialServiceImpl implements WxMpMaterialService {
|
||||
private static final String MEDIA_API_URL_PREFIX = "https://api.weixin.qq.com/cgi-bin/media";
|
||||
private static final String MATERIAL_API_URL_PREFIX = "https://api.weixin.qq.com/cgi-bin/material";
|
||||
|
||||
private WxMpService wxMpService;
|
||||
|
||||
public WxMpMaterialServiceImpl(WxMpService wxMpService) {
|
||||
@ -45,28 +44,26 @@ public class WxMpMaterialServiceImpl implements WxMpMaterialService {
|
||||
|
||||
@Override
|
||||
public WxMediaUploadResult mediaUpload(String mediaType, File file) throws WxErrorException {
|
||||
String url = MEDIA_API_URL_PREFIX + "/upload?type=" + mediaType;
|
||||
String url = String.format(MEDIA_UPLOAD_URL, mediaType);
|
||||
return this.wxMpService.execute(MediaUploadRequestExecutor.create(this.wxMpService.getRequestHttp()), url, file);
|
||||
}
|
||||
|
||||
@Override
|
||||
public File mediaDownload(String media_id) throws WxErrorException {
|
||||
String url = MEDIA_API_URL_PREFIX + "/get";
|
||||
public File mediaDownload(String mediaId) throws WxErrorException {
|
||||
return this.wxMpService.execute(
|
||||
MediaDownloadRequestExecutor.create(this.wxMpService.getRequestHttp(), this.wxMpService.getWxMpConfigStorage().getTmpDirFile()),
|
||||
url,
|
||||
"media_id=" + media_id);
|
||||
MEDIA_GET_URL,
|
||||
"media_id=" + mediaId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMediaImgUploadResult mediaImgUpload(File file) throws WxErrorException {
|
||||
String url = MEDIA_API_URL_PREFIX + "/uploadimg";
|
||||
return this.wxMpService.execute(MediaImgUploadRequestExecutor.create(this.wxMpService.getRequestHttp()), url, file);
|
||||
return this.wxMpService.execute(MediaImgUploadRequestExecutor.create(this.wxMpService.getRequestHttp()), IMG_UPLOAD_URL, file);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpMaterialUploadResult materialFileUpload(String mediaType, WxMpMaterial material) throws WxErrorException {
|
||||
String url = MATERIAL_API_URL_PREFIX + "/add_material?type=" + mediaType;
|
||||
String url = String.format(MATERIAL_ADD_URL, mediaType);
|
||||
return this.wxMpService.execute(MaterialUploadRequestExecutor.create(this.wxMpService.getRequestHttp()), url, material);
|
||||
}
|
||||
|
||||
@ -75,33 +72,29 @@ public class WxMpMaterialServiceImpl implements WxMpMaterialService {
|
||||
if (news == null || news.isEmpty()) {
|
||||
throw new IllegalArgumentException("news is empty!");
|
||||
}
|
||||
String url = MATERIAL_API_URL_PREFIX + "/add_news";
|
||||
String responseContent = this.wxMpService.post(url, news.toJson());
|
||||
String responseContent = this.wxMpService.post(NEWS_ADD_URL, news.toJson());
|
||||
return WxMpMaterialUploadResult.fromJson(responseContent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream materialImageOrVoiceDownload(String media_id) throws WxErrorException {
|
||||
String url = MATERIAL_API_URL_PREFIX + "/get_material";
|
||||
return this.wxMpService.execute(MaterialVoiceAndImageDownloadRequestExecutor.create(this.wxMpService.getRequestHttp(), this.wxMpService.getWxMpConfigStorage().getTmpDirFile()), url, media_id);
|
||||
return this.wxMpService.execute(MaterialVoiceAndImageDownloadRequestExecutor
|
||||
.create(this.wxMpService.getRequestHttp(), this.wxMpService.getWxMpConfigStorage().getTmpDirFile()), MATERIAL_GET_URL, media_id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpMaterialVideoInfoResult materialVideoInfo(String media_id) throws WxErrorException {
|
||||
String url = MATERIAL_API_URL_PREFIX + "/get_material";
|
||||
return this.wxMpService.execute(MaterialVideoInfoRequestExecutor.create(this.wxMpService.getRequestHttp()), url, media_id);
|
||||
return this.wxMpService.execute(MaterialVideoInfoRequestExecutor.create(this.wxMpService.getRequestHttp()), MATERIAL_GET_URL, media_id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpMaterialNews materialNewsInfo(String media_id) throws WxErrorException {
|
||||
String url = MATERIAL_API_URL_PREFIX + "/get_material";
|
||||
return this.wxMpService.execute(MaterialNewsInfoRequestExecutor.create(this.wxMpService.getRequestHttp()), url, media_id);
|
||||
return this.wxMpService.execute(MaterialNewsInfoRequestExecutor.create(this.wxMpService.getRequestHttp()), MATERIAL_GET_URL, media_id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean materialNewsUpdate(WxMpMaterialArticleUpdate wxMpMaterialArticleUpdate) throws WxErrorException {
|
||||
String url = MATERIAL_API_URL_PREFIX + "/update_news";
|
||||
String responseText = this.wxMpService.post(url, wxMpMaterialArticleUpdate.toJson());
|
||||
String responseText = this.wxMpService.post(NEWS_UPDATE_URL, wxMpMaterialArticleUpdate.toJson());
|
||||
WxError wxError = WxError.fromJson(responseText);
|
||||
if (wxError.getErrorCode() == 0) {
|
||||
return true;
|
||||
@ -112,14 +105,12 @@ public class WxMpMaterialServiceImpl implements WxMpMaterialService {
|
||||
|
||||
@Override
|
||||
public boolean materialDelete(String media_id) throws WxErrorException {
|
||||
String url = MATERIAL_API_URL_PREFIX + "/del_material";
|
||||
return this.wxMpService.execute(MaterialDeleteRequestExecutor.create(this.wxMpService.getRequestHttp()), url, media_id);
|
||||
return this.wxMpService.execute(MaterialDeleteRequestExecutor.create(this.wxMpService.getRequestHttp()), MATERIAL_DEL_URL, media_id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpMaterialCountResult materialCount() throws WxErrorException {
|
||||
String url = MATERIAL_API_URL_PREFIX + "/get_materialcount";
|
||||
String responseText = this.wxMpService.get(url, null);
|
||||
String responseText = this.wxMpService.get(MATERIAL_GET_COUNT_URL, null);
|
||||
WxError wxError = WxError.fromJson(responseText);
|
||||
if (wxError.getErrorCode() == 0) {
|
||||
return WxMpGsonBuilder.create().fromJson(responseText, WxMpMaterialCountResult.class);
|
||||
@ -130,12 +121,11 @@ public class WxMpMaterialServiceImpl implements WxMpMaterialService {
|
||||
|
||||
@Override
|
||||
public WxMpMaterialNewsBatchGetResult materialNewsBatchGet(int offset, int count) throws WxErrorException {
|
||||
String url = MATERIAL_API_URL_PREFIX + "/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));
|
||||
String responseText = this.wxMpService.post(MATERIAL_BATCHGET_URL, WxGsonBuilder.create().toJson(params));
|
||||
WxError wxError = WxError.fromJson(responseText);
|
||||
if (wxError.getErrorCode() == 0) {
|
||||
return WxMpGsonBuilder.create().fromJson(responseText, WxMpMaterialNewsBatchGetResult.class);
|
||||
@ -146,12 +136,11 @@ public class WxMpMaterialServiceImpl implements WxMpMaterialService {
|
||||
|
||||
@Override
|
||||
public WxMpMaterialFileBatchGetResult materialFileBatchGet(String type, int offset, int count) throws WxErrorException {
|
||||
String url = MATERIAL_API_URL_PREFIX + "/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));
|
||||
String responseText = this.wxMpService.post(MATERIAL_BATCHGET_URL, WxGsonBuilder.create().toJson(params));
|
||||
WxError wxError = WxError.fromJson(responseText);
|
||||
if (wxError.getErrorCode() == 0) {
|
||||
return WxMpGsonBuilder.create().fromJson(responseText, WxMpMaterialFileBatchGetResult.class);
|
||||
|
Loading…
Reference in New Issue
Block a user