|
|
|
@@ -0,0 +1,113 @@ |
|
|
|
package com.iformall.douyin.web.api; |
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON; |
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
import me.chanjar.weixin.common.WxType; |
|
|
|
import me.chanjar.weixin.common.error.WxError; |
|
|
|
import me.chanjar.weixin.common.error.WxErrorException; |
|
|
|
import me.chanjar.weixin.common.util.http.RequestExecutor; |
|
|
|
import me.chanjar.weixin.common.util.http.RequestHttp; |
|
|
|
import me.chanjar.weixin.common.util.http.ResponseHandler; |
|
|
|
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.apache.http.HttpHost; |
|
|
|
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.impl.client.CloseableHttpClient; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
import java.util.regex.Matcher; |
|
|
|
import java.util.regex.Pattern; |
|
|
|
|
|
|
|
/** |
|
|
|
* . |
|
|
|
* |
|
|
|
* @author ecoolper |
|
|
|
* @date 2017/5/4 |
|
|
|
*/ |
|
|
|
public class TtWebGoodsCategoryGetRequestExecutor implements RequestExecutor<String, String> { |
|
|
|
private final Logger logger = LoggerFactory.getLogger(this.getClass()); |
|
|
|
|
|
|
|
protected RequestHttp<CloseableHttpClient, HttpHost> requestHttp; |
|
|
|
|
|
|
|
public TtWebGoodsCategoryGetRequestExecutor(RequestHttp requestHttp) { |
|
|
|
this.requestHttp = requestHttp; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void execute(String uri, String data, ResponseHandler<String> handler, WxType wxType) throws WxErrorException, IOException { |
|
|
|
handler.handle(this.execute(uri, data, wxType)); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public String execute(String uri, String queryParam, WxType wxType) throws WxErrorException, IOException { |
|
|
|
|
|
|
|
String access_token = handleUrl(uri); |
|
|
|
if(StringUtils.isBlank(access_token)){ |
|
|
|
WxError wxError = WxError.fromJson("{\n" + |
|
|
|
" \"description\": \"未找到access_token\",\n" + |
|
|
|
" \"error_code\": -1\n" + |
|
|
|
" }"); |
|
|
|
throw new WxErrorException(wxError); |
|
|
|
} |
|
|
|
|
|
|
|
if (queryParam != null) { |
|
|
|
if (uri.indexOf('?') == -1) { |
|
|
|
uri += '?'; |
|
|
|
} |
|
|
|
uri += uri.endsWith("?") ? queryParam : '&' + queryParam; |
|
|
|
} |
|
|
|
|
|
|
|
HttpGet httpGet = new HttpGet(uri); |
|
|
|
httpGet.addHeader("Content-Type","application/json"); |
|
|
|
httpGet.addHeader("access-token",access_token); |
|
|
|
// logger.info("get请求地址uri{}"+uri); |
|
|
|
// logger.info("header-----access_token{}"+access_token); |
|
|
|
|
|
|
|
if (requestHttp.getRequestHttpProxy() != null) { |
|
|
|
RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build(); |
|
|
|
httpGet.setConfig(config); |
|
|
|
} |
|
|
|
|
|
|
|
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpGet)) { |
|
|
|
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); |
|
|
|
logger.info("response{}"+responseContent); |
|
|
|
int code = 0; |
|
|
|
String msg = null; |
|
|
|
try { |
|
|
|
JSONObject jsonObject = JSON.parseObject(responseContent); |
|
|
|
JSONObject base = jsonObject.getJSONObject("data"); |
|
|
|
code = base.getInteger("error_code"); |
|
|
|
msg = base.getString("description"); |
|
|
|
}catch(Exception e){} |
|
|
|
if(code != 0){ |
|
|
|
throw new WxErrorException(WxError.builder().errorCode(code).errorMsg(msg).build()); |
|
|
|
} |
|
|
|
return responseContent; |
|
|
|
// return data.toString(); |
|
|
|
} finally { |
|
|
|
httpGet.releaseConnection(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//去除链接中access_token 并返回 |
|
|
|
private String handleUrl(String url){ |
|
|
|
String access_token = null; |
|
|
|
Pattern pXM = Pattern.compile("access_token=([^&]*)"); |
|
|
|
Matcher mXM = pXM.matcher(url); |
|
|
|
int i = 0; |
|
|
|
while (mXM.find()) { |
|
|
|
i++; |
|
|
|
access_token = mXM.group(1); |
|
|
|
} |
|
|
|
if(i > 1){ |
|
|
|
return null; |
|
|
|
} |
|
|
|
// url = url.replaceAll("&?access_token=[^&]*",""); |
|
|
|
return access_token; |
|
|
|
} |
|
|
|
|
|
|
|
} |