Skip to content

Commit 554fd08

Browse files
committed
binarywang#195 抽取素材管理请求URL到常量类中
1 parent 22344eb commit 554fd08

File tree

2 files changed

+29
-30
lines changed

2 files changed

+29
-30
lines changed

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpMaterialService.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@
1616
* </pre>
1717
*/
1818
public interface WxMpMaterialService {
19+
String MEDIA_GET_URL = "https://api.weixin.qq.com/cgi-bin/media/get";
20+
String MEDIA_UPLOAD_URL = "https://api.weixin.qq.com/cgi-bin/media/upload?type=%s";
21+
String IMG_UPLOAD_URL = "https://api.weixin.qq.com/cgi-bin/media/uploadimg";
22+
String MATERIAL_ADD_URL = "https://api.weixin.qq.com/cgi-bin/material/add_material?type=%s";
23+
String NEWS_ADD_URL = "https://api.weixin.qq.com/cgi-bin/material/add_news";
24+
String MATERIAL_GET_URL = "https://api.weixin.qq.com/cgi-bin/material/get_material";
25+
String NEWS_UPDATE_URL = "https://api.weixin.qq.com/cgi-bin/material/update_news";
26+
String MATERIAL_DEL_URL = "https://api.weixin.qq.com/cgi-bin/material/del_material";
27+
String MATERIAL_GET_COUNT_URL = "https://api.weixin.qq.com/cgi-bin/material/get_materialcount";
28+
String MATERIAL_BATCHGET_URL = "https://api.weixin.qq.com/cgi-bin/material/batchget_material";
1929

2030
/**
2131
* <pre>
@@ -73,11 +83,11 @@ public interface WxMpMaterialService {
7383
* 接口url格式:https://api.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id=MEDIA_ID
7484
* </pre>
7585
*
76-
* @param media_id
86+
* @param mediaId 媒体文件Id
7787
* @return 保存到本地的临时文件
7888
* @throws WxErrorException
7989
*/
80-
File mediaDownload(String media_id) throws WxErrorException;
90+
File mediaDownload(String mediaId) throws WxErrorException;
8191

8292
/**
8393
* <pre>

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpMaterialServiceImpl.java

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525
* Created by Binary Wang on 2016/7/21.
2626
*/
2727
public class WxMpMaterialServiceImpl implements WxMpMaterialService {
28-
private static final String MEDIA_API_URL_PREFIX = "https://api.weixin.qq.com/cgi-bin/media";
29-
private static final String MATERIAL_API_URL_PREFIX = "https://api.weixin.qq.com/cgi-bin/material";
28+
3029
private WxMpService wxMpService;
3130

3231
public WxMpMaterialServiceImpl(WxMpService wxMpService) {
@@ -45,28 +44,26 @@ public WxMediaUploadResult mediaUpload(String mediaType, String fileType, InputS
4544

4645
@Override
4746
public WxMediaUploadResult mediaUpload(String mediaType, File file) throws WxErrorException {
48-
String url = MEDIA_API_URL_PREFIX + "/upload?type=" + mediaType;
47+
String url = String.format(MEDIA_UPLOAD_URL, mediaType);
4948
return this.wxMpService.execute(MediaUploadRequestExecutor.create(this.wxMpService.getRequestHttp()), url, file);
5049
}
5150

5251
@Override
53-
public File mediaDownload(String media_id) throws WxErrorException {
54-
String url = MEDIA_API_URL_PREFIX + "/get";
52+
public File mediaDownload(String mediaId) throws WxErrorException {
5553
return this.wxMpService.execute(
5654
MediaDownloadRequestExecutor.create(this.wxMpService.getRequestHttp(), this.wxMpService.getWxMpConfigStorage().getTmpDirFile()),
57-
url,
58-
"media_id=" + media_id);
55+
MEDIA_GET_URL,
56+
"media_id=" + mediaId);
5957
}
6058

6159
@Override
6260
public WxMediaImgUploadResult mediaImgUpload(File file) throws WxErrorException {
63-
String url = MEDIA_API_URL_PREFIX + "/uploadimg";
64-
return this.wxMpService.execute(MediaImgUploadRequestExecutor.create(this.wxMpService.getRequestHttp()), url, file);
61+
return this.wxMpService.execute(MediaImgUploadRequestExecutor.create(this.wxMpService.getRequestHttp()), IMG_UPLOAD_URL, file);
6562
}
6663

6764
@Override
6865
public WxMpMaterialUploadResult materialFileUpload(String mediaType, WxMpMaterial material) throws WxErrorException {
69-
String url = MATERIAL_API_URL_PREFIX + "/add_material?type=" + mediaType;
66+
String url = String.format(MATERIAL_ADD_URL, mediaType);
7067
return this.wxMpService.execute(MaterialUploadRequestExecutor.create(this.wxMpService.getRequestHttp()), url, material);
7168
}
7269

@@ -75,33 +72,29 @@ public WxMpMaterialUploadResult materialNewsUpload(WxMpMaterialNews news) throws
7572
if (news == null || news.isEmpty()) {
7673
throw new IllegalArgumentException("news is empty!");
7774
}
78-
String url = MATERIAL_API_URL_PREFIX + "/add_news";
79-
String responseContent = this.wxMpService.post(url, news.toJson());
75+
String responseContent = this.wxMpService.post(NEWS_ADD_URL, news.toJson());
8076
return WxMpMaterialUploadResult.fromJson(responseContent);
8177
}
8278

8379
@Override
8480
public InputStream materialImageOrVoiceDownload(String media_id) throws WxErrorException {
85-
String url = MATERIAL_API_URL_PREFIX + "/get_material";
86-
return this.wxMpService.execute(MaterialVoiceAndImageDownloadRequestExecutor.create(this.wxMpService.getRequestHttp(), this.wxMpService.getWxMpConfigStorage().getTmpDirFile()), url, media_id);
81+
return this.wxMpService.execute(MaterialVoiceAndImageDownloadRequestExecutor
82+
.create(this.wxMpService.getRequestHttp(), this.wxMpService.getWxMpConfigStorage().getTmpDirFile()), MATERIAL_GET_URL, media_id);
8783
}
8884

8985
@Override
9086
public WxMpMaterialVideoInfoResult materialVideoInfo(String media_id) throws WxErrorException {
91-
String url = MATERIAL_API_URL_PREFIX + "/get_material";
92-
return this.wxMpService.execute(MaterialVideoInfoRequestExecutor.create(this.wxMpService.getRequestHttp()), url, media_id);
87+
return this.wxMpService.execute(MaterialVideoInfoRequestExecutor.create(this.wxMpService.getRequestHttp()), MATERIAL_GET_URL, media_id);
9388
}
9489

9590
@Override
9691
public WxMpMaterialNews materialNewsInfo(String media_id) throws WxErrorException {
97-
String url = MATERIAL_API_URL_PREFIX + "/get_material";
98-
return this.wxMpService.execute(MaterialNewsInfoRequestExecutor.create(this.wxMpService.getRequestHttp()), url, media_id);
92+
return this.wxMpService.execute(MaterialNewsInfoRequestExecutor.create(this.wxMpService.getRequestHttp()), MATERIAL_GET_URL, media_id);
9993
}
10094

10195
@Override
10296
public boolean materialNewsUpdate(WxMpMaterialArticleUpdate wxMpMaterialArticleUpdate) throws WxErrorException {
103-
String url = MATERIAL_API_URL_PREFIX + "/update_news";
104-
String responseText = this.wxMpService.post(url, wxMpMaterialArticleUpdate.toJson());
97+
String responseText = this.wxMpService.post(NEWS_UPDATE_URL, wxMpMaterialArticleUpdate.toJson());
10598
WxError wxError = WxError.fromJson(responseText);
10699
if (wxError.getErrorCode() == 0) {
107100
return true;
@@ -112,14 +105,12 @@ public boolean materialNewsUpdate(WxMpMaterialArticleUpdate wxMpMaterialArticleU
112105

113106
@Override
114107
public boolean materialDelete(String media_id) throws WxErrorException {
115-
String url = MATERIAL_API_URL_PREFIX + "/del_material";
116-
return this.wxMpService.execute(MaterialDeleteRequestExecutor.create(this.wxMpService.getRequestHttp()), url, media_id);
108+
return this.wxMpService.execute(MaterialDeleteRequestExecutor.create(this.wxMpService.getRequestHttp()), MATERIAL_DEL_URL, media_id);
117109
}
118110

119111
@Override
120112
public WxMpMaterialCountResult materialCount() throws WxErrorException {
121-
String url = MATERIAL_API_URL_PREFIX + "/get_materialcount";
122-
String responseText = this.wxMpService.get(url, null);
113+
String responseText = this.wxMpService.get(MATERIAL_GET_COUNT_URL, null);
123114
WxError wxError = WxError.fromJson(responseText);
124115
if (wxError.getErrorCode() == 0) {
125116
return WxMpGsonBuilder.create().fromJson(responseText, WxMpMaterialCountResult.class);
@@ -130,12 +121,11 @@ public WxMpMaterialCountResult materialCount() throws WxErrorException {
130121

131122
@Override
132123
public WxMpMaterialNewsBatchGetResult materialNewsBatchGet(int offset, int count) throws WxErrorException {
133-
String url = MATERIAL_API_URL_PREFIX + "/batchget_material";
134124
Map<String, Object> params = new HashMap<>();
135125
params.put("type", WxConsts.MATERIAL_NEWS);
136126
params.put("offset", offset);
137127
params.put("count", count);
138-
String responseText = this.wxMpService.post(url, WxGsonBuilder.create().toJson(params));
128+
String responseText = this.wxMpService.post(MATERIAL_BATCHGET_URL, WxGsonBuilder.create().toJson(params));
139129
WxError wxError = WxError.fromJson(responseText);
140130
if (wxError.getErrorCode() == 0) {
141131
return WxMpGsonBuilder.create().fromJson(responseText, WxMpMaterialNewsBatchGetResult.class);
@@ -146,12 +136,11 @@ public WxMpMaterialNewsBatchGetResult materialNewsBatchGet(int offset, int count
146136

147137
@Override
148138
public WxMpMaterialFileBatchGetResult materialFileBatchGet(String type, int offset, int count) throws WxErrorException {
149-
String url = MATERIAL_API_URL_PREFIX + "/batchget_material";
150139
Map<String, Object> params = new HashMap<>();
151140
params.put("type", type);
152141
params.put("offset", offset);
153142
params.put("count", count);
154-
String responseText = this.wxMpService.post(url, WxGsonBuilder.create().toJson(params));
143+
String responseText = this.wxMpService.post(MATERIAL_BATCHGET_URL, WxGsonBuilder.create().toJson(params));
155144
WxError wxError = WxError.fromJson(responseText);
156145
if (wxError.getErrorCode() == 0) {
157146
return WxMpGsonBuilder.create().fromJson(responseText, WxMpMaterialFileBatchGetResult.class);

0 commit comments

Comments
 (0)