Merge branch 'master' of https://github.com/foxinmy/weixin4j into wyying

This commit is contained in:
fengyapeng 2016-12-23 17:24:45 +08:00
commit a00c40d3bb
11 changed files with 121 additions and 40 deletions

View File

@ -21,6 +21,7 @@ import com.foxinmy.weixin4j.payment.mch.SettlementRecord;
import com.foxinmy.weixin4j.type.CurrencyType; import com.foxinmy.weixin4j.type.CurrencyType;
import com.foxinmy.weixin4j.util.DateUtil; import com.foxinmy.weixin4j.util.DateUtil;
import com.foxinmy.weixin4j.util.RandomUtil; import com.foxinmy.weixin4j.util.RandomUtil;
import com.foxinmy.weixin4j.util.StringUtil;
import com.foxinmy.weixin4j.xml.XmlStream; import com.foxinmy.weixin4j.xml.XmlStream;
/** /**
@ -48,6 +49,20 @@ public class CashApi extends MchApi {
* *
* @param redpacket * @param redpacket
* 红包信息 * 红包信息
* @see #sendRedpack(Redpacket,String)
*/
public RedpacketSendResult sendRedpack(Redpacket redpacket)
throws WeixinException {
return sendRedpack(redpacket, null);
}
/**
* 发放红包 企业向微信用户个人发现金红包
*
* @param redpacket
* 红包信息
* @param appId
* 应用ID 可为空 主要是针对企业号支付时传入的agentid
* @return 发放结果 * @return 发放结果
* @see com.foxinmy.weixin4j.payment.mch.Redpacket * @see com.foxinmy.weixin4j.payment.mch.Redpacket
* @see com.foxinmy.weixin4j.payment.mch.RedpacketSendResult * @see com.foxinmy.weixin4j.payment.mch.RedpacketSendResult
@ -59,10 +74,13 @@ public class CashApi extends MchApi {
* 发放裂变红包接口</a> * 发放裂变红包接口</a>
* @throws WeixinException * @throws WeixinException
*/ */
public RedpacketSendResult sendRedpack(Redpacket redpacket) public RedpacketSendResult sendRedpack(Redpacket redpacket, String appId)
throws WeixinException { throws WeixinException {
super.declareMerchant(redpacket); super.declareMerchant(redpacket);
JSONObject obj = (JSONObject) JSON.toJSON(redpacket); JSONObject obj = (JSONObject) JSON.toJSON(redpacket);
if (StringUtil.isNotBlank(appId)) {
obj.put("appid", appId);
}
obj.put("wxappid", obj.remove("appid")); obj.put("wxappid", obj.remove("appid"));
obj.put("sign", weixinSignature.sign(obj)); obj.put("sign", weixinSignature.sign(obj));
String param = XmlStream.map2xml(obj); String param = XmlStream.map2xml(obj);

View File

@ -38,12 +38,14 @@ import com.foxinmy.weixin4j.type.CurrencyType;
import com.foxinmy.weixin4j.type.IdQuery; import com.foxinmy.weixin4j.type.IdQuery;
import com.foxinmy.weixin4j.type.IdType; import com.foxinmy.weixin4j.type.IdType;
import com.foxinmy.weixin4j.type.SignType; import com.foxinmy.weixin4j.type.SignType;
import com.foxinmy.weixin4j.type.TarType;
import com.foxinmy.weixin4j.type.TradeType; import com.foxinmy.weixin4j.type.TradeType;
import com.foxinmy.weixin4j.type.mch.BillType; import com.foxinmy.weixin4j.type.mch.BillType;
import com.foxinmy.weixin4j.type.mch.RefundAccountType; import com.foxinmy.weixin4j.type.mch.RefundAccountType;
import com.foxinmy.weixin4j.util.Consts; import com.foxinmy.weixin4j.util.Consts;
import com.foxinmy.weixin4j.util.DateUtil; import com.foxinmy.weixin4j.util.DateUtil;
import com.foxinmy.weixin4j.util.DigestUtil; import com.foxinmy.weixin4j.util.DigestUtil;
import com.foxinmy.weixin4j.util.IOUtil;
import com.foxinmy.weixin4j.util.MapUtil; import com.foxinmy.weixin4j.util.MapUtil;
import com.foxinmy.weixin4j.util.RandomUtil; import com.foxinmy.weixin4j.util.RandomUtil;
import com.foxinmy.weixin4j.util.StringUtil; import com.foxinmy.weixin4j.util.StringUtil;
@ -864,6 +866,8 @@ public class PayApi extends MchApi {
* REFUND,返回当日退款订单 * REFUND,返回当日退款订单
* @param outputStream * @param outputStream
* 输出流 * 输出流
* @param tarType
* 非必传参数固定值GZIP返回格式为.gzip的压缩包账单不传则默认为数据流形式
* @since V3 * @since V3
* @see <a href= * @see <a href=
* "http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_6"> * "http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_6">
@ -871,7 +875,7 @@ public class PayApi extends MchApi {
* @throws WeixinException * @throws WeixinException
*/ */
public void downloadBill(Date billDate, BillType billType, public void downloadBill(Date billDate, BillType billType,
OutputStream outputStream) throws WeixinException { OutputStream outputStream, TarType tarType) throws WeixinException {
if (billDate == null) { if (billDate == null) {
Calendar now = Calendar.getInstance(); Calendar now = Calendar.getInstance();
now.add(Calendar.DAY_OF_MONTH, -1); now.add(Calendar.DAY_OF_MONTH, -1);
@ -884,16 +888,26 @@ public class PayApi extends MchApi {
Map<String, String> map = createBaseRequestMap(null); Map<String, String> map = createBaseRequestMap(null);
map.put("bill_date", formatBillDate); map.put("bill_date", formatBillDate);
map.put("bill_type", billType.name()); map.put("bill_type", billType.name());
if (tarType != null) {
map.put("tar_type", tarType.name());
}
map.put("sign", weixinSignature.sign(map)); map.put("sign", weixinSignature.sign(map));
String param = XmlStream.map2xml(map); String param = XmlStream.map2xml(map);
WeixinResponse response = weixinExecutor.post( WeixinResponse response = weixinExecutor.post(
getRequestUri("downloadbill_uri"), param); getRequestUri("downloadbill_uri"), param);
if (TarType.GZIP == tarType) {
try {
IOUtil.copy(response.getBody(), outputStream);
} catch (IOException e) {
;
}
} else {
BufferedReader reader = null; BufferedReader reader = null;
BufferedWriter writer = null; BufferedWriter writer = null;
try { try {
writer = new BufferedWriter(new OutputStreamWriter(outputStream, writer = new BufferedWriter(new OutputStreamWriter(
Consts.UTF_8)); outputStream, Consts.UTF_8));
reader = new BufferedReader(new InputStreamReader( reader = new BufferedReader(new InputStreamReader(
response.getBody(), Consts.UTF_8)); response.getBody(), Consts.UTF_8));
String line = null; String line = null;
@ -916,6 +930,7 @@ public class PayApi extends MchApi {
} }
} }
} }
}
/** /**
* 退款查询 * 退款查询

View File

@ -58,6 +58,8 @@ public class MimeType implements Serializable {
STREAM_MIMETYPES.add(valueOf("image/*")); STREAM_MIMETYPES.add(valueOf("image/*"));
STREAM_MIMETYPES.add(valueOf("audio/*")); STREAM_MIMETYPES.add(valueOf("audio/*"));
STREAM_MIMETYPES.add(valueOf("video/*")); STREAM_MIMETYPES.add(valueOf("video/*"));
STREAM_MIMETYPES.add(valueOf("application/zip"));
STREAM_MIMETYPES.add(valueOf("application/x-gzip"));
} }
public MimeType(String type) { public MimeType(String type) {

View File

@ -73,11 +73,14 @@ public class WeixinResponse implements HttpResponse {
return messageConverter.convert(clazz, response); return messageConverter.convert(clazz, response);
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException("IO error on convert to " throw new RuntimeException("IO error on convert to "
+ typeReference, e); + clazz, e);
} }
} }
} }
throw new RuntimeException("cannot convert to " + typeReference); if (clazz.isAssignableFrom(ApiResult.class)) {
return (T) new ApiResult();
}
throw new RuntimeException("cannot convert to " + clazz);
} }
@Override @Override

View File

@ -29,8 +29,13 @@ public class MediaItem implements Serializable {
/** /**
* 媒体素材名称 * 媒体素材名称
*/ */
@JSONField(name = "filename") @JSONField(name = "name")
private String name; private String name;
/**
* 图文页的URL或者当获取的列表是图片素材列表时该字段是图片的URL
*/
@JSONField(name = "url")
private String url;
/** /**
* 媒体素材最后更新时间 * 媒体素材最后更新时间
*/ */
@ -58,6 +63,14 @@ public class MediaItem implements Serializable {
this.name = name; this.name = name;
} }
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUpdateTime() { public String getUpdateTime() {
return updateTime; return updateTime;
} }
@ -81,7 +94,8 @@ public class MediaItem implements Serializable {
@Override @Override
public String toString() { public String toString() {
return "MediaItem [mediaId=" + mediaId + ", name=" + name return "MediaItem [mediaId=" + mediaId + ", name=" + name + ",url="
+ ", updateTime=" + updateTime + ", articles=" + articles + "]"; + url + ", updateTime=" + updateTime + ", articles=" + articles
+ "]";
} }
} }

View File

@ -39,6 +39,7 @@ import com.foxinmy.weixin4j.sign.WeixinSignature;
import com.foxinmy.weixin4j.type.CurrencyType; import com.foxinmy.weixin4j.type.CurrencyType;
import com.foxinmy.weixin4j.type.CustomsCity; import com.foxinmy.weixin4j.type.CustomsCity;
import com.foxinmy.weixin4j.type.IdQuery; import com.foxinmy.weixin4j.type.IdQuery;
import com.foxinmy.weixin4j.type.TarType;
import com.foxinmy.weixin4j.type.mch.BillType; import com.foxinmy.weixin4j.type.mch.BillType;
import com.foxinmy.weixin4j.type.mch.RefundAccountType; import com.foxinmy.weixin4j.type.mch.RefundAccountType;
import com.foxinmy.weixin4j.util.Weixin4jConfigUtil; import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;
@ -511,6 +512,8 @@ public class WeixinPayProxy {
* 下载对账单的类型 ALL,返回当日所有订单信息, 默认值 SUCCESS,返回当日成功支付的订单 * 下载对账单的类型 ALL,返回当日所有订单信息, 默认值 SUCCESS,返回当日成功支付的订单
* REFUND,返回当日退款订单 * REFUND,返回当日退款订单
* @para outputStream 输出流 * @para outputStream 输出流
* @param tarType
* 非必传参数固定值GZIP返回格式为.gzip的压缩包账单不传则默认为数据流形式
* @since V2 & V3 * @since V2 & V3
* @see com.foxinmy.weixin4j.api.PayApi * @see com.foxinmy.weixin4j.api.PayApi
* @see <a href= * @see <a href=
@ -519,8 +522,8 @@ public class WeixinPayProxy {
* @throws WeixinException * @throws WeixinException
*/ */
public void downloadBill(Date billDate, BillType billType, public void downloadBill(Date billDate, BillType billType,
OutputStream outputStream) throws WeixinException { OutputStream outputStream, TarType tarType) throws WeixinException {
payApi.downloadBill(billDate, billType, outputStream); payApi.downloadBill(billDate, billType, outputStream, tarType);
} }
/** /**
@ -676,6 +679,20 @@ public class WeixinPayProxy {
* *
* @param redpacket * @param redpacket
* 红包信息 * 红包信息
* @see #sendRedpack(Redpacket,String)
*/
public RedpacketSendResult sendRedpack(Redpacket redpacket)
throws WeixinException {
return cashApi.sendRedpack(redpacket);
}
/**
* 发放红包 企业向微信用户个人发现金红包
*
* @param redpacket
* 红包信息
* @param appId
* 应用ID 可为空 主要是针对企业号支付时传入的agentid
* @return 发放结果 * @return 发放结果
* @see com.foxinmy.weixin4j.api.CashApi * @see com.foxinmy.weixin4j.api.CashApi
* @see com.foxinmy.weixin4j.payment.mch.Redpacket * @see com.foxinmy.weixin4j.payment.mch.Redpacket
@ -688,9 +705,9 @@ public class WeixinPayProxy {
* 发放裂变红包接口</a> * 发放裂变红包接口</a>
* @throws WeixinException * @throws WeixinException
*/ */
public RedpacketSendResult sendRedpack(Redpacket redpacket) public RedpacketSendResult sendRedpack(Redpacket redpacket, String appId)
throws WeixinException { throws WeixinException {
return cashApi.sendRedpack(redpacket); return cashApi.sendRedpack(redpacket, appId);
} }
/** /**

View File

@ -0,0 +1,14 @@
package com.foxinmy.weixin4j.type;
/**
* 压缩类型
*
* @className TarType
* @author jinyu(foxinmy@gmail.com)
* @date 2016年12月21日
* @since JDK 1.6
* @see
*/
public enum TarType {
GZIP
}

View File

@ -1559,7 +1559,7 @@ public class WeixinProxy {
* *
* @param tplMessage * @param tplMessage
* 模板消息主体 * 模板消息主体
* @return 发送结果 * @return 发送的消息ID
* @throws WeixinException * @throws WeixinException
* @see <a href= * @see <a href=
* "https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277&token=&lang=zh_CN"> * "https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277&token=&lang=zh_CN">

View File

@ -147,7 +147,7 @@ public class TmplApi extends MpApi {
* *
* @param tplMessage * @param tplMessage
* 消息对象 * 消息对象
* @return 发送结果 * @return 发送的消息ID
* @throws WeixinException * @throws WeixinException
* @see <a * @see <a
* href="https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277&token=&lang=zh_CN">模板消息</a> * href="https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277&token=&lang=zh_CN">模板消息</a>

View File

@ -176,7 +176,7 @@ public class TemplateMessage implements Serializable {
* *
* @param items * @param items
*/ */
public void pushItems(Map<String, NameValue> items) { public void setItems(Map<String, NameValue> items) {
this.content = items; this.content = items;
} }

View File

@ -1,11 +1,9 @@
package com.foxinmy.weixin4j.mp.test; package com.foxinmy.weixin4j.mp.test;
import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import com.foxinmy.weixin4j.exception.WeixinException; import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.http.weixin.ApiResult;
import com.foxinmy.weixin4j.mp.api.TmplApi; import com.foxinmy.weixin4j.mp.api.TmplApi;
import com.foxinmy.weixin4j.mp.message.TemplateMessage; import com.foxinmy.weixin4j.mp.message.TemplateMessage;
import com.foxinmy.weixin4j.mp.type.IndustryType; import com.foxinmy.weixin4j.mp.type.IndustryType;
@ -39,7 +37,7 @@ public class TemplateTest extends TokenTest {
TemplateMessage tplMessage = new TemplateMessage("touser", TemplateMessage tplMessage = new TemplateMessage("touser",
"template_id", "url"); "template_id", "url");
tplMessage.pushHead("head").pushTail("tail").pushItem("key1", "text1"); tplMessage.pushHead("head").pushTail("tail").pushItem("key1", "text1");
ApiResult result = tmplApi.sendTmplMessage(tplMessage); String result = tmplApi.sendTmplMessage(tplMessage);
Assert.assertEquals("0", result.getReturnCode()); System.out.println(result);
} }
} }