mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2026-03-10 00:13:40 +08:00
#759 增加三方平台代小程序业务接口:代码管理、模板管理、成员管理、发布审核等
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
package me.chanjar.weixin.open.util.requestexecuter.ma;
|
||||
|
||||
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.fs.FileUtils;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.apache.InputStreamResponseHandler;
|
||||
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
|
||||
import me.chanjar.weixin.open.bean.ma.WxMaQrcodeParam;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.http.Header;
|
||||
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.entity.ContentType;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* @author yqx
|
||||
* @date 2018-09-13
|
||||
*/
|
||||
public class MaQrCodeApacheHttpRequestExecutor extends MaQrCodeRequestExecutor<CloseableHttpClient, HttpHost> {
|
||||
public MaQrCodeApacheHttpRequestExecutor(RequestHttp requestHttp) {
|
||||
super(requestHttp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public File execute(String uri, WxMaQrcodeParam qrcodeParam) throws WxErrorException, IOException {
|
||||
if (qrcodeParam != null && StringUtils.isNotBlank(qrcodeParam.getPagePath())) {
|
||||
if (uri.indexOf('?') == -1) {
|
||||
uri += '?';
|
||||
}
|
||||
uri += uri.endsWith("?")
|
||||
? "path=" + URLEncoder.encode(qrcodeParam.getRequestPath(), "UTF-8")
|
||||
: "&path=" + URLEncoder.encode(qrcodeParam.getRequestPath(), "UTF-8");
|
||||
}
|
||||
|
||||
HttpGet httpGet = new HttpGet(uri);
|
||||
if (requestHttp.getRequestHttpProxy() != null) {
|
||||
RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build();
|
||||
httpGet.setConfig(config);
|
||||
}
|
||||
|
||||
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpGet);
|
||||
InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response);) {
|
||||
Header[] contentTypeHeader = response.getHeaders("Content-Type");
|
||||
if (contentTypeHeader != null && contentTypeHeader.length > 0) {
|
||||
// 出错
|
||||
if (ContentType.TEXT_PLAIN.getMimeType()
|
||||
.equals(ContentType.parse(contentTypeHeader[0].getValue()).getMimeType())) {
|
||||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
|
||||
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp));
|
||||
}
|
||||
}
|
||||
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg");
|
||||
} finally {
|
||||
httpGet.releaseConnection();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package me.chanjar.weixin.open.util.requestexecuter.ma;
|
||||
|
||||
import jodd.http.HttpConnectionProvider;
|
||||
import jodd.http.HttpRequest;
|
||||
import jodd.http.HttpResponse;
|
||||
import jodd.http.ProxyInfo;
|
||||
import jodd.util.MimeTypes;
|
||||
import jodd.util.StringPool;
|
||||
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.fs.FileUtils;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.open.bean.ma.WxMaQrcodeParam;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* @author yqx
|
||||
* @date 2018-09-13
|
||||
*/
|
||||
public class MaQrCodeJoddHttpRequestExecutor extends MaQrCodeRequestExecutor<HttpConnectionProvider, ProxyInfo> {
|
||||
public MaQrCodeJoddHttpRequestExecutor(RequestHttp requestHttp) {
|
||||
super(requestHttp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public File execute(String uri, WxMaQrcodeParam qrcodeParam) throws WxErrorException, IOException {
|
||||
if (qrcodeParam != null && StringUtils.isNotBlank(qrcodeParam.getPagePath())) {
|
||||
if (uri.indexOf('?') == -1) {
|
||||
uri += '?';
|
||||
}
|
||||
uri += uri.endsWith("?")
|
||||
? "path=" + URLEncoder.encode(qrcodeParam.getRequestPath(), "UTF-8")
|
||||
: "&path=" + URLEncoder.encode(qrcodeParam.getRequestPath(), "UTF-8");
|
||||
}
|
||||
|
||||
|
||||
HttpRequest request = HttpRequest.get(uri);
|
||||
if (requestHttp.getRequestHttpProxy() != null) {
|
||||
requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy());
|
||||
}
|
||||
request.withConnectionProvider(requestHttp.getRequestHttpClient());
|
||||
|
||||
HttpResponse response = request.send();
|
||||
response.charset(StringPool.UTF_8);
|
||||
String contentTypeHeader = response.header("Content-Type");
|
||||
if (MimeTypes.MIME_TEXT_PLAIN.equals(contentTypeHeader)) {
|
||||
String responseContent = response.bodyText();
|
||||
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp));
|
||||
}
|
||||
try (InputStream inputStream = new ByteArrayInputStream(response.bodyBytes())) {
|
||||
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package me.chanjar.weixin.open.util.requestexecuter.ma;
|
||||
|
||||
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.fs.FileUtils;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
|
||||
import me.chanjar.weixin.open.bean.ma.WxMaQrcodeParam;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* @author yqx
|
||||
* @date 2018-09-13
|
||||
*/
|
||||
public class MaQrCodeOkhttpRequestExecutor extends MaQrCodeRequestExecutor<OkHttpClient, OkHttpProxyInfo> {
|
||||
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||
|
||||
public MaQrCodeOkhttpRequestExecutor(RequestHttp requestHttp) {
|
||||
super(requestHttp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public File execute(String uri, WxMaQrcodeParam qrcodeParam) throws WxErrorException, IOException {
|
||||
if (qrcodeParam != null && StringUtils.isNotBlank(qrcodeParam.getPagePath())) {
|
||||
if (uri.indexOf('?') == -1) {
|
||||
uri += '?';
|
||||
}
|
||||
uri += uri.endsWith("?")
|
||||
? "path=" + URLEncoder.encode(qrcodeParam.getRequestPath(), "UTF-8")
|
||||
: "&path=" + URLEncoder.encode(qrcodeParam.getRequestPath(), "UTF-8");
|
||||
}
|
||||
|
||||
OkHttpClient client = requestHttp.getRequestHttpClient();
|
||||
Request request = new Request.Builder().url(uri).get().build();
|
||||
Response response = client.newCall(request).execute();
|
||||
String contentTypeHeader = response.header("Content-Type");
|
||||
if ("text/plain".equals(contentTypeHeader)) {
|
||||
String responseContent = response.body().string();
|
||||
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MP));
|
||||
}
|
||||
|
||||
try (InputStream inputStream = response.body().byteStream()) {
|
||||
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package me.chanjar.weixin.open.util.requestexecuter.ma;
|
||||
|
||||
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.open.bean.ma.WxMaQrcodeParam;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* 获得小程序体验QrCode图片 请求执行器
|
||||
*
|
||||
* @author yqx
|
||||
* @date 2018-09-13
|
||||
*/
|
||||
public abstract class MaQrCodeRequestExecutor<H, P> implements RequestExecutor<File, WxMaQrcodeParam> {
|
||||
protected RequestHttp<H, P> requestHttp;
|
||||
|
||||
public MaQrCodeRequestExecutor(RequestHttp requestHttp) {
|
||||
this.requestHttp = requestHttp;
|
||||
}
|
||||
|
||||
public static RequestExecutor<File, WxMaQrcodeParam> create(RequestHttp requestHttp) throws WxErrorException {
|
||||
switch (requestHttp.getRequestType()) {
|
||||
case APACHE_HTTP:
|
||||
return new MaQrCodeApacheHttpRequestExecutor(requestHttp);
|
||||
case JODD_HTTP:
|
||||
return new MaQrCodeJoddHttpRequestExecutor(requestHttp);
|
||||
case OK_HTTP:
|
||||
return new MaQrCodeOkhttpRequestExecutor(requestHttp);
|
||||
default:
|
||||
throw new WxErrorException(WxError.builder().errorCode(-1).errorMsg("不支持的http框架").build());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user