Просмотр исходного кода

/、qrcode

release_toaliyun_real
xhxu 4 лет назад
Родитель
Сommit
454a26458f
2 измененных файлов: 72 добавлений и 4 удалений
  1. +69
    -0
      mallinkService/src/main/java/com/iformall/douyin/miniapp/api/TtQrcodeRequestExecutor.java
  2. +3
    -4
      mallinkService/src/main/java/com/iformall/douyin/miniapp/api/impl/TtMaQrcodeServiceImpl.java

+ 69
- 0
mallinkService/src/main/java/com/iformall/douyin/miniapp/api/TtQrcodeRequestExecutor.java Просмотреть файл

@@ -0,0 +1,69 @@
package com.iformall.douyin.miniapp.api;

import cn.binarywang.wx.miniapp.bean.AbstractWxMaQrcodeWrapper;
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.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.InputStreamResponseHandler;
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
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.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;

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

/**
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
public class TtQrcodeRequestExecutor implements RequestExecutor<File, AbstractWxMaQrcodeWrapper> {
protected RequestHttp<CloseableHttpClient, HttpHost> requestHttp;

public TtQrcodeRequestExecutor(RequestHttp requestHttp) {
this.requestHttp = requestHttp;
}

@Override
public void execute(String uri, AbstractWxMaQrcodeWrapper data, ResponseHandler<File> handler, WxType wxType) throws WxErrorException, IOException {
handler.handle(this.execute(uri, data, wxType));
}

@Override
public File execute(String uri, AbstractWxMaQrcodeWrapper qrcodeWrapper, WxType wxType) throws WxErrorException, IOException {
HttpPost httpPost = new HttpPost(uri);
if (requestHttp.getRequestHttpProxy() != null) {
httpPost.setConfig(
RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build()
);
}
httpPost.addHeader("Content-Type","application/json");

httpPost.setEntity(new StringEntity(qrcodeWrapper.toJson()));

try (final CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost);
final InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response)) {
Header[] contentTypeHeader = response.getHeaders("Content-Type");
if (contentTypeHeader != null && contentTypeHeader.length > 0
&& ContentType.APPLICATION_JSON.getMimeType()
.equals(ContentType.parse(contentTypeHeader[0].getValue()).getMimeType())) {
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
throw new WxErrorException(WxError.fromJson(responseContent, wxType));
}

return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg");
} finally {
httpPost.releaseConnection();
}
}
}

+ 3
- 4
mallinkService/src/main/java/com/iformall/douyin/miniapp/api/impl/TtMaQrcodeServiceImpl.java Просмотреть файл

@@ -1,14 +1,13 @@
package com.iformall.douyin.miniapp.api.impl;

import cn.binarywang.wx.miniapp.util.QrcodeRequestExecutor;
import com.iformall.douyin.miniapp.api.TtMaQrcode;
import com.iformall.douyin.miniapp.api.TtMaQrcodeService;
import com.iformall.douyin.miniapp.api.TtMaService;
import com.iformall.douyin.miniapp.api.TtQrcodeRequestExecutor;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;

import java.io.File;
import java.lang.reflect.Field;

/**
* @author <a href="https://github.com/binarywang">Binary Wang</a>
@@ -20,8 +19,8 @@ public class TtMaQrcodeServiceImpl implements TtMaQrcodeService {

@Override
public File createTtaCode(String appname, String path, int width) throws WxErrorException {
final QrcodeRequestExecutor executor = new QrcodeRequestExecutor(this.ttMaService.getRequestHttp());
return this.ttMaService.execute(executor, GET_TTACODE_URL, new TtMaQrcode(null,null, width));
final TtQrcodeRequestExecutor executor = new TtQrcodeRequestExecutor(this.ttMaService.getRequestHttp());
return this.ttMaService.execute(executor, GET_TTACODE_URL, new TtMaQrcode(appname,path, width));
}

}

Загрузка…
Отмена
Сохранить