Skip to content

Commit

Permalink
上传图文信息
Browse files Browse the repository at this point in the history
  • Loading branch information
yfdai committed Jun 17, 2017
1 parent c855e13 commit 24b3e56
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 34 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>cn.buk.api.wechat</groupId>
<artifactId>weniu</artifactId>
<version>1.2.27-SNAPSHOT</version>
<version>1.2.28-SNAPSHOT</version>
<packaging>pom</packaging>

<name>weniu</name>
Expand Down
2 changes: 1 addition & 1 deletion weniu-dao/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>cn.buk.api.wechat</groupId>
<artifactId>weniu</artifactId>
<version>1.2.21-SNAPSHOT</version>
<version>1.2.28-SNAPSHOT</version>
</parent>

<artifactId>weniu-dao</artifactId>
Expand Down
2 changes: 2 additions & 0 deletions weniu-dao/src/main/java/cn/buk/api/wechat/dao/WeixinDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ public interface WeixinDao {
int createCustomMenu(WeixinCustomMenu o);

int createWeixinMaterial(WeixinMaterial wm);
int updateWeixinMaterial(WeixinMaterial wm);

List<WeixinMaterial> searchMaterials(int enterpriseId, CommonSearchCriteria sc);
List<WeixinMaterial> searchMaterials(int enterpriseId, String mediaId);

WeixinMaterial searchWeixinMaterial(int weixinId, int id);
}
36 changes: 36 additions & 0 deletions weniu-dao/src/main/java/cn/buk/api/wechat/dao/WeixinDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,30 @@ public int createWeixinMaterial(WeixinMaterial o) {
return retCode == 1 ? o.getId(): retCode;
}

@Override
public int updateWeixinMaterial(WeixinMaterial wm) {
int retCode;

EntityManager em = createEntityManager();
try {
em.getTransaction().begin();
em.merge(wm);
em.getTransaction().commit();

retCode = 1;
} catch (Exception ex) {
if (em.getTransaction().isActive())
em.getTransaction().rollback();

retCode = -100;
ex.printStackTrace();
} finally {
em.close();
}

return retCode;
}

/**
* 本地查找微信永久素材
* @param enterpriseId
Expand Down Expand Up @@ -318,6 +342,18 @@ public List<WeixinMaterial> searchMaterials(int enterpriseId, CommonSearchCriter
return results;
}

public List<WeixinMaterial> searchMaterials(int enterpriseId, String mediaId) {
EntityManager em = createEntityManager();
try {
return em.createQuery("select o from WeixinMaterial o where o.ownerId = :ownerId and o.mediaId = :mediaId")
.setParameter("ownerId", enterpriseId)
.setParameter("mediaId", mediaId)
.getResultList();
} finally {
em.close();
}
}

@Override
public WeixinMaterial searchWeixinMaterial(int weixinId, int id) {
EntityManager em = createEntityManager();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public class WeixinMaterial {
*/
private String url;

private String name;

@Temporal(TemporalType.TIMESTAMP)
@Column(insertable = false, updatable = false)
Expand Down Expand Up @@ -119,4 +120,12 @@ public String getMaterialType() {
public void setMaterialType(String materialType) {
this.materialType = materialType;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
2 changes: 1 addition & 1 deletion weniu-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>cn.buk.api.wechat</groupId>
<artifactId>weniu</artifactId>
<version>1.2.21-SNAPSHOT</version>
<version>1.2.28-SNAPSHOT</version>
</parent>

<name>weniu-service</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public interface WeixinService {

// void testImgUrl();

int getWeixinId();

/**
* 验证消息来源是否微信发送的
* @param signature 签名
Expand Down Expand Up @@ -119,7 +121,7 @@ public interface WeixinService {
* 新增永久图文素材
* @return
*/
String addMaterialNews(WxNewsRequest request);
WxMediaResponse addMaterialNews(WxNewsRequest request);

/**
* 上传图文消息内的图片获取URL
Expand Down Expand Up @@ -152,6 +154,7 @@ public interface WeixinService {
* @return
*/
List<WeixinMaterial> searchMaterials(int enterpriseId, CommonSearchCriteria sc);
List<WeixinMaterial> searchMaterials(int enterpriseId, String mediaId);

/**
* 保存永久素材信息在本地
Expand All @@ -160,7 +163,7 @@ public interface WeixinService {
* @param url
* @return
*/
int createWeixinMaterial(String mediaType, String mediaId, String url);
int createWeixinMaterial(int enterpriseId, String mediaType, String mediaId, String url, String name);

WeixinMaterial searchWeixinMaterial(int id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ public class WeixinServiceImpl implements WeixinService {
private static final String IMGSRC_REG = "http:\"?(.*?)(\"|>|\\s+)";


private static final String DOWNLOAD_DIR = "/temp";



public static String postFile(String url, String filePath) {
File file = new File(filePath);
Expand Down Expand Up @@ -289,6 +292,11 @@ public void testImgUrl() {
//this.Download(imgSrc);
}

@Override
public int getWeixinId() {
return this.weixinId;
}

public boolean verifyWeixinSource(String signature, String timestamp, String nonce) {
try {
ArrayList<String> al = new ArrayList<>();
Expand Down Expand Up @@ -490,11 +498,11 @@ public WxMaterials getMaterials(final String mediaType, final int offset, final

String result = HttpUtil.postUrl(url, jsonBody);

try {
result = new String(result.getBytes("ISO-8859-1"), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// try {
// //result = new String(result.getBytes("ISO-8859-1"), "UTF-8");
// } catch (UnsupportedEncodingException e) {
// e.printStackTrace();
// }

WxMaterials rs = JSON.parseObject(result, WxMaterials.class);

Expand All @@ -520,10 +528,6 @@ public WxMediaResponse addMaterial(final String filePath, final String mediaType

String result = postFile(url, filePath);

// {
// "media_id":"D66eMY48x0SJaUhRU4Ggil4iBLv367MC1aHjvKLCvj4",
// "url":"http:\/\/mmbiz.qpic.cn\/mmbiz_jpg\/5JPNn1NvhAo6NicG9Ne8oPVNNThVSQyMEhwcxpia1d8iclaibPLrZibNFvku3fpLiaG11RnIEILUJMm6KFRT5J8VUibicw\/0?wx_fmt=jpeg"}

WxMediaResponse rs = JSON.parseObject(result, WxMediaResponse.class);

if (rs.getErrcode() <= 0) {
Expand All @@ -547,7 +551,7 @@ public WxMediaResponse addMaterial(final String filePath, final String mediaType
* 新增永久图文素材
* @return
*/
public String addMaterialNews(WxNewsRequest request) {
public WxMediaResponse addMaterialNews(WxNewsRequest request) {
//检查内容中是否有图片链接,有的话把图片上传到微信,并替换链接
replaceImageUrl(request.getArticles());

Expand All @@ -564,10 +568,12 @@ public String addMaterialNews(WxNewsRequest request) {
String result = postUrl(url, jsonBody);

logger.info(result);

WxMediaResponse rs = JSON.parseObject(result, WxMediaResponse.class);
// {
// "media_id":MEDIA_ID
// }
return result;
return rs;
}

private void replaceImageUrl(List<WxNews> articles) {
Expand All @@ -577,7 +583,8 @@ private void replaceImageUrl(List<WxNews> articles) {
for(String url: urls) {
//1.下载该图片
logger.info(url);
String filename = HttpUtil.download(url, null);
String filepath = getFilePath(url);
String filename = HttpUtil.download(url, filepath);

logger.info(filename);
if (filename != null) {
Expand All @@ -592,6 +599,17 @@ private void replaceImageUrl(List<WxNews> articles) {
}
}

private String getFilePath(String url) {
String[] vals = url.split("/");
String val = vals[vals.length - 1];

if (val.indexOf(".") > 0) {
return DOWNLOAD_DIR + "/" + val;
} else {
return null;
}
}

/**
* 上传图文消息内的图片获取URL
本接口所上传的图片不占用公众号的素材库中图片数量的5000个的限制。图片仅支持jpg/png格式,大小必须在1MB以下。
Expand Down Expand Up @@ -916,14 +934,33 @@ public List<WeixinMaterial> searchMaterials(int enterpriseId, CommonSearchCriter
}

@Override
public int createWeixinMaterial(String mediaType, String mediaId, String url) {
WeixinMaterial wm = new WeixinMaterial();
wm.setOwnerId(this.weixinId);
wm.setMaterialType(mediaType);
wm.setMediaId(mediaId);
wm.setUrl(url);

return weixinDao.createWeixinMaterial(wm);
public List<WeixinMaterial> searchMaterials(int enterpriseId, String mediaId) {
return weixinDao.searchMaterials(enterpriseId, mediaId);
}

@Override
public int createWeixinMaterial(int enterpriseId, String mediaType, String mediaId, String url, String name) {
if (enterpriseId != this.weixinId) return -1;

List<WeixinMaterial> list = searchMaterials(this.weixinId, mediaId);

if (list == null || list.size() == 0) {
WeixinMaterial wm = new WeixinMaterial();
wm.setOwnerId(this.weixinId);
wm.setMaterialType(mediaType);
wm.setMediaId(mediaId);
wm.setUrl(url);
wm.setName(name);

return weixinDao.createWeixinMaterial(wm);
} else {
WeixinMaterial wm = list.get(0);
wm.setMaterialType(mediaType);
wm.setUrl(url);
wm.setName(name);

return weixinDao.updateWeixinMaterial(wm);
}
}

@Override
Expand Down Expand Up @@ -1043,23 +1080,16 @@ public int deleteCustomMenu(int enterpriseId, int id) {
return weixinDao.deleteCustomMenu(enterpriseId, id);
}

@Override
public int createCustomMenu(final int enterpriseId, String name, String type, String url, String key, int level, int parentId) {
if (enterpriseId != this.weixinId) return -1;

WeixinCustomMenu o = new WeixinCustomMenu();

o.setEnterpriseId(enterpriseId);

o.setName(name);
o.setType(type);
o.setUrl(url);
o.setKey(key);
o.setLevel(level);
o.setParentId(parentId);

int retCode = weixinDao.createCustomMenu(o);

return retCode;
return weixinDao.createCustomMenu(o);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ public static String download(String url, String filepath) {
HttpGet httpget = new HttpGet(url);
HttpResponse response = httpClient.execute(httpget);

outHeaders(response);

HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
if (filepath == null)
Expand Down
2 changes: 1 addition & 1 deletion weniu-util/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>cn.buk.api.wechat</groupId>
<artifactId>weniu</artifactId>
<version>1.2.21-SNAPSHOT</version>
<version>1.2.28-SNAPSHOT</version>
</parent>

<artifactId>weniu-util</artifactId>
Expand Down

0 comments on commit 24b3e56

Please sign in to comment.