diff --git a/CHANGE.md b/CHANGE.md index fa52e2f9..d9cc6b08 100644 --- a/CHANGE.md +++ b/CHANGE.md @@ -774,4 +774,12 @@ * 2016-12-13 - + version upgrade to 1.7.3 \ No newline at end of file + + version upgrade to 1.7.3 + +* 2017-01-09 + + + 新增批量发红包接口 + + + 新增摇一摇周边接口 + + + version upgrade to 1.7.4 \ No newline at end of file diff --git a/pom.xml b/pom.xml index fec44966..a6a21a45 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 com.foxinmy weixin4j - 1.7.4-SNAPSHOT + 1.7.4 pom weixin4j https://github.com/foxinmy/weixin4j diff --git a/weixin4j-base/pom.xml b/weixin4j-base/pom.xml index 35f59332..1c99922d 100644 --- a/weixin4j-base/pom.xml +++ b/weixin4j-base/pom.xml @@ -5,7 +5,7 @@ com.foxinmy weixin4j - 1.7.4-SNAPSHOT + 1.7.4 weixin4j-base weixin4j-base diff --git a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/api/CashApi.java b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/api/CashApi.java index 5a09f443..d514ee40 100644 --- a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/api/CashApi.java +++ b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/api/CashApi.java @@ -6,7 +6,8 @@ import java.util.Date; import java.util.List; import java.util.Map; import java.util.concurrent.Callable; -import java.util.concurrent.ExecutionException; +import java.util.concurrent.CompletionService; +import java.util.concurrent.ExecutorCompletionService; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; @@ -56,28 +57,6 @@ public class CashApi extends MchApi { * * @param redpacket * 红包信息 - * @param openId - * 接受收红包的用户的openid 必填 - * @see #sendRedpacks(Redpacket, String...) - */ - public RedpacketSendResult sendRedpack(Redpacket redpacket, String openId) - throws WeixinException { - try { - return sendRedpacks(redpacket, openId).get(0).get(); - } catch (InterruptedException e) { - throw new WeixinException("send redpack error", e); - } catch (ExecutionException e) { - throw new WeixinException("send redpack error", e); - } - } - - /** - * 发放红包 企业向微信用户个人发现金红包 - * - * @param redpacket - * 红包信息 - * @param openIds - * 接受收红包的用户的openid 必填 * @return 发放结果 * @see com.foxinmy.weixin4j.payment.mch.Redpacket * @see com.foxinmy.weixin4j.payment.mch.RedpacketSendResult @@ -89,8 +68,8 @@ public class CashApi extends MchApi { * 发放裂变红包接口 * @throws WeixinException */ - public List> sendRedpacks(Redpacket redpacket, - String... openIds) { + public RedpacketSendResult sendRedpack(Redpacket redpacket) + throws WeixinException { String appId = redpacket.getAppId(); super.declareMerchant(redpacket); final JSONObject obj = (JSONObject) JSON.toJSON(redpacket); @@ -100,26 +79,39 @@ public class CashApi extends MchApi { obj.put("wxappid", obj.remove("appid")); final String redpack_uri = redpacket.getTotalNum() > 1 ? getRequestUri("groupredpack_send_uri") : getRequestUri("redpack_send_uri"); - int sendLength = openIds.length; + obj.put("sign", weixinSignature.sign(obj)); + String param = XmlStream.map2xml(obj); + WeixinResponse response = getWeixinSSLExecutor().post(redpack_uri, + param); + String text = response.getAsString() + .replaceFirst("", "") + .replaceFirst("", ""); + return XmlStream.fromXML(text, RedpacketSendResult.class); + } + + /** + * 批量发放红包 企业向微信用户个人发现金红包 + * + * @param redpacket + * 多个红包信息 + * @return 发放结果 + * @see #sendRedpacks(Redpacket...) + * @throws WeixinException + */ + public List> sendRedpacks( + Redpacket... redpackets) { ExecutorService sendExecutor = Executors.newFixedThreadPool(Math.max(1, - sendLength / 10)); // 十分之一? + redpackets.length / 10)); // 十分之一? + CompletionService completion = new ExecutorCompletionService( + sendExecutor); List> callSendList = new ArrayList>( - sendLength); - for (final String openId : openIds) { - Future futureSend = sendExecutor + redpackets.length); + for (final Redpacket redpacket : redpackets) { + Future futureSend = completion .submit(new Callable() { @Override public RedpacketSendResult call() throws Exception { - obj.put("re_openid", openId); - obj.put("sign", weixinSignature.sign(obj)); - String param = XmlStream.map2xml(obj); - WeixinResponse response = getWeixinSSLExecutor() - .post(redpack_uri, param); - String text = response.getAsString() - .replaceFirst("", "") - .replaceFirst("", ""); - return XmlStream.fromXML(text, - RedpacketSendResult.class); + return sendRedpack(redpacket); } }); callSendList.add(futureSend); diff --git a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/payment/WeixinPayProxy.java b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/payment/WeixinPayProxy.java index bec415f2..b2b547d4 100644 --- a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/payment/WeixinPayProxy.java +++ b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/payment/WeixinPayProxy.java @@ -681,24 +681,6 @@ public class WeixinPayProxy { * * @param redpacket * 红包信息 - * @param openId - * 接受收红包的用户的openid 必填 - * @see com.foxinmy.weixin4j.api.CashApi - * @throws WeixinException - * @see #sendRedpacks(Redpacket, String...) - */ - public RedpacketSendResult sendRedpack(Redpacket redpacket, String openId) - throws WeixinException { - return cashApi.sendRedpack(redpacket, openId); - } - - /** - * 发放红包 企业向微信用户个人发现金红包 - * - * @param redpacket - * 红包信息 - * @param openIds - * 接受收红包的用户的openid 必填 * @return 发放结果 * @see com.foxinmy.weixin4j.api.CashApi * @see com.foxinmy.weixin4j.payment.mch.Redpacket @@ -711,9 +693,24 @@ public class WeixinPayProxy { * 发放裂变红包接口 * @throws WeixinException */ - public List> sendRedpacks(Redpacket redpacket, - String... openIds) { - return cashApi.sendRedpacks(redpacket, openIds); + public RedpacketSendResult sendRedpack(Redpacket redpacket) + throws WeixinException { + return cashApi.sendRedpack(redpacket); + } + + /** + * 批量发放红包 企业向微信用户个人发现金红包 + * + * @param redpacket + * 多个红包信息 + * @return 发放结果 + * @see com.foxinmy.weixin4j.api.CashApi + * @see #sendRedpacks(Redpacket...) + * @throws WeixinException + */ + public List> sendRedpacks( + Redpacket... redpackets) { + return cashApi.sendRedpacks(redpackets); } /** @@ -882,5 +879,5 @@ public class WeixinPayProxy { return customsApi.queryCustomsOrder(idQuery, customsCity); } - public final static String VERSION = "1.7.3"; + public final static String VERSION = "1.7.4"; } diff --git a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/payment/mch/APPPayRequest.java b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/payment/mch/APPPayRequest.java index 66e1a8a7..eaf311b7 100644 --- a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/payment/mch/APPPayRequest.java +++ b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/payment/mch/APPPayRequest.java @@ -36,7 +36,7 @@ public class APPPayRequest extends AbstractPayRequest { public PayRequest toRequestObject() { PayRequest payRequest = new PayRequest(getPaymentAccount().getId(), "Sign=WXPay"); - payRequest.setPartnerId(getPaymentAccount().getPartnerId()); + payRequest.setPartnerId(getPaymentAccount().getMchId()); payRequest.setPrepayId(getPrePayId()); String sign = DigestUtil.MD5( String.format("%s&key=%s", @@ -55,7 +55,7 @@ public class APPPayRequest extends AbstractPayRequest { content.append(String.format("", payRequest.getAppId())); content.append(String.format("", - getPaymentAccount().getMchId())); + getPaymentAccount().getPartnerId())); content.append(String.format("", payRequest.getPrepayId())); content.append(String.format("", diff --git a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/payment/mch/Redpacket.java b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/payment/mch/Redpacket.java index 47339592..0000003f 100644 --- a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/payment/mch/Redpacket.java +++ b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/payment/mch/Redpacket.java @@ -33,6 +33,12 @@ public class Redpacket extends MerchantResult { @XmlElement(name = "mch_billno") @JSONField(name = "mch_billno") private String outTradeNo; + /** + * 接受收红包的用户的openid 必填 + */ + @XmlElement(name = "re_openid") + @JSONField(name = "re_openid") + private String openId; /** * 红包发送者名称 必填 */ @@ -113,6 +119,8 @@ public class Redpacket extends MerchantResult { * * @param outTradeNo * 商户侧一天内不可重复的订单号 接口根据商户订单号支持重入 如出现超时可再调用 必填 + * @param openId + * 接受收红包的用户的openid 必填 * @param sendName * 红包发送者名称 必填 * @param totalAmount @@ -128,24 +136,48 @@ public class Redpacket extends MerchantResult { * @param remark * 备注 必填 */ - public Redpacket(String outTradeNo, String sendName, double totalAmount, - int totalNum, String wishing, String clientIp, String actName, - String remark) { + public Redpacket(String outTradeNo, String openId, String sendName, + double totalAmount, int totalNum, String wishing, String clientIp, + String actName, String remark) { this.outTradeNo = outTradeNo; + this.openId = openId; this.sendName = sendName; + this.totalAmount = DateUtil.formatYuan2Fen(totalAmount); this.totalNum = totalNum; this.wishing = wishing; this.clientIp = clientIp; this.actName = actName; this.remark = remark; - this.totalAmount = DateUtil.formatYuan2Fen(totalAmount); this.amtType = totalNum > 1 ? "ALL_RAND" : null; } + /** + * 批量发送时可能需要 + * + * @param outTradeNo + * 订单号 + * @param openId + * 用户ID + * @return 红包实体 + */ + public Redpacket copy(String outTradeNo, String openId) { + Redpacket readpacket = new Redpacket(outTradeNo, openId, sendName, + totalAmount, totalNum, wishing, clientIp, actName, remark); + readpacket.setMsgAppId(msgAppId); + readpacket.setConsumeMchId(consumeMchId); + readpacket.setSceneType(sceneType); + readpacket.setRisk(risk); + return readpacket; + } + public String getOutTradeNo() { return outTradeNo; } + public String getOpenId() { + return openId; + } + public String getSendName() { return sendName; } @@ -226,12 +258,12 @@ public class Redpacket extends MerchantResult { @Override public String toString() { - return "Redpacket [outTradeNo=" + outTradeNo + ", sendName=" + sendName - + ", totalAmount=" + totalAmount + ", totalNum=" + totalNum - + ", amtType=" + amtType + ", wishing=" + wishing - + ", clientIp=" + clientIp + ", actName=" + actName - + ", remark=" + remark + ", msgAppId=" + msgAppId - + ", consumeMchId=" + consumeMchId + ", sceneType=" + sceneType - + ", risk=" + risk + ", " + super.toString() + "]"; + return "Redpacket [outTradeNo=" + outTradeNo + ", openId=" + openId + + ", sendName=" + sendName + ", totalAmount=" + totalAmount + + ", totalNum=" + totalNum + ", amtType=" + amtType + + ", wishing=" + wishing + ", clientIp=" + clientIp + + ", actName=" + actName + ", remark=" + remark + ", msgAppId=" + + msgAppId + ", consumeMchId=" + consumeMchId + ", sceneType=" + + sceneType + ", risk=" + risk + ", " + super.toString() + "]"; } } diff --git a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/util/WeixinErrorUtil2.java b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/util/WeixinErrorUtil2.java deleted file mode 100644 index 3f0634ba..00000000 --- a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/util/WeixinErrorUtil2.java +++ /dev/null @@ -1,112 +0,0 @@ -package com.foxinmy.weixin4j.util; - -import com.foxinmy.weixin4j.http.weixin.WeixinResponse; - -import org.xml.sax.Attributes; -import org.xml.sax.ContentHandler; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; -import org.xml.sax.XMLReader; -import org.xml.sax.helpers.DefaultHandler; -import org.xml.sax.helpers.XMLReaderFactory; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -/** - * 接口调用错误获取 - * - * @author fengyapeng - * @className WeixinErrorUtil2 - * @date - * @see - * @since JDK 1.6 - */ -public final class WeixinErrorUtil2 { - - private static byte[] errorXmlByteArray; - private final static Map errorCacheMap; - - static { - errorCacheMap = new ConcurrentHashMap(); - try { - errorXmlByteArray = IOUtil.toByteArray(WeixinResponse.class.getResourceAsStream("error.xml")); - XMLReader xmlReader = XMLReaderFactory.createXMLReader(); - ContentHandler textHandler = new ErrorTextHandler(errorCacheMap); - xmlReader.setContentHandler(textHandler); - xmlReader.parse(new InputSource(new ByteArrayInputStream(errorXmlByteArray))); - } catch (IOException e) { - ; - } catch (SAXException e) { - - } - } - - private static class ErrorTextHandler extends DefaultHandler { - - private Map errorCacheMap; - - public ErrorTextHandler(Map errorCacheMap) { - this.errorCacheMap = errorCacheMap; - } - - private String code; - private String text; - private boolean codeElement; - private boolean textElement; - private int isPair = 0; - - @Override - public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { - codeElement = qName.equalsIgnoreCase("code"); - textElement = qName.equalsIgnoreCase("text"); - } - - @Override - public void endElement(String uri, String localName, String qName) throws SAXException { - } - - @Override - public void characters(char[] ch, int start, int length) throws SAXException { - String _text = new String(ch, start, length); - if (codeElement) { - isPair++; - code = _text; - codeElement = false; - } - if (textElement) { - isPair++; - text = _text; - textElement = false; - } - if (isPair == 2) { - // 配对成功 - errorCacheMap.put(code, text); - isPair = 0; - code = null; - text = null; - } - } - } - - public static String getText(String code) throws RuntimeException { - return errorCacheMap.get(code); - } - - public static String getText(String code, String defaultMsg) throws RuntimeException { - String text = getText(code); - if (StringUtil.isNotBlank(text)) { - return text; - } - return defaultMsg; - } - - - public static void main(String[] args) { - System.out.println(getText("40001")); - System.out.println(getText("30002")); - System.out.println(getText("1234")); - } -} diff --git a/weixin4j-base/src/test/java/com/foxinmy/weixin4j/base/test/CashTest.java b/weixin4j-base/src/test/java/com/foxinmy/weixin4j/base/test/CashTest.java index 85e3366e..29b106d8 100644 --- a/weixin4j-base/src/test/java/com/foxinmy/weixin4j/base/test/CashTest.java +++ b/weixin4j-base/src/test/java/com/foxinmy/weixin4j/base/test/CashTest.java @@ -26,10 +26,10 @@ public class CashTest extends PayTest { @Test public void sendRedpacket() throws WeixinException { - Redpacket redpacket = new Redpacket("HB001", "无忧钱庄", 1d, 1, "红包测试", + Redpacket redpacket = new Redpacket("HB001", + "oyFLst1bqtuTcxK-ojF8hOGtLQao", "无忧钱庄", 1d, 1, "红包测试", "127.0.0.1", "快来领取红包吧!", "来就送钱"); - RedpacketSendResult result = PAY.sendRedpack(redpacket, - "oyFLst1bqtuTcxK-ojF8hOGtLQao"); + RedpacketSendResult result = PAY.sendRedpack(redpacket); Assert.assertEquals(Consts.SUCCESS, result.getReturnCode()); Assert.assertEquals(Consts.SUCCESS, result.getResultCode()); System.err.println(result); diff --git a/weixin4j-mp/CHANGE.md b/weixin4j-mp/CHANGE.md index 2ededc69..d69bd491 100644 --- a/weixin4j-mp/CHANGE.md +++ b/weixin4j-mp/CHANGE.md @@ -260,4 +260,10 @@ * 2016-12-13 - + version upgrade to 1.7.3 \ No newline at end of file + + version upgrade to 1.7.3 + +* 2017-01-09 + + + 新增批量发红包接口 + + + version upgrade to 1.7.4 \ No newline at end of file diff --git a/weixin4j-mp/pom.xml b/weixin4j-mp/pom.xml index ff30b420..e344f8dd 100644 --- a/weixin4j-mp/pom.xml +++ b/weixin4j-mp/pom.xml @@ -5,7 +5,7 @@ com.foxinmy weixin4j - 1.7.4-SNAPSHOT + 1.7.4 weixin4j-mp weixin4j-mp diff --git a/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/WeixinComponentProxy.java b/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/WeixinComponentProxy.java index 8f66eec2..bc5b1387 100644 --- a/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/WeixinComponentProxy.java +++ b/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/WeixinComponentProxy.java @@ -225,5 +225,5 @@ public class WeixinComponentProxy { authAppId), component(componentId).getTokenManager()); } - public final static String VERSION = "1.7.3"; + public final static String VERSION = "1.7.4"; } diff --git a/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/WeixinProxy.java b/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/WeixinProxy.java index f6e586ef..0fbae03b 100644 --- a/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/WeixinProxy.java +++ b/weixin4j-mp/src/main/java/com/foxinmy/weixin4j/mp/WeixinProxy.java @@ -2063,5 +2063,5 @@ public class WeixinProxy { return cardApi.createCardQR(expireSeconds, cardQRs); } - public final static String VERSION = "1.7.3"; + public final static String VERSION = "1.7.4"; } diff --git a/weixin4j-qy/CHANGE.md b/weixin4j-qy/CHANGE.md index 96338603..9ed99efe 100644 --- a/weixin4j-qy/CHANGE.md +++ b/weixin4j-qy/CHANGE.md @@ -199,4 +199,12 @@ * 2016-12-13 - + version upgrade to 1.7.3 \ No newline at end of file + + version upgrade to 1.7.3 + +* 2017-01-09 + + + 新增批量发红包接口 + + + 新增摇一摇周边接口 + + + version upgrade to 1.7.4 \ No newline at end of file diff --git a/weixin4j-qy/pom.xml b/weixin4j-qy/pom.xml index b10f053e..af2ffa9d 100644 --- a/weixin4j-qy/pom.xml +++ b/weixin4j-qy/pom.xml @@ -5,7 +5,7 @@ com.foxinmy weixin4j - 1.7.4-SNAPSHOT + 1.7.4 weixin4j-qy weixin4j-qy diff --git a/weixin4j-qy/src/main/java/com/foxinmy/weixin4j/qy/WeixinProxy.java b/weixin4j-qy/src/main/java/com/foxinmy/weixin4j/qy/WeixinProxy.java index 2748d886..d6de2232 100644 --- a/weixin4j-qy/src/main/java/com/foxinmy/weixin4j/qy/WeixinProxy.java +++ b/weixin4j-qy/src/main/java/com/foxinmy/weixin4j/qy/WeixinProxy.java @@ -1433,5 +1433,5 @@ public class WeixinProxy { return chatApi.sendChatMessage(message); } - public final static String VERSION = "1.7.3"; + public final static String VERSION = "1.7.4"; } diff --git a/weixin4j-qy/src/main/java/com/foxinmy/weixin4j/qy/WeixinSuiteProxy.java b/weixin4j-qy/src/main/java/com/foxinmy/weixin4j/qy/WeixinSuiteProxy.java index 017cd8cd..819bc263 100644 --- a/weixin4j-qy/src/main/java/com/foxinmy/weixin4j/qy/WeixinSuiteProxy.java +++ b/weixin4j-qy/src/main/java/com/foxinmy/weixin4j/qy/WeixinSuiteProxy.java @@ -285,5 +285,5 @@ public class WeixinSuiteProxy { suite(suiteId).getTokenManager()); } - public final static String VERSION = "1.7.3"; + public final static String VERSION = "1.7.4"; }