|
@@ -25,8 +25,7 @@ import java.util.UUID; |
|
|
* Created by Binary Wang on 2016/7/21. |
|
|
* Created by Binary Wang on 2016/7/21. |
|
|
*/ |
|
|
*/ |
|
|
public class WxMpMaterialServiceImpl implements WxMpMaterialService { |
|
|
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; |
|
|
private WxMpService wxMpService; |
|
|
|
|
|
|
|
|
public WxMpMaterialServiceImpl(WxMpService wxMpService) { |
|
|
public WxMpMaterialServiceImpl(WxMpService wxMpService) { |
|
@@ -45,28 +44,26 @@ public class WxMpMaterialServiceImpl implements WxMpMaterialService { |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public WxMediaUploadResult mediaUpload(String mediaType, File file) throws WxErrorException { |
|
|
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); |
|
|
return this.wxMpService.execute(MediaUploadRequestExecutor.create(this.wxMpService.getRequestHttp()), url, file); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@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( |
|
|
return this.wxMpService.execute( |
|
|
MediaDownloadRequestExecutor.create(this.wxMpService.getRequestHttp(), this.wxMpService.getWxMpConfigStorage().getTmpDirFile()), |
|
|
MediaDownloadRequestExecutor.create(this.wxMpService.getRequestHttp(), this.wxMpService.getWxMpConfigStorage().getTmpDirFile()), |
|
|
url, |
|
|
|
|
|
"media_id=" + media_id); |
|
|
|
|
|
|
|
|
MEDIA_GET_URL, |
|
|
|
|
|
"media_id=" + mediaId); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public WxMediaImgUploadResult mediaImgUpload(File file) throws WxErrorException { |
|
|
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 |
|
|
@Override |
|
|
public WxMpMaterialUploadResult materialFileUpload(String mediaType, WxMpMaterial material) throws WxErrorException { |
|
|
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); |
|
|
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()) { |
|
|
if (news == null || news.isEmpty()) { |
|
|
throw new IllegalArgumentException("news is empty!"); |
|
|
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); |
|
|
return WxMpMaterialUploadResult.fromJson(responseContent); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public InputStream materialImageOrVoiceDownload(String media_id) throws WxErrorException { |
|
|
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 |
|
|
@Override |
|
|
public WxMpMaterialVideoInfoResult materialVideoInfo(String media_id) throws WxErrorException { |
|
|
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 |
|
|
@Override |
|
|
public WxMpMaterialNews materialNewsInfo(String media_id) throws WxErrorException { |
|
|
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 |
|
|
@Override |
|
|
public boolean materialNewsUpdate(WxMpMaterialArticleUpdate wxMpMaterialArticleUpdate) throws WxErrorException { |
|
|
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); |
|
|
WxError wxError = WxError.fromJson(responseText); |
|
|
if (wxError.getErrorCode() == 0) { |
|
|
if (wxError.getErrorCode() == 0) { |
|
|
return true; |
|
|
return true; |
|
@@ -112,14 +105,12 @@ public class WxMpMaterialServiceImpl implements WxMpMaterialService { |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public boolean materialDelete(String media_id) throws WxErrorException { |
|
|
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 |
|
|
@Override |
|
|
public WxMpMaterialCountResult materialCount() throws WxErrorException { |
|
|
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); |
|
|
WxError wxError = WxError.fromJson(responseText); |
|
|
if (wxError.getErrorCode() == 0) { |
|
|
if (wxError.getErrorCode() == 0) { |
|
|
return WxMpGsonBuilder.create().fromJson(responseText, WxMpMaterialCountResult.class); |
|
|
return WxMpGsonBuilder.create().fromJson(responseText, WxMpMaterialCountResult.class); |
|
@@ -130,12 +121,11 @@ public class WxMpMaterialServiceImpl implements WxMpMaterialService { |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public WxMpMaterialNewsBatchGetResult materialNewsBatchGet(int offset, int count) throws WxErrorException { |
|
|
public WxMpMaterialNewsBatchGetResult materialNewsBatchGet(int offset, int count) throws WxErrorException { |
|
|
String url = MATERIAL_API_URL_PREFIX + "/batchget_material"; |
|
|
|
|
|
Map<String, Object> params = new HashMap<>(); |
|
|
Map<String, Object> params = new HashMap<>(); |
|
|
params.put("type", WxConsts.MATERIAL_NEWS); |
|
|
params.put("type", WxConsts.MATERIAL_NEWS); |
|
|
params.put("offset", offset); |
|
|
params.put("offset", offset); |
|
|
params.put("count", count); |
|
|
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); |
|
|
WxError wxError = WxError.fromJson(responseText); |
|
|
if (wxError.getErrorCode() == 0) { |
|
|
if (wxError.getErrorCode() == 0) { |
|
|
return WxMpGsonBuilder.create().fromJson(responseText, WxMpMaterialNewsBatchGetResult.class); |
|
|
return WxMpGsonBuilder.create().fromJson(responseText, WxMpMaterialNewsBatchGetResult.class); |
|
@@ -146,12 +136,11 @@ public class WxMpMaterialServiceImpl implements WxMpMaterialService { |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public WxMpMaterialFileBatchGetResult materialFileBatchGet(String type, int offset, int count) throws WxErrorException { |
|
|
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<>(); |
|
|
Map<String, Object> params = new HashMap<>(); |
|
|
params.put("type", type); |
|
|
params.put("type", type); |
|
|
params.put("offset", offset); |
|
|
params.put("offset", offset); |
|
|
params.put("count", count); |
|
|
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); |
|
|
WxError wxError = WxError.fromJson(responseText); |
|
|
if (wxError.getErrorCode() == 0) { |
|
|
if (wxError.getErrorCode() == 0) { |
|
|
return WxMpGsonBuilder.create().fromJson(responseText, WxMpMaterialFileBatchGetResult.class); |
|
|
return WxMpGsonBuilder.create().fromJson(responseText, WxMpMaterialFileBatchGetResult.class); |
|
|