Skip to content

Commit 73bf3b0

Browse files
committed
重构代码,将素材管理相关的接口移到单独一个类中管理
1 parent b033490 commit 73bf3b0

File tree

9 files changed

+454
-410
lines changed

9 files changed

+454
-410
lines changed
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
package me.chanjar.weixin.mp.api;
2+
3+
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
4+
import me.chanjar.weixin.common.exception.WxErrorException;
5+
import me.chanjar.weixin.mp.bean.*;
6+
import me.chanjar.weixin.mp.bean.result.*;
7+
8+
import java.io.File;
9+
import java.io.IOException;
10+
import java.io.InputStream;
11+
12+
/**
13+
* Created by Binary Wang on 2016/7/21.
14+
* 素材管理的相关接口,包括媒体管理的接口,
15+
* 即以https://api.weixin.qq.com/cgi-bin/material
16+
* 和 https://api.weixin.qq.com/cgi-bin/media开头的接口
17+
*/
18+
public interface WxMpMaterialService {
19+
20+
/**
21+
* 新增临时素材
22+
* @see #mediaUpload(String, String, InputStream)
23+
* @param mediaType
24+
* @param file
25+
* @throws WxErrorException
26+
*/
27+
public WxMediaUploadResult mediaUpload(String mediaType, File file) throws WxErrorException;
28+
29+
/**
30+
* <pre>
31+
* 获取临时素材
32+
* 本接口即为原“下载多媒体文件”接口。
33+
* 根据微信文档,视频文件下载不了,会返回null
34+
* 详情请见: <a href="http://mp.weixin.qq.com/wiki/9/677a85e3f3849af35de54bb5516c2521.html">获取临时素材</a>
35+
* </pre>
36+
* @param media_id
37+
* @return 保存到本地的临时文件
38+
* @throws WxErrorException
39+
*/
40+
public File mediaDownload(String media_id) throws WxErrorException;
41+
42+
/**
43+
* <pre>
44+
* 上传图文消息内的图片获取URL
45+
* 详情请见: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
46+
* </pre>
47+
* @param file
48+
* @return WxMediaImgUploadResult 返回图片url
49+
* @throws WxErrorException
50+
*/
51+
public WxMediaImgUploadResult mediaImgUpload(File file) throws WxErrorException;
52+
53+
/**
54+
* <pre>
55+
* 新增临时素材
56+
* 本接口即为原“上传多媒体文件”接口。
57+
*
58+
* 上传的多媒体文件有格式和大小限制,如下:
59+
* 图片(image): 1M,支持JPG格式
60+
* 语音(voice):2M,播放长度不超过60s,支持AMR\MP3格式
61+
* 视频(video):10MB,支持MP4格式
62+
* 缩略图(thumb):64KB,支持JPG格式
63+
*
64+
* 详情请见: <a href="http://mp.weixin.qq.com/wiki/15/2d353966323806a202cd2deaafe8e557.html">新增临时素材</a>
65+
* </pre>
66+
* @param mediaType 媒体类型, 请看{@link me.chanjar.weixin.common.api.WxConsts}
67+
* @param fileType 文件类型,请看{@link me.chanjar.weixin.common.api.WxConsts}
68+
* @param inputStream 输入流
69+
* @throws WxErrorException
70+
*/
71+
public WxMediaUploadResult mediaUpload(String mediaType, String fileType, InputStream inputStream) throws WxErrorException, IOException;
72+
73+
/**
74+
* <pre>
75+
* 上传非图文永久素材
76+
*
77+
* 上传的多媒体文件有格式和大小限制,如下:
78+
* 图片(image): 图片大小不超过2M,支持bmp/png/jpeg/jpg/gif格式
79+
* 语音(voice):语音大小不超过5M,长度不超过60秒,支持mp3/wma/wav/amr格式
80+
* 视频(video):在上传视频素材时需要POST另一个表单,id为description,包含素材的描述信息,内容格式为JSON
81+
* 缩略图(thumb):文档未说明
82+
*
83+
* 详情请见: http://mp.weixin.qq.com/wiki/14/7e6c03263063f4813141c3e17dd4350a.html
84+
* </pre>
85+
* @param mediaType 媒体类型, 请看{@link me.chanjar.weixin.common.api.WxConsts}
86+
* @param material 上传的素材, 请看{@link me.chanjar.weixin.mp.bean.WxMpMaterial}
87+
*/
88+
public WxMpMaterialUploadResult materialFileUpload(String mediaType, WxMpMaterial material) throws WxErrorException;
89+
90+
/**
91+
* <pre>
92+
* 上传永久图文素材
93+
*
94+
* 详情请见: http://mp.weixin.qq.com/wiki/14/7e6c03263063f4813141c3e17dd4350a.html
95+
* </pre>
96+
* @param news 上传的图文消息, 请看{@link me.chanjar.weixin.mp.bean.WxMpMaterialNews}
97+
*/
98+
public WxMpMaterialUploadResult materialNewsUpload(WxMpMaterialNews news) throws WxErrorException;
99+
100+
/**
101+
* <pre>
102+
* 下载声音或者图片永久素材
103+
*
104+
* 详情请见: http://mp.weixin.qq.com/wiki/4/b3546879f07623cb30df9ca0e420a5d0.html
105+
* </pre>
106+
* @param media_id 永久素材的id
107+
*/
108+
public InputStream materialImageOrVoiceDownload(String media_id) throws WxErrorException;
109+
110+
/**
111+
* <pre>
112+
* 获取视频永久素材的信息和下载地址
113+
*
114+
* 详情请见: http://mp.weixin.qq.com/wiki/4/b3546879f07623cb30df9ca0e420a5d0.html
115+
* </pre>
116+
* @param media_id 永久素材的id
117+
*/
118+
public WxMpMaterialVideoInfoResult materialVideoInfo(String media_id) throws WxErrorException;
119+
120+
/**
121+
* <pre>
122+
* 获取图文永久素材的信息
123+
*
124+
* 详情请见: http://mp.weixin.qq.com/wiki/4/b3546879f07623cb30df9ca0e420a5d0.html
125+
* </pre>
126+
* @param media_id 永久素材的id
127+
*/
128+
public WxMpMaterialNews materialNewsInfo(String media_id) throws WxErrorException;
129+
130+
/**
131+
* <pre>
132+
* 更新图文永久素材
133+
*
134+
* 详情请见: http://mp.weixin.qq.com/wiki/4/19a59cba020d506e767360ca1be29450.html
135+
* </pre>
136+
* @param wxMpMaterialArticleUpdate 用来更新图文素材的bean, 请看{@link me.chanjar.weixin.mp.bean.WxMpMaterialArticleUpdate}
137+
*/
138+
public boolean materialNewsUpdate(WxMpMaterialArticleUpdate wxMpMaterialArticleUpdate) throws WxErrorException;
139+
140+
/**
141+
* <pre>
142+
* 删除永久素材
143+
*
144+
* 详情请见: http://mp.weixin.qq.com/wiki/5/e66f61c303db51a6c0f90f46b15af5f5.html
145+
* </pre>
146+
* @param media_id 永久素材的id
147+
*/
148+
public boolean materialDelete(String media_id) throws WxErrorException;
149+
150+
/**
151+
* <pre>
152+
* 获取各类素材总数
153+
*
154+
* 详情请见: http://mp.weixin.qq.com/wiki/16/8cc64f8c189674b421bee3ed403993b8.html
155+
* </pre>
156+
*/
157+
public WxMpMaterialCountResult materialCount() throws WxErrorException;
158+
159+
/**
160+
* <pre>
161+
* 分页获取图文素材列表
162+
*
163+
* 详情请见: http://mp.weixin.qq.com/wiki/12/2108cd7aafff7f388f41f37efa710204.html
164+
* </pre>
165+
* @param offset 从全部素材的该偏移位置开始返回,0表示从第一个素材 返回
166+
* @param count 返回素材的数量,取值在1到20之间
167+
*/
168+
public WxMpMaterialNewsBatchGetResult materialNewsBatchGet(int offset, int count) throws WxErrorException;
169+
170+
/**
171+
* <pre>
172+
* 分页获取其他媒体素材列表
173+
*
174+
* 详情请见: http://mp.weixin.qq.com/wiki/12/2108cd7aafff7f388f41f37efa710204.html
175+
* </pre>
176+
* @param type 媒体类型, 请看{@link me.chanjar.weixin.common.api.WxConsts}
177+
* @param offset 从全部素材的该偏移位置开始返回,0表示从第一个素材 返回
178+
* @param count 返回素材的数量,取值在1到20之间
179+
*/
180+
public WxMpMaterialFileBatchGetResult materialFileBatchGet(String type, int offset, int count) throws WxErrorException;
181+
182+
}

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

Lines changed: 12 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -110,157 +110,6 @@ public interface WxMpService {
110110
*/
111111
public WxJsapiSignature createJsapiSignature(String url) throws WxErrorException;
112112

113-
/**
114-
* <pre>
115-
* 新增临时素材
116-
* 本接口即为原“上传多媒体文件”接口。
117-
*
118-
* 上传的多媒体文件有格式和大小限制,如下:
119-
* 图片(image): 1M,支持JPG格式
120-
* 语音(voice):2M,播放长度不超过60s,支持AMR\MP3格式
121-
* 视频(video):10MB,支持MP4格式
122-
* 缩略图(thumb):64KB,支持JPG格式
123-
*
124-
* 详情请见: <a href="http://mp.weixin.qq.com/wiki/15/2d353966323806a202cd2deaafe8e557.html">新增临时素材</a>
125-
* </pre>
126-
* @param mediaType 媒体类型, 请看{@link me.chanjar.weixin.common.api.WxConsts}
127-
* @param fileType 文件类型,请看{@link me.chanjar.weixin.common.api.WxConsts}
128-
* @param inputStream 输入流
129-
* @throws WxErrorException
130-
*/
131-
public WxMediaUploadResult mediaUpload(String mediaType, String fileType, InputStream inputStream) throws WxErrorException, IOException;
132-
133-
/**
134-
* <pre>
135-
* 上传非图文永久素材
136-
*
137-
* 上传的多媒体文件有格式和大小限制,如下:
138-
* 图片(image): 图片大小不超过2M,支持bmp/png/jpeg/jpg/gif格式
139-
* 语音(voice):语音大小不超过5M,长度不超过60秒,支持mp3/wma/wav/amr格式
140-
* 视频(video):在上传视频素材时需要POST另一个表单,id为description,包含素材的描述信息,内容格式为JSON
141-
* 缩略图(thumb):文档未说明
142-
*
143-
* 详情请见: http://mp.weixin.qq.com/wiki/14/7e6c03263063f4813141c3e17dd4350a.html
144-
* </pre>
145-
* @param mediaType 媒体类型, 请看{@link me.chanjar.weixin.common.api.WxConsts}
146-
* @param material 上传的素材, 请看{@link me.chanjar.weixin.mp.bean.WxMpMaterial}
147-
*/
148-
public WxMpMaterialUploadResult materialFileUpload(String mediaType, WxMpMaterial material) throws WxErrorException;
149-
150-
/**
151-
* <pre>
152-
* 上传永久图文素材
153-
*
154-
* 详情请见: http://mp.weixin.qq.com/wiki/14/7e6c03263063f4813141c3e17dd4350a.html
155-
* </pre>
156-
* @param news 上传的图文消息, 请看{@link me.chanjar.weixin.mp.bean.WxMpMaterialNews}
157-
*/
158-
public WxMpMaterialUploadResult materialNewsUpload(WxMpMaterialNews news) throws WxErrorException;
159-
160-
/**
161-
* <pre>
162-
* 下载声音或者图片永久素材
163-
*
164-
* 详情请见: http://mp.weixin.qq.com/wiki/4/b3546879f07623cb30df9ca0e420a5d0.html
165-
* </pre>
166-
* @param media_id 永久素材的id
167-
*/
168-
public InputStream materialImageOrVoiceDownload(String media_id) throws WxErrorException;
169-
170-
/**
171-
* <pre>
172-
* 获取视频永久素材的信息和下载地址
173-
*
174-
* 详情请见: http://mp.weixin.qq.com/wiki/4/b3546879f07623cb30df9ca0e420a5d0.html
175-
* </pre>
176-
* @param media_id 永久素材的id
177-
*/
178-
public WxMpMaterialVideoInfoResult materialVideoInfo(String media_id) throws WxErrorException;
179-
180-
/**
181-
* <pre>
182-
* 获取图文永久素材的信息
183-
*
184-
* 详情请见: http://mp.weixin.qq.com/wiki/4/b3546879f07623cb30df9ca0e420a5d0.html
185-
* </pre>
186-
* @param media_id 永久素材的id
187-
*/
188-
public WxMpMaterialNews materialNewsInfo(String media_id) throws WxErrorException;
189-
190-
/**
191-
* <pre>
192-
* 更新图文永久素材
193-
*
194-
* 详情请见: http://mp.weixin.qq.com/wiki/4/19a59cba020d506e767360ca1be29450.html
195-
* </pre>
196-
* @param wxMpMaterialArticleUpdate 用来更新图文素材的bean, 请看{@link me.chanjar.weixin.mp.bean.WxMpMaterialArticleUpdate}
197-
*/
198-
public boolean materialNewsUpdate(WxMpMaterialArticleUpdate wxMpMaterialArticleUpdate) throws WxErrorException;
199-
200-
/**
201-
* <pre>
202-
* 删除永久素材
203-
*
204-
* 详情请见: http://mp.weixin.qq.com/wiki/5/e66f61c303db51a6c0f90f46b15af5f5.html
205-
* </pre>
206-
* @param media_id 永久素材的id
207-
*/
208-
public boolean materialDelete(String media_id) throws WxErrorException;
209-
210-
/**
211-
* <pre>
212-
* 获取各类素材总数
213-
*
214-
* 详情请见: http://mp.weixin.qq.com/wiki/16/8cc64f8c189674b421bee3ed403993b8.html
215-
* </pre>
216-
*/
217-
public WxMpMaterialCountResult materialCount() throws WxErrorException;
218-
219-
/**
220-
* <pre>
221-
* 分页获取图文素材列表
222-
*
223-
* 详情请见: http://mp.weixin.qq.com/wiki/12/2108cd7aafff7f388f41f37efa710204.html
224-
* </pre>
225-
* @param offset 从全部素材的该偏移位置开始返回,0表示从第一个素材 返回
226-
* @param count 返回素材的数量,取值在1到20之间
227-
*/
228-
public WxMpMaterialNewsBatchGetResult materialNewsBatchGet(int offset, int count) throws WxErrorException;
229-
230-
/**
231-
* <pre>
232-
* 分页获取其他媒体素材列表
233-
*
234-
* 详情请见: http://mp.weixin.qq.com/wiki/12/2108cd7aafff7f388f41f37efa710204.html
235-
* </pre>
236-
* @param type 媒体类型, 请看{@link me.chanjar.weixin.common.api.WxConsts}
237-
* @param offset 从全部素材的该偏移位置开始返回,0表示从第一个素材 返回
238-
* @param count 返回素材的数量,取值在1到20之间
239-
*/
240-
public WxMpMaterialFileBatchGetResult materialFileBatchGet(String type, int offset, int count) throws WxErrorException;
241-
242-
/**
243-
* 新增临时素材
244-
* @see #mediaUpload(String, String, InputStream)
245-
* @param mediaType
246-
* @param file
247-
* @throws WxErrorException
248-
*/
249-
public WxMediaUploadResult mediaUpload(String mediaType, File file) throws WxErrorException;
250-
251-
/**
252-
* <pre>
253-
* 获取临时素材
254-
* 本接口即为原“下载多媒体文件”接口。
255-
* 根据微信文档,视频文件下载不了,会返回null
256-
* 详情请见: <a href="http://mp.weixin.qq.com/wiki/9/677a85e3f3849af35de54bb5516c2521.html">获取临时素材</a>
257-
* </pre>
258-
* @param media_id
259-
* @return 保存到本地的临时文件
260-
* @throws WxErrorException
261-
*/
262-
public File mediaDownload(String media_id) throws WxErrorException;
263-
264113
/**
265114
* <pre>
266115
* 发送客服消息
@@ -881,17 +730,6 @@ public void markCardCode(String code, String cardId, String openId, boolean isMa
881730
*/
882731
public WxMpMassSendResult massMessagePreview(WxMpMassPreviewMessage wxMpMassPreviewMessage) throws Exception;
883732

884-
/**
885-
* <pre>
886-
* 上传图文消息内的图片获取URL
887-
* 详情请见: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
888-
* </pre>
889-
* @param file
890-
* @return WxMediaImgUploadResult 返回图片url
891-
* @throws WxErrorException
892-
*/
893-
public WxMediaImgUploadResult mediaImgUpload(File file) throws WxErrorException;
894-
895733
/**
896734
* <pre>
897735
* 设置所属行业
@@ -920,5 +758,16 @@ public void markCardCode(String code, String cardId, String openId, boolean isMa
920758
* @return WxMpKefuService
921759
*/
922760
WxMpKefuService getKefuService();
923-
761+
762+
/**
763+
* 返回客服接口方法实现类,以方便调用个其各种接口
764+
* @return WxMpMaterialService
765+
*/
766+
WxMpMaterialService getMaterialService();
767+
768+
/**
769+
* 获取WxMpConfigStorage 对象
770+
* @return WxMpConfigStorage
771+
*/
772+
WxMpConfigStorage getWxMpConfigStorage();
924773
}

0 commit comments

Comments
 (0)