|
|
@@ -18,17 +18,18 @@ import com.fs.qwApi.param.*;
|
|
|
import com.fs.qwApi.service.QwApiService;
|
|
|
import com.fs.voice.utils.StringUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import okhttp3.OkHttpClient;
|
|
|
import org.apache.hc.core5.net.URIBuilder;
|
|
|
import org.apache.http.HttpEntity;
|
|
|
-import org.apache.http.HttpResponse;
|
|
|
-import org.apache.http.client.HttpClient;
|
|
|
+import org.apache.http.client.config.RequestConfig;
|
|
|
+import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
import org.apache.http.client.methods.HttpGet;
|
|
|
import org.apache.http.client.methods.HttpPost;
|
|
|
import org.apache.http.entity.ContentType;
|
|
|
import org.apache.http.entity.StringEntity;
|
|
|
import org.apache.http.entity.mime.MultipartEntityBuilder;
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
import org.apache.http.impl.client.HttpClients;
|
|
|
+import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
|
|
import org.apache.http.util.EntityUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
@@ -58,11 +59,27 @@ public class QwApiServiceImpl implements QwApiService {
|
|
|
@Autowired
|
|
|
private RedisCache redisCache;
|
|
|
|
|
|
- private static final OkHttpClient client = new OkHttpClient.Builder()
|
|
|
- .connectTimeout(30, TimeUnit.SECONDS)
|
|
|
- .readTimeout(30, TimeUnit.SECONDS)
|
|
|
- .writeTimeout(30, TimeUnit.SECONDS)
|
|
|
- .build();
|
|
|
+ private final CloseableHttpClient httpClient;
|
|
|
+ {
|
|
|
+ // 1. 手动创建连接池管理器
|
|
|
+ PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
|
|
|
+ cm.setMaxTotal(200);
|
|
|
+ cm.setDefaultMaxPerRoute(50);
|
|
|
+
|
|
|
+ // 2. 手动创建配置
|
|
|
+ RequestConfig requestConfig = RequestConfig.custom()
|
|
|
+ .setConnectTimeout(5000)
|
|
|
+ .setSocketTimeout(10000)
|
|
|
+ .setConnectionRequestTimeout(2000)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ // 3. 通过 Builder 构建
|
|
|
+ httpClient = HttpClients.custom()
|
|
|
+ .setConnectionManager(cm)
|
|
|
+ .setDefaultRequestConfig(requestConfig)
|
|
|
+ .build();
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 获取客户群列表 ownerFilterList 过滤的群主list,String cursor分页数据游标
|
|
|
*/
|
|
|
@@ -627,7 +644,6 @@ public class QwApiServiceImpl implements QwApiService {
|
|
|
|
|
|
public QwUploadImgResult uploadimgs(String url, String corpId) throws IOException {
|
|
|
|
|
|
- HttpClient httpClient = HttpClients.createDefault();
|
|
|
// CompanyConfig companyConfig = companyConfigService.selectCompanyConfigByKey(corpId, "sys:qw:config");
|
|
|
// QWCompanyConfig qwCompanyConfig = JSON.parseObject(companyConfig.getConfigValue(), QWCompanyConfig.class);
|
|
|
// String corpId = qwCompanyConfig.getCorpId();
|
|
|
@@ -664,14 +680,12 @@ public class QwApiServiceImpl implements QwApiService {
|
|
|
httpPost.setEntity(multipart);
|
|
|
|
|
|
// 发送请求
|
|
|
- HttpResponse response = httpClient.execute(httpPost);
|
|
|
- String json = EntityUtils.toString(response.getEntity());
|
|
|
-
|
|
|
- QwUploadImgResult qwUploadImgResult = JSON.parseObject(json, QwUploadImgResult.class);
|
|
|
-
|
|
|
- return qwUploadImgResult;
|
|
|
+ try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
|
|
|
+ String json = EntityUtils.toString(response.getEntity());
|
|
|
+ return JSON.parseObject(json, QwUploadImgResult.class);
|
|
|
+ }
|
|
|
} catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ log.error("uploadimgs err={}", e.getMessage(), e);
|
|
|
}finally {
|
|
|
inputStream.close();
|
|
|
}
|
|
|
@@ -684,7 +698,6 @@ public class QwApiServiceImpl implements QwApiService {
|
|
|
@Override
|
|
|
public QwUploadResult upload(File file, String type,String corpId) throws IOException {
|
|
|
|
|
|
- HttpClient httpClient = HttpClients.createDefault();
|
|
|
// CompanyConfig companyConfig = companyConfigService.selectCompanyConfigByKey(corpId, "sys:qw:config");
|
|
|
// QWCompanyConfig qwCompanyConfig = JSON.parseObject(companyConfig.getConfigValue(), QWCompanyConfig.class);
|
|
|
// String corpId = qwCompanyConfig.getCorpId();
|
|
|
@@ -700,7 +713,7 @@ public class QwApiServiceImpl implements QwApiService {
|
|
|
|
|
|
String token = getToken(corpId, openSecret);
|
|
|
|
|
|
- URIBuilder uriBuilder = null;
|
|
|
+ URIBuilder uriBuilder;
|
|
|
try {
|
|
|
uriBuilder = new URIBuilder(QwApiConfig.upload);
|
|
|
uriBuilder.setParameter("access_token", token);
|
|
|
@@ -719,12 +732,10 @@ public class QwApiServiceImpl implements QwApiService {
|
|
|
httpPost.setHeader("Content-Type", "multipart/form-data; charset=UTF-8");
|
|
|
|
|
|
// 发送请求
|
|
|
- HttpResponse response = httpClient.execute(httpPost);
|
|
|
- String json = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
|
|
|
-
|
|
|
- QwUploadResult qwUploadResult = JSON.parseObject(json, QwUploadResult.class);
|
|
|
-
|
|
|
- return qwUploadResult;
|
|
|
+ try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
|
|
|
+ String json = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
|
|
|
+ return JSON.parseObject(json, QwUploadResult.class);
|
|
|
+ }
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
@@ -922,11 +933,11 @@ public class QwApiServiceImpl implements QwApiService {
|
|
|
|
|
|
@Override
|
|
|
public String getToken(String corpId,String corpSecret) {
|
|
|
- String key =(String)redisCache.getCacheObject("qwServer:corpId:" + corpId + ":" + corpSecret);
|
|
|
+ String key = redisCache.getCacheObject("qwServer:corpId:" + corpId + ":" + corpSecret);
|
|
|
if (!StringUtil.strIsNullOrEmpty(key)){
|
|
|
return key;
|
|
|
}
|
|
|
- HttpClient httpClient = HttpClients.createDefault();
|
|
|
+
|
|
|
String token=null;
|
|
|
try {
|
|
|
URIBuilder builder = new URIBuilder(QwApiConfig.getTokenUrl);
|
|
|
@@ -934,15 +945,15 @@ public class QwApiServiceImpl implements QwApiService {
|
|
|
builder.setParameter("corpsecret", corpSecret);
|
|
|
URI uri = builder.build();
|
|
|
HttpGet httpGet = new HttpGet(uri);
|
|
|
- HttpResponse response = httpClient.execute(httpGet);
|
|
|
-
|
|
|
- String tokenJson = EntityUtils.toString(response.getEntity());
|
|
|
- log.info("获取token:{}", tokenJson);
|
|
|
- JSONObject jsonObject = JSON.parseObject(tokenJson);
|
|
|
- token = jsonObject.getString("access_token");
|
|
|
- redisCache.setCacheObject("qwServer:corpId:"+corpId+":"+corpSecret,token,1,TimeUnit.HOURS);
|
|
|
+ try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
|
|
|
+ String tokenJson = EntityUtils.toString(response.getEntity());
|
|
|
+ log.info("获取token:{}", tokenJson);
|
|
|
+ JSONObject jsonObject = JSON.parseObject(tokenJson);
|
|
|
+ token = jsonObject.getString("access_token");
|
|
|
+ redisCache.setCacheObject("qwServer:corpId:"+corpId+":"+corpSecret,token,1,TimeUnit.HOURS);
|
|
|
+ }
|
|
|
} catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ log.error("获取token失败 err={}", e.getMessage(), e);
|
|
|
}
|
|
|
return token ;
|
|
|
}
|
|
|
@@ -951,19 +962,18 @@ public class QwApiServiceImpl implements QwApiService {
|
|
|
public QwcorpidToOpencorpidResult openCorpid(String corpId, String providerAccessToken) {
|
|
|
JSONObject json=new JSONObject();
|
|
|
json.put("corpid",corpId);
|
|
|
- HttpClient httpClient = HttpClients.createDefault();
|
|
|
try {
|
|
|
URIBuilder builder = new URIBuilder(QwApiConfig.corpidToOpencorpid);
|
|
|
builder.setParameter("provider_access_token", providerAccessToken);
|
|
|
URI uri = builder.build();
|
|
|
HttpPost httpPost = new HttpPost(uri);
|
|
|
httpPost.setEntity( new StringEntity(JSON.toJSONString(json),StandardCharsets.UTF_8));
|
|
|
- HttpResponse response = httpClient.execute(httpPost);
|
|
|
- String reJson = EntityUtils.toString(response.getEntity());
|
|
|
- QwcorpidToOpencorpidResult qwcorpid = JSON.parseObject(reJson, QwcorpidToOpencorpidResult.class);
|
|
|
- return qwcorpid;
|
|
|
+ try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
|
|
|
+ String reJson = EntityUtils.toString(response.getEntity());
|
|
|
+ return JSON.parseObject(reJson, QwcorpidToOpencorpidResult.class);
|
|
|
+ }
|
|
|
} catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ log.error("openCorpid err={}", e.getMessage(), e);
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
@@ -975,7 +985,6 @@ public class QwApiServiceImpl implements QwApiService {
|
|
|
@Override
|
|
|
public QwPermanentCodeResult getPermanentCode(QwPermanentCodeParam param, String suite_access_token) {
|
|
|
|
|
|
- HttpClient httpClient = HttpClients.createDefault();
|
|
|
try {
|
|
|
URIBuilder builder = new URIBuilder(QwApiConfig.getPermanentCode);
|
|
|
|
|
|
@@ -983,13 +992,12 @@ public class QwApiServiceImpl implements QwApiService {
|
|
|
URI uri = builder.build();
|
|
|
HttpPost httpPost = new HttpPost(uri);
|
|
|
httpPost.setEntity( new StringEntity(JSON.toJSONString(param),StandardCharsets.UTF_8));
|
|
|
- HttpResponse response = httpClient.execute(httpPost);
|
|
|
- String reJson = EntityUtils.toString(response.getEntity());
|
|
|
- QwPermanentCodeResult qwPermanentCodeResult = JSON.parseObject(reJson, QwPermanentCodeResult.class);
|
|
|
- return qwPermanentCodeResult;
|
|
|
-
|
|
|
+ try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
|
|
|
+ String reJson = EntityUtils.toString(response.getEntity());
|
|
|
+ return JSON.parseObject(reJson, QwPermanentCodeResult.class);
|
|
|
+ }
|
|
|
} catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ log.error("getPermanentCode err={}", e.getMessage(), e);
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
@@ -1004,19 +1012,19 @@ public class QwApiServiceImpl implements QwApiService {
|
|
|
if (StringUtils.isNotBlank(suite_access_token)) {
|
|
|
return suite_access_token;
|
|
|
}
|
|
|
- HttpClient httpClient = HttpClients.createDefault();
|
|
|
try {
|
|
|
URIBuilder builder = new URIBuilder(QwApiConfig.getSuiteToken);
|
|
|
URI uri = builder.build();
|
|
|
HttpPost httpPost = new HttpPost(uri);
|
|
|
httpPost.setEntity( new StringEntity(JSON.toJSONString(param),StandardCharsets.UTF_8));
|
|
|
- HttpResponse response = httpClient.execute(httpPost);
|
|
|
- String reJson = EntityUtils.toString(response.getEntity());
|
|
|
- JSONObject jsonObject = JSON.parseObject(reJson);
|
|
|
- token = jsonObject.getString("suite_access_token");
|
|
|
- redisCache.setCacheObject("qwServer:suite_access_token:"+param.getSuite_id(),token,2,TimeUnit.HOURS);
|
|
|
+ try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
|
|
|
+ String reJson = EntityUtils.toString(response.getEntity());
|
|
|
+ JSONObject jsonObject = JSON.parseObject(reJson);
|
|
|
+ token = jsonObject.getString("suite_access_token");
|
|
|
+ redisCache.setCacheObject("qwServer:suite_access_token:"+param.getSuite_id(),token,2,TimeUnit.HOURS);
|
|
|
+ }
|
|
|
} catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ log.error("getSuiteToken err={}", e.getMessage(), e);
|
|
|
}
|
|
|
return token;
|
|
|
}
|
|
|
@@ -1030,20 +1038,20 @@ public class QwApiServiceImpl implements QwApiService {
|
|
|
if (StringUtils.isNotBlank(providerToken)) {
|
|
|
return providerToken;
|
|
|
}
|
|
|
- HttpClient httpClient = HttpClients.createDefault();
|
|
|
String token=null;
|
|
|
try {
|
|
|
URIBuilder builder = new URIBuilder(QwApiConfig.getProviderToken);
|
|
|
URI uri = builder.build();
|
|
|
HttpPost httpPost = new HttpPost(uri);
|
|
|
httpPost.setEntity( new StringEntity(JSON.toJSONString(params),StandardCharsets.UTF_8));
|
|
|
- HttpResponse response = httpClient.execute(httpPost);
|
|
|
- String tokenJson = EntityUtils.toString(response.getEntity());
|
|
|
- JSONObject jsonObject = JSON.parseObject(tokenJson);
|
|
|
- token = jsonObject.getString("provider_access_token");
|
|
|
- redisCache.setCacheObject("qwServer:provider_access_token:"+params.getCorpid(),token,1,TimeUnit.HOURS);
|
|
|
+ try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
|
|
|
+ String tokenJson = EntityUtils.toString(response.getEntity());
|
|
|
+ JSONObject jsonObject = JSON.parseObject(tokenJson);
|
|
|
+ token = jsonObject.getString("provider_access_token");
|
|
|
+ redisCache.setCacheObject("qwServer:provider_access_token:"+params.getCorpid(),token,1,TimeUnit.HOURS);
|
|
|
+ }
|
|
|
} catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ log.error("getProviderToken err={}", e.getMessage(), e);
|
|
|
}
|
|
|
return token;
|
|
|
}
|
|
|
@@ -1059,7 +1067,6 @@ public class QwApiServiceImpl implements QwApiService {
|
|
|
return providerToken;
|
|
|
}
|
|
|
|
|
|
- HttpClient httpClient = HttpClients.createDefault();
|
|
|
String token=null;
|
|
|
try {
|
|
|
URIBuilder builder = new URIBuilder(QwApiConfig.getCorp_token);
|
|
|
@@ -1067,14 +1074,14 @@ public class QwApiServiceImpl implements QwApiService {
|
|
|
URI uri = builder.build();
|
|
|
HttpPost httpPost = new HttpPost(uri);
|
|
|
httpPost.setEntity( new StringEntity(JSON.toJSONString(param),StandardCharsets.UTF_8));
|
|
|
- HttpResponse response = httpClient.execute(httpPost);
|
|
|
- String tokenJson = EntityUtils.toString(response.getEntity());
|
|
|
- JSONObject jsonObject = JSON.parseObject(tokenJson);
|
|
|
- token = jsonObject.getString("access_token");
|
|
|
- redisCache.setCacheObject("qwServer:serverBook_access_token:"+param.getAuth_corpid(),token,1,TimeUnit.HOURS);
|
|
|
-
|
|
|
+ try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
|
|
|
+ String tokenJson = EntityUtils.toString(response.getEntity());
|
|
|
+ JSONObject jsonObject = JSON.parseObject(tokenJson);
|
|
|
+ token = jsonObject.getString("access_token");
|
|
|
+ redisCache.setCacheObject("qwServer:serverBook_access_token:"+param.getAuth_corpid(),token,1,TimeUnit.HOURS);
|
|
|
+ }
|
|
|
} catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ log.error("getBookAccessToken err={}", e.getMessage(), e);
|
|
|
}
|
|
|
return token;
|
|
|
}
|
|
|
@@ -1085,7 +1092,6 @@ public class QwApiServiceImpl implements QwApiService {
|
|
|
@Override
|
|
|
public String getServerQwUserName(String corpId,String Secret, String userid) {
|
|
|
|
|
|
- HttpClient httpClient = HttpClients.createDefault();
|
|
|
String name = "";
|
|
|
try {
|
|
|
URIBuilder builder = new URIBuilder(QwApiConfig.getServerQwUserName);
|
|
|
@@ -1093,15 +1099,15 @@ public class QwApiServiceImpl implements QwApiService {
|
|
|
builder.setParameter("userid", userid);
|
|
|
URI uri = builder.build();
|
|
|
HttpPost httpPost = new HttpPost(uri);
|
|
|
- HttpResponse response = httpClient.execute(httpPost);
|
|
|
- String reJson = EntityUtils.toString(response.getEntity());
|
|
|
- log.info("接口返回数据:{}", reJson);
|
|
|
- JSONObject jsonObject = JSON.parseObject(reJson);
|
|
|
- name = jsonObject.getString("name");
|
|
|
-
|
|
|
- return name;
|
|
|
+ try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
|
|
|
+ String reJson = EntityUtils.toString(response.getEntity());
|
|
|
+ log.info("接口返回数据:{}", reJson);
|
|
|
+ JSONObject jsonObject = JSON.parseObject(reJson);
|
|
|
+ name = jsonObject.getString("name");
|
|
|
+ return name;
|
|
|
+ }
|
|
|
} catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ log.error("getServerQwUserName err={}", e.getMessage(), e);
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
@@ -1114,19 +1120,19 @@ public class QwApiServiceImpl implements QwApiService {
|
|
|
|
|
|
QwJsapiTicketResult qwResult=new QwJsapiTicketResult();
|
|
|
|
|
|
- HttpClient httpClient = HttpClients.createDefault();
|
|
|
try {
|
|
|
URIBuilder builder = new URIBuilder(QwApiConfig.getuserinfo);
|
|
|
builder.setParameter("access_token", getToken(corpId,appSecret));
|
|
|
builder.setParameter("code",code);
|
|
|
URI uri = builder.build();
|
|
|
HttpGet httpGet = new HttpGet(uri);
|
|
|
- HttpResponse response = httpClient.execute(httpGet);
|
|
|
- String json = EntityUtils.toString(response.getEntity());
|
|
|
- qwResult = JSON.parseObject(json, QwJsapiTicketResult.class);
|
|
|
+ try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
|
|
|
+ String json = EntityUtils.toString(response.getEntity());
|
|
|
+ qwResult = JSON.parseObject(json, QwJsapiTicketResult.class);
|
|
|
+ }
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ log.error("getQwUserid err={}", e.getMessage(), e);
|
|
|
}
|
|
|
return qwResult;
|
|
|
}
|
|
|
@@ -1147,22 +1153,21 @@ public class QwApiServiceImpl implements QwApiService {
|
|
|
return qwResult;
|
|
|
}
|
|
|
|
|
|
- HttpClient httpClient = HttpClients.createDefault();
|
|
|
try {
|
|
|
URIBuilder builder = new URIBuilder(QwApiConfig.getJsapiTicket);
|
|
|
builder.setParameter("access_token", getToken(bookCorpId,bookSecret));
|
|
|
URI uri = builder.build();
|
|
|
HttpGet httpGet = new HttpGet(uri);
|
|
|
- HttpResponse response = httpClient.execute(httpGet);
|
|
|
- String json = EntityUtils.toString(response.getEntity());
|
|
|
+ try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
|
|
|
+ String json = EntityUtils.toString(response.getEntity());
|
|
|
|
|
|
- qwResult = JSON.parseObject(json, QwJsapiTicketResult.class);
|
|
|
+ qwResult = JSON.parseObject(json, QwJsapiTicketResult.class);
|
|
|
|
|
|
// redisTemplate.opsForValue().set("ticket:corpId"+":"+corpId,qwResult.getTicket(),1,TimeUnit.HOURS);
|
|
|
- redisCache.setCacheObject("ticket:corpId"+":"+bookCorpId,qwResult.getTicket(),1,TimeUnit.HOURS);
|
|
|
-
|
|
|
+ redisCache.setCacheObject("ticket:corpId"+":"+bookCorpId,qwResult.getTicket(),1,TimeUnit.HOURS);
|
|
|
+ }
|
|
|
} catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ log.error("getJsapiTicket err={}", e.getMessage(), e);
|
|
|
}
|
|
|
return qwResult;
|
|
|
}
|
|
|
@@ -1184,24 +1189,24 @@ public class QwApiServiceImpl implements QwApiService {
|
|
|
return qwResult;
|
|
|
}
|
|
|
|
|
|
- HttpClient httpClient = HttpClients.createDefault();
|
|
|
try {
|
|
|
URIBuilder builder = new URIBuilder(QwApiConfig.jsapiTicket);
|
|
|
builder.setParameter("access_token", getToken(corpId,appSecret));
|
|
|
builder.setParameter("type","agent_config");
|
|
|
URI uri = builder.build();
|
|
|
HttpGet httpGet = new HttpGet(uri);
|
|
|
- HttpResponse response = httpClient.execute(httpGet);
|
|
|
- String json = EntityUtils.toString(response.getEntity());
|
|
|
+ try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
|
|
|
+ String json = EntityUtils.toString(response.getEntity());
|
|
|
|
|
|
- qwResult = JSON.parseObject(json, QwJsapiTicketResult.class);
|
|
|
- qwResult.setCorpId(corpId);
|
|
|
+ qwResult = JSON.parseObject(json, QwJsapiTicketResult.class);
|
|
|
+ qwResult.setCorpId(corpId);
|
|
|
|
|
|
// redisTemplate.opsForValue().set("ticketApp:corpId"+":"+corpId,qwResult.getTicket(),1,TimeUnit.HOURS);
|
|
|
- redisCache.setCacheObject("ticketApp:corpId"+":"+corpId,qwResult.getTicket(),1,TimeUnit.HOURS);
|
|
|
- return qwResult;
|
|
|
+ redisCache.setCacheObject("ticketApp:corpId"+":"+corpId,qwResult.getTicket(),1,TimeUnit.HOURS);
|
|
|
+ return qwResult;
|
|
|
+ }
|
|
|
} catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ log.error("getJsapiTicketApp err={}", e.getMessage(), e);
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
@@ -1212,7 +1217,6 @@ public class QwApiServiceImpl implements QwApiService {
|
|
|
@Override
|
|
|
public QwUploadAttachmentResult uploadAttachment(String url, String type, String corpId) throws IOException {
|
|
|
|
|
|
- HttpClient httpClient = HttpClients.createDefault();
|
|
|
// CompanyConfig companyConfig = companyConfigService.selectCompanyConfigByKey(corpId, "sys:qw:config");
|
|
|
// QWCompanyConfig qwCompanyConfig = JSON.parseObject(companyConfig.getConfigValue(), QWCompanyConfig.class);
|
|
|
|
|
|
@@ -1248,14 +1252,13 @@ public class QwApiServiceImpl implements QwApiService {
|
|
|
httpPost.setEntity(multipart);
|
|
|
|
|
|
// 发送请求
|
|
|
- HttpResponse response = httpClient.execute(httpPost);
|
|
|
- String json = EntityUtils.toString(response.getEntity());
|
|
|
-
|
|
|
- QwUploadAttachmentResult qwUploadImgResult = JSON.parseObject(json, QwUploadAttachmentResult.class);
|
|
|
+ try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
|
|
|
+ String json = EntityUtils.toString(response.getEntity());
|
|
|
|
|
|
- return qwUploadImgResult;
|
|
|
+ return JSON.parseObject(json, QwUploadAttachmentResult.class);
|
|
|
+ }
|
|
|
} catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ log.error("uploadAttachment err={}", e.getMessage(), e);
|
|
|
}finally {
|
|
|
inputStream.close();
|
|
|
}
|
|
|
@@ -1288,7 +1291,6 @@ public class QwApiServiceImpl implements QwApiService {
|
|
|
|
|
|
String appSecret = qwCompany.getOpenSecret();
|
|
|
|
|
|
- HttpClient httpClient = HttpClients.createDefault();
|
|
|
try {
|
|
|
|
|
|
URIBuilder builder = new URIBuilder(QwApiConfig.getMomentTaskResult);
|
|
|
@@ -1296,13 +1298,12 @@ public class QwApiServiceImpl implements QwApiService {
|
|
|
builder.setParameter("jobid", jobid);
|
|
|
URI uri = builder.build();
|
|
|
HttpGet httpGet = new HttpGet(uri);
|
|
|
- HttpResponse response = httpClient.execute(httpGet);
|
|
|
- String json = EntityUtils.toString(response.getEntity());
|
|
|
-
|
|
|
- QwGetMomentTaskResult qwResult = JSON.parseObject(json, QwGetMomentTaskResult.class);
|
|
|
- return qwResult;
|
|
|
+ try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
|
|
|
+ String json = EntityUtils.toString(response.getEntity());
|
|
|
+ return JSON.parseObject(json, QwGetMomentTaskResult.class);
|
|
|
+ }
|
|
|
} catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ log.error("getMomentTaskResult err={}", e.getMessage(), e);
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
@@ -1373,25 +1374,25 @@ public class QwApiServiceImpl implements QwApiService {
|
|
|
while (retryCount < maxRetries) {
|
|
|
try {
|
|
|
// 2. 构造并发送请求
|
|
|
- HttpClient httpClient = HttpClients.createDefault();
|
|
|
URIBuilder builder = new URIBuilder(url);
|
|
|
builder.setParameter("access_token", getToken(corpId, appSecret));
|
|
|
URI uri = builder.build();
|
|
|
HttpPost httpPost = new HttpPost(uri);
|
|
|
httpPost.setEntity(new StringEntity(JSON.toJSONString(param), StandardCharsets.UTF_8));
|
|
|
- HttpResponse response = httpClient.execute(httpPost);
|
|
|
- // 3. 解析结果
|
|
|
- String reJson = EntityUtils.toString(response.getEntity());
|
|
|
- QwResult qwResult = JSON.parseObject(reJson, QwResult.class);
|
|
|
- // 4. 判断错误码
|
|
|
- if (qwResult.getErrcode() == 45035) {
|
|
|
- retryCount++;
|
|
|
- Thread.sleep(2000);
|
|
|
- } else {
|
|
|
- return reJson;
|
|
|
+ try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
|
|
|
+ // 3. 解析结果
|
|
|
+ String reJson = EntityUtils.toString(response.getEntity());
|
|
|
+ QwResult qwResult = JSON.parseObject(reJson, QwResult.class);
|
|
|
+ // 4. 判断错误码
|
|
|
+ if (qwResult.getErrcode() == 45035) {
|
|
|
+ retryCount++;
|
|
|
+ Thread.sleep(2000);
|
|
|
+ } else {
|
|
|
+ return reJson;
|
|
|
+ }
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ log.error("sendPost err={}", e.getMessage(), e);
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
@@ -1437,7 +1438,6 @@ public class QwApiServiceImpl implements QwApiService {
|
|
|
|
|
|
String bookSecret = qwCompany.getServerBookSecret();
|
|
|
|
|
|
- HttpClient httpClient = HttpClients.createDefault();
|
|
|
try {
|
|
|
URIBuilder builder = new URIBuilder(url);
|
|
|
|
|
|
@@ -1445,11 +1445,11 @@ public class QwApiServiceImpl implements QwApiService {
|
|
|
URI uri = builder.build();
|
|
|
HttpPost httpPost = new HttpPost(uri);
|
|
|
httpPost.setEntity( new StringEntity(JSON.toJSONString(param),StandardCharsets.UTF_8));
|
|
|
- HttpResponse response = httpClient.execute(httpPost);
|
|
|
- String reJson = EntityUtils.toString(response.getEntity());
|
|
|
- return reJson;
|
|
|
+ try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
|
|
|
+ return EntityUtils.toString(response.getEntity());
|
|
|
+ }
|
|
|
} catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ log.error("sendBookPost err={}", e.getMessage(), e);
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
@@ -1501,7 +1501,6 @@ public class QwApiServiceImpl implements QwApiService {
|
|
|
|
|
|
@Override
|
|
|
public QwUserIdResult getUserList(String corpId) {
|
|
|
- HttpClient httpClient = HttpClients.createDefault();
|
|
|
QwCompany qwCompany = iQwCompanyService.selectQwCompanyByCorpId(corpId);
|
|
|
String bookSecret = qwCompany.getServerBookSecret();
|
|
|
try {
|
|
|
@@ -1513,13 +1512,12 @@ public class QwApiServiceImpl implements QwApiService {
|
|
|
Map<String, Object> map = new HashMap<>();
|
|
|
map.put("limit", 10000);
|
|
|
httpPost.setEntity( new StringEntity(JSON.toJSONString(map)));
|
|
|
- HttpResponse response = httpClient.execute(httpPost);
|
|
|
- String reJson = EntityUtils.toString(response.getEntity());
|
|
|
- QwUserIdResult qwResult = JSON.parseObject(reJson, QwUserIdResult.class);
|
|
|
-
|
|
|
- return qwResult;
|
|
|
+ try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
|
|
|
+ String reJson = EntityUtils.toString(response.getEntity());
|
|
|
+ return JSON.parseObject(reJson, QwUserIdResult.class);
|
|
|
+ }
|
|
|
} catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ log.error("getUserList err={}", e.getMessage(), e);
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
@@ -1537,18 +1535,17 @@ public class QwApiServiceImpl implements QwApiService {
|
|
|
|
|
|
String bookSecret = qwCompany.getOpenSecret();
|
|
|
|
|
|
- HttpClient httpClient = HttpClients.createDefault();
|
|
|
try {
|
|
|
URIBuilder builder = new URIBuilder(QwApiConfig.getDepartmentList);
|
|
|
builder.setParameter("access_token", getToken(corpId,bookSecret));
|
|
|
URI uri = builder.build();
|
|
|
HttpGet httpGet = new HttpGet(uri);
|
|
|
- HttpResponse response = httpClient.execute(httpGet);
|
|
|
- String json = EntityUtils.toString(response.getEntity());
|
|
|
- QwDeptResult qwResult = JSON.parseObject(json, QwDeptResult.class);
|
|
|
- return qwResult;
|
|
|
+ try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
|
|
|
+ String json = EntityUtils.toString(response.getEntity());
|
|
|
+ return JSON.parseObject(json, QwDeptResult.class);
|
|
|
+ }
|
|
|
} catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ log.error("getDepartmentList err={}", e.getMessage(), e);
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
@@ -1565,7 +1562,6 @@ public class QwApiServiceImpl implements QwApiService {
|
|
|
QwCompany qwCompany = iQwCompanyService.selectQwCompanyByCorpId(corpId);
|
|
|
|
|
|
String appSecret = qwCompany.getOpenSecret();
|
|
|
- HttpClient httpClient = HttpClients.createDefault();
|
|
|
try {
|
|
|
|
|
|
URIBuilder builder = new URIBuilder(QwApiConfig.externalcontactListUrl);
|
|
|
@@ -1573,12 +1569,12 @@ public class QwApiServiceImpl implements QwApiService {
|
|
|
builder.setParameter("userid", userId);
|
|
|
URI uri = builder.build();
|
|
|
HttpGet httpGet = new HttpGet(uri);
|
|
|
- HttpResponse response = httpClient.execute(httpGet);
|
|
|
- String json = EntityUtils.toString(response.getEntity());
|
|
|
- QwExternalContactListResult qwResult = JSON.parseObject(json, QwExternalContactListResult.class);
|
|
|
- return qwResult;
|
|
|
+ try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
|
|
|
+ String json = EntityUtils.toString(response.getEntity(), "UTF-8");
|
|
|
+ return JSON.parseObject(json, QwExternalContactListResult.class);
|
|
|
+ }
|
|
|
} catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ log.error("获取外部联系人列表失败, userId={}, corpId={}, err={}", userId, corpId, e.getMessage(), e);
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
@@ -1588,19 +1584,18 @@ public class QwApiServiceImpl implements QwApiService {
|
|
|
String appSecret = getAppSecret(corpId);
|
|
|
|
|
|
String token = getToken(corpId, appSecret);
|
|
|
- HttpClient httpClient = HttpClients.createDefault();
|
|
|
try {
|
|
|
URIBuilder builder = new URIBuilder(QwApiConfig.externalcontactUrl);
|
|
|
builder.setParameter("access_token", token);
|
|
|
builder.setParameter("external_userid", userId);
|
|
|
URI uri = builder.build();
|
|
|
HttpGet httpGet = new HttpGet(uri);
|
|
|
- HttpResponse response = httpClient.execute(httpGet);
|
|
|
- String json = EntityUtils.toString(response.getEntity());
|
|
|
- QwExternalContactResult qwResult = JSON.parseObject(json, QwExternalContactResult.class);
|
|
|
- return qwResult;
|
|
|
+ try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
|
|
|
+ String json = EntityUtils.toString(response.getEntity());
|
|
|
+ return JSON.parseObject(json, QwExternalContactResult.class);
|
|
|
+ }
|
|
|
} catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ log.error("getExternalcontact, userId={}, corpId={}, err={}", userId, corpId, e.getMessage(), e);
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
@@ -1845,7 +1840,6 @@ public class QwApiServiceImpl implements QwApiService {
|
|
|
* 封装的post请求
|
|
|
*/
|
|
|
public String templateResultPost(JSONObject object, String url,String corpId){
|
|
|
- HttpClient httpClient = HttpClients.createDefault();
|
|
|
URIBuilder builder = null;
|
|
|
try {
|
|
|
builder = new URIBuilder(url);
|
|
|
@@ -1869,12 +1863,11 @@ public class QwApiServiceImpl implements QwApiService {
|
|
|
httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
|
|
|
|
|
|
httpPost.setEntity( new StringEntity(JSON.toJSONString(object),"UTF-8"));
|
|
|
- HttpResponse response = httpClient.execute(httpPost);
|
|
|
- String reJson = EntityUtils.toString(response.getEntity());
|
|
|
-
|
|
|
- return reJson;
|
|
|
+ try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
|
|
|
+ return EntityUtils.toString(response.getEntity());
|
|
|
+ }
|
|
|
} catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ log.error("templateResultPost err={}", e.getMessage(), e);
|
|
|
}
|
|
|
return null;
|
|
|
}
|