mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-10-31 18:46:10 +08:00 
			
		
		
		
	#195 抽取素材管理请求URL到常量类中
This commit is contained in:
		| @ -16,6 +16,16 @@ import java.io.InputStream; | |||||||
|  * </pre> |  * </pre> | ||||||
|  */ |  */ | ||||||
| public interface WxMpMaterialService { | 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> |    * <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 |    * 接口url格式:https://api.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id=MEDIA_ID | ||||||
|    * </pre> |    * </pre> | ||||||
|    * |    * | ||||||
|    * @param media_id |    * @param mediaId 媒体文件Id | ||||||
|    * @return 保存到本地的临时文件 |    * @return 保存到本地的临时文件 | ||||||
|    * @throws WxErrorException |    * @throws WxErrorException | ||||||
|    */ |    */ | ||||||
|   File mediaDownload(String media_id) throws WxErrorException; |   File mediaDownload(String mediaId) throws WxErrorException; | ||||||
|  |  | ||||||
|   /** |   /** | ||||||
|    * <pre> |    * <pre> | ||||||
|  | |||||||
| @ -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 { |   public File mediaDownload(String mediaId) throws WxErrorException { | ||||||
|     String url = MEDIA_API_URL_PREFIX + "/get"; |  | ||||||
|     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_GET_URL, | ||||||
|       "media_id=" + media_id); |       "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()), IMG_UPLOAD_URL, file); | ||||||
|     return this.wxMpService.execute(MediaImgUploadRequestExecutor.create(this.wxMpService.getRequestHttp()), 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(NEWS_ADD_URL, news.toJson()); | ||||||
|     String responseContent = this.wxMpService.post(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 | ||||||
|     return this.wxMpService.execute(MaterialVoiceAndImageDownloadRequestExecutor.create(this.wxMpService.getRequestHttp(), this.wxMpService.getWxMpConfigStorage().getTmpDirFile()), url, media_id); |       .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()), MATERIAL_GET_URL, media_id); | ||||||
|     return this.wxMpService.execute(MaterialVideoInfoRequestExecutor.create(this.wxMpService.getRequestHttp()), 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()), MATERIAL_GET_URL, media_id); | ||||||
|     return this.wxMpService.execute(MaterialNewsInfoRequestExecutor.create(this.wxMpService.getRequestHttp()), 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(NEWS_UPDATE_URL, wxMpMaterialArticleUpdate.toJson()); | ||||||
|     String responseText = this.wxMpService.post(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()), MATERIAL_DEL_URL, media_id); | ||||||
|     return this.wxMpService.execute(MaterialDeleteRequestExecutor.create(this.wxMpService.getRequestHttp()), 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(MATERIAL_GET_COUNT_URL, null); | ||||||
|     String responseText = this.wxMpService.get(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); | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 Binary Wang
					Binary Wang