-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🆕 #1831 生成小程序二维码的相关接口增加指定文件路径参数的重载方法
- Loading branch information
1 parent
aa26d6b
commit e6a550d
Showing
4 changed files
with
256 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...n-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/util/QrcodeFileRequestExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package cn.binarywang.wx.miniapp.util; | ||
|
||
import cn.binarywang.wx.miniapp.bean.AbstractWxMaQrcodeWrapper; | ||
import me.chanjar.weixin.common.enums.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 org.apache.commons.lang3.StringUtils; | ||
import org.apache.http.Header; | ||
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 java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.file.Paths; | ||
import java.util.UUID; | ||
|
||
/** | ||
* @author <a href="https://github.com/gentryhuang">gentryhuang</a> | ||
*/ | ||
public class QrcodeFileRequestExecutor extends QrcodeRequestExecutor { | ||
/** | ||
* 二维码生成的文件路径,例如: /var/temp | ||
*/ | ||
private final String filePath; | ||
|
||
public QrcodeFileRequestExecutor(RequestHttp requestHttp, String filePath) { | ||
super(requestHttp); | ||
this.filePath = filePath; | ||
} | ||
|
||
/** | ||
* 执行http请求. | ||
* | ||
* @param uri uri | ||
* @param qrcodeWrapper 数据 | ||
* @param wxType 微信模块类型 | ||
* @return 响应结果 | ||
* @throws WxErrorException 自定义异常 | ||
* @throws IOException io异常 | ||
*/ | ||
@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.setEntity(new StringEntity(qrcodeWrapper.toJson(), ContentType.APPLICATION_JSON)); | ||
|
||
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)); | ||
} | ||
if (StringUtils.isBlank(filePath)) { | ||
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg"); | ||
} | ||
|
||
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg", Paths.get(filePath).toFile()); | ||
} finally { | ||
httpPost.releaseConnection(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters