Skip to content

Commit

Permalink
2.4.4
Browse files Browse the repository at this point in the history
  • Loading branch information
liyiorg committed Aug 5, 2015
1 parent f4dbe2f commit 6cd49db
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 3 deletions.
5 changes: 5 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ WEIXIN-POPULAR CHANGELOG
===========================
https://github.com/liyiorg/weixin-popular

Changes in version 2.4.4 (2015-08-05)
-------------------------------------
*issue #17 对帐单bug修复
*MediaAPI 添加 mediaUploadimg,上传图文消息内的图片获取URL

Changes in version 2.4.3 (2015-08-03)
-------------------------------------
*issue #17 MchBaseResult XML 转换错误
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>weixin</groupId>
<artifactId>weixin-popular</artifactId>
<version>2.4.3-RELEASE</version>
<version>2.4.4-RELEASE</version>
<url>https://github.com/liyiorg/weixin-popular</url>

<dependencies>
Expand Down
74 changes: 74 additions & 0 deletions src/main/java/weixin/popular/api/MediaAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.io.InputStream;
import java.net.URI;
import java.nio.charset.UnsupportedCharsetException;
import java.util.UUID;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
Expand All @@ -23,6 +24,7 @@

import weixin.popular.bean.Media;
import weixin.popular.bean.MediaType;
import weixin.popular.bean.UploadimgResult;
import weixin.popular.client.LocalHttpClient;

public class MediaAPI extends BaseAPI{
Expand Down Expand Up @@ -141,4 +143,76 @@ public static byte[] mediaGet(String access_token,String media_id){
return null;
}

/**
* 上传图文消息内的图片获取URL
* 请注意,本接口所上传的图片不占用公众号的素材库中图片数量的5000个的限制。图片仅支持jpg/png格式,大小必须在1MB以下。
* @param access_token
* @param media
* @return
*/
public static UploadimgResult mediaUploadimg(String access_token,File media){
HttpPost httpPost = new HttpPost(MEDIA_URI+"/cgi-bin/media/uploadimg");
FileBody bin = new FileBody(media);
HttpEntity reqEntity = MultipartEntityBuilder.create()
.addPart("media", bin)
.addTextBody("access_token", access_token)
.build();
httpPost.setEntity(reqEntity);
return LocalHttpClient.executeJsonResult(httpPost,UploadimgResult.class);
}

/**
* 上传图文消息内的图片获取URL
* 请注意,本接口所上传的图片不占用公众号的素材库中图片数量的5000个的限制。图片仅支持jpg/png格式,大小必须在1MB以下。
* @param access_token
* @param inputStream
* @return
*/
public static UploadimgResult mediaUploadimg(String access_token,InputStream inputStream){
HttpPost httpPost = new HttpPost(MEDIA_URI+"/cgi-bin/media/uploadimg");
InputStreamBody inputStreamBody = new InputStreamBody(inputStream, ContentType.MULTIPART_FORM_DATA, UUID.randomUUID().toString()+".jpg");
HttpEntity reqEntity = MultipartEntityBuilder.create()
.addPart("media",inputStreamBody)
.addTextBody("access_token", access_token)
.build();
httpPost.setEntity(reqEntity);
return LocalHttpClient.executeJsonResult(httpPost,UploadimgResult.class);
}


/**
* 上传图文消息内的图片获取URL
* 请注意,本接口所上传的图片不占用公众号的素材库中图片数量的5000个的限制。图片仅支持jpg/png格式,大小必须在1MB以下。
* @param access_token
* @param uri
* @return
*/
public static UploadimgResult mediaUploadimg(String access_token,URI uri){
HttpPost httpPost = new HttpPost(MEDIA_URI+"/cgi-bin/media/uploadimg");
CloseableHttpClient tempHttpClient = HttpClients.createDefault();
try {
HttpEntity entity = tempHttpClient.execute(RequestBuilder.get().setUri(uri).build()).getEntity();
HttpEntity reqEntity = MultipartEntityBuilder.create()
.addBinaryBody("media",EntityUtils.toByteArray(entity),ContentType.get(entity),UUID.randomUUID().toString()+".jpg")
.addTextBody("access_token", access_token)
.build();
httpPost.setEntity(reqEntity);
return LocalHttpClient.executeJsonResult(httpPost,UploadimgResult.class);
} catch (UnsupportedCharsetException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
tempHttpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}
31 changes: 29 additions & 2 deletions src/main/java/weixin/popular/api/PayMchAPI.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package weixin.popular.api;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;

import weixin.popular.bean.paymch.Closeorder;
import weixin.popular.bean.paymch.DownloadbillResult;
import weixin.popular.bean.paymch.MchBaseResult;
import weixin.popular.bean.paymch.MchDownloadbill;
import weixin.popular.bean.paymch.MchOrderInfoResult;
Expand Down Expand Up @@ -212,7 +219,7 @@ public static RefundqueryResult payRefundquery(Refundquery refundquery,String ke
* @param key
* @return
*/
public static MchBaseResult payDownloadbill(MchDownloadbill downloadbill,String key){
public static DownloadbillResult payDownloadbill(MchDownloadbill downloadbill,String key){
Map<String,String> map = MapUtil.objectToMap(downloadbill);
String sign = SignatureUtil.generateSign(map,key);
downloadbill.setSign(sign);
Expand All @@ -222,7 +229,27 @@ public static MchBaseResult payDownloadbill(MchDownloadbill downloadbill,String
.setUri(MCH_URI + "/pay/downloadbill")
.setEntity(new StringEntity(closeorderXML,Charset.forName("utf-8")))
.build();
return LocalHttpClient.executeXmlResult(httpUriRequest,MchBaseResult.class);
return LocalHttpClient.execute(httpUriRequest,new ResponseHandler<DownloadbillResult>() {

@Override
public DownloadbillResult handleResponse(HttpResponse response)
throws ClientProtocolException, IOException {
int status = response.getStatusLine().getStatusCode();
if (status >= 200 && status < 300) {
HttpEntity entity = response.getEntity();
String str = EntityUtils.toString(entity,"utf-8");
if(str.startsWith("<xml>")){
return XMLConverUtil.convertToObject(DownloadbillResult.class,str);
}else{
DownloadbillResult dr = new DownloadbillResult();
dr.setData(str);
return dr;
}
} else {
throw new ClientProtocolException("Unexpected response status: " + status);
}
}
});
}

/**
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/weixin/popular/bean/UploadimgResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package weixin.popular.bean;



public class UploadimgResult extends BaseResult{

private String url;

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}


}
22 changes: 22 additions & 0 deletions src/main/java/weixin/popular/bean/paymch/DownloadbillResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package weixin.popular.bean.paymch;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="xml")
@XmlAccessorType(XmlAccessType.FIELD)
public class DownloadbillResult extends MchBase{

private String data;

public String getData() {
return data;
}

public void setData(String data) {
this.data = data;
}


}

0 comments on commit 6cd49db

Please sign in to comment.