();
+ for (final WxMpMessageRouterRule rule : matchRules) {
+ // 返回最后一个非异步的rule的执行结果
+ if(rule.isAsync()) {
+ futures.add(
+ executorService.submit(new Runnable() {
+ public void run() {
+ rule.service(wxMessage, wxMpService, sessionManager, exceptionHandler);
+ }
+ })
+ );
+ } else {
+ res = rule.service(wxMessage, wxMpService, sessionManager, exceptionHandler);
+ // 在同步操作结束,session访问结束
+ log.debug("End session access: async=false, sessionId={}", wxMessage.getFromUserName());
+ sessionEndAccess(wxMessage);
+ }
+ }
+
+ if (futures.size() > 0) {
+ executorService.submit(new Runnable() {
+ @Override
+ public void run() {
+ for (Future future : futures) {
+ try {
+ future.get();
+ log.debug("End session access: async=true, sessionId={}", wxMessage.getFromUserName());
+ // 异步操作结束,session访问结束
+ sessionEndAccess(wxMessage);
+ } catch (InterruptedException e) {
+ log.error("Error happened when wait task finish", e);
+ } catch (ExecutionException e) {
+ log.error("Error happened when wait task finish", e);
+ }
+ }
+ }
+ });
+ }
+ return res;
+ }
+
+ protected boolean isDuplicateMessage(WxMpXmlMessage wxMessage) {
+
+ StringBuffer messageId = new StringBuffer();
+ if (wxMessage.getMsgId() == null) {
+ messageId.append(wxMessage.getCreateTime())
+ .append("-").append(wxMessage.getFromUserName())
+ .append("-").append(wxMessage.getEventKey() == null ? "" : wxMessage.getEventKey())
+ .append("-").append(wxMessage.getEvent() == null ? "" : wxMessage.getEvent())
+ ;
+ } else {
+ messageId.append(wxMessage.getMsgId());
+ }
+
+ if (messageDuplicateChecker.isDuplicate(messageId.toString())) {
+ return true;
+ }
+ return false;
+
+ }
+
+ /**
+ * 对session的访问结束
+ * @param wxMessage
+ */
+ protected void sessionEndAccess(WxMpXmlMessage wxMessage) {
+
+ InternalSession session = ((InternalSessionManager)sessionManager).findSession(wxMessage.getFromUserName());
+ if (session != null) {
+ session.endAccess();
+ }
+
+ }
+}
diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java
index 16ca6bca2..d822fc334 100644
--- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java
+++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java
@@ -94,7 +94,8 @@ public interface WxMpService {
/**
*
- * 上传多媒体文件
+ * 新增临时素材
+ * 本接口即为原“上传多媒体文件”接口。
*
* 上传的多媒体文件有格式和大小限制,如下:
* 图片(image): 1M,支持JPG格式
@@ -102,7 +103,7 @@ public interface WxMpService {
* 视频(video):10MB,支持MP4格式
* 缩略图(thumb):64KB,支持JPG格式
*
- * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=上传下载多媒体文件
+ * 详情请见: 新增临时素材
*
* @param mediaType 媒体类型, 请看{@link me.chanjar.weixin.common.api.WxConsts}
* @param fileType 文件类型,请看{@link me.chanjar.weixin.common.api.WxConsts}
@@ -241,6 +242,7 @@ public interface WxMpService {
public WxMpMaterialFileBatchGetResult materialFileBatchGet(String type, int offset, int count) throws WxErrorException;
/**
+ * 新增临时素材
* @see #mediaUpload(String, String, InputStream)
* @param mediaType
* @param file
@@ -250,9 +252,10 @@ public interface WxMpService {
/**
*
- * 下载多媒体文件
+ * 获取临时素材
+ * 本接口即为原“下载多媒体文件”接口。
* 根据微信文档,视频文件下载不了,会返回null
- * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=上传下载多媒体文件
+ * 详情请见: 获取临时素材
*
* @params media_id
* @return 保存到本地的临时文件
@@ -351,7 +354,7 @@ public interface WxMpService {
* @throws WxErrorException
*/
public void menuDelete(String menuid) throws WxErrorException;
-
+
/**
*
* 自定义菜单查询接口
@@ -361,7 +364,7 @@ public interface WxMpService {
* @throws WxErrorException
*/
public WxMenu menuGet() throws WxErrorException;
-
+
/**
*
* 测试个性化菜单匹配结果
@@ -465,7 +468,7 @@ public interface WxMpService {
/**
*
* 换取临时二维码ticket
- * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=生成带参数的二维码
+ * 详情请见: 生成带参数的二维码
*
* @param scene_id 参数。
* @param expire_seconds 过期秒数,默认60秒,最小60秒,最大1800秒
@@ -477,7 +480,7 @@ public interface WxMpService {
/**
*
* 换取永久二维码ticket
- * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=生成带参数的二维码
+ * 详情请见: 生成带参数的二维码
*
* @param scene_id 参数。永久二维码时最大值为100000(目前参数只支持1--100000)
* @return
@@ -488,7 +491,7 @@ public interface WxMpService {
/**
*
* 换取永久字符串二维码ticket
- * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=生成带参数的二维码
+ * 详情请见: 生成带参数的二维码
*
*
* @param scene_str 参数。字符串类型长度现在为1到64
@@ -500,7 +503,7 @@ public interface WxMpService {
/**
*
* 换取二维码图片文件,jpg格式
- * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=生成带参数的二维码
+ * 详情请见: 生成带参数的二维码
*
* @param ticket 二维码ticket
* @return
@@ -508,10 +511,21 @@ public interface WxMpService {
*/
public File qrCodePicture(WxMpQrCodeTicket ticket) throws WxErrorException;
+ /**
+ *
+ * 换取二维码图片url地址(可以选择是否生成压缩的网址)
+ * 详情请见: 生成带参数的二维码
+ *
+ * @param ticket 二维码ticket
+ * @param needShortUrl 是否需要压缩的二维码地址
+ * @return
+ * @throws WxErrorException
+ */
+ public String qrCodePictureUrl(String ticket, boolean needShortUrl) throws WxErrorException;
/**
*
* 换取二维码图片url地址
- * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=生成带参数的二维码
+ * 详情请见: 生成带参数的二维码
*
* @param ticket 二维码ticket
* @return
@@ -723,7 +737,7 @@ public interface WxMpService {
/**
* 统一下单(详见http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1)
* 在发起微信支付前,需要调用统一下单接口,获取"预支付交易会话标识"
- *
+ *
* @param parameters
* All required/optional parameters for weixin payment
* @return
@@ -732,97 +746,114 @@ public interface WxMpService {
WxMpPrepayIdResult getPrepayId(Map parameters);
/**
- * 该接口调用“统一下单”接口,并拼装JSSDK发起支付请求需要的参数
+ * 该接口调用“统一下单”接口,并拼装发起支付请求需要的参数
* 详见http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#.E5.8F.91.E8.B5.B7.E4.B8.80.E4.B8.AA.E5.BE.AE.E4.BF.A1.E6.94.AF.E4.BB.98.E8.AF.B7.E6.B1.82
* @param parameters
* the required or optional parameters
* @return
- * @throws WxErrorException
+ * @throws WxErrorException
*/
- Map getJSSDKPayInfo(Map parameters) throws WxErrorException;
+ Map getPayInfo(Map parameters) throws WxErrorException;
/**
- * 该接口调用“统一下单”接口,并拼装JSSDK发起支付请求需要的参数
+ * 该接口调用“统一下单”接口,并拼装NATIVE发起支付请求需要的参数
* 详见http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#.E5.8F.91.E8.B5.B7.E4.B8.80.E4.B8.AA.E5.BE.AE.E4.BF.A1.E6.94.AF.E4.BB.98.E8.AF.B7.E6.B1.82
- * @param openId 支付人openId
+ * tradeType 交易类型 NATIVE (其他交易类型JSAPI,APP,WAP)
+ * @param productId 商户商品ID
* @param outTradeNo 商户端对应订单号
* @param amt 金额(单位元)
* @param body 商品描述
- * @param tradeType 交易类型 JSAPI,NATIVE,APP,WAP
* @param ip 发起支付的客户端IP
* @param notifyUrl 通知地址
* @return
* @throws WxErrorException
- * @deprecated Use me.chanjar.weixin.mp.api.WxMpService.getJSSDKPayInfo(Map) instead
+ * @deprecated Use me.chanjar.weixin.mp.api.WxMpService.getPayInfo(Map) instead
*/
@Deprecated
- Map getJSSDKPayInfo(String openId, String outTradeNo, double amt, String body, String tradeType, String ip, String notifyUrl) throws WxErrorException;
+ Map getNativePayInfo(String productId, String outTradeNo, double amt, String body, String ip, String notifyUrl) throws WxErrorException;
- /**
- * 该接口提供所有微信支付订单的查询,当支付通知处理异常戒丢失的情冴,商户可以通过该接口查询订单支付状态。
- * 详见http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_2
- * @param transactionId
- * @param outTradeNo
- */
- WxMpPayResult getJSSDKPayResult(String transactionId, String outTradeNo);
+ /**
+ * 该接口调用“统一下单”接口,并拼装JSAPI发起支付请求需要的参数
+ * 详见http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#.E5.8F.91.E8.B5.B7.E4.B8.80.E4.B8.AA.E5.BE.AE.E4.BF.A1.E6.94.AF.E4.BB.98.E8.AF.B7.E6.B1.82
+ * tradeType 交易类型 JSAPI(其他交易类型NATIVE,APP,WAP)
+ * @param openId 支付人openId
+ * @param outTradeNo 商户端对应订单号
+ * @param amt 金额(单位元)
+ * @param body 商品描述
+ * @param ip 发起支付的客户端IP
+ * @param notifyUrl 通知地址
+ * @return
+ * @throws WxErrorException
+ * @deprecated Use me.chanjar.weixin.mp.api.WxMpService.getPayInfo(Map) instead
+ */
+ @Deprecated
+ Map getJsapiPayInfo(String openId, String outTradeNo, double amt, String body, String ip, String notifyUrl) throws WxErrorException;
- /**
- * 读取支付结果通知
- * 详见http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_7
- * @param xmlData
- * @return
- */
- WxMpPayCallback getJSSDKCallbackData(String xmlData);
-
- /**
- * 微信支付-申请退款
- * 详见 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_4
- * @param parameters 需要传入的退款参数的Map。以下几项为参数的必须项:
- * transaction_id
- * out_trade_no (仅在上述transaction_id为空时是必须项)
- * out_refund_no
- * total_fee
- * refund_fee
- * @return 退款操作结果
- * @throws WxErrorException
- */
- public WxMpPayRefundResult refundPay(Map parameters) throws WxErrorException;
-
- /**
- *
- * 计算Map键值对是否和签名相符,
- * 按照字段名的 ASCII 码从小到大排序(字典序)后,使用 URL 键值对的 格式(即 key1=value1&key2=value2...)拼接成字符串
- *
- * @param kvm
- * @param signature
- * @return
- */
- public boolean checkJSSDKCallbackDataSignature(Map kvm, String signature);
+ /**
+ * 该接口提供所有微信支付订单的查询,当支付通知处理异常戒丢失的情冴,商户可以通过该接口查询订单支付状态。
+ * 详见http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_2
+ * @param transactionId
+ * @param outTradeNo
+ */
+ WxMpPayResult getJSSDKPayResult(String transactionId, String outTradeNo);
- /**
- * 发送微信红包给个人用户
- *
- * 需要传入的必填参数如下:
- * mch_billno//商户订单号
- * send_name//商户名称
- * re_openid//用户openid
- * total_amount//红包总额
- * total_num//红包发放总人数
- * wishing//红包祝福语
- * client_ip//服务器Ip地址
- * act_name//活动名称
- * remark //备注
- * 文档详见:https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_5
- *
- * 使用现金红包功能需要在xml配置文件中额外设置:
- * 微信商户平台ID
- * 商户平台设置的API密钥
- *
- * @param parameters
- * @return
- * @throws WxErrorException
- */
- public WxRedpackResult sendRedpack(Map parameters) throws WxErrorException;
+ /**
+ * 读取支付结果通知
+ * 详见http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_7
+ * @param xmlData
+ * @return
+ */
+ WxMpPayCallback getJSSDKCallbackData(String xmlData);
+
+ /**
+ * 微信支付-申请退款
+ * 详见 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_4
+ * @param parameters 需要传入的退款参数的Map。以下几项为参数的必须项:
+ * transaction_id
+ * out_trade_no (仅在上述transaction_id为空时是必须项)
+ * out_refund_no
+ * total_fee
+ * refund_fee
+ * @return 退款操作结果
+ * @throws WxErrorException
+ */
+ public WxMpPayRefundResult refundPay(Map parameters) throws WxErrorException;
+
+ /**
+ *
+ * 计算Map键值对是否和签名相符,
+ * 按照字段名的 ASCII 码从小到大排序(字典序)后,使用 URL 键值对的 格式(即 key1=value1&key2=value2...)拼接成字符串
+ *
+ * @param kvm
+ * @param signature
+ * @return
+ */
+ public boolean checkJSSDKCallbackDataSignature(Map kvm, String signature);
+
+ /**
+ * 发送微信红包给个人用户
+ *
+ * 需要传入的必填参数如下:
+ * mch_billno//商户订单号
+ * send_name//商户名称
+ * re_openid//用户openid
+ * total_amount//红包总额
+ * total_num//红包发放总人数
+ * wishing//红包祝福语
+ * client_ip//服务器Ip地址
+ * act_name//活动名称
+ * remark //备注
+ * 文档详见:https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_5
+ *
+ * 使用现金红包功能需要在xml配置文件中额外设置:
+ * 微信商户平台ID
+ * 商户平台设置的API密钥
+ *
+ * @param parameters
+ * @return
+ * @throws WxErrorException
+ */
+ public WxRedpackResult sendRedpack(Map parameters) throws WxErrorException;
/**
* 获得卡券api_ticket,不强制刷新卡券api_ticket
@@ -914,7 +945,7 @@ public interface WxMpService {
*/
public void markCardCode(String code, String cardId, String openId, boolean isMark) throws
WxErrorException;
-
+
/**
* 查看卡券详情接口
* 详见 https://mp.weixin.qq.com/wiki/14/8dd77aeaee85f922db5f8aa6386d385e.html#.E6.9F.A5.E7.9C.8B.E5.8D.A1.E5.88.B8.E8.AF.A6.E6.83.85
@@ -925,4 +956,49 @@ public interface WxMpService {
* @throws WxErrorException
*/
public String getCardDetail(String cardId) throws WxErrorException;
+
+ /**
+ *
+ * 预览接口
+ * 详情请见:http://mp.weixin.qq.com/wiki/15/40b6865b893947b764e2de8e4a1fb55f.html#.E9.A2.84.E8.A7.88.E6.8E.A5.E5.8F.A3.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
+ *
+ * @param wxMpMassPreviewMessage
+ * @return wxMpMassSendResult
+ * @throws WxErrorException
+ */
+ public WxMpMassSendResult massMessagePreview(WxMpMassPreviewMessage wxMpMassPreviewMessage) throws Exception;
+
+ /**
+ *
+ * 上传图文消息内的图片获取URL
+ * 详情请见: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
+ *
+ * @param file
+ * @return WxMediaImgUploadResult 返回图片url
+ * @throws WxErrorException
+ */
+ public WxMediaImgUploadResult mediaImgUpload(File file) throws WxErrorException;
+
+ /**
+ *
+ * 设置所属行业
+ * 官方文档中暂未告知响应内容
+ * 详情请见:http://mp.weixin.qq.com/wiki/5/6dde9eaa909f83354e0094dc3ad99e05.html#.E8.AE.BE.E7.BD.AE.E6.89.80.E5.B1.9E.E8.A1.8C.E4.B8.9A
+ *
+ * @param wxMpIndustry
+ * @return JsonObject
+ * @throws WxErrorException
+ */
+ String setIndustry(WxMpIndustry wxMpIndustry) throws WxErrorException;
+
+ /***
+ *
+ * 获取设置的行业信息
+ * 详情请见:http://mp.weixin.qq.com/wiki/5/6dde9eaa909f83354e0094dc3ad99e05.html#.E8.AE.BE.E7.BD.AE.E6.89.80.E5.B1.9E.E8.A1.8C.E4.B8.9A
+ *
+ *
+ * @return wxMpIndustry
+ * @throws WxErrorException
+ */
+ WxMpIndustry getIndustry() throws WxErrorException;
}
diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpServiceImpl.java
index 1805b2710..18fb3aefe 100644
--- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpServiceImpl.java
+++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpServiceImpl.java
@@ -50,7 +50,6 @@ import java.io.InputStream;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
-import java.nio.charset.Charset;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.Map.Entry;
@@ -86,32 +85,36 @@ public class WxMpServiceImpl implements WxMpService {
protected WxSessionManager sessionManager = new StandardSessionManager();
+ @Override
public boolean checkSignature(String timestamp, String nonce, String signature) {
try {
- return SHA1.gen(wxMpConfigStorage.getToken(), timestamp, nonce).equals(signature);
+ return SHA1.gen(this.wxMpConfigStorage.getToken(), timestamp, nonce).equals(signature);
} catch (Exception e) {
return false;
}
}
+ @Override
public String getAccessToken() throws WxErrorException {
return getAccessToken(false);
}
+ @Override
public String getAccessToken(boolean forceRefresh) throws WxErrorException {
if (forceRefresh) {
- wxMpConfigStorage.expireAccessToken();
+ this.wxMpConfigStorage.expireAccessToken();
}
- if (wxMpConfigStorage.isAccessTokenExpired()) {
- synchronized (globalAccessTokenRefreshLock) {
- if (wxMpConfigStorage.isAccessTokenExpired()) {
- String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential"
- + "&appid=" + wxMpConfigStorage.getAppId()
- + "&secret=" + wxMpConfigStorage.getSecret();
+ if (this.wxMpConfigStorage.isAccessTokenExpired()) {
+ synchronized (this.globalAccessTokenRefreshLock) {
+ if (this.wxMpConfigStorage.isAccessTokenExpired()) {
+ String url = new StringBuffer()
+ .append("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential")
+ .append("&appid=").append(this.wxMpConfigStorage.getAppId())
+ .append("&secret=").append(this.wxMpConfigStorage.getSecret()).toString();
try {
HttpGet httpGet = new HttpGet(url);
- if (httpProxy != null) {
- RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
+ if (this.httpProxy != null) {
+ RequestConfig config = RequestConfig.custom().setProxy(this.httpProxy).build();
httpGet.setConfig(config);
}
try (CloseableHttpResponse response = getHttpclient().execute(httpGet)) {
@@ -121,7 +124,7 @@ public class WxMpServiceImpl implements WxMpService {
throw new WxErrorException(error);
}
WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
- wxMpConfigStorage.updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
+ this.wxMpConfigStorage.updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
}finally {
httpGet.releaseConnection();
}
@@ -133,33 +136,36 @@ public class WxMpServiceImpl implements WxMpService {
}
}
}
- return wxMpConfigStorage.getAccessToken();
+ return this.wxMpConfigStorage.getAccessToken();
}
+ @Override
public String getJsapiTicket() throws WxErrorException {
return getJsapiTicket(false);
}
+ @Override
public String getJsapiTicket(boolean forceRefresh) throws WxErrorException {
if (forceRefresh) {
- wxMpConfigStorage.expireJsapiTicket();
+ this.wxMpConfigStorage.expireJsapiTicket();
}
- if (wxMpConfigStorage.isJsapiTicketExpired()) {
- synchronized (globalJsapiTicketRefreshLock) {
- if (wxMpConfigStorage.isJsapiTicketExpired()) {
+ if (this.wxMpConfigStorage.isJsapiTicketExpired()) {
+ synchronized (this.globalJsapiTicketRefreshLock) {
+ if (this.wxMpConfigStorage.isJsapiTicketExpired()) {
String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi";
String responseContent = execute(new SimpleGetRequestExecutor(), url, null);
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject();
String jsapiTicket = tmpJsonObject.get("ticket").getAsString();
int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt();
- wxMpConfigStorage.updateJsapiTicket(jsapiTicket, expiresInSeconds);
+ this.wxMpConfigStorage.updateJsapiTicket(jsapiTicket, expiresInSeconds);
}
}
}
- return wxMpConfigStorage.getJsapiTicket();
+ return this.wxMpConfigStorage.getJsapiTicket();
}
+ @Override
public WxJsapiSignature createJsapiSignature(String url) throws WxErrorException {
long timestamp = System.currentTimeMillis() / 1000;
String noncestr = RandomUtils.getRandomStr();
@@ -172,7 +178,7 @@ public class WxMpServiceImpl implements WxMpService {
"url=" + url
);
WxJsapiSignature jsapiSignature = new WxJsapiSignature();
- jsapiSignature.setAppid(wxMpConfigStorage.getAppId());
+ jsapiSignature.setAppid(this.wxMpConfigStorage.getAppId());
jsapiSignature.setTimestamp(timestamp);
jsapiSignature.setNoncestr(noncestr);
jsapiSignature.setUrl(url);
@@ -183,11 +189,13 @@ public class WxMpServiceImpl implements WxMpService {
}
}
+ @Override
public void customMessageSend(WxMpCustomMessage message) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/message/custom/send";
execute(new SimplePostRequestExecutor(), url, message.toJson());
}
+ @Override
public void menuCreate(WxMenu menu) throws WxErrorException {
if (menu.getMatchRule() != null) {
String url = "https://api.weixin.qq.com/cgi-bin/menu/addconditional";
@@ -198,16 +206,19 @@ public class WxMpServiceImpl implements WxMpService {
}
}
+ @Override
public void menuDelete() throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/menu/delete";
execute(new SimpleGetRequestExecutor(), url, null);
}
+ @Override
public void menuDelete(String menuid) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/menu/delconditional";
execute(new SimpleGetRequestExecutor(), url, "menuid=" + menuid);
}
+ @Override
public WxMenu menuGet() throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/menu/get";
try {
@@ -222,6 +233,7 @@ public class WxMpServiceImpl implements WxMpService {
}
}
+ @Override
public WxMenu menuTryMatch(String userid) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/menu/trymatch";
try {
@@ -236,25 +248,30 @@ public class WxMpServiceImpl implements WxMpService {
}
}
+ @Override
public WxMediaUploadResult mediaUpload(String mediaType, String fileType, InputStream inputStream) throws WxErrorException, IOException {
return mediaUpload(mediaType, FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), fileType));
}
+ @Override
public WxMediaUploadResult mediaUpload(String mediaType, File file) throws WxErrorException {
- String url = "http://file.api.weixin.qq.com/cgi-bin/media/upload?type=" + mediaType;
+ String url = "https://api.weixin.qq.com/cgi-bin/media/upload?type=" + mediaType;
return execute(new MediaUploadRequestExecutor(), url, file);
}
+ @Override
public File mediaDownload(String media_id) throws WxErrorException {
- String url = "http://file.api.weixin.qq.com/cgi-bin/media/get";
- return execute(new MediaDownloadRequestExecutor(wxMpConfigStorage.getTmpDirFile()), url, "media_id=" + media_id);
+ String url = "https://api.weixin.qq.com/cgi-bin/media/get";
+ return execute(new MediaDownloadRequestExecutor(this.wxMpConfigStorage.getTmpDirFile()), url, "media_id=" + media_id);
}
+ @Override
public WxMpMaterialUploadResult materialFileUpload(String mediaType, WxMpMaterial material) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/material/add_material?type=" + mediaType;
return execute(new MaterialUploadRequestExecutor(), url, material);
}
+ @Override
public WxMpMaterialUploadResult materialNewsUpload(WxMpMaterialNews news) throws WxErrorException {
if (news == null || news.isEmpty()) {
throw new IllegalArgumentException("news is empty!");
@@ -264,21 +281,25 @@ public class WxMpServiceImpl implements WxMpService {
return WxMpMaterialUploadResult.fromJson(responseContent);
}
+ @Override
public InputStream materialImageOrVoiceDownload(String media_id) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/material/get_material";
- return execute(new MaterialVoiceAndImageDownloadRequestExecutor(wxMpConfigStorage.getTmpDirFile()), url, media_id);
+ return execute(new MaterialVoiceAndImageDownloadRequestExecutor(this.wxMpConfigStorage.getTmpDirFile()), url, media_id);
}
+ @Override
public WxMpMaterialVideoInfoResult materialVideoInfo(String media_id) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/material/get_material";
return execute(new MaterialVideoInfoRequestExecutor(), url, media_id);
}
+ @Override
public WxMpMaterialNews materialNewsInfo(String media_id) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/material/get_material";
return execute(new MaterialNewsInfoRequestExecutor(), url, media_id);
}
+ @Override
public boolean materialNewsUpdate(WxMpMaterialArticleUpdate wxMpMaterialArticleUpdate) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/material/update_news";
String responseText = post(url, wxMpMaterialArticleUpdate.toJson());
@@ -290,11 +311,13 @@ public class WxMpServiceImpl implements WxMpService {
}
}
+ @Override
public boolean materialDelete(String media_id) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/material/del_material";
return execute(new MaterialDeleteRequestExecutor(), url, media_id);
}
+ @Override
public WxMpMaterialCountResult materialCount() throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/material/get_materialcount";
String responseText = get(url, null);
@@ -306,6 +329,7 @@ public class WxMpServiceImpl implements WxMpService {
}
}
+ @Override
public WxMpMaterialNewsBatchGetResult materialNewsBatchGet(int offset, int count) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material";
Map params = new HashMap<>();
@@ -321,6 +345,7 @@ public class WxMpServiceImpl implements WxMpService {
}
}
+ @Override
public WxMpMaterialFileBatchGetResult materialFileBatchGet(String type, int offset, int count) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material";
Map params = new HashMap<>();
@@ -336,30 +361,35 @@ public class WxMpServiceImpl implements WxMpService {
}
}
+ @Override
public WxMpMassUploadResult massNewsUpload(WxMpMassNews news) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/media/uploadnews";
String responseContent = execute(new SimplePostRequestExecutor(), url, news.toJson());
return WxMpMassUploadResult.fromJson(responseContent);
}
+ @Override
public WxMpMassUploadResult massVideoUpload(WxMpMassVideo video) throws WxErrorException {
- String url = "http://file.api.weixin.qq.com/cgi-bin/media/uploadvideo";
+ String url = "https://api.weixin.qq.com/cgi-bin/media/uploadvideo";
String responseContent = execute(new SimplePostRequestExecutor(), url, video.toJson());
return WxMpMassUploadResult.fromJson(responseContent);
}
+ @Override
public WxMpMassSendResult massGroupMessageSend(WxMpMassGroupMessage message) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall";
String responseContent = execute(new SimplePostRequestExecutor(), url, message.toJson());
return WxMpMassSendResult.fromJson(responseContent);
}
+ @Override
public WxMpMassSendResult massOpenIdsMessageSend(WxMpMassOpenIdsMessage message) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/message/mass/send";
String responseContent = execute(new SimplePostRequestExecutor(), url, message.toJson());
return WxMpMassSendResult.fromJson(responseContent);
}
+ @Override
public WxMpGroup groupCreate(String name) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/groups/create";
JsonObject json = new JsonObject();
@@ -374,6 +404,7 @@ public class WxMpServiceImpl implements WxMpService {
return WxMpGroup.fromJson(responseContent);
}
+ @Override
public List groupGet() throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/groups/get";
String responseContent = execute(new SimpleGetRequestExecutor(), url, null);
@@ -387,6 +418,7 @@ public class WxMpServiceImpl implements WxMpService {
}.getType());
}
+ @Override
public long userGetGroup(String openid) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/groups/getid";
JsonObject o = new JsonObject();
@@ -396,11 +428,13 @@ public class WxMpServiceImpl implements WxMpService {
return GsonHelper.getAsLong(tmpJsonElement.getAsJsonObject().get("groupid"));
}
+ @Override
public void groupUpdate(WxMpGroup group) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/groups/update";
execute(new SimplePostRequestExecutor(), url, group.toJson());
}
+ @Override
public void userUpdateGroup(String openid, long to_groupid) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/groups/members/update";
JsonObject json = new JsonObject();
@@ -409,6 +443,7 @@ public class WxMpServiceImpl implements WxMpService {
execute(new SimplePostRequestExecutor(), url, json.toString());
}
+ @Override
public void userUpdateRemark(String openid, String remark) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/user/info/updateremark";
JsonObject json = new JsonObject();
@@ -417,6 +452,7 @@ public class WxMpServiceImpl implements WxMpService {
execute(new SimplePostRequestExecutor(), url, json.toString());
}
+ @Override
public WxMpUser userInfo(String openid, String lang) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/user/info";
lang = lang == null ? "zh_CN" : lang;
@@ -424,12 +460,14 @@ public class WxMpServiceImpl implements WxMpService {
return WxMpUser.fromJson(responseContent);
}
+ @Override
public WxMpUserList userList(String next_openid) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/user/get";
String responseContent = execute(new SimpleGetRequestExecutor(), url, next_openid == null ? null : "next_openid=" + next_openid);
return WxMpUserList.fromJson(responseContent);
}
+ @Override
public WxMpQrCodeTicket qrCodeCreateTmpTicket(int scene_id, Integer expire_seconds) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/qrcode/create";
JsonObject json = new JsonObject();
@@ -446,6 +484,7 @@ public class WxMpServiceImpl implements WxMpService {
return WxMpQrCodeTicket.fromJson(responseContent);
}
+ @Override
public WxMpQrCodeTicket qrCodeCreateLastTicket(int scene_id) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/qrcode/create";
JsonObject json = new JsonObject();
@@ -459,6 +498,7 @@ public class WxMpServiceImpl implements WxMpService {
return WxMpQrCodeTicket.fromJson(responseContent);
}
+ @Override
public WxMpQrCodeTicket qrCodeCreateLastTicket(String scene_str) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/qrcode/create";
JsonObject json = new JsonObject();
@@ -472,24 +512,36 @@ public class WxMpServiceImpl implements WxMpService {
return WxMpQrCodeTicket.fromJson(responseContent);
}
+ @Override
public File qrCodePicture(WxMpQrCodeTicket ticket) throws WxErrorException {
String url = "https://mp.weixin.qq.com/cgi-bin/showqrcode";
return execute(new QrCodeRequestExecutor(), url, ticket);
}
@Override
- public String qrCodePictureUrl(String ticket) throws WxErrorException {
+ public String qrCodePictureUrl(String ticket, boolean needShortUrl) throws WxErrorException {
String url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=%s";
try {
- return String.format(url, URLEncoder.encode(ticket, Charsets.UTF_8.name()));
+ String resultUrl = String.format(url,
+ URLEncoder.encode(ticket, Charsets.UTF_8.name()));
+ if(needShortUrl){
+ return this.shortUrl(resultUrl);
+ }
+
+ return resultUrl;
} catch (UnsupportedEncodingException e) {
- WxError error = new WxError();
- error.setErrorCode(-1);
- error.setErrorMsg(e.getMessage());
+ WxError error = WxError.newBuilder().setErrorCode(-1)
+ .setErrorMsg(e.getMessage()).build();
throw new WxErrorException(error);
}
}
+
+ @Override
+ public String qrCodePictureUrl(String ticket) throws WxErrorException {
+ return qrCodePictureUrl(ticket, false);
+ }
+ @Override
public String shortUrl(String long_url) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/shorturl";
JsonObject o = new JsonObject();
@@ -500,6 +552,7 @@ public class WxMpServiceImpl implements WxMpService {
return tmpJsonElement.getAsJsonObject().get("short_url").getAsString();
}
+ @Override
public String templateSend(WxMpTemplateMessage templateMessage) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/message/template/send";
String responseContent = execute(new SimplePostRequestExecutor(), url, templateMessage.toJson());
@@ -510,6 +563,7 @@ public class WxMpServiceImpl implements WxMpService {
throw new WxErrorException(WxError.fromJson(responseContent));
}
+ @Override
public WxMpSemanticQueryResult semanticQuery(WxMpSemanticQuery semanticQuery) throws WxErrorException {
String url = "https://api.weixin.qq.com/semantic/semproxy/search";
String responseContent = execute(new SimplePostRequestExecutor(), url, semanticQuery.toJson());
@@ -518,34 +572,36 @@ public class WxMpServiceImpl implements WxMpService {
@Override
public String oauth2buildAuthorizationUrl(String scope, String state) {
- return this.oauth2buildAuthorizationUrl(wxMpConfigStorage.getOauth2redirectUri(), scope, state);
+ return this.oauth2buildAuthorizationUrl(this.wxMpConfigStorage.getOauth2redirectUri(), scope, state);
}
@Override
public String oauth2buildAuthorizationUrl(String redirectURI, String scope, String state) {
- String url = "https://open.weixin.qq.com/connect/oauth2/authorize?";
- url += "appid=" + wxMpConfigStorage.getAppId();
- url += "&redirect_uri=" + URIUtil.encodeURIComponent(redirectURI);
- url += "&response_type=code";
- url += "&scope=" + scope;
+ StringBuffer url = new StringBuffer();
+ url.append("https://open.weixin.qq.com/connect/oauth2/authorize?");
+ url.append("appid=").append(this.wxMpConfigStorage.getAppId());
+ url.append("&redirect_uri=").append(URIUtil.encodeURIComponent(redirectURI));
+ url.append("&response_type=code");
+ url.append("&scope=").append(scope);
if (state != null) {
- url += "&state=" + state;
+ url.append("&state=").append(state);
}
- url += "#wechat_redirect";
- return url;
+ url.append("#wechat_redirect");
+ return url.toString();
}
@Override
public WxMpOAuth2AccessToken oauth2getAccessToken(String code) throws WxErrorException {
- String url = "https://api.weixin.qq.com/sns/oauth2/access_token?";
- url += "appid=" + wxMpConfigStorage.getAppId();
- url += "&secret=" + wxMpConfigStorage.getSecret();
- url += "&code=" + code;
- url += "&grant_type=authorization_code";
+ StringBuffer url = new StringBuffer();
+ url.append("https://api.weixin.qq.com/sns/oauth2/access_token?");
+ url.append("appid=").append(this.wxMpConfigStorage.getAppId());
+ url.append("&secret=").append(this.wxMpConfigStorage.getSecret());
+ url.append("&code=").append(code);
+ url.append("&grant_type=authorization_code");
try {
RequestExecutor executor = new SimpleGetRequestExecutor();
- String responseText = executor.execute(getHttpclient(), httpProxy, url, null);
+ String responseText = executor.execute(getHttpclient(), this.httpProxy, url.toString(), null);
return WxMpOAuth2AccessToken.fromJson(responseText);
} catch (ClientProtocolException e) {
throw new RuntimeException(e);
@@ -556,14 +612,15 @@ public class WxMpServiceImpl implements WxMpService {
@Override
public WxMpOAuth2AccessToken oauth2refreshAccessToken(String refreshToken) throws WxErrorException {
- String url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?";
- url += "appid=" + wxMpConfigStorage.getAppId();
- url += "&grant_type=refresh_token";
- url += "&refresh_token=" + refreshToken;
+ StringBuffer url = new StringBuffer();
+ url.append("https://api.weixin.qq.com/sns/oauth2/refresh_token?");
+ url.append("appid=").append(this.wxMpConfigStorage.getAppId());
+ url.append("&grant_type=refresh_token");
+ url.append("&refresh_token=").append(refreshToken);
try {
RequestExecutor executor = new SimpleGetRequestExecutor();
- String responseText = executor.execute(getHttpclient(), httpProxy, url, null);
+ String responseText = executor.execute(getHttpclient(), this.httpProxy, url.toString(), null);
return WxMpOAuth2AccessToken.fromJson(responseText);
} catch (ClientProtocolException e) {
throw new RuntimeException(e);
@@ -574,18 +631,19 @@ public class WxMpServiceImpl implements WxMpService {
@Override
public WxMpUser oauth2getUserInfo(WxMpOAuth2AccessToken oAuth2AccessToken, String lang) throws WxErrorException {
- String url = "https://api.weixin.qq.com/sns/userinfo?";
- url += "access_token=" + oAuth2AccessToken.getAccessToken();
- url += "&openid=" + oAuth2AccessToken.getOpenId();
+ StringBuffer url = new StringBuffer();
+ url.append("https://api.weixin.qq.com/sns/userinfo?");
+ url.append("access_token=").append(oAuth2AccessToken.getAccessToken());
+ url.append("&openid=").append(oAuth2AccessToken.getOpenId());
if (lang == null) {
- url += "&lang=zh_CN";
+ url.append("&lang=zh_CN");
} else {
- url += "&lang=" + lang;
+ url.append("&lang=").append(lang);
}
try {
RequestExecutor executor = new SimpleGetRequestExecutor();
- String responseText = executor.execute(getHttpclient(), httpProxy, url, null);
+ String responseText = executor.execute(getHttpclient(), this.httpProxy, url.toString(), null);
return WxMpUser.fromJson(responseText);
} catch (ClientProtocolException e) {
throw new RuntimeException(e);
@@ -596,13 +654,14 @@ public class WxMpServiceImpl implements WxMpService {
@Override
public boolean oauth2validateAccessToken(WxMpOAuth2AccessToken oAuth2AccessToken) {
- String url = "https://api.weixin.qq.com/sns/auth?";
- url += "access_token=" + oAuth2AccessToken.getAccessToken();
- url += "&openid=" + oAuth2AccessToken.getOpenId();
+ StringBuffer url = new StringBuffer();
+ url.append("https://api.weixin.qq.com/sns/auth?");
+ url.append("access_token=").append(oAuth2AccessToken.getAccessToken());
+ url.append("&openid=").append(oAuth2AccessToken.getOpenId());
try {
RequestExecutor executor = new SimpleGetRequestExecutor();
- executor.execute(getHttpclient(), httpProxy, url, null);
+ executor.execute(getHttpclient(), this.httpProxy, url.toString(), null);
} catch (ClientProtocolException e) {
throw new RuntimeException(e);
} catch (IOException e) {
@@ -653,10 +712,12 @@ public class WxMpServiceImpl implements WxMpService {
}.getType());
}
+ @Override
public String get(String url, String queryParam) throws WxErrorException {
return execute(new SimpleGetRequestExecutor(), url, queryParam);
}
+ @Override
public String post(String url, String postData) throws WxErrorException {
return execute(new SimplePostRequestExecutor(), url, postData);
}
@@ -670,6 +731,7 @@ public class WxMpServiceImpl implements WxMpService {
* @return
* @throws WxErrorException
*/
+ @Override
public T execute(RequestExecutor executor, String uri, E data) throws WxErrorException {
int retryTimes = 0;
do {
@@ -681,9 +743,9 @@ public class WxMpServiceImpl implements WxMpService {
* -1 系统繁忙, 1000ms后重试
*/
if (error.getErrorCode() == -1) {
- int sleepMillis = retrySleepMillis * (1 << retryTimes);
+ int sleepMillis = this.retrySleepMillis * (1 << retryTimes);
try {
- log.debug("微信系统繁忙,{}ms 后重试(第{}次)", sleepMillis, retryTimes + 1);
+ this.log.debug("微信系统繁忙,{}ms 后重试(第{}次)", sleepMillis, retryTimes + 1);
Thread.sleep(sleepMillis);
} catch (InterruptedException e1) {
throw new RuntimeException(e1);
@@ -692,7 +754,7 @@ public class WxMpServiceImpl implements WxMpService {
throw e;
}
}
- } while (++retryTimes < maxRetryTimes);
+ } while (++retryTimes < this.maxRetryTimes);
throw new RuntimeException("微信服务端异常,超出重试次数");
}
@@ -707,7 +769,7 @@ public class WxMpServiceImpl implements WxMpService {
uriWithAccessToken += uri.indexOf('?') == -1 ? "?access_token=" + accessToken : "&access_token=" + accessToken;
try {
- return executor.execute(getHttpclient(), httpProxy, uriWithAccessToken, data);
+ return executor.execute(getHttpclient(), this.httpProxy, uriWithAccessToken, data);
} catch (WxErrorException e) {
WxError error = e.getError();
/*
@@ -717,7 +779,7 @@ public class WxMpServiceImpl implements WxMpService {
*/
if (error.getErrorCode() == 42001 || error.getErrorCode() == 40001) {
// 强制设置wxMpConfigStorage它的access token过期了,这样在下一次请求里就会刷新access token
- wxMpConfigStorage.expireAccessToken();
+ this.wxMpConfigStorage.expireAccessToken();
return execute(executor, uri, data);
}
if (error.getErrorCode() != 0) {
@@ -732,20 +794,21 @@ public class WxMpServiceImpl implements WxMpService {
}
protected CloseableHttpClient getHttpclient() {
- return httpClient;
+ return this.httpClient;
}
+ @Override
public void setWxMpConfigStorage(WxMpConfigStorage wxConfigProvider) {
this.wxMpConfigStorage = wxConfigProvider;
- ApacheHttpClientBuilder apacheHttpClientBuilder = wxMpConfigStorage.getApacheHttpClientBuilder();
+ ApacheHttpClientBuilder apacheHttpClientBuilder = this.wxMpConfigStorage.getApacheHttpClientBuilder();
if (null == apacheHttpClientBuilder) {
apacheHttpClientBuilder = DefaultApacheHttpHttpClientBuilder.get();
}
- apacheHttpClientBuilder.httpProxyHost(wxMpConfigStorage.getHttp_proxy_host())
- .httpProxyPort(wxMpConfigStorage.getHttp_proxy_port())
- .httpProxyUsername(wxMpConfigStorage.getHttp_proxy_username())
- .httpProxyPassword(wxMpConfigStorage.getHttp_proxy_password());
+ apacheHttpClientBuilder.httpProxyHost(this.wxMpConfigStorage.getHttp_proxy_host())
+ .httpProxyPort(this.wxMpConfigStorage.getHttp_proxy_port())
+ .httpProxyUsername(this.wxMpConfigStorage.getHttp_proxy_username())
+ .httpProxyPassword(this.wxMpConfigStorage.getHttp_proxy_password());
if (wxConfigProvider.getSSLContext() != null){
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
@@ -756,7 +819,7 @@ public class WxMpServiceImpl implements WxMpService {
apacheHttpClientBuilder.sslConnectionSocketFactory(sslsf);
}
- httpClient = apacheHttpClientBuilder.build();
+ this.httpClient = apacheHttpClientBuilder.build();
}
@Override
@@ -772,9 +835,9 @@ public class WxMpServiceImpl implements WxMpService {
@Override
public WxMpPrepayIdResult getPrepayId(String openId, String outTradeNo, double amt, String body, String tradeType, String ip, String callbackUrl) {
- Map packageParams = new HashMap();
- packageParams.put("appid", wxMpConfigStorage.getAppId());
- packageParams.put("mch_id", wxMpConfigStorage.getPartnerId());
+ Map packageParams = new HashMap<>();
+ packageParams.put("appid", this.wxMpConfigStorage.getAppId());
+ packageParams.put("mch_id", this.wxMpConfigStorage.getPartnerId());
packageParams.put("body", body);
packageParams.put("out_trade_no", outTradeNo);
packageParams.put("total_fee", (int) (amt * 100) + "");
@@ -786,16 +849,17 @@ public class WxMpServiceImpl implements WxMpService {
return getPrepayId(packageParams);
}
+ @Override
public WxMpPrepayIdResult getPrepayId(final Map parameters) {
String nonce_str = System.currentTimeMillis() + "";
- final SortedMap packageParams = new TreeMap(parameters);
- packageParams.put("appid", wxMpConfigStorage.getAppId());
- packageParams.put("mch_id", wxMpConfigStorage.getPartnerId());
+ final SortedMap packageParams = new TreeMap<>(parameters);
+ packageParams.put("appid", this.wxMpConfigStorage.getAppId());
+ packageParams.put("mch_id", this.wxMpConfigStorage.getPartnerId());
packageParams.put("nonce_str", nonce_str);
checkParameters(packageParams);
- String sign = WxCryptUtil.createSign(packageParams, wxMpConfigStorage.getPartnerKey());
+ String sign = WxCryptUtil.createSign(packageParams, this.wxMpConfigStorage.getPartnerKey());
packageParams.put("sign", sign);
StringBuilder request = new StringBuilder("");
@@ -805,8 +869,8 @@ public class WxMpServiceImpl implements WxMpService {
request.append("");
HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/pay/unifiedorder");
- if (httpProxy != null) {
- RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
+ if (this.httpProxy != null) {
+ RequestConfig config = RequestConfig.custom().setProxy(this.httpProxy).build();
httpPost.setConfig(config);
}
@@ -829,7 +893,7 @@ public class WxMpServiceImpl implements WxMpService {
"trade_type", };
private void checkParameters(Map parameters) {
- for (String para : REQUIRED_ORDER_PARAMETERS) {
+ for (String para : this.REQUIRED_ORDER_PARAMETERS) {
if (!parameters.containsKey(para))
throw new IllegalArgumentException("Reqiured argument '" + para + "' is missing.");
}
@@ -839,25 +903,40 @@ public class WxMpServiceImpl implements WxMpService {
throw new IllegalArgumentException("Reqiured argument 'product_id' is missing when trade_type is 'NATIVE'.");
}
+ @Override
+ public Map getJsapiPayInfo(String openId,String outTradeNo, double amt, String body,String ip, String callbackUrl) throws WxErrorException{
+ Map packageParams = new HashMap<>();
+ packageParams.put("appid", this.wxMpConfigStorage.getAppId());
+ packageParams.put("mch_id", this.wxMpConfigStorage.getPartnerId());
+ packageParams.put("body", body);
+ packageParams.put("out_trade_no", outTradeNo);
+ packageParams.put("total_fee", (int) (amt * 100) + "");
+ packageParams.put("spbill_create_ip", ip);
+ packageParams.put("notify_url", callbackUrl);
+ packageParams.put("trade_type", "JSAPI");
+ packageParams.put("openid", openId);
+
+ return getPayInfo(packageParams);
+ }
+
+ @Override
+ public Map getNativePayInfo(String productId,String outTradeNo, double amt, String body,String ip, String callbackUrl) throws WxErrorException{
+ Map packageParams = new HashMap<>();
+ packageParams.put("appid", this.wxMpConfigStorage.getAppId());
+ packageParams.put("mch_id", this.wxMpConfigStorage.getPartnerId());
+ packageParams.put("body", body);
+ packageParams.put("out_trade_no", outTradeNo);
+ packageParams.put("total_fee", (int) (amt * 100) + "");
+ packageParams.put("spbill_create_ip", ip);
+ packageParams.put("notify_url", callbackUrl);
+ packageParams.put("trade_type", "NATIVE");
+ packageParams.put("product_id", productId);
+
+ return getPayInfo(packageParams);
+ }
+
@Override
- public Map getJSSDKPayInfo(String openId, String outTradeNo, double amt, String body, String tradeType, String ip, String callbackUrl)
- throws WxErrorException {
- Map packageParams = new HashMap();
- packageParams.put("appid", wxMpConfigStorage.getAppId());
- packageParams.put("mch_id", wxMpConfigStorage.getPartnerId());
- packageParams.put("body", body);
- packageParams.put("out_trade_no", outTradeNo);
- packageParams.put("total_fee", String.format("%.0f", amt * 100));
- packageParams.put("spbill_create_ip", ip);
- packageParams.put("notify_url", callbackUrl);
- packageParams.put("trade_type", tradeType);
- packageParams.put("openid", openId);
-
- return getJSSDKPayInfo(packageParams);
- }
-
- @Override
- public Map getJSSDKPayInfo(Map parameters) throws WxErrorException {
+ public Map getPayInfo(Map parameters) throws WxErrorException {
WxMpPrepayIdResult wxMpPrepayIdResult = getPrepayId(parameters);
if (!"SUCCESS".equalsIgnoreCase(wxMpPrepayIdResult.getReturn_code())
@@ -877,16 +956,18 @@ public class WxMpServiceImpl implements WxMpService {
throw new RuntimeException(String.format("Failed to get prepay id due to error code '%s'(%s).", wxMpPrepayIdResult.getErr_code(), wxMpPrepayIdResult.getErr_code_des()));
}
- Map payInfo = new HashMap();
- payInfo.put("appId", wxMpConfigStorage.getAppId());
+ Map payInfo = new HashMap<>();
+ payInfo.put("appId", this.wxMpConfigStorage.getAppId());
// 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
payInfo.put("timeStamp", String.valueOf(System.currentTimeMillis() / 1000));
payInfo.put("nonceStr", System.currentTimeMillis() + "");
payInfo.put("package", "prepay_id=" + prepayId);
payInfo.put("signType", "MD5");
- payInfo.put("code_url",wxMpPrepayIdResult.getCode_url());
+ if("NATIVE".equals(parameters.get("trade_type"))){
+ payInfo.put("codeUrl", wxMpPrepayIdResult.getCode_url());
+ }
- String finalSign = WxCryptUtil.createSign(payInfo, wxMpConfigStorage.getPartnerKey());
+ String finalSign = WxCryptUtil.createSign(payInfo, this.wxMpConfigStorage.getPartnerKey());
payInfo.put("paySign", finalSign);
return payInfo;
}
@@ -895,9 +976,9 @@ public class WxMpServiceImpl implements WxMpService {
public WxMpPayResult getJSSDKPayResult(String transactionId, String outTradeNo) {
String nonce_str = System.currentTimeMillis() + "";
- SortedMap packageParams = new TreeMap();
- packageParams.put("appid", wxMpConfigStorage.getAppId());
- packageParams.put("mch_id", wxMpConfigStorage.getPartnerId());
+ SortedMap packageParams = new TreeMap<>();
+ packageParams.put("appid", this.wxMpConfigStorage.getAppId());
+ packageParams.put("mch_id", this.wxMpConfigStorage.getPartnerId());
if (transactionId != null && !"".equals(transactionId.trim()))
packageParams.put("transaction_id", transactionId);
else if (outTradeNo != null && !"".equals(outTradeNo.trim()))
@@ -905,7 +986,7 @@ public class WxMpServiceImpl implements WxMpService {
else
throw new IllegalArgumentException("Either 'transactionId' or 'outTradeNo' must be given.");
packageParams.put("nonce_str", nonce_str);
- packageParams.put("sign", WxCryptUtil.createSign(packageParams, wxMpConfigStorage.getPartnerKey()));
+ packageParams.put("sign", WxCryptUtil.createSign(packageParams, this.wxMpConfigStorage.getPartnerKey()));
StringBuilder request = new StringBuilder("");
for (Entry para : packageParams.entrySet()) {
@@ -914,14 +995,14 @@ public class WxMpServiceImpl implements WxMpService {
request.append("");
HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/pay/orderquery");
- if (httpProxy != null) {
- RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
+ if (this.httpProxy != null) {
+ RequestConfig config = RequestConfig.custom().setProxy(this.httpProxy).build();
httpPost.setConfig(config);
}
StringEntity entity = new StringEntity(request.toString(), Consts.UTF_8);
httpPost.setEntity(entity);
- try(CloseableHttpResponse response = httpClient.execute(httpPost)) {
+ try(CloseableHttpResponse response = this.httpClient.execute(httpPost)) {
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
XStream xstream = XStreamInitializer.getInstance();
xstream.alias("xml", WxMpPayResult.class);
@@ -947,12 +1028,12 @@ public class WxMpServiceImpl implements WxMpService {
@Override
public WxMpPayRefundResult refundPay(Map parameters) throws WxErrorException {
- SortedMap refundParams = new TreeMap(parameters);
- refundParams.put("appid", wxMpConfigStorage.getAppId());
- refundParams.put("mch_id", wxMpConfigStorage.getPartnerId());
+ SortedMap refundParams = new TreeMap<>(parameters);
+ refundParams.put("appid", this.wxMpConfigStorage.getAppId());
+ refundParams.put("mch_id", this.wxMpConfigStorage.getPartnerId());
refundParams.put("nonce_str", System.currentTimeMillis() + "");
- refundParams.put("op_user_id", wxMpConfigStorage.getPartnerId());
- String sign = WxCryptUtil.createSign(refundParams, wxMpConfigStorage.getPartnerKey());
+ refundParams.put("op_user_id", this.wxMpConfigStorage.getPartnerId());
+ String sign = WxCryptUtil.createSign(refundParams, this.wxMpConfigStorage.getPartnerKey());
refundParams.put("sign", sign);
StringBuilder request = new StringBuilder("");
@@ -962,8 +1043,8 @@ public class WxMpServiceImpl implements WxMpService {
request.append("");
HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/secapi/pay/refund");
- if (httpProxy != null) {
- RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
+ if (this.httpProxy != null) {
+ RequestConfig config = RequestConfig.custom().setProxy(this.httpProxy).build();
httpPost.setConfig(config);
}
@@ -990,7 +1071,7 @@ public class WxMpServiceImpl implements WxMpService {
return wxMpPayRefundResult;
} catch (IOException e) {
- log.error(MessageFormatter.format("The exception was happened when sending refund '{}'.", request.toString()).getMessage(), e);
+ this.log.error(MessageFormatter.format("The exception was happened when sending refund '{}'.", request.toString()).getMessage(), e);
WxError error = new WxError();
error.setErrorCode(-1);
error.setErrorMsg("incorrect response.");
@@ -1002,19 +1083,19 @@ public class WxMpServiceImpl implements WxMpService {
@Override
public boolean checkJSSDKCallbackDataSignature(Map kvm, String signature) {
- return signature.equals(WxCryptUtil.createSign(kvm, wxMpConfigStorage.getPartnerKey()));
+ return signature.equals(WxCryptUtil.createSign(kvm, this.wxMpConfigStorage.getPartnerKey()));
}
@Override
public WxRedpackResult sendRedpack(Map parameters) throws WxErrorException {
String nonce_str = System.currentTimeMillis() + "";
- SortedMap packageParams = new TreeMap(parameters);
- packageParams.put("wxappid", wxMpConfigStorage.getAppId());
- packageParams.put("mch_id", wxMpConfigStorage.getPartnerId());
+ SortedMap packageParams = new TreeMap<>(parameters);
+ packageParams.put("wxappid", this.wxMpConfigStorage.getAppId());
+ packageParams.put("mch_id", this.wxMpConfigStorage.getPartnerId());
packageParams.put("nonce_str", nonce_str);
- String sign = WxCryptUtil.createSign(packageParams, wxMpConfigStorage.getPartnerKey());
+ String sign = WxCryptUtil.createSign(packageParams, this.wxMpConfigStorage.getPartnerKey());
packageParams.put("sign", sign);
StringBuilder request = new StringBuilder("");
@@ -1024,8 +1105,8 @@ public class WxMpServiceImpl implements WxMpService {
request.append("");
HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack");
- if (httpProxy != null) {
- RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
+ if (this.httpProxy != null) {
+ RequestConfig config = RequestConfig.custom().setProxy(this.httpProxy).build();
httpPost.setConfig(config);
}
@@ -1038,7 +1119,7 @@ public class WxMpServiceImpl implements WxMpService {
WxRedpackResult wxMpRedpackResult = (WxRedpackResult) xstream.fromXML(responseContent);
return wxMpRedpackResult;
} catch (IOException e) {
- log.error(MessageFormatter.format("The exception was happened when sending redpack '{}'.", request.toString()).getMessage(), e);
+ this.log.error(MessageFormatter.format("The exception was happened when sending redpack '{}'.", request.toString()).getMessage(), e);
WxError error = new WxError();
error.setErrorCode(-1);
throw new WxErrorException(error);
@@ -1076,22 +1157,22 @@ public class WxMpServiceImpl implements WxMpService {
@Override
public String getCardApiTicket(boolean forceRefresh) throws WxErrorException {
if (forceRefresh) {
- wxMpConfigStorage.expireCardApiTicket();
+ this.wxMpConfigStorage.expireCardApiTicket();
}
- if (wxMpConfigStorage.isCardApiTicketExpired()) {
- synchronized (globalCardApiTicketRefreshLock) {
- if (wxMpConfigStorage.isCardApiTicketExpired()) {
+ if (this.wxMpConfigStorage.isCardApiTicketExpired()) {
+ synchronized (this.globalCardApiTicketRefreshLock) {
+ if (this.wxMpConfigStorage.isCardApiTicketExpired()) {
String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=wx_card";
String responseContent = execute(new SimpleGetRequestExecutor(), url, null);
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject();
String cardApiTicket = tmpJsonObject.get("ticket").getAsString();
int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt();
- wxMpConfigStorage.updateCardApiTicket(cardApiTicket, expiresInSeconds);
+ this.wxMpConfigStorage.updateCardApiTicket(cardApiTicket, expiresInSeconds);
}
}
}
- return wxMpConfigStorage.getCardApiTicket();
+ return this.wxMpConfigStorage.getCardApiTicket();
}
/**
@@ -1234,7 +1315,7 @@ public class WxMpServiceImpl implements WxMpService {
WxMpCardResult cardResult = WxMpGsonBuilder.INSTANCE.create().fromJson(tmpJsonElement,
new TypeToken() { }.getType());
if (!cardResult.getErrorCode().equals("0")) {
- log.warn("朋友的券mark失败:{}", cardResult.getErrorMsg());
+ this.log.warn("朋友的券mark失败:{}", cardResult.getErrorMsg());
}
}
@@ -1259,4 +1340,33 @@ public class WxMpServiceImpl implements WxMpService {
return responseContent;
}
+ @Override
+ public WxMpMassSendResult massMessagePreview(WxMpMassPreviewMessage wxMpMassPreviewMessage) throws Exception {
+ String url = "https://api.weixin.qq.com/cgi-bin/message/mass/preview";
+ String responseContent = execute(new SimplePostRequestExecutor(), url, wxMpMassPreviewMessage.toJson());
+ return WxMpMassSendResult.fromJson(responseContent);
+ }
+
+ @Override
+ public WxMediaImgUploadResult mediaImgUpload(File file) throws WxErrorException {
+ String url = "https://api.weixin.qq.com/cgi-bin/media/uploadimg";
+ return execute(new MediaImgUploadRequestExecutor(), url, file);
+ }
+
+ @Override
+ public String setIndustry(WxMpIndustry wxMpIndustry) throws WxErrorException {
+ if (null == wxMpIndustry.getPrimaryIndustry() || null == wxMpIndustry.getPrimaryIndustry().getId()
+ || null == wxMpIndustry.getSecondIndustry() || null == wxMpIndustry.getSecondIndustry().getId()) {
+ throw new IllegalArgumentException("industry id is empty");
+ }
+ String url = "https://api.weixin.qq.com/cgi-bin/template/api_set_industry";
+ return execute(new SimplePostRequestExecutor(), url, wxMpIndustry.toJson());
+ }
+
+ @Override
+ public WxMpIndustry getIndustry() throws WxErrorException {
+ String url = "https://api.weixin.qq.com/cgi-bin/template/get_industry";
+ String responseContent = execute(new SimpleGetRequestExecutor(), url, null);
+ return WxMpIndustry.fromJson(responseContent);
+ }
}
diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/Industry.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/Industry.java
new file mode 100644
index 000000000..1270e7f5c
--- /dev/null
+++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/Industry.java
@@ -0,0 +1,38 @@
+package me.chanjar.weixin.mp.bean;
+
+import java.io.Serializable;
+
+/**
+ * @author miller
+ * 官方文档中,创建和获取的数据结构不一样。所以采用冗余字段的方式,实现相应的接口
+ */
+public class Industry implements Serializable {
+ private static final long serialVersionUID = -1707184885588012142L;
+ private String id;
+ private String firstClass;
+ private String secondClass;
+
+ public String getId() {
+ return id;
+ }
+
+ public void setId(String id) {
+ this.id = id;
+ }
+
+ public String getFirstClass() {
+ return firstClass;
+ }
+
+ public void setFirstClass(String firstClass) {
+ this.firstClass = firstClass;
+ }
+
+ public String getSecondClass() {
+ return secondClass;
+ }
+
+ public void setSecondClass(String secondClass) {
+ this.secondClass = secondClass;
+ }
+}
diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/WxMpIndustry.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/WxMpIndustry.java
new file mode 100644
index 000000000..be62084de
--- /dev/null
+++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/WxMpIndustry.java
@@ -0,0 +1,39 @@
+package me.chanjar.weixin.mp.bean;
+
+
+import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
+
+import java.io.Serializable;
+
+/**
+ * @author miller
+ */
+public class WxMpIndustry implements Serializable {
+ private static final long serialVersionUID = -7700398224795914722L;
+ private Industry primaryIndustry;
+ private Industry secondIndustry;
+
+ public static WxMpIndustry fromJson(String json) {
+ return WxMpGsonBuilder.create().fromJson(json, WxMpIndustry.class);
+ }
+
+ public String toJson() {
+ return WxMpGsonBuilder.create().toJson(this);
+ }
+
+ public Industry getPrimaryIndustry() {
+ return primaryIndustry;
+ }
+
+ public void setPrimaryIndustry(Industry primaryIndustry) {
+ this.primaryIndustry = primaryIndustry;
+ }
+
+ public Industry getSecondIndustry() {
+ return secondIndustry;
+ }
+
+ public void setSecondIndustry(Industry secondIndustry) {
+ this.secondIndustry = secondIndustry;
+ }
+}
diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/WxMpMassPreviewMessage.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/WxMpMassPreviewMessage.java
new file mode 100644
index 000000000..154195dbb
--- /dev/null
+++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/WxMpMassPreviewMessage.java
@@ -0,0 +1,68 @@
+package me.chanjar.weixin.mp.bean;
+
+import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
+
+import java.io.Serializable;
+
+/**
+ * @author miller
+ */
+public class WxMpMassPreviewMessage implements Serializable {
+ private String toWxUsername;
+ private String msgType;
+ private String content;
+ private String mediaId;
+
+ public WxMpMassPreviewMessage() {
+ super();
+ }
+
+ public String getToWxUsername() {
+ return toWxUsername;
+ }
+
+ public void setToWxUsername(String toWxUsername) {
+ this.toWxUsername = toWxUsername;
+ }
+
+ public String getMsgType() {
+ return msgType;
+ }
+
+ /**
+ *
+ * 请使用
+ * {@link me.chanjar.weixin.common.api.WxConsts#MASS_MSG_IMAGE}
+ * {@link me.chanjar.weixin.common.api.WxConsts#MASS_MSG_NEWS}
+ * {@link me.chanjar.weixin.common.api.WxConsts#MASS_MSG_TEXT}
+ * {@link me.chanjar.weixin.common.api.WxConsts#MASS_MSG_VIDEO}
+ * {@link me.chanjar.weixin.common.api.WxConsts#MASS_MSG_VOICE}
+ * 如果msgtype和media_id不匹配的话,会返回系统繁忙的错误
+ *
+ *
+ * @param msgType
+ */
+ public void setMsgType(String msgType) {
+ this.msgType = msgType;
+ }
+
+ public String getContent() {
+ return content;
+ }
+
+ public void setContent(String content) {
+ this.content = content;
+ }
+
+ public String getMediaId() {
+ return mediaId;
+ }
+
+ public void setMediaId(String mediaId) {
+ this.mediaId = mediaId;
+ }
+
+ public String toJson() {
+ return WxMpGsonBuilder.INSTANCE.create().toJson(this);
+ }
+}
diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/WxMpTemplateMessage.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/WxMpTemplateMessage.java
index aa061e3b2..dd822ba52 100644
--- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/WxMpTemplateMessage.java
+++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/WxMpTemplateMessage.java
@@ -12,7 +12,7 @@ public class WxMpTemplateMessage implements Serializable {
private String templateId;
private String url;
private String topColor;
- private List datas = new ArrayList();
+ private List data = new ArrayList();
public String getToUser() {
return toUser;
@@ -46,12 +46,16 @@ public class WxMpTemplateMessage implements Serializable {
this.topColor = topColor;
}
- public List getDatas() {
- return datas;
+ public List getData() {
+ return data;
}
- public void setDatas(List datas) {
- this.datas = datas;
+ public void setData(List data) {
+ this.data = data;
+ }
+
+ public void addWxMpTemplateData(WxMpTemplateData datum) {
+ this.data.add(datum);
}
public String toJson() {
diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/result/WxMediaImgUploadResult.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/result/WxMediaImgUploadResult.java
new file mode 100644
index 000000000..3e5ff6b2f
--- /dev/null
+++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/result/WxMediaImgUploadResult.java
@@ -0,0 +1,24 @@
+package me.chanjar.weixin.mp.bean.result;
+
+import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
+
+import java.io.Serializable;
+
+/**
+ * @author miller
+ */
+public class WxMediaImgUploadResult implements Serializable {
+ private String url;
+
+ public static WxMediaImgUploadResult fromJson(String json) {
+ return WxMpGsonBuilder.create().fromJson(json, WxMediaImgUploadResult.class);
+ }
+
+ public String getUrl() {
+ return url;
+ }
+
+ public void setUrl(String url) {
+ this.url = url;
+ }
+}
diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/result/WxMpMassSendResult.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/result/WxMpMassSendResult.java
index adf6ebbc5..dd12a53a9 100644
--- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/result/WxMpMassSendResult.java
+++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/result/WxMpMassSendResult.java
@@ -20,6 +20,7 @@ public class WxMpMassSendResult implements Serializable {
private String errorCode;
private String errorMsg;
private String msgId;
+ private String msgDataId;
public String getErrorCode() {
return errorCode;
@@ -44,7 +45,15 @@ public class WxMpMassSendResult implements Serializable {
public void setMsgId(String msgId) {
this.msgId = msgId;
}
-
+
+ public String getMsgDataId() {
+ return msgDataId;
+ }
+
+ public void setMsgDataId(String msgDataId) {
+ this.msgDataId = msgDataId;
+ }
+
public static WxMpMassSendResult fromJson(String json) {
return WxMpGsonBuilder.create().fromJson(json, WxMpMassSendResult.class);
}
diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialUploadRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialUploadRequestExecutor.java
index 5e90c07a3..2491e0ab1 100644
--- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialUploadRequestExecutor.java
+++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialUploadRequestExecutor.java
@@ -30,23 +30,27 @@ public class MaterialUploadRequestExecutor implements RequestExecutor form = material.getForm();
- if (material.getForm() != null) {
- multipartEntityBuilder.addTextBody("description", WxGsonBuilder.create().toJson(form));
- }
- httpPost.setEntity(multipartEntityBuilder.build());
- httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString());
+ if (material == null) {
+ throw new WxErrorException(WxError.newBuilder().setErrorMsg("非法请求,material参数为空").build());
}
+ File file = material.getFile();
+ if (file == null || !file.exists()) {
+ throw new FileNotFoundException();
+ }
+
+ MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
+ multipartEntityBuilder
+ .addBinaryBody("media", file)
+ .setMode(HttpMultipartMode.RFC6532);
+ Map form = material.getForm();
+ if (material.getForm() != null) {
+ multipartEntityBuilder.addTextBody("description", WxGsonBuilder.create().toJson(form));
+ }
+
+ httpPost.setEntity(multipartEntityBuilder.build());
+ httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString());
+
try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
WxError error = WxError.fromJson(responseContent);
@@ -55,7 +59,7 @@ public class MaterialUploadRequestExecutor implements RequestExecutor {
+ @Override
+ public WxMediaImgUploadResult execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, File data) throws WxErrorException, ClientProtocolException, IOException {
+ HttpPost httpPost = new HttpPost(uri);
+ if (httpProxy != null) {
+ RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
+ httpPost.setConfig(config);
+ }
+ if (data != null) {
+ HttpEntity entity = MultipartEntityBuilder
+ .create()
+ .addBinaryBody("media", data)
+ .setMode(HttpMultipartMode.RFC6532)
+ .build();
+ httpPost.setEntity(entity);
+ httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString());
+ }
+ try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
+ String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
+ WxError error = WxError.fromJson(responseContent);
+ if (error.getErrorCode() != 0) {
+ throw new WxErrorException(error);
+ }
+ return WxMediaImgUploadResult.fromJson(responseContent);
+ }
+ }
+}
\ No newline at end of file
diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/QrCodeRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/QrCodeRequestExecutor.java
index bde25cfbb..a643c68da 100644
--- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/QrCodeRequestExecutor.java
+++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/QrCodeRequestExecutor.java
@@ -30,15 +30,15 @@ import java.util.UUID;
public class QrCodeRequestExecutor implements RequestExecutor {
@Override
- public File execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, WxMpQrCodeTicket ticket) throws WxErrorException, ClientProtocolException, IOException {
+ public File execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri,
+ WxMpQrCodeTicket ticket) throws WxErrorException, ClientProtocolException, IOException {
if (ticket != null) {
if (uri.indexOf('?') == -1) {
uri += '?';
}
- uri += uri.endsWith("?") ?
- "ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8")
- :
- "&ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8");
+ uri += uri.endsWith("?")
+ ? "ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8")
+ : "&ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8");
}
HttpGet httpGet = new HttpGet(uri);
@@ -59,7 +59,7 @@ public class QrCodeRequestExecutor implements RequestExecutor {
+ @Override
+ public WxMediaImgUploadResult deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
+ WxMediaImgUploadResult wxMediaImgUploadResult = new WxMediaImgUploadResult();
+ JsonObject jsonObject = json.getAsJsonObject();
+ if (null != jsonObject.get("url") && !jsonObject.get("url").isJsonNull()) {
+ wxMediaImgUploadResult.setUrl(GsonHelper.getAsString(jsonObject.get("url")));
+ }
+ return wxMediaImgUploadResult;
+ }
+}
diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpCardGsonAdapter.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpCardGsonAdapter.java
index dab414a3e..331485778 100644
--- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpCardGsonAdapter.java
+++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpCardGsonAdapter.java
@@ -1,18 +1,15 @@
package me.chanjar.weixin.mp.util.json;
+import java.lang.reflect.Type;
+
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
-import com.google.gson.reflect.TypeToken;
+
import me.chanjar.weixin.common.util.json.GsonHelper;
import me.chanjar.weixin.mp.bean.WxMpCard;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.lang.reflect.Type;
-import java.util.List;
/**
* Created by YuJian on 15/11/11.
diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpCardResultGsonAdapter.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpCardResultGsonAdapter.java
index be1a12da3..f595cf7c8 100644
--- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpCardResultGsonAdapter.java
+++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpCardResultGsonAdapter.java
@@ -1,20 +1,17 @@
package me.chanjar.weixin.mp.util.json;
+import java.lang.reflect.Type;
+
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.reflect.TypeToken;
+
import me.chanjar.weixin.common.util.json.GsonHelper;
import me.chanjar.weixin.mp.bean.WxMpCard;
import me.chanjar.weixin.mp.bean.result.WxMpCardResult;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.lang.reflect.Type;
-import java.text.ParseException;
-import java.util.List;
/**
* Created by YuJian on 15/11/11.
diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpGsonBuilder.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpGsonBuilder.java
index 316f22f54..34570b30e 100644
--- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpGsonBuilder.java
+++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpGsonBuilder.java
@@ -40,6 +40,9 @@ public class WxMpGsonBuilder {
INSTANCE.registerTypeAdapter(WxMpMaterialFileBatchGetResult.WxMaterialFileBatchGetNewsItem.class, new WxMpMaterialFileBatchGetGsonItemAdapter());
INSTANCE.registerTypeAdapter(WxMpCardResult.class, new WxMpCardResultGsonAdapter());
INSTANCE.registerTypeAdapter(WxMpCard.class, new WxMpCardGsonAdapter());
+ INSTANCE.registerTypeAdapter(WxMpMassPreviewMessage.class, new WxMpMassPreviewMessageGsonAdapter());
+ INSTANCE.registerTypeAdapter(WxMediaImgUploadResult.class, new WxMediaImgUploadResultGsonAdapter());
+ INSTANCE.registerTypeAdapter(WxMpIndustry.class, new WxMpIndustryGsonAdapter());
}
public static Gson create() {
diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpIndustryGsonAdapter.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpIndustryGsonAdapter.java
new file mode 100644
index 000000000..79fb5f0bb
--- /dev/null
+++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpIndustryGsonAdapter.java
@@ -0,0 +1,44 @@
+package me.chanjar.weixin.mp.util.json;
+
+import com.google.gson.JsonDeserializationContext;
+import com.google.gson.JsonDeserializer;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParseException;
+import com.google.gson.JsonSerializationContext;
+import com.google.gson.JsonSerializer;
+import me.chanjar.weixin.common.util.json.GsonHelper;
+import me.chanjar.weixin.mp.bean.Industry;
+import me.chanjar.weixin.mp.bean.WxMpIndustry;
+
+import java.lang.reflect.Type;
+
+/**
+ * @author miller
+ */
+public class WxMpIndustryGsonAdapter implements JsonSerializer, JsonDeserializer {
+ @Override
+ public JsonElement serialize(WxMpIndustry wxMpIndustry, Type type, JsonSerializationContext jsonSerializationContext) {
+ JsonObject json = new JsonObject();
+ json.addProperty("industry_id1", wxMpIndustry.getPrimaryIndustry().getId());
+ json.addProperty("industry_id2", wxMpIndustry.getSecondIndustry().getId());
+ return json;
+ }
+
+ @Override
+ public WxMpIndustry deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
+ WxMpIndustry wxMpIndustry = new WxMpIndustry();
+ JsonObject primaryIndustry = jsonElement.getAsJsonObject().get("primary_industry").getAsJsonObject();
+ wxMpIndustry.setPrimaryIndustry(convertFromJson(primaryIndustry));
+ JsonObject secondaryIndustry = jsonElement.getAsJsonObject().get("secondary_industry").getAsJsonObject();
+ wxMpIndustry.setSecondIndustry(convertFromJson(secondaryIndustry));
+ return wxMpIndustry;
+ }
+
+ private Industry convertFromJson(JsonObject json) {
+ Industry industry = new Industry();
+ industry.setFirstClass(GsonHelper.getString(json, "first_class"));
+ industry.setSecondClass(GsonHelper.getString(json, "second_class"));
+ return industry;
+ }
+}
diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpMassPreviewMessageGsonAdapter.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpMassPreviewMessageGsonAdapter.java
new file mode 100644
index 000000000..fd3e52df3
--- /dev/null
+++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpMassPreviewMessageGsonAdapter.java
@@ -0,0 +1,48 @@
+package me.chanjar.weixin.mp.util.json;
+
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonSerializationContext;
+import com.google.gson.JsonSerializer;
+import me.chanjar.weixin.common.api.WxConsts;
+import me.chanjar.weixin.mp.bean.WxMpMassPreviewMessage;
+
+import java.lang.reflect.Type;
+
+/**
+ * @author miller
+ */
+public class WxMpMassPreviewMessageGsonAdapter implements JsonSerializer {
+ @Override
+ public JsonElement serialize(WxMpMassPreviewMessage wxMpMassPreviewMessage, Type type, JsonSerializationContext jsonSerializationContext) {
+ JsonObject jsonObject = new JsonObject();
+ jsonObject.addProperty("towxname", wxMpMassPreviewMessage.getToWxUsername());
+ if (WxConsts.MASS_MSG_NEWS.equals(wxMpMassPreviewMessage.getMsgType())) {
+ JsonObject news = new JsonObject();
+ news.addProperty("media_id", wxMpMassPreviewMessage.getMediaId());
+ jsonObject.add(WxConsts.MASS_MSG_NEWS, news);
+ }
+ if (WxConsts.MASS_MSG_TEXT.equals(wxMpMassPreviewMessage.getMsgType())) {
+ JsonObject sub = new JsonObject();
+ sub.addProperty("content", wxMpMassPreviewMessage.getContent());
+ jsonObject.add(WxConsts.MASS_MSG_TEXT, sub);
+ }
+ if (WxConsts.MASS_MSG_VOICE.equals(wxMpMassPreviewMessage.getMsgType())) {
+ JsonObject sub = new JsonObject();
+ sub.addProperty("media_id", wxMpMassPreviewMessage.getMediaId());
+ jsonObject.add(WxConsts.MASS_MSG_VOICE, sub);
+ }
+ if (WxConsts.MASS_MSG_IMAGE.equals(wxMpMassPreviewMessage.getMsgType())) {
+ JsonObject sub = new JsonObject();
+ sub.addProperty("media_id", wxMpMassPreviewMessage.getMediaId());
+ jsonObject.add(WxConsts.MASS_MSG_IMAGE, sub);
+ }
+ if (WxConsts.MASS_MSG_VIDEO.equals(wxMpMassPreviewMessage.getMsgType())) {
+ JsonObject sub = new JsonObject();
+ sub.addProperty("media_id", wxMpMassPreviewMessage.getMediaId());
+ jsonObject.add(WxConsts.MASS_MSG_VIDEO, sub);
+ }
+ jsonObject.addProperty("msgtype", wxMpMassPreviewMessage.getMsgType());
+ return jsonObject;
+ }
+}
diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpMassSendResultAdapter.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpMassSendResultAdapter.java
index 99fc02b89..658ac358f 100644
--- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpMassSendResultAdapter.java
+++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpMassSendResultAdapter.java
@@ -34,6 +34,9 @@ public class WxMpMassSendResultAdapter implements JsonDeserializer media_ids = new ArrayList();
+ private List media_ids = new ArrayList<>();
@Test(dataProvider="uploadMedia")
public void testUploadMedia(String mediaType, String fileType, String fileName) throws WxErrorException, IOException {
- InputStream inputStream = ClassLoader.getSystemResourceAsStream(fileName);
- WxMediaUploadResult res = wxService.mediaUpload(mediaType, fileType, inputStream);
- Assert.assertNotNull(res.getType());
- Assert.assertNotNull(res.getCreatedAt());
- Assert.assertTrue(res.getMediaId() != null || res.getThumbMediaId() != null);
-
- if (res.getMediaId() != null) {
- media_ids.add(res.getMediaId());
- }
- if (res.getThumbMediaId() != null) {
- media_ids.add(res.getThumbMediaId());
+ try(InputStream inputStream = ClassLoader.getSystemResourceAsStream(fileName)){
+ WxMediaUploadResult res = this.wxService.mediaUpload(mediaType, fileType, inputStream);
+ Assert.assertNotNull(res.getType());
+ Assert.assertNotNull(res.getCreatedAt());
+ Assert.assertTrue(res.getMediaId() != null || res.getThumbMediaId() != null);
+
+ if (res.getMediaId() != null) {
+ this.media_ids.add(res.getMediaId());
+ }
+
+ if (res.getThumbMediaId() != null) {
+ this.media_ids.add(res.getThumbMediaId());
+ }
}
}
@@ -56,7 +58,7 @@ public class WxMpMediaAPITest {
@Test(dependsOnMethods = { "testUploadMedia" }, dataProvider="downloadMedia")
public void testDownloadMedia(String media_id) throws WxErrorException {
- wxService.mediaDownload(media_id);
+ this.wxService.mediaDownload(media_id);
}
@DataProvider
diff --git a/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/WxMpQrCodeAPITest.java b/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/WxMpQrCodeAPITest.java
index 1fa76e493..7af300c9c 100644
--- a/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/WxMpQrCodeAPITest.java
+++ b/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/WxMpQrCodeAPITest.java
@@ -22,23 +22,29 @@ public class WxMpQrCodeAPITest {
protected WxMpServiceImpl wxService;
public void testQrCodeCreateTmpTicket() throws WxErrorException {
- WxMpQrCodeTicket ticket = wxService.qrCodeCreateTmpTicket(1, null);
+ WxMpQrCodeTicket ticket = this.wxService.qrCodeCreateTmpTicket(1, null);
Assert.assertNotNull(ticket.getUrl());
Assert.assertNotNull(ticket.getTicket());
Assert.assertTrue(ticket.getExpire_seconds() != -1);
}
public void testQrCodeCreateLastTicket() throws WxErrorException {
- WxMpQrCodeTicket ticket = wxService.qrCodeCreateLastTicket(1);
+ WxMpQrCodeTicket ticket = this.wxService.qrCodeCreateLastTicket(1);
Assert.assertNotNull(ticket.getUrl());
Assert.assertNotNull(ticket.getTicket());
Assert.assertTrue(ticket.getExpire_seconds() == -1);
}
public void testQrCodePicture() throws WxErrorException {
- WxMpQrCodeTicket ticket = wxService.qrCodeCreateLastTicket(1);
- File file = wxService.qrCodePicture(ticket);
+ WxMpQrCodeTicket ticket = this.wxService.qrCodeCreateLastTicket(1);
+ File file = this.wxService.qrCodePicture(ticket);
Assert.assertNotNull(file);
}
+
+ public void testQrCodePictureUrl() throws WxErrorException {
+ WxMpQrCodeTicket ticket = this.wxService.qrCodeCreateLastTicket(1);
+ String url = this.wxService.qrCodePictureUrl(ticket.getTicket());
+ Assert.assertNotNull(url);
+ }
}