瀏覽代碼

mp usage jodd-http

master
ecoolper 8 年之前
父節點
當前提交
d476047c4c
共有 37 個檔案被更改,包括 1772 行新增118 行删除
  1. +8
    -2
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpCardService.java
  2. +1
    -1
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpConfigStorage.java
  3. +1
    -1
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpInMemoryConfigStorage.java
  4. +24
    -8
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java
  5. +1
    -1
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpStoreService.java
  6. +1
    -1
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpDeviceServiceImpl.java
  7. +3
    -5
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpUserTagServiceImpl.java
  8. +251
    -0
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/apache/WxMpCardServiceImpl.java
  9. +12
    -17
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/apache/WxMpKefuServiceImpl.java
  10. +7
    -5
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/apache/WxMpMaterialServiceImpl.java
  11. +120
    -0
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/apache/WxMpQrcodeServiceImpl.java
  12. +17
    -9
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/apache/WxMpServiceImpl.java
  13. +52
    -0
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/apache/WxMpUserBlacklistServiceImpl.java
  14. +26
    -17
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/jodd/WxMpCardServiceImpl.java
  15. +183
    -0
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/jodd/WxMpKefuServiceImpl.java
  16. +165
    -0
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/jodd/WxMpMaterialServiceImpl.java
  17. +5
    -3
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/jodd/WxMpQrcodeServiceImpl.java
  18. +510
    -0
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/jodd/WxMpServiceImpl.java
  19. +5
    -3
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/jodd/WxMpUserBlacklistServiceImpl.java
  20. +3
    -3
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/apache/MaterialDeleteRequestExecutor.java
  21. +3
    -3
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/apache/MaterialNewsInfoRequestExecutor.java
  22. +6
    -4
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/apache/MaterialUploadRequestExecutor.java
  23. +3
    -3
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/apache/MaterialVideoInfoRequestExecutor.java
  24. +13
    -14
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/apache/MaterialVoiceAndImageDownloadRequestExecutor.java
  25. +3
    -3
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/apache/MediaImgUploadRequestExecutor.java
  26. +8
    -8
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/apache/QrCodeRequestExecutor.java
  27. +39
    -0
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/jodd/MaterialDeleteRequestExecutor.java
  28. +41
    -0
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/jodd/MaterialNewsInfoRequestExecutor.java
  29. +53
    -0
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/jodd/MaterialUploadRequestExecutor.java
  30. +39
    -0
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/jodd/MaterialVideoInfoRequestExecutor.java
  31. +57
    -0
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/jodd/MaterialVoiceAndImageDownloadRequestExecutor.java
  32. +42
    -0
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/jodd/MediaImgUploadRequestExecutor.java
  33. +60
    -0
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/jodd/QrCodeRequestExecutor.java
  34. +7
    -4
      weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/WxMpBusyRetryTest.java
  35. +1
    -1
      weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpStoreServiceImplTest.java
  36. +1
    -1
      weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/test/ApiTestModule.java
  37. +1
    -1
      weixin-java-mp/src/test/java/me/chanjar/weixin/mp/demo/WxMpDemoServer.java

+ 8
- 2
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpCardService.java 查看文件

@@ -6,9 +6,15 @@ import me.chanjar.weixin.mp.bean.result.WxMpCardResult;

/**
* 卡券相关接口
* @author YuJian(mgcnrx11@hotmail.com) on 01/11/2016
* @author YuJian(mgcnrx11@hotmail.com) on 01/11/2016
*/
public interface WxMpCardService {
public interface WxMpCardService<H, P> {

/**
* 得到WxMpService
* @return
*/
WxMpService<H, P> getWxMpService();

/**
* 获得卡券api_ticket,不强制刷新卡券api_ticket


+ 1
- 1
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpConfigStorage.java 查看文件

@@ -1,7 +1,7 @@
package me.chanjar.weixin.mp.api;

import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.common.util.http.ApacheHttpClientBuilder;
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;

import java.io.File;
import java.util.concurrent.locks.Lock;


+ 1
- 1
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpInMemoryConfigStorage.java 查看文件

@@ -2,7 +2,7 @@ package me.chanjar.weixin.mp.api;

import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.common.util.ToStringUtils;
import me.chanjar.weixin.common.util.http.ApacheHttpClientBuilder;
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;

import java.io.File;
import java.util.concurrent.locks.Lock;


+ 24
- 8
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java 查看文件

@@ -3,14 +3,14 @@ package me.chanjar.weixin.mp.api;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.common.util.http.apache.MediaUploadRequestExecutor;
import me.chanjar.weixin.mp.bean.*;
import me.chanjar.weixin.mp.bean.result.*;
import org.apache.http.HttpHost;

/**
* 微信API的Service
*/
public interface WxMpService {
public interface WxMpService<H, P> {

/**
* <pre>
@@ -224,15 +224,18 @@ public interface WxMpService {
* <pre>
* Service没有实现某个API的时候,可以用这个,
* 比{@link #get}和{@link #post}方法更灵活,可以自己构造RequestExecutor用来处理不同的参数和不同的返回类型。
* 可以参考,{@link me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor}的实现方法
* 可以参考,{@link MediaUploadRequestExecutor}的实现方法
* </pre>
*/
<T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException;
<T, E> T execute(RequestExecutor<T, H, P, E> executor, String uri, E data) throws WxErrorException;

/**
* 获取代理对象
*/
HttpHost getHttpProxy();
<T, E> T executeInternal(RequestExecutor<T, H, P, E> executor, String uri, E data) throws WxErrorException;


/**
* 获取代理对象
*/
//HttpHost getHttpProxy();

/**
* 注入 {@link WxMpConfigStorage} 的实现
@@ -345,4 +348,17 @@ public interface WxMpService {
* @return WxMpDeviceService
*/
WxMpDeviceService getDeviceService();

/**
*
* @return
*/
H getHttpclient();

/**
*
* @return
*/
P getHttpProxy();

}

+ 1
- 1
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpStoreService.java 查看文件

@@ -12,7 +12,7 @@ import java.util.List;
* @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a>
* Created by Binary Wang on 2016-09-23.
*/
public interface WxMpStoreService {
public interface WxMpStoreService<H,P> {
/**
* <pre>
* 创建门店


+ 1
- 1
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpDeviceServiceImpl.java 查看文件

@@ -16,7 +16,7 @@ public class WxMpDeviceServiceImpl implements WxMpDeviceService {

private WxMpService wxMpService;

WxMpDeviceServiceImpl(WxMpService wxMpService) {
public WxMpDeviceServiceImpl(WxMpService wxMpService) {
this.wxMpService = wxMpService;
}



+ 3
- 5
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpUserTagServiceImpl.java 查看文件

@@ -1,14 +1,9 @@
package me.chanjar.weixin.mp.api.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;

import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
@@ -16,6 +11,9 @@ import me.chanjar.weixin.mp.api.WxMpUserTagService;
import me.chanjar.weixin.mp.bean.tag.WxTagListUser;
import me.chanjar.weixin.mp.bean.tag.WxUserTag;
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
*


+ 251
- 0
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/apache/WxMpCardServiceImpl.java 查看文件

@@ -0,0 +1,251 @@
package me.chanjar.weixin.mp.api.impl.apache;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import com.google.gson.reflect.TypeToken;
import me.chanjar.weixin.common.bean.WxCardApiSignature;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.RandomUtils;
import me.chanjar.weixin.common.util.crypto.SHA1;
import me.chanjar.weixin.common.util.http.apache.SimpleGetRequestExecutor;
import me.chanjar.weixin.mp.api.WxMpCardService;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpCardResult;
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
import org.apache.http.HttpHost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.concurrent.locks.Lock;

/**
* Created by Binary Wang on 2016/7/27.
*/
public class WxMpCardServiceImpl implements WxMpCardService<CloseableHttpClient, HttpHost> {

private final Logger log = LoggerFactory.getLogger(WxMpCardServiceImpl.class);

private WxMpService<CloseableHttpClient, HttpHost> wxMpService;

public WxMpCardServiceImpl(WxMpService wxMpService) {
this.wxMpService = wxMpService;
}

/**
* 得到WxMpService
* @return
*/
@Override
public WxMpService<CloseableHttpClient, HttpHost> getWxMpService(){
return this.wxMpService;
}

/**
* 获得卡券api_ticket,不强制刷新卡券api_ticket
*
* @return 卡券api_ticket
* @see #getCardApiTicket(boolean)
*/
@Override
public String getCardApiTicket() throws WxErrorException {
return getCardApiTicket(false);
}

/**
* <pre>
* 获得卡券api_ticket
* 获得时会检查卡券apiToken是否过期,如果过期了,那么就刷新一下,否则就什么都不干
*
* 详情请见:http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#.E9.99.84.E5.BD
* .954-.E5.8D.A1.E5.88.B8.E6.89.A9.E5.B1.95.E5.AD.97.E6.AE.B5.E5.8F.8A.E7.AD.BE.E5.90.8D.E7.94
* .9F.E6.88.90.E7.AE.97.E6.B3.95
* </pre>
*
* @param forceRefresh 强制刷新
* @return 卡券api_ticket
*/
@Override
public String getCardApiTicket(boolean forceRefresh) throws WxErrorException {
Lock lock = getWxMpService().getWxMpConfigStorage().getCardApiTicketLock();
try {
lock.lock();

if (forceRefresh) {
this.getWxMpService().getWxMpConfigStorage().expireCardApiTicket();
}

if (this.getWxMpService().getWxMpConfigStorage().isCardApiTicketExpired()) {
String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=wx_card";
String responseContent = this.wxMpService.execute(new SimpleGetRequestExecutor(), url, null);
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject();
String cardApiTicket = tmpJsonObject.get("ticket").getAsString();
int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt();
this.getWxMpService().getWxMpConfigStorage().updateCardApiTicket(cardApiTicket, expiresInSeconds);
}
} finally {
lock.unlock();
}
return this.getWxMpService().getWxMpConfigStorage().getCardApiTicket();
}

/**
* <pre>
* 创建调用卡券api时所需要的签名
*
* 详情请见:http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#.E9.99.84.E5.BD
* .954-.E5.8D.A1.E5.88.B8.E6.89.A9.E5.B1.95.E5.AD.97.E6.AE.B5.E5.8F.8A.E7.AD.BE.E5.90.8D.E7.94
* .9F.E6.88.90.E7.AE.97.E6.B3.95
* </pre>
*
* @param optionalSignParam 参与签名的参数数组。
* 可以为下列字段:app_id, card_id, card_type, code, openid, location_id
* </br>注意:当做wx.chooseCard调用时,必须传入app_id参与签名,否则会造成签名失败导致拉取卡券列表为空
* @return 卡券Api签名对象
*/
@Override
public WxCardApiSignature createCardApiSignature(String... optionalSignParam) throws
WxErrorException {
long timestamp = System.currentTimeMillis() / 1000;
String nonceStr = RandomUtils.getRandomStr();
String cardApiTicket = getCardApiTicket(false);

String[] signParam = Arrays.copyOf(optionalSignParam, optionalSignParam.length + 3);
signParam[optionalSignParam.length] = String.valueOf(timestamp);
signParam[optionalSignParam.length + 1] = nonceStr;
signParam[optionalSignParam.length + 2] = cardApiTicket;
String signature = SHA1.gen(signParam);
WxCardApiSignature cardApiSignature = new WxCardApiSignature();
cardApiSignature.setTimestamp(timestamp);
cardApiSignature.setNonceStr(nonceStr);
cardApiSignature.setSignature(signature);
return cardApiSignature;
}

/**
* 卡券Code解码
*
* @param encryptCode 加密Code,通过JSSDK的chooseCard接口获得
* @return 解密后的Code
*/
@Override
public String decryptCardCode(String encryptCode) throws WxErrorException {
String url = "https://api.weixin.qq.com/card/code/decrypt";
JsonObject param = new JsonObject();
param.addProperty("encrypt_code", encryptCode);
String responseContent = this.wxMpService.post(url, param.toString());
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject();
JsonPrimitive jsonPrimitive = tmpJsonObject.getAsJsonPrimitive("code");
return jsonPrimitive.getAsString();
}

/**
* 卡券Code查询
*
* @param cardId 卡券ID代表一类卡券
* @param code 单张卡券的唯一标准
* @param checkConsume 是否校验code核销状态,填入true和false时的code异常状态返回数据不同
* @return WxMpCardResult对象
*/
@Override
public WxMpCardResult queryCardCode(String cardId, String code, boolean checkConsume) throws WxErrorException {
String url = "https://api.weixin.qq.com/card/code/get";
JsonObject param = new JsonObject();
param.addProperty("card_id", cardId);
param.addProperty("code", code);
param.addProperty("check_consume", checkConsume);
String responseContent = this.wxMpService.post(url, param.toString());
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
return WxMpGsonBuilder.INSTANCE.create().fromJson(tmpJsonElement,
new TypeToken<WxMpCardResult>() {
}.getType());
}

/**
* 卡券Code核销。核销失败会抛出异常
*
* @param code 单张卡券的唯一标准
* @return 调用返回的JSON字符串。
* <br>可用 com.google.gson.JsonParser#parse 等方法直接取JSON串中的errcode等信息。
*/
@Override
public String consumeCardCode(String code) throws WxErrorException {
return consumeCardCode(code, null);
}

/**
* 卡券Code核销。核销失败会抛出异常
*
* @param code 单张卡券的唯一标准
* @param cardId 当自定义Code卡券时需要传入card_id
* @return 调用返回的JSON字符串。
* <br>可用 com.google.gson.JsonParser#parse 等方法直接取JSON串中的errcode等信息。
*/
@Override
public String consumeCardCode(String code, String cardId) throws WxErrorException {
String url = "https://api.weixin.qq.com/card/code/consume";
JsonObject param = new JsonObject();
param.addProperty("code", code);

if (cardId != null && !"".equals(cardId)) {
param.addProperty("card_id", cardId);
}

return this.wxMpService.post(url, param.toString());
}

/**
* 卡券Mark接口。
* 开发者在帮助消费者核销卡券之前,必须帮助先将此code(卡券串码)与一个openid绑定(即mark住),
* 才能进一步调用核销接口,否则报错。
*
* @param code 卡券的code码
* @param cardId 卡券的ID
* @param openId 用券用户的openid
* @param isMark 是否要mark(占用)这个code,填写true或者false,表示占用或解除占用
*/
@Override
public void markCardCode(String code, String cardId, String openId, boolean isMark) throws
WxErrorException {
String url = "https://api.weixin.qq.com/card/code/mark";
JsonObject param = new JsonObject();
param.addProperty("code", code);
param.addProperty("card_id", cardId);
param.addProperty("openid", openId);
param.addProperty("is_mark", isMark);
String responseContent = this.getWxMpService().post(url, param.toString());
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
WxMpCardResult cardResult = WxMpGsonBuilder.INSTANCE.create().fromJson(tmpJsonElement,
new TypeToken<WxMpCardResult>() { }.getType());
if (!cardResult.getErrorCode().equals("0")) {
this.log.warn("朋友的券mark失败:{}", cardResult.getErrorMsg());
}
}

@Override
public String getCardDetail(String cardId) throws WxErrorException {
String url = "https://api.weixin.qq.com/card/get";
JsonObject param = new JsonObject();
param.addProperty("card_id", cardId);
String responseContent = this.wxMpService.post(url, param.toString());

// 判断返回值
JsonObject json = (new JsonParser()).parse(responseContent).getAsJsonObject();
String errcode = json.get("errcode").getAsString();
if (!"0".equals(errcode)) {
String errmsg = json.get("errmsg").getAsString();
WxError error = new WxError();
error.setErrorCode(Integer.valueOf(errcode));
error.setErrorMsg(errmsg);
throw new WxErrorException(error);
}

return responseContent;
}
}

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpKefuServiceImpl.java → weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/apache/WxMpKefuServiceImpl.java 查看文件

@@ -1,28 +1,23 @@
package me.chanjar.weixin.mp.api.impl;

import java.io.File;
import java.util.Date;

import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
package me.chanjar.weixin.mp.api.impl.apache;

import com.google.gson.JsonObject;

import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
import me.chanjar.weixin.common.util.http.apache.MediaUploadRequestExecutor;
import me.chanjar.weixin.mp.api.WxMpKefuService;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
import me.chanjar.weixin.mp.bean.kefu.request.WxMpKfAccountRequest;
import me.chanjar.weixin.mp.bean.kefu.request.WxMpKfSessionRequest;
import me.chanjar.weixin.mp.bean.kefu.result.WxMpKfList;
import me.chanjar.weixin.mp.bean.kefu.result.WxMpKfMsgList;
import me.chanjar.weixin.mp.bean.kefu.result.WxMpKfOnlineList;
import me.chanjar.weixin.mp.bean.kefu.result.WxMpKfSessionGetResult;
import me.chanjar.weixin.mp.bean.kefu.result.WxMpKfSessionList;
import me.chanjar.weixin.mp.bean.kefu.result.WxMpKfSessionWaitCaseList;
import me.chanjar.weixin.mp.bean.kefu.result.*;
import org.apache.http.HttpHost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.Date;

/**
*
@@ -34,7 +29,7 @@ public class WxMpKefuServiceImpl implements WxMpKefuService {
.getLogger(WxMpKefuServiceImpl.class);
private static final String API_URL_PREFIX = "https://api.weixin.qq.com/customservice";
private static final String API_URL_PREFIX_WITH_CGI_BIN = "https://api.weixin.qq.com/cgi-bin/customservice";
private WxMpService wxMpService;
private WxMpService<CloseableHttpClient, HttpHost> wxMpService;

public WxMpKefuServiceImpl(WxMpService wxMpService) {
this.wxMpService = wxMpService;

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpMaterialServiceImpl.java → weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/apache/WxMpMaterialServiceImpl.java 查看文件

@@ -1,12 +1,12 @@
package me.chanjar.weixin.mp.api.impl;
package me.chanjar.weixin.mp.api.impl.apache;

import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.fs.FileUtils;
import me.chanjar.weixin.common.util.http.MediaDownloadRequestExecutor;
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
import me.chanjar.weixin.common.util.http.apache.MediaDownloadRequestExecutor;
import me.chanjar.weixin.common.util.http.apache.MediaUploadRequestExecutor;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import me.chanjar.weixin.mp.api.WxMpMaterialService;
import me.chanjar.weixin.mp.api.WxMpService;
@@ -14,8 +14,10 @@ import me.chanjar.weixin.mp.bean.material.WxMpMaterial;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialArticleUpdate;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialNews;
import me.chanjar.weixin.mp.bean.material.*;
import me.chanjar.weixin.mp.util.http.*;
import me.chanjar.weixin.mp.util.http.apache.*;
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
import org.apache.http.HttpHost;
import org.apache.http.impl.client.CloseableHttpClient;

import java.io.File;
import java.io.IOException;
@@ -30,7 +32,7 @@ import java.util.UUID;
public class WxMpMaterialServiceImpl implements WxMpMaterialService {
private static final String MEDIA_API_URL_PREFIX = "https://api.weixin.qq.com/cgi-bin/media";
private static final String MATERIAL_API_URL_PREFIX = "https://api.weixin.qq.com/cgi-bin/material";
private WxMpService wxMpService;
private WxMpService<CloseableHttpClient, HttpHost> wxMpService;

public WxMpMaterialServiceImpl(WxMpService wxMpService) {
this.wxMpService = wxMpService;

+ 120
- 0
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/apache/WxMpQrcodeServiceImpl.java 查看文件

@@ -0,0 +1,120 @@
package me.chanjar.weixin.mp.api.impl.apache;

import com.google.gson.JsonObject;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpQrcodeService;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
import me.chanjar.weixin.mp.util.http.apache.QrCodeRequestExecutor;
import org.apache.http.HttpHost;
import org.apache.http.impl.client.CloseableHttpClient;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

/**
* Created by Binary Wang on 2016/7/21.
*/
public class WxMpQrcodeServiceImpl implements WxMpQrcodeService {
private static final String API_URL_PREFIX = "https://api.weixin.qq.com/cgi-bin/qrcode";
private WxMpService<CloseableHttpClient, HttpHost> wxMpService;

public WxMpQrcodeServiceImpl(WxMpService wxMpService) {
this.wxMpService = wxMpService;
}

@Override
public WxMpQrCodeTicket qrCodeCreateTmpTicket(int sceneId, Integer expireSeconds) throws WxErrorException {
if (sceneId == 0) {
throw new WxErrorException(WxError.newBuilder().setErrorCode(-1).setErrorMsg("临时二维码场景值不能为0!").build());
}

//expireSeconds 该二维码有效时间,以秒为单位。 最大不超过2592000(即30天),此字段如果不填,则默认有效期为30秒。
if (expireSeconds != null && expireSeconds > 2592000) {
throw new WxErrorException(WxError.newBuilder().setErrorCode(-1)
.setErrorMsg("临时二维码有效时间最大不能超过2592000(即30天)!").build());
}

if (expireSeconds == null) {
expireSeconds = 30;
}

String url = API_URL_PREFIX + "/create";
JsonObject json = new JsonObject();
json.addProperty("action_name", "QR_SCENE");
json.addProperty("expire_seconds", expireSeconds);

JsonObject actionInfo = new JsonObject();
JsonObject scene = new JsonObject();
scene.addProperty("scene_id", sceneId);
actionInfo.add("scene", scene);
json.add("action_info", actionInfo);
String responseContent = this.wxMpService.post(url, json.toString());
return WxMpQrCodeTicket.fromJson(responseContent);
}

@Override
public WxMpQrCodeTicket qrCodeCreateLastTicket(int sceneId) throws WxErrorException {
if (sceneId < 1 || sceneId > 100000) {
throw new WxErrorException(WxError.newBuilder().setErrorCode(-1).setErrorMsg("永久二维码的场景值目前只支持1--100000!").build());
}

String url = API_URL_PREFIX + "/create";
JsonObject json = new JsonObject();
json.addProperty("action_name", "QR_LIMIT_SCENE");
JsonObject actionInfo = new JsonObject();
JsonObject scene = new JsonObject();
scene.addProperty("scene_id", sceneId);
actionInfo.add("scene", scene);
json.add("action_info", actionInfo);
String responseContent = this.wxMpService.post(url, json.toString());
return WxMpQrCodeTicket.fromJson(responseContent);
}

@Override
public WxMpQrCodeTicket qrCodeCreateLastTicket(String sceneStr) throws WxErrorException {
String url = API_URL_PREFIX + "/create";
JsonObject json = new JsonObject();
json.addProperty("action_name", "QR_LIMIT_STR_SCENE");
JsonObject actionInfo = new JsonObject();
JsonObject scene = new JsonObject();
scene.addProperty("scene_str", sceneStr);
actionInfo.add("scene", scene);
json.add("action_info", actionInfo);
String responseContent = this.wxMpService.post(url, json.toString());
return WxMpQrCodeTicket.fromJson(responseContent);
}

@Override
public File qrCodePicture(WxMpQrCodeTicket ticket) throws WxErrorException {
String url = "https://mp.weixin.qq.com/cgi-bin/showqrcode";
return this.wxMpService.execute(new QrCodeRequestExecutor(), url, ticket);
}

@Override
public String qrCodePictureUrl(String ticket, boolean needShortUrl) throws WxErrorException {
String url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=%s";
try {
String resultUrl = String.format(url,
URLEncoder.encode(ticket, StandardCharsets.UTF_8.name()));
if (needShortUrl) {
return this.wxMpService.shortUrl(resultUrl);
}

return resultUrl;
} catch (UnsupportedEncodingException e) {
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);
}

}

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceImpl.java → weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/apache/WxMpServiceImpl.java 查看文件

@@ -1,4 +1,4 @@
package me.chanjar.weixin.mp.api.impl;
package me.chanjar.weixin.mp.api.impl.apache;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
@@ -12,8 +12,14 @@ import me.chanjar.weixin.common.session.StandardSessionManager;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.common.util.RandomUtils;
import me.chanjar.weixin.common.util.crypto.SHA1;
import me.chanjar.weixin.common.util.http.*;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.common.util.http.URIUtil;
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder;
import me.chanjar.weixin.common.util.http.apache.SimpleGetRequestExecutor;
import me.chanjar.weixin.common.util.http.apache.SimplePostRequestExecutor;
import me.chanjar.weixin.mp.api.*;
import me.chanjar.weixin.mp.api.impl.*;
import me.chanjar.weixin.mp.bean.*;
import me.chanjar.weixin.mp.bean.result.*;
import org.apache.http.HttpHost;
@@ -28,7 +34,7 @@ import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.concurrent.locks.Lock;

public class WxMpServiceImpl implements WxMpService {
public class WxMpServiceImpl implements WxMpService<CloseableHttpClient, HttpHost> {

private static final JsonParser JSON_PARSER = new JsonParser();

@@ -242,7 +248,7 @@ public class WxMpServiceImpl implements WxMpService {

private WxMpOAuth2AccessToken getOAuth2AccessToken(StringBuilder url) throws WxErrorException {
try {
RequestExecutor<String, String> executor = new SimpleGetRequestExecutor();
RequestExecutor<String, CloseableHttpClient, HttpHost, String> executor = new SimpleGetRequestExecutor();
String responseText = executor.execute(this.getHttpclient(), this.httpProxy, url.toString(), null);
return WxMpOAuth2AccessToken.fromJson(responseText);
} catch (IOException e) {
@@ -286,7 +292,7 @@ public class WxMpServiceImpl implements WxMpService {
}

try {
RequestExecutor<String, String> executor = new SimpleGetRequestExecutor();
RequestExecutor<String, CloseableHttpClient, HttpHost, String> executor = new SimpleGetRequestExecutor();
String responseText = executor.execute(getHttpclient(), this.httpProxy, url.toString(), null);
return WxMpUser.fromJson(responseText);
} catch (IOException e) {
@@ -302,7 +308,7 @@ public class WxMpServiceImpl implements WxMpService {
url.append("&openid=").append(oAuth2AccessToken.getOpenId());

try {
RequestExecutor<String, String> executor = new SimpleGetRequestExecutor();
RequestExecutor<String, CloseableHttpClient, HttpHost, String> executor = new SimpleGetRequestExecutor();
executor.execute(getHttpclient(), this.httpProxy, url.toString(), null);
} catch (IOException e) {
throw new RuntimeException(e);
@@ -339,7 +345,7 @@ public class WxMpServiceImpl implements WxMpService {
* 向微信端发送请求,在这里执行的策略是当发生access_token过期时才去刷新,然后重新执行请求,而不是全局定时请求
*/
@Override
public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException {
public <T, E> T execute(RequestExecutor<T, CloseableHttpClient, HttpHost, E> executor, String uri, E data) throws WxErrorException {
int retryTimes = 0;
do {
try {
@@ -373,7 +379,8 @@ public class WxMpServiceImpl implements WxMpService {
throw new RuntimeException("微信服务端异常,超出重试次数");
}

protected synchronized <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException {
@Override
public synchronized <T, E> T executeInternal(RequestExecutor<T, CloseableHttpClient, HttpHost, E> executor, String uri, E data) throws WxErrorException {
if (uri.indexOf("access_token=") != -1) {
throw new IllegalArgumentException("uri参数中不允许有access_token: " + uri);
}
@@ -383,7 +390,7 @@ public class WxMpServiceImpl implements WxMpService {
uriWithAccessToken += uri.indexOf('?') == -1 ? "?access_token=" + accessToken : "&access_token=" + accessToken;

try {
return executor.execute(getHttpclient(), this.httpProxy, uriWithAccessToken, data);
return executor.execute(getHttpclient(), getHttpProxy(), uriWithAccessToken, data);
} catch (WxErrorException e) {
WxError error = e.getError();
/*
@@ -415,6 +422,7 @@ public class WxMpServiceImpl implements WxMpService {
return this.httpProxy;
}

@Override
public CloseableHttpClient getHttpclient() {
return this.httpClient;
}

+ 52
- 0
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/apache/WxMpUserBlacklistServiceImpl.java 查看文件

@@ -0,0 +1,52 @@
package me.chanjar.weixin.mp.api.impl.apache;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.apache.SimplePostRequestExecutor;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.WxMpUserBlacklistService;
import me.chanjar.weixin.mp.bean.result.WxMpUserBlacklistGetResult;
import org.apache.http.HttpHost;
import org.apache.http.impl.client.CloseableHttpClient;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author miller
*/
public class WxMpUserBlacklistServiceImpl implements WxMpUserBlacklistService {
private static final String API_BLACKLIST_PREFIX = "https://api.weixin.qq.com/cgi-bin/tags/members";
private WxMpService<CloseableHttpClient, HttpHost> wxMpService;

public WxMpUserBlacklistServiceImpl(WxMpService wxMpService) {
this.wxMpService = wxMpService;
}

@Override
public WxMpUserBlacklistGetResult getBlacklist(String nextOpenid) throws WxErrorException {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("begin_openid", nextOpenid);
String url = API_BLACKLIST_PREFIX + "/getblacklist";
String responseContent = this.wxMpService.execute(new SimplePostRequestExecutor(), url, jsonObject.toString());
return WxMpUserBlacklistGetResult.fromJson(responseContent);
}

@Override
public void pushToBlacklist(List<String> openidList) throws WxErrorException {
Map<String, Object> map = new HashMap<>();
map.put("openid_list", openidList);
String url = API_BLACKLIST_PREFIX + "/batchblacklist";
this.wxMpService.execute(new SimplePostRequestExecutor(), url, new Gson().toJson(map));
}

@Override
public void pullFromBlacklist(List<String> openidList) throws WxErrorException {
Map<String, Object> map = new HashMap<>();
map.put("openid_list", openidList);
String url = API_BLACKLIST_PREFIX + "/batchunblacklist";
this.wxMpService.execute(new SimplePostRequestExecutor(), url, new Gson().toJson(map));
}
}

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpCardServiceImpl.java → weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/jodd/WxMpCardServiceImpl.java 查看文件

@@ -1,41 +1,50 @@
package me.chanjar.weixin.mp.api.impl;

import java.util.Arrays;
import java.util.concurrent.locks.Lock;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
package me.chanjar.weixin.mp.api.impl.jodd;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import com.google.gson.reflect.TypeToken;

import jodd.http.HttpConnectionProvider;
import jodd.http.ProxyInfo;
import me.chanjar.weixin.common.bean.WxCardApiSignature;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.RandomUtils;
import me.chanjar.weixin.common.util.crypto.SHA1;
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
import me.chanjar.weixin.common.util.http.jodd.SimpleGetRequestExecutor;
import me.chanjar.weixin.mp.api.WxMpCardService;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpCardResult;
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.concurrent.locks.Lock;

/**
* Created by Binary Wang on 2016/7/27.
*/
public class WxMpCardServiceImpl implements WxMpCardService {
public class WxMpCardServiceImpl implements WxMpCardService<HttpConnectionProvider, ProxyInfo> {

private final Logger log = LoggerFactory.getLogger(WxMpCardServiceImpl.class);

private WxMpService wxMpService;
private WxMpService<HttpConnectionProvider, ProxyInfo> wxMpService;

public WxMpCardServiceImpl(WxMpService wxMpService) {
this.wxMpService = wxMpService;
}

/**
* 得到WxMpService
* @return
*/
@Override
public WxMpService<HttpConnectionProvider, ProxyInfo> getWxMpService(){
return this.wxMpService;
}

/**
* 获得卡券api_ticket,不强制刷新卡券api_ticket
*
@@ -62,27 +71,27 @@ public class WxMpCardServiceImpl implements WxMpCardService {
*/
@Override
public String getCardApiTicket(boolean forceRefresh) throws WxErrorException {
Lock lock = wxMpService.getWxMpConfigStorage().getCardApiTicketLock();
Lock lock = getWxMpService().getWxMpConfigStorage().getCardApiTicketLock();
try {
lock.lock();

if (forceRefresh) {
this.wxMpService.getWxMpConfigStorage().expireCardApiTicket();
this.getWxMpService().getWxMpConfigStorage().expireCardApiTicket();
}

if (this.wxMpService.getWxMpConfigStorage().isCardApiTicketExpired()) {
if (this.getWxMpService().getWxMpConfigStorage().isCardApiTicketExpired()) {
String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=wx_card";
String responseContent = this.wxMpService.execute(new SimpleGetRequestExecutor(), url, null);
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject();
String cardApiTicket = tmpJsonObject.get("ticket").getAsString();
int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt();
this.wxMpService.getWxMpConfigStorage().updateCardApiTicket(cardApiTicket, expiresInSeconds);
this.getWxMpService().getWxMpConfigStorage().updateCardApiTicket(cardApiTicket, expiresInSeconds);
}
} finally {
lock.unlock();
}
return this.wxMpService.getWxMpConfigStorage().getCardApiTicket();
return this.getWxMpService().getWxMpConfigStorage().getCardApiTicket();
}

/**
@@ -210,7 +219,7 @@ public class WxMpCardServiceImpl implements WxMpCardService {
param.addProperty("card_id", cardId);
param.addProperty("openid", openId);
param.addProperty("is_mark", isMark);
String responseContent = this.wxMpService.post(url, param.toString());
String responseContent = this.getWxMpService().post(url, param.toString());
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
WxMpCardResult cardResult = WxMpGsonBuilder.INSTANCE.create().fromJson(tmpJsonElement,
new TypeToken<WxMpCardResult>() { }.getType());

+ 183
- 0
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/jodd/WxMpKefuServiceImpl.java 查看文件

@@ -0,0 +1,183 @@
package me.chanjar.weixin.mp.api.impl.jodd;

import com.google.gson.JsonObject;
import jodd.http.HttpConnectionProvider;
import jodd.http.ProxyInfo;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.jodd.MediaUploadRequestExecutor;
import me.chanjar.weixin.mp.api.WxMpKefuService;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
import me.chanjar.weixin.mp.bean.kefu.request.WxMpKfAccountRequest;
import me.chanjar.weixin.mp.bean.kefu.request.WxMpKfSessionRequest;
import me.chanjar.weixin.mp.bean.kefu.result.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.Date;

/**
*
* @author Binary Wang
*
*/
public class WxMpKefuServiceImpl implements WxMpKefuService {
protected final Logger log = LoggerFactory
.getLogger(WxMpKefuServiceImpl.class);
private static final String API_URL_PREFIX = "https://api.weixin.qq.com/customservice";
private static final String API_URL_PREFIX_WITH_CGI_BIN = "https://api.weixin.qq.com/cgi-bin/customservice";
private WxMpService<HttpConnectionProvider, ProxyInfo> wxMpService;

public WxMpKefuServiceImpl(WxMpService wxMpService) {
this.wxMpService = wxMpService;
}

@Override
public boolean sendKefuMessage(WxMpKefuMessage message)
throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/message/custom/send";
String responseContent = this.wxMpService.post(url, message.toJson());
return responseContent != null;
}

@Override
public WxMpKfList kfList() throws WxErrorException {
String url = API_URL_PREFIX_WITH_CGI_BIN + "/getkflist";
String responseContent = this.wxMpService.get(url, null);
return WxMpKfList.fromJson(responseContent);
}

@Override
public WxMpKfOnlineList kfOnlineList() throws WxErrorException {
String url = API_URL_PREFIX_WITH_CGI_BIN + "/getonlinekflist";
String responseContent = this.wxMpService.get(url, null);
return WxMpKfOnlineList.fromJson(responseContent);
}

@Override
public boolean kfAccountAdd(WxMpKfAccountRequest request)
throws WxErrorException {
String url = API_URL_PREFIX + "/kfaccount/add";
String responseContent = this.wxMpService.post(url, request.toJson());
return responseContent != null;
}

@Override
public boolean kfAccountUpdate(WxMpKfAccountRequest request)
throws WxErrorException {
String url = API_URL_PREFIX + "/kfaccount/update";
String responseContent = this.wxMpService.post(url, request.toJson());
return responseContent != null;
}

@Override
public boolean kfAccountInviteWorker(WxMpKfAccountRequest request) throws WxErrorException {
String url = API_URL_PREFIX + "/kfaccount/inviteworker";
String responseContent = this.wxMpService.post(url, request.toJson());
return responseContent != null;
}

@Override
public boolean kfAccountUploadHeadImg(String kfAccount, File imgFile)
throws WxErrorException {
String url = API_URL_PREFIX + "/kfaccount/uploadheadimg?kf_account=" + kfAccount;
WxMediaUploadResult responseContent = this.wxMpService
.execute(new MediaUploadRequestExecutor(), url, imgFile);
return responseContent != null;
}

@Override
public boolean kfAccountDel(String kfAccount) throws WxErrorException {
String url = API_URL_PREFIX + "/kfaccount/del?kf_account=" + kfAccount;
String responseContent = this.wxMpService.get(url, null);
return responseContent != null;
}

@Override
public boolean kfSessionCreate(String openid, String kfAccount)
throws WxErrorException {
WxMpKfSessionRequest request = new WxMpKfSessionRequest(kfAccount, openid);
String url = API_URL_PREFIX + "/kfsession/create";
String responseContent = this.wxMpService.post(url, request.toJson());
return responseContent != null;
}

@Override
public boolean kfSessionClose(String openid, String kfAccount)
throws WxErrorException {
WxMpKfSessionRequest request = new WxMpKfSessionRequest(kfAccount, openid);
String url = API_URL_PREFIX + "/kfsession/close";
String responseContent = this.wxMpService.post(url, request.toJson());
return responseContent != null;
}

@Override
public WxMpKfSessionGetResult kfSessionGet(String openid)
throws WxErrorException {
String url = API_URL_PREFIX + "/kfsession/getsession?openid=" + openid;
String responseContent = this.wxMpService.get(url, null);
return WxMpKfSessionGetResult.fromJson(responseContent);
}

@Override
public WxMpKfSessionList kfSessionList(String kfAccount)
throws WxErrorException {
String url = API_URL_PREFIX + "/kfsession/getsessionlist?kf_account=" + kfAccount;
String responseContent = this.wxMpService.get(url, null);
return WxMpKfSessionList.fromJson(responseContent);
}

@Override
public WxMpKfSessionWaitCaseList kfSessionGetWaitCase()
throws WxErrorException {
String url = API_URL_PREFIX + "/kfsession/getwaitcase";
String responseContent = this.wxMpService.get(url, null);
return WxMpKfSessionWaitCaseList.fromJson(responseContent);
}

@Override
public WxMpKfMsgList kfMsgList(Date startTime, Date endTime, Long msgId, Integer number) throws WxErrorException {
if(number > 10000){
throw new WxErrorException(WxError.newBuilder().setErrorMsg("非法参数请求,每次最多查询10000条记录!").build());
}

if(startTime.after(endTime)){
throw new WxErrorException(WxError.newBuilder().setErrorMsg("起始时间不能晚于结束时间!").build());
}

String url = API_URL_PREFIX + "/msgrecord/getmsglist";

JsonObject param = new JsonObject();
param.addProperty("starttime", startTime.getTime() / 1000); //starttime 起始时间,unix时间戳
param.addProperty("endtime", endTime.getTime() / 1000); //endtime 结束时间,unix时间戳,每次查询时段不能超过24小时
param.addProperty("msgid", msgId); //msgid 消息id顺序从小到大,从1开始
param.addProperty("number", number); //number 每次获取条数,最多10000条

String responseContent = this.wxMpService.post(url, param.toString());

return WxMpKfMsgList.fromJson(responseContent);
}

@Override
public WxMpKfMsgList kfMsgList(Date startTime, Date endTime) throws WxErrorException {
int number = 10000;
WxMpKfMsgList result = this.kfMsgList(startTime,endTime, 1L, number);

if(result != null && result.getNumber() == number){
Long msgId = result.getMsgId();
WxMpKfMsgList followingResult = this.kfMsgList(startTime,endTime, msgId, number);
while(followingResult != null && followingResult.getRecords().size() > 0){
result.getRecords().addAll(followingResult.getRecords());
result.setNumber(result.getNumber() + followingResult.getNumber());
result.setMsgId(followingResult.getMsgId());
followingResult = this.kfMsgList(startTime,endTime, followingResult.getMsgId(), number);
}
}

return result;
}

}

+ 165
- 0
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/jodd/WxMpMaterialServiceImpl.java 查看文件

@@ -0,0 +1,165 @@
package me.chanjar.weixin.mp.api.impl.jodd;

import jodd.http.HttpConnectionProvider;
import jodd.http.ProxyInfo;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.fs.FileUtils;
import me.chanjar.weixin.common.util.http.jodd.MediaDownloadRequestExecutor;
import me.chanjar.weixin.common.util.http.jodd.MediaUploadRequestExecutor;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import me.chanjar.weixin.mp.api.WxMpMaterialService;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.material.*;
import me.chanjar.weixin.mp.util.http.jodd.*;
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
* Created by Binary Wang on 2016/7/21.
*/
public class WxMpMaterialServiceImpl implements WxMpMaterialService {
private static final String MEDIA_API_URL_PREFIX = "https://api.weixin.qq.com/cgi-bin/media";
private static final String MATERIAL_API_URL_PREFIX = "https://api.weixin.qq.com/cgi-bin/material";
private WxMpService<HttpConnectionProvider, ProxyInfo> wxMpService;

public WxMpMaterialServiceImpl(WxMpService wxMpService) {
this.wxMpService = wxMpService;
}

@Override
public WxMediaUploadResult mediaUpload(String mediaType, String fileType, InputStream inputStream) throws WxErrorException {
try {
return this.mediaUpload(mediaType, FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), fileType));
} catch (IOException e) {
e.printStackTrace();
throw new WxErrorException(WxError.newBuilder().setErrorMsg(e.getMessage()).build());
}
}

@Override
public WxMediaUploadResult mediaUpload(String mediaType, File file) throws WxErrorException {
String url = MEDIA_API_URL_PREFIX + "/upload?type=" + mediaType;
return this.wxMpService.execute(new MediaUploadRequestExecutor(), url, file);
}

@Override
public File mediaDownload(String media_id) throws WxErrorException {
String url = MEDIA_API_URL_PREFIX + "/get";
return this.wxMpService.execute(
new MediaDownloadRequestExecutor(this.wxMpService.getWxMpConfigStorage().getTmpDirFile()),
url,
"media_id=" + media_id);
}

@Override
public WxMediaImgUploadResult mediaImgUpload(File file) throws WxErrorException {
String url = MEDIA_API_URL_PREFIX + "/uploadimg";
return this.wxMpService.execute(new MediaImgUploadRequestExecutor(), url, file);
}

@Override
public WxMpMaterialUploadResult materialFileUpload(String mediaType, WxMpMaterial material) throws WxErrorException {
String url = MATERIAL_API_URL_PREFIX + "/add_material?type=" + mediaType;
return this.wxMpService.execute(new MaterialUploadRequestExecutor(), url, material);
}

@Override
public WxMpMaterialUploadResult materialNewsUpload(WxMpMaterialNews news) throws WxErrorException {
if (news == null || news.isEmpty()) {
throw new IllegalArgumentException("news is empty!");
}
String url = MATERIAL_API_URL_PREFIX + "/add_news";
String responseContent = this.wxMpService.post(url, news.toJson());
return WxMpMaterialUploadResult.fromJson(responseContent);
}

@Override
public InputStream materialImageOrVoiceDownload(String media_id) throws WxErrorException {
String url = MATERIAL_API_URL_PREFIX + "/get_material";
return this.wxMpService.execute(new MaterialVoiceAndImageDownloadRequestExecutor(this.wxMpService.getWxMpConfigStorage().getTmpDirFile()), url, media_id);
}

@Override
public WxMpMaterialVideoInfoResult materialVideoInfo(String media_id) throws WxErrorException {
String url = MATERIAL_API_URL_PREFIX + "/get_material";
return this.wxMpService.execute(new MaterialVideoInfoRequestExecutor(), url, media_id);
}

@Override
public WxMpMaterialNews materialNewsInfo(String media_id) throws WxErrorException {
String url = MATERIAL_API_URL_PREFIX + "/get_material";
return this.wxMpService.execute(new MaterialNewsInfoRequestExecutor(), url, media_id);
}

@Override
public boolean materialNewsUpdate(WxMpMaterialArticleUpdate wxMpMaterialArticleUpdate) throws WxErrorException {
String url = MATERIAL_API_URL_PREFIX + "/update_news";
String responseText = this.wxMpService.post(url, wxMpMaterialArticleUpdate.toJson());
WxError wxError = WxError.fromJson(responseText);
if (wxError.getErrorCode() == 0) {
return true;
} else {
throw new WxErrorException(wxError);
}
}

@Override
public boolean materialDelete(String media_id) throws WxErrorException {
String url = MATERIAL_API_URL_PREFIX + "/del_material";
return this.wxMpService.execute(new MaterialDeleteRequestExecutor(), url, media_id);
}

@Override
public WxMpMaterialCountResult materialCount() throws WxErrorException {
String url = MATERIAL_API_URL_PREFIX + "/get_materialcount";
String responseText = this.wxMpService.get(url, null);
WxError wxError = WxError.fromJson(responseText);
if (wxError.getErrorCode() == 0) {
return WxMpGsonBuilder.create().fromJson(responseText, WxMpMaterialCountResult.class);
} else {
throw new WxErrorException(wxError);
}
}

@Override
public WxMpMaterialNewsBatchGetResult materialNewsBatchGet(int offset, int count) throws WxErrorException {
String url = MATERIAL_API_URL_PREFIX + "/batchget_material";
Map<String, Object> params = new HashMap<>();
params.put("type", WxConsts.MATERIAL_NEWS);
params.put("offset", offset);
params.put("count", count);
String responseText = this.wxMpService.post(url, WxGsonBuilder.create().toJson(params));
WxError wxError = WxError.fromJson(responseText);
if (wxError.getErrorCode() == 0) {
return WxMpGsonBuilder.create().fromJson(responseText, WxMpMaterialNewsBatchGetResult.class);
} else {
throw new WxErrorException(wxError);
}
}

@Override
public WxMpMaterialFileBatchGetResult materialFileBatchGet(String type, int offset, int count) throws WxErrorException {
String url = MATERIAL_API_URL_PREFIX + "/batchget_material";
Map<String, Object> params = new HashMap<>();
params.put("type", type);
params.put("offset", offset);
params.put("count", count);
String responseText = this.wxMpService.post(url, WxGsonBuilder.create().toJson(params));
WxError wxError = WxError.fromJson(responseText);
if (wxError.getErrorCode() == 0) {
return WxMpGsonBuilder.create().fromJson(responseText, WxMpMaterialFileBatchGetResult.class);
} else {
throw new WxErrorException(wxError);
}
}

}

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpQrcodeServiceImpl.java → weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/jodd/WxMpQrcodeServiceImpl.java 查看文件

@@ -1,12 +1,14 @@
package me.chanjar.weixin.mp.api.impl;
package me.chanjar.weixin.mp.api.impl.jodd;

import com.google.gson.JsonObject;
import jodd.http.HttpConnectionProvider;
import jodd.http.ProxyInfo;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpQrcodeService;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
import me.chanjar.weixin.mp.util.http.QrCodeRequestExecutor;
import me.chanjar.weixin.mp.util.http.jodd.QrCodeRequestExecutor;

import java.io.File;
import java.io.UnsupportedEncodingException;
@@ -18,7 +20,7 @@ import java.nio.charset.StandardCharsets;
*/
public class WxMpQrcodeServiceImpl implements WxMpQrcodeService {
private static final String API_URL_PREFIX = "https://api.weixin.qq.com/cgi-bin/qrcode";
private WxMpService wxMpService;
private WxMpService<HttpConnectionProvider, ProxyInfo> wxMpService;

public WxMpQrcodeServiceImpl(WxMpService wxMpService) {
this.wxMpService = wxMpService;

+ 510
- 0
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/jodd/WxMpServiceImpl.java 查看文件

@@ -0,0 +1,510 @@
package me.chanjar.weixin.mp.api.impl.jodd;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import jodd.http.*;
import jodd.http.net.SocketHttpConnectionProvider;
import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.session.StandardSessionManager;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.common.util.RandomUtils;
import me.chanjar.weixin.common.util.crypto.SHA1;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.common.util.http.URIUtil;
import me.chanjar.weixin.common.util.http.jodd.SimpleGetRequestExecutor;
import me.chanjar.weixin.common.util.http.jodd.SimplePostRequestExecutor;
import me.chanjar.weixin.mp.api.*;
import me.chanjar.weixin.mp.api.impl.*;
import me.chanjar.weixin.mp.bean.*;
import me.chanjar.weixin.mp.bean.result.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.concurrent.locks.Lock;

public class WxMpServiceImpl implements WxMpService<HttpConnectionProvider, ProxyInfo> {

private static final JsonParser JSON_PARSER = new JsonParser();

protected final Logger log = LoggerFactory.getLogger(this.getClass());
protected WxSessionManager sessionManager = new StandardSessionManager();
private WxMpConfigStorage wxMpConfigStorage;
private WxMpKefuService kefuService = new WxMpKefuServiceImpl(this);
private WxMpMaterialService materialService = new WxMpMaterialServiceImpl(this);
private WxMpMenuService menuService = new WxMpMenuServiceImpl(this);
private WxMpUserService userService = new WxMpUserServiceImpl(this);
private WxMpUserTagService tagService = new WxMpUserTagServiceImpl(this);
private WxMpQrcodeService qrCodeService = new WxMpQrcodeServiceImpl(this);
private WxMpCardService cardService = new WxMpCardServiceImpl(this);
private WxMpStoreService storeService = new WxMpStoreServiceImpl(this);
private WxMpDataCubeService dataCubeService = new WxMpDataCubeServiceImpl(this);
private WxMpUserBlacklistService blackListService = new WxMpUserBlacklistServiceImpl(this);
private WxMpTemplateMsgService templateMsgService = new WxMpTemplateMsgServiceImpl(this);
private WxMpDeviceService deviceService = new WxMpDeviceServiceImpl(this);

private HttpConnectionProvider httpClient;
private ProxyInfo httpProxy;
private int retrySleepMillis = 1000;
private int maxRetryTimes = 5;

private void initHttpClient() {
WxMpConfigStorage configStorage = this.getWxMpConfigStorage();

if (configStorage.getHttpProxyHost() != null && configStorage.getHttpProxyPort() > 0) {
httpProxy = new ProxyInfo(ProxyInfo.ProxyType.HTTP, configStorage.getHttpProxyHost(), configStorage.getHttpProxyPort(), configStorage.getHttpProxyUsername(), configStorage.getHttpProxyPassword());
}

httpClient = JoddHttp.httpConnectionProvider;
}

@Override
public boolean checkSignature(String timestamp, String nonce, String signature) {
try {
return SHA1.gen(this.getWxMpConfigStorage().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 {
Lock lock = this.getWxMpConfigStorage().getAccessTokenLock();
try {
lock.lock();

if (forceRefresh) {
this.getWxMpConfigStorage().expireAccessToken();
}

if (this.getWxMpConfigStorage().isAccessTokenExpired()) {
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential" +
"&appid=" + this.getWxMpConfigStorage().getAppId() + "&secret="
+ this.getWxMpConfigStorage().getSecret();

HttpRequest request = HttpRequest.get(url);
if (this.httpProxy != null) {
SocketHttpConnectionProvider provider = new SocketHttpConnectionProvider();
provider.useProxy(httpProxy);
request.withConnectionProvider(provider);
}
HttpResponse response = request.send();
String resultContent = response.bodyText();
WxError error = WxError.fromJson(resultContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
}
WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
this.getWxMpConfigStorage().updateAccessToken(accessToken.getAccessToken(),
accessToken.getExpiresIn());
}
} finally {
lock.unlock();
}
return this.getWxMpConfigStorage().getAccessToken();
}

@Override
public String getJsapiTicket() throws WxErrorException {
return getJsapiTicket(false);
}

@Override
public String getJsapiTicket(boolean forceRefresh) throws WxErrorException {
Lock lock = this.getWxMpConfigStorage().getJsapiTicketLock();
try {
lock.lock();

if (forceRefresh) {
this.getWxMpConfigStorage().expireJsapiTicket();
}

if (this.getWxMpConfigStorage().isJsapiTicketExpired()) {
String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi";
String responseContent = execute(new SimpleGetRequestExecutor(), url, null);
JsonElement tmpJsonElement = JSON_PARSER.parse(responseContent);
JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject();
String jsapiTicket = tmpJsonObject.get("ticket").getAsString();
int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt();
this.getWxMpConfigStorage().updateJsapiTicket(jsapiTicket, expiresInSeconds);
}
} finally {
lock.unlock();
}
return this.getWxMpConfigStorage().getJsapiTicket();
}

@Override
public WxJsapiSignature createJsapiSignature(String url) throws WxErrorException {
long timestamp = System.currentTimeMillis() / 1000;
String noncestr = RandomUtils.getRandomStr();
String jsapiTicket = getJsapiTicket(false);
String signature = SHA1.genWithAmple("jsapi_ticket=" + jsapiTicket,
"noncestr=" + noncestr, "timestamp=" + timestamp, "url=" + url);
WxJsapiSignature jsapiSignature = new WxJsapiSignature();
jsapiSignature.setAppId(this.getWxMpConfigStorage().getAppId());
jsapiSignature.setTimestamp(timestamp);
jsapiSignature.setNonceStr(noncestr);
jsapiSignature.setUrl(url);
jsapiSignature.setSignature(signature);
return jsapiSignature;
}

@Override
public WxMpMassUploadResult massNewsUpload(WxMpMassNews news) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/media/uploadnews";
String responseContent = this.post(url, news.toJson());
return WxMpMassUploadResult.fromJson(responseContent);
}

@Override
public WxMpMassUploadResult massVideoUpload(WxMpMassVideo video) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/media/uploadvideo";
String responseContent = this.post(url, video.toJson());
return WxMpMassUploadResult.fromJson(responseContent);
}

@Override
public WxMpMassSendResult massGroupMessageSend(WxMpMassTagMessage message) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall";
String responseContent = this.post(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 = this.post(url, message.toJson());
return WxMpMassSendResult.fromJson(responseContent);
}

@Override
public WxMpMassSendResult massMessagePreview(WxMpMassPreviewMessage wxMpMassPreviewMessage) throws Exception {
String url = "https://api.weixin.qq.com/cgi-bin/message/mass/preview";
String responseContent = this.post(url, wxMpMassPreviewMessage.toJson());
return WxMpMassSendResult.fromJson(responseContent);
}

@Override
public String shortUrl(String long_url) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/shorturl";
JsonObject o = new JsonObject();
o.addProperty("action", "long2short");
o.addProperty("long_url", long_url);
String responseContent = this.post(url, o.toString());
JsonElement tmpJsonElement = JSON_PARSER.parse(responseContent);
return tmpJsonElement.getAsJsonObject().get("short_url").getAsString();
}

@Override
public WxMpSemanticQueryResult semanticQuery(WxMpSemanticQuery semanticQuery) throws WxErrorException {
String url = "https://api.weixin.qq.com/semantic/semproxy/search";
String responseContent = this.post(url, semanticQuery.toJson());
return WxMpSemanticQueryResult.fromJson(responseContent);
}

@Override
public String oauth2buildAuthorizationUrl(String redirectURI, String scope, String state) {
StringBuilder url = new StringBuilder();
url.append("https://open.weixin.qq.com/connect/oauth2/authorize?");
url.append("appid=").append(this.getWxMpConfigStorage().getAppId());
url.append("&redirect_uri=").append(URIUtil.encodeURIComponent(redirectURI));
url.append("&response_type=code");
url.append("&scope=").append(scope);
if (state != null) {
url.append("&state=").append(state);
}
url.append("#wechat_redirect");
return url.toString();
}

@Override
public String buildQrConnectUrl(String redirectURI, String scope,
String state) {
StringBuilder url = new StringBuilder();
url.append("https://open.weixin.qq.com/connect/qrconnect?");
url.append("appid=").append(this.getWxMpConfigStorage().getAppId());
url.append("&redirect_uri=").append(URIUtil.encodeURIComponent(redirectURI));
url.append("&response_type=code");
url.append("&scope=").append(scope);
if (state != null) {
url.append("&state=").append(state);
}

url.append("#wechat_redirect");
return url.toString();
}

private WxMpOAuth2AccessToken getOAuth2AccessToken(StringBuilder url) throws WxErrorException {
try {
RequestExecutor<String, HttpConnectionProvider, ProxyInfo, String> executor = new SimpleGetRequestExecutor();
String responseText = executor.execute(getHttpclient(), getHttpProxy(), url.toString(), null);
return WxMpOAuth2AccessToken.fromJson(responseText);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@Override
public WxMpOAuth2AccessToken oauth2getAccessToken(String code) throws WxErrorException {
StringBuilder url = new StringBuilder();
url.append("https://api.weixin.qq.com/sns/oauth2/access_token?");
url.append("appid=").append(this.getWxMpConfigStorage().getAppId());
url.append("&secret=").append(this.getWxMpConfigStorage().getSecret());
url.append("&code=").append(code);
url.append("&grant_type=authorization_code");

return this.getOAuth2AccessToken(url);
}

@Override
public WxMpOAuth2AccessToken oauth2refreshAccessToken(String refreshToken) throws WxErrorException {
StringBuilder url = new StringBuilder();
url.append("https://api.weixin.qq.com/sns/oauth2/refresh_token?");
url.append("appid=").append(this.getWxMpConfigStorage().getAppId());
url.append("&grant_type=refresh_token");
url.append("&refresh_token=").append(refreshToken);

return this.getOAuth2AccessToken(url);
}

@Override
public WxMpUser oauth2getUserInfo(WxMpOAuth2AccessToken oAuth2AccessToken, String lang) throws WxErrorException {
StringBuilder url = new StringBuilder();
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.append("&lang=zh_CN");
} else {
url.append("&lang=").append(lang);
}

try {
RequestExecutor<String, HttpConnectionProvider, ProxyInfo, String> executor = new SimpleGetRequestExecutor();
String responseText = executor.execute(getHttpclient(), getHttpProxy(), url.toString(), null);
return WxMpUser.fromJson(responseText);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@Override
public boolean oauth2validateAccessToken(WxMpOAuth2AccessToken oAuth2AccessToken) {
StringBuilder url = new StringBuilder();
url.append("https://api.weixin.qq.com/sns/auth?");
url.append("access_token=").append(oAuth2AccessToken.getAccessToken());
url.append("&openid=").append(oAuth2AccessToken.getOpenId());

try {
RequestExecutor<String, HttpConnectionProvider, ProxyInfo, String> executor = new SimpleGetRequestExecutor();
executor.execute(getHttpclient(), getHttpProxy(), url.toString(), null);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (WxErrorException e) {
return false;
}
return true;
}

@Override
public String[] getCallbackIP() throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/getcallbackip";
String responseContent = get(url, null);
JsonElement tmpJsonElement = JSON_PARSER.parse(responseContent);
JsonArray ipList = tmpJsonElement.getAsJsonObject().get("ip_list").getAsJsonArray();
String[] ipArray = new String[ipList.size()];
for (int i = 0; i < ipList.size(); i++) {
ipArray[i] = ipList.get(i).getAsString();
}
return ipArray;
}

@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);
}

@Override
public HttpConnectionProvider getHttpclient() {
return this.httpClient;
}

/**
* 向微信端发送请求,在这里执行的策略是当发生access_token过期时才去刷新,然后重新执行请求,而不是全局定时请求
*/
public <T, E> T execute(RequestExecutor<T, HttpConnectionProvider, ProxyInfo, E> executor, String uri, E data) throws WxErrorException {
int retryTimes = 0;
do {
try {
T result = executeInternal(executor, uri, data);
this.log.debug("\n[URL]: {}\n[PARAMS]: {}\n[RESPONSE]: {}", uri, data, result);
return result;
} catch (WxErrorException e) {
if (retryTimes + 1 > this.maxRetryTimes) {
this.log.warn("重试达到最大次数【{}】", maxRetryTimes);
//最后一次重试失败后,直接抛出异常,不再等待
throw new RuntimeException("微信服务端异常,超出重试次数");
}

WxError error = e.getError();
// -1 系统繁忙, 1000ms后重试
if (error.getErrorCode() == -1) {
int sleepMillis = this.retrySleepMillis * (1 << retryTimes);
try {
this.log.warn("微信系统繁忙,{} ms 后重试(第{}次)", sleepMillis, retryTimes + 1);
Thread.sleep(sleepMillis);
} catch (InterruptedException e1) {
throw new RuntimeException(e1);
}
} else {
throw e;
}
}
} while (retryTimes++ < this.maxRetryTimes);

this.log.warn("重试达到最大次数【{}】", this.maxRetryTimes);
throw new RuntimeException("微信服务端异常,超出重试次数");
}

@Override
public synchronized <T, E> T executeInternal(RequestExecutor<T, HttpConnectionProvider, ProxyInfo, E> executor, String uri, E data) throws WxErrorException {
if (uri.indexOf("access_token=") != -1) {
throw new IllegalArgumentException("uri参数中不允许有access_token: " + uri);
}
String accessToken = getAccessToken(false);

String uriWithAccessToken = uri;
uriWithAccessToken += uri.indexOf('?') == -1 ? "?access_token=" + accessToken : "&access_token=" + accessToken;

try {
return executor.execute(getHttpclient(), getHttpProxy(), uriWithAccessToken, data);
} catch (WxErrorException e) {
WxError error = e.getError();
/*
* 发生以下情况时尝试刷新access_token
* 40001 获取access_token时AppSecret错误,或者access_token无效
* 42001 access_token超时
*/
if (error.getErrorCode() == 42001 || error.getErrorCode() == 40001) {
// 强制设置wxMpConfigStorage它的access token过期了,这样在下一次请求里就会刷新access token
this.getWxMpConfigStorage().expireAccessToken();
if (this.getWxMpConfigStorage().autoRefreshToken()) {
return this.execute(executor, uri, data);
}
}

if (error.getErrorCode() != 0) {
this.log.error("\n[URL]: {}\n[PARAMS]: {}\n[RESPONSE]: {}", uri, data, error);
throw new WxErrorException(error);
}
return null;
} catch (IOException e) {
this.log.error("\n[URL]: {}\n[PARAMS]: {}\n[EXCEPTION]: {}", uri, data, e.getMessage());
throw new RuntimeException(e);
}
}

@Override
public ProxyInfo getHttpProxy() {
return this.httpProxy;
}


@Override
public WxMpConfigStorage getWxMpConfigStorage() {
return this.wxMpConfigStorage;
}

@Override
public void setWxMpConfigStorage(WxMpConfigStorage wxConfigProvider) {
this.wxMpConfigStorage = wxConfigProvider;
this.initHttpClient();
}

@Override
public void setRetrySleepMillis(int retrySleepMillis) {
this.retrySleepMillis = retrySleepMillis;
}

@Override
public void setMaxRetryTimes(int maxRetryTimes) {
this.maxRetryTimes = maxRetryTimes;
}

@Override
public WxMpKefuService getKefuService() {
return this.kefuService;
}

@Override
public WxMpMaterialService getMaterialService() {
return this.materialService;
}

@Override
public WxMpMenuService getMenuService() {
return this.menuService;
}

@Override
public WxMpUserService getUserService() {
return this.userService;
}

@Override
public WxMpUserTagService getUserTagService() {
return this.tagService;
}

@Override
public WxMpQrcodeService getQrcodeService() {
return this.qrCodeService;
}

@Override
public WxMpCardService getCardService() {
return this.cardService;
}

@Override
public WxMpDataCubeService getDataCubeService() {
return this.dataCubeService;
}

@Override
public WxMpUserBlacklistService getBlackListService() {
return this.blackListService;
}

@Override
public WxMpStoreService getStoreService() {
return this.storeService;
}

@Override
public WxMpTemplateMsgService getTemplateMsgService() {
return this.templateMsgService;
}

@Override
public WxMpDeviceService getDeviceService() {
return this.deviceService;
}
}

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpUserBlacklistServiceImpl.java → weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/jodd/WxMpUserBlacklistServiceImpl.java 查看文件

@@ -1,9 +1,11 @@
package me.chanjar.weixin.mp.api.impl;
package me.chanjar.weixin.mp.api.impl.jodd;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import jodd.http.HttpConnectionProvider;
import jodd.http.ProxyInfo;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
import me.chanjar.weixin.common.util.http.jodd.SimplePostRequestExecutor;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.WxMpUserBlacklistService;
import me.chanjar.weixin.mp.bean.result.WxMpUserBlacklistGetResult;
@@ -17,7 +19,7 @@ import java.util.Map;
*/
public class WxMpUserBlacklistServiceImpl implements WxMpUserBlacklistService {
private static final String API_BLACKLIST_PREFIX = "https://api.weixin.qq.com/cgi-bin/tags/members";
private WxMpService wxMpService;
private WxMpService<HttpConnectionProvider, ProxyInfo> wxMpService;

public WxMpUserBlacklistServiceImpl(WxMpService wxMpService) {
this.wxMpService = wxMpService;

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialDeleteRequestExecutor.java → weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/apache/MaterialDeleteRequestExecutor.java 查看文件

@@ -1,9 +1,9 @@
package me.chanjar.weixin.mp.util.http;
package me.chanjar.weixin.mp.util.http.apache;

import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.common.util.http.Utf8ResponseHandler;
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
@@ -16,7 +16,7 @@ import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class MaterialDeleteRequestExecutor implements RequestExecutor<Boolean, String> {
public class MaterialDeleteRequestExecutor implements RequestExecutor<Boolean,CloseableHttpClient, HttpHost, String> {


public MaterialDeleteRequestExecutor() {

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialNewsInfoRequestExecutor.java → weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/apache/MaterialNewsInfoRequestExecutor.java 查看文件

@@ -1,9 +1,9 @@
package me.chanjar.weixin.mp.util.http;
package me.chanjar.weixin.mp.util.http.apache;

import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.common.util.http.Utf8ResponseHandler;
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialNews;
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
@@ -18,7 +18,7 @@ import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class MaterialNewsInfoRequestExecutor implements RequestExecutor<WxMpMaterialNews, String> {
public class MaterialNewsInfoRequestExecutor implements RequestExecutor<WxMpMaterialNews,CloseableHttpClient, HttpHost, String> {

public MaterialNewsInfoRequestExecutor() {
super();

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialUploadRequestExecutor.java → weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/apache/MaterialUploadRequestExecutor.java 查看文件

@@ -1,9 +1,9 @@
package me.chanjar.weixin.mp.util.http;
package me.chanjar.weixin.mp.util.http.apache;

import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.common.util.http.Utf8ResponseHandler;
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import me.chanjar.weixin.mp.bean.material.WxMpMaterial;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialUploadResult;
@@ -16,10 +16,12 @@ import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;

import java.io.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Map;

public class MaterialUploadRequestExecutor implements RequestExecutor<WxMpMaterialUploadResult, WxMpMaterial> {
public class MaterialUploadRequestExecutor implements RequestExecutor<WxMpMaterialUploadResult,CloseableHttpClient, HttpHost, WxMpMaterial> {

@Override
public WxMpMaterialUploadResult execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, WxMpMaterial material) throws WxErrorException, IOException {

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialVideoInfoRequestExecutor.java → weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/apache/MaterialVideoInfoRequestExecutor.java 查看文件

@@ -1,9 +1,9 @@
package me.chanjar.weixin.mp.util.http;
package me.chanjar.weixin.mp.util.http.apache;

import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.common.util.http.Utf8ResponseHandler;
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialVideoInfoResult;
import org.apache.http.HttpHost;
@@ -17,7 +17,7 @@ import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class MaterialVideoInfoRequestExecutor implements RequestExecutor<WxMpMaterialVideoInfoResult, String> {
public class MaterialVideoInfoRequestExecutor implements RequestExecutor<WxMpMaterialVideoInfoResult, CloseableHttpClient, HttpHost, String> {

public MaterialVideoInfoRequestExecutor() {
super();

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MaterialVoiceAndImageDownloadRequestExecutor.java → weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/apache/MaterialVoiceAndImageDownloadRequestExecutor.java 查看文件

@@ -1,12 +1,10 @@
package me.chanjar.weixin.mp.util.http;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
package me.chanjar.weixin.mp.util.http.apache;

import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.common.util.http.apache.InputStreamResponseHandler;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
@@ -15,13 +13,14 @@ import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;

import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.InputStreamResponseHandler;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public class MaterialVoiceAndImageDownloadRequestExecutor implements RequestExecutor<InputStream, String> {
public class MaterialVoiceAndImageDownloadRequestExecutor implements RequestExecutor<InputStream,CloseableHttpClient, HttpHost, String> {


public MaterialVoiceAndImageDownloadRequestExecutor() {

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/MediaImgUploadRequestExecutor.java → weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/apache/MediaImgUploadRequestExecutor.java 查看文件

@@ -1,9 +1,9 @@
package me.chanjar.weixin.mp.util.http;
package me.chanjar.weixin.mp.util.http.apache;

import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.common.util.http.Utf8ResponseHandler;
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
import me.chanjar.weixin.mp.bean.material.WxMediaImgUploadResult;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
@@ -21,7 +21,7 @@ import java.io.IOException;
/**
* @author miller
*/
public class MediaImgUploadRequestExecutor implements RequestExecutor<WxMediaImgUploadResult, File> {
public class MediaImgUploadRequestExecutor implements RequestExecutor<WxMediaImgUploadResult, CloseableHttpClient, HttpHost, File> {
@Override
public WxMediaImgUploadResult execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, File data) throws WxErrorException, IOException {
if (data == null) {

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/QrCodeRequestExecutor.java → weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/apache/QrCodeRequestExecutor.java 查看文件

@@ -1,11 +1,11 @@
package me.chanjar.weixin.mp.util.http;
package me.chanjar.weixin.mp.util.http.apache;

import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.fs.FileUtils;
import me.chanjar.weixin.common.util.http.InputStreamResponseHandler;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.common.util.http.Utf8ResponseHandler;
import me.chanjar.weixin.common.util.http.apache.InputStreamResponseHandler;
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
import org.apache.http.Header;
import org.apache.http.HttpHost;
@@ -26,20 +26,20 @@ import java.util.UUID;
* @author chanjarster
*
*/
public class QrCodeRequestExecutor implements RequestExecutor<File, WxMpQrCodeTicket> {
public class QrCodeRequestExecutor implements RequestExecutor<File, CloseableHttpClient, HttpHost, WxMpQrCodeTicket> {

@Override
public File execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri,
public File execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri,
WxMpQrCodeTicket ticket) throws WxErrorException, IOException {
if (ticket != null) {
if (uri.indexOf('?') == -1) {
uri += '?';
}
uri += uri.endsWith("?")
? "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);
if (httpProxy != null) {
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();

+ 39
- 0
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/jodd/MaterialDeleteRequestExecutor.java 查看文件

@@ -0,0 +1,39 @@
package me.chanjar.weixin.mp.util.http.jodd;

import jodd.http.HttpConnectionProvider;
import jodd.http.HttpRequest;
import jodd.http.HttpResponse;
import jodd.http.ProxyInfo;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestExecutor;

import java.io.IOException;

public class MaterialDeleteRequestExecutor implements RequestExecutor<Boolean, HttpConnectionProvider, ProxyInfo, String> {


public MaterialDeleteRequestExecutor() {
super();
}

@Override
public Boolean execute(HttpConnectionProvider httpclient, ProxyInfo httpProxy, String uri, String materialId) throws WxErrorException, IOException {
HttpRequest request = HttpRequest.post(uri);
if (httpProxy != null) {
httpclient.useProxy(httpProxy);
}
request.withConnectionProvider(httpclient);

request.query("media_id", materialId);
HttpResponse response = request.send();
String responseContent = response.bodyText();
WxError error = WxError.fromJson(responseContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
} else {
return true;
}
}

}

+ 41
- 0
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/jodd/MaterialNewsInfoRequestExecutor.java 查看文件

@@ -0,0 +1,41 @@
package me.chanjar.weixin.mp.util.http.jodd;

import jodd.http.HttpConnectionProvider;
import jodd.http.HttpRequest;
import jodd.http.HttpResponse;
import jodd.http.ProxyInfo;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialNews;
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;

import java.io.IOException;

public class MaterialNewsInfoRequestExecutor implements RequestExecutor<WxMpMaterialNews, HttpConnectionProvider, ProxyInfo, String> {

public MaterialNewsInfoRequestExecutor() {
super();
}

@Override
public WxMpMaterialNews execute(HttpConnectionProvider httpclient, ProxyInfo httpProxy, String uri, String materialId) throws WxErrorException, IOException {
HttpRequest request = HttpRequest.post(uri);
if (httpProxy != null) {
httpclient.useProxy(httpProxy);
}
request.withConnectionProvider(httpclient);

request.query("media_id", materialId);
HttpResponse response = request.send();

String responseContent = request.bodyText();
WxError error = WxError.fromJson(responseContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
} else {
return WxMpGsonBuilder.create().fromJson(responseContent, WxMpMaterialNews.class);
}
}

}

+ 53
- 0
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/jodd/MaterialUploadRequestExecutor.java 查看文件

@@ -0,0 +1,53 @@
package me.chanjar.weixin.mp.util.http.jodd;

import jodd.http.HttpConnectionProvider;
import jodd.http.HttpRequest;
import jodd.http.HttpResponse;
import jodd.http.ProxyInfo;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import me.chanjar.weixin.mp.bean.material.WxMpMaterial;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialUploadResult;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Map;

public class MaterialUploadRequestExecutor implements RequestExecutor<WxMpMaterialUploadResult, HttpConnectionProvider, ProxyInfo, WxMpMaterial> {

@Override
public WxMpMaterialUploadResult execute(HttpConnectionProvider provider, ProxyInfo httpProxy, String uri, WxMpMaterial material) throws WxErrorException, IOException {
HttpRequest request = HttpRequest.post(uri);
if (httpProxy != null) {
provider.useProxy(httpProxy);
}
request.withConnectionProvider(provider);

if (material == null) {
throw new WxErrorException(WxError.newBuilder().setErrorMsg("非法请求,material参数为空").build());
}

File file = material.getFile();
if (file == null || !file.exists()) {
throw new FileNotFoundException();
}
request.form("media", file);
Map<String, String> form = material.getForm();
if (material.getForm() != null) {
request.form("description", WxGsonBuilder.create().toJson(form));
}

HttpResponse response = request.send();
String responseContent = response.bodyText();
WxError error = WxError.fromJson(responseContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
} else {
return WxMpMaterialUploadResult.fromJson(responseContent);
}
}

}

+ 39
- 0
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/jodd/MaterialVideoInfoRequestExecutor.java 查看文件

@@ -0,0 +1,39 @@
package me.chanjar.weixin.mp.util.http.jodd;

import jodd.http.HttpConnectionProvider;
import jodd.http.HttpRequest;
import jodd.http.HttpResponse;
import jodd.http.ProxyInfo;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialVideoInfoResult;

import java.io.IOException;

public class MaterialVideoInfoRequestExecutor implements RequestExecutor<WxMpMaterialVideoInfoResult, HttpConnectionProvider, ProxyInfo, String> {

public MaterialVideoInfoRequestExecutor() {
super();
}

@Override
public WxMpMaterialVideoInfoResult execute(HttpConnectionProvider provider, ProxyInfo httpProxy, String uri, String materialId) throws WxErrorException, IOException {
HttpRequest request = HttpRequest.post(uri);
if (httpProxy != null) {
provider.useProxy(httpProxy);
}
request.withConnectionProvider(provider);

request.query("media_id", materialId);
HttpResponse response =request.send();
String responseContent = response.bodyText();
WxError error = WxError.fromJson(responseContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
} else {
return WxMpMaterialVideoInfoResult.fromJson(responseContent);
}
}

}

+ 57
- 0
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/jodd/MaterialVoiceAndImageDownloadRequestExecutor.java 查看文件

@@ -0,0 +1,57 @@
package me.chanjar.weixin.mp.util.http.jodd;

import jodd.http.HttpConnectionProvider;
import jodd.http.HttpRequest;
import jodd.http.HttpResponse;
import jodd.http.ProxyInfo;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import org.apache.commons.io.IOUtils;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

public class MaterialVoiceAndImageDownloadRequestExecutor implements RequestExecutor<InputStream, HttpConnectionProvider, ProxyInfo, String> {


public MaterialVoiceAndImageDownloadRequestExecutor() {
super();
}

public MaterialVoiceAndImageDownloadRequestExecutor(File tmpDirFile) {
super();
}

@Override
public InputStream execute(HttpConnectionProvider provider, ProxyInfo httpProxy, String uri, String materialId) throws WxErrorException, IOException {
HttpRequest request = HttpRequest.post(uri);
if (httpProxy != null) {
provider.useProxy(httpProxy);
}
request.withConnectionProvider(provider);

request.query("media_id", materialId);
HttpResponse response = request.send();

InputStream inputStream = new ByteArrayInputStream(response.bodyBytes());
// 下载媒体文件出错
byte[] responseContent = IOUtils.toByteArray(inputStream);
String responseContentString = new String(responseContent, "UTF-8");
if (responseContentString.length() < 100) {
try {
WxError wxError = WxGsonBuilder.create().fromJson(responseContentString, WxError.class);
if (wxError.getErrorCode() != 0) {
throw new WxErrorException(wxError);
}
} catch (com.google.gson.JsonSyntaxException ex) {
return new ByteArrayInputStream(responseContent);
}
}
return new ByteArrayInputStream(responseContent);
}

}

+ 42
- 0
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/jodd/MediaImgUploadRequestExecutor.java 查看文件

@@ -0,0 +1,42 @@
package me.chanjar.weixin.mp.util.http.jodd;

import jodd.http.HttpConnectionProvider;
import jodd.http.HttpRequest;
import jodd.http.HttpResponse;
import jodd.http.ProxyInfo;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.mp.bean.material.WxMediaImgUploadResult;

import java.io.File;
import java.io.IOException;

/**
* @author miller
*/
public class MediaImgUploadRequestExecutor implements RequestExecutor<WxMediaImgUploadResult, HttpConnectionProvider, ProxyInfo, File> {
@Override
public WxMediaImgUploadResult execute(HttpConnectionProvider provider, ProxyInfo httpProxy, String uri, File data) throws WxErrorException, IOException {
if (data == null) {
throw new WxErrorException(WxError.newBuilder().setErrorMsg("文件对象为空").build());
}

HttpRequest request = HttpRequest.post(uri);
if (httpProxy != null) {
provider.useProxy(httpProxy);
}
request.withConnectionProvider(provider);

request.form("media", data);
HttpResponse response =request.send();

String responseContent =response.bodyText();
WxError error = WxError.fromJson(responseContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
}

return WxMediaImgUploadResult.fromJson(responseContent);
}
}

+ 60
- 0
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/jodd/QrCodeRequestExecutor.java 查看文件

@@ -0,0 +1,60 @@
package me.chanjar.weixin.mp.util.http.jodd;

import jodd.http.HttpConnectionProvider;
import jodd.http.HttpRequest;
import jodd.http.HttpResponse;
import jodd.http.ProxyInfo;
import jodd.util.MimeTypes;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.fs.FileUtils;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.UUID;

/**
* 获得QrCode图片 请求执行器
*
* @author chanjarster
*/
public class QrCodeRequestExecutor implements RequestExecutor<File, HttpConnectionProvider, ProxyInfo, WxMpQrCodeTicket> {

@Override
public File execute(HttpConnectionProvider provider, ProxyInfo httpProxy, String uri,
WxMpQrCodeTicket ticket) throws WxErrorException, 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");
}


HttpRequest request = HttpRequest.get(uri);
if (httpProxy != null) {
provider.useProxy(httpProxy);
}
request.withConnectionProvider(provider);

HttpResponse response = request.send();
try (
InputStream inputStream = new ByteArrayInputStream(response.bodyBytes());) {
String contentTypeHeader = response.header("Content-Type");
// 出错
if (MimeTypes.MIME_TEXT_PLAIN.equals(contentTypeHeader)) {
String responseContent = response.bodyText();
throw new WxErrorException(WxError.fromJson(responseContent));
}
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg");
}
}

}

+ 7
- 4
weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/WxMpBusyRetryTest.java 查看文件

@@ -3,8 +3,11 @@ package me.chanjar.weixin.mp.api;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import org.testng.annotations.*;
import me.chanjar.weixin.mp.api.impl.apache.WxMpServiceImpl;
import org.apache.http.HttpHost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
@@ -19,8 +22,8 @@ public class WxMpBusyRetryTest {
WxMpService service = new WxMpServiceImpl() {

@Override
protected synchronized <T, E> T executeInternal(
RequestExecutor<T, E> executor, String uri, E data)
public synchronized <T, E> T executeInternal(
RequestExecutor<T, CloseableHttpClient, HttpHost, E> executor, String uri, E data)
throws WxErrorException {
this.log.info("Executed");
WxError error = new WxError();


+ 1
- 1
weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpStoreServiceImplTest.java 查看文件

@@ -25,7 +25,7 @@ public class WxMpStoreServiceImplTest {
private WxMpService wxMpService;

/**
* Test method for {@link me.chanjar.weixin.mp.api.impl.WxMpStoreServiceImpl#add(me.chanjar.weixin.mp.bean.store.WxMpStoreBaseInfo)}.
* Test method for {@link WxMpStoreServiceImpl#add(me.chanjar.weixin.mp.bean.store.WxMpStoreBaseInfo)}.
*
* @throws WxErrorException
*/


+ 1
- 1
weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/test/ApiTestModule.java 查看文件

@@ -6,7 +6,7 @@ import com.thoughtworks.xstream.XStream;
import me.chanjar.weixin.common.util.xml.XStreamInitializer;
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.api.impl.apache.WxMpServiceImpl;

import java.io.IOException;
import java.io.InputStream;


+ 1
- 1
weixin-java-mp/src/test/java/me/chanjar/weixin/mp/demo/WxMpDemoServer.java 查看文件

@@ -5,7 +5,7 @@ import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpMessageHandler;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.api.impl.apache.WxMpServiceImpl;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;


Loading…
取消
儲存