Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
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.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
Expand All @@ -30,14 +28,7 @@ public ApacheHttpChannelMediaDownloadRequestExecutor(RequestHttp<CloseableHttpCl

@Override
public ChannelImageResponse execute(String uri, String data, WxType wxType) throws WxErrorException, IOException {
if (data != null) {
if (uri.indexOf('?') == -1) {
uri += '?';
}
uri += uri.endsWith("?") ? data : '&' + data;
}

HttpGet httpGet = new HttpGet(uri);
HttpGet httpGet = new HttpGet(this.appendDataToUri(uri, data));
if (requestHttp.getRequestHttpProxy() != null) {
RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build();
httpGet.setConfig(config);
Expand All @@ -56,20 +47,7 @@ public ChannelImageResponse execute(String uri, String data, WxType wxType) thro
}
}

String fileName = this.getFileName(response);
if (StringUtils.isBlank(fileName)) {
fileName = String.valueOf(System.currentTimeMillis());
}

String baseName = FilenameUtils.getBaseName(fileName);
if (StringUtils.isBlank(fileName) || baseName.length() < 3) {
baseName = String.valueOf(System.currentTimeMillis());
}
String extension = FilenameUtils.getExtension(fileName);
if (StringUtils.isBlank(extension)) {
extension = "unknown";
}
File file = createTmpFile(inputStream, baseName, extension, tmpDirFile);
File file = this.saveTmpFile(inputStream, this.getFileName(response));
return new ChannelImageResponse(file, contentType);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import me.chanjar.weixin.channel.bean.image.ChannelImageResponse;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.common.util.http.RequestHttp;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -64,6 +66,45 @@ public static File createTmpFile(InputStream inputStream, String name, String ex
return resultFile;
}

/**
* 把请求参数拼接到请求地址上
*
* @param uri 请求地址
* @param data 请求参数,可为空
* @return 拼接后的请求地址
*/
protected String appendDataToUri(String uri, String data) {
if (data == null) {
return uri;
}
String result = uri;
if (result.indexOf('?') == -1) {
result += '?';
}
return result + (result.endsWith("?") ? data : '&' + data);
}

/**
* 按照响应中的文件名把媒体内容写入临时文件
*
* @param inputStream 媒体内容
* @param fileName 响应中解析出的文件名,可为空
* @return 临时文件
*/
protected File saveTmpFile(InputStream inputStream, String fileName) throws IOException {
String name = StringUtils.isBlank(fileName) ? String.valueOf(System.currentTimeMillis()) : fileName;

String baseName = FilenameUtils.getBaseName(name);
if (StringUtils.isBlank(baseName) || baseName.length() < 3) {
baseName = String.valueOf(System.currentTimeMillis());
}
String extension = FilenameUtils.getExtension(name);
if (StringUtils.isBlank(extension)) {
extension = "unknown";
}
return createTmpFile(inputStream, baseName, extension, tmpDirFile);
}

protected String createDefaultFileName() {
return UUID.randomUUID().toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import me.chanjar.weixin.common.util.http.ResponseHandler;
import me.chanjar.weixin.common.util.http.hc.InputStreamResponseHandler;
import me.chanjar.weixin.common.util.http.hc.Utf8ResponseHandler;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.hc.client5.http.ClientProtocolException;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.config.RequestConfig;
Expand All @@ -32,14 +30,7 @@ public HttpComponentsChannelMediaDownloadRequestExecutor(RequestHttp<CloseableHt

@Override
public ChannelImageResponse execute(String uri, String data, WxType wxType) throws WxErrorException, IOException {
if (data != null) {
if (uri.indexOf('?') == -1) {
uri += '?';
}
uri += uri.endsWith("?") ? data : '&' + data;
}

HttpGet httpGet = new HttpGet(uri);
HttpGet httpGet = new HttpGet(this.appendDataToUri(uri, data));
if (requestHttp.getRequestHttpProxy() != null) {
RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build();
httpGet.setConfig(config);
Expand All @@ -58,20 +49,7 @@ public ChannelImageResponse execute(String uri, String data, WxType wxType) thro
}
}

String fileName = this.getFileName(response);
if (StringUtils.isBlank(fileName)) {
fileName = String.valueOf(System.currentTimeMillis());
}

String baseName = FilenameUtils.getBaseName(fileName);
if (StringUtils.isBlank(fileName) || baseName.length() < 3) {
baseName = String.valueOf(System.currentTimeMillis());
}
String extension = FilenameUtils.getExtension(fileName);
if (StringUtils.isBlank(extension)) {
extension = "unknown";
}
File file = createTmpFile(inputStream, baseName, extension, tmpDirFile);
File file = this.saveTmpFile(inputStream, this.getFileName(response));
return new ChannelImageResponse(file, contentType);
} catch (HttpException httpException) {
throw new ClientProtocolException(httpException.getMessage(), httpException);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package me.chanjar.weixin.open.api.impl;

import lombok.NonNull;
import me.chanjar.weixin.common.redis.WxRedisOps;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

/**
* 基于 {@link WxRedisOps} 的开放平台 redis 配置存储公共实现,
* 各具体实现类只需提供对应的 {@link WxRedisOps}。
*
* @author yangyidian
*/
public abstract class AbstractWxOpenInRedisOpsConfigStorage extends AbstractWxOpenInRedisConfigStorage {

/**
* redis 操作实现
*/
protected final WxRedisOps redisOps;

protected AbstractWxOpenInRedisOpsConfigStorage(@NonNull WxRedisOps redisOps, String keyPrefix) {
this.redisOps = redisOps;
this.keyPrefix = keyPrefix;
}

@Override
public String getComponentVerifyTicket() {
return redisOps.getValue(this.componentVerifyTicketKey);
}

@Override
public void setComponentVerifyTicket(String componentVerifyTicket) {
redisOps.setValue(this.componentVerifyTicketKey, componentVerifyTicket, 43200, TimeUnit.SECONDS);
}

@Override
public String getComponentAccessToken() {
return redisOps.getValue(this.componentAccessTokenKey);
}

@Override
public boolean isComponentAccessTokenExpired() {
Long expire = redisOps.getExpire(this.componentAccessTokenKey);
return expire == null || expire < 2;
}

@Override
public void expireComponentAccessToken() {
redisOps.expire(this.componentAccessTokenKey, 0, TimeUnit.SECONDS);
}

@Override
public void updateComponentAccessToken(String componentAccessToken, int expiresInSeconds) {
redisOps.setValue(this.componentAccessTokenKey, componentAccessToken, expiresInSeconds - 200, TimeUnit.SECONDS);
}

@Override
public String getAuthorizerRefreshToken(String appId) {
return redisOps.getValue(this.getKey(this.authorizerRefreshTokenKey, appId));
}

@Override
public void setAuthorizerRefreshToken(String appId, String authorizerRefreshToken) {
redisOps.setValue(this.getKey(this.authorizerRefreshTokenKey, appId), authorizerRefreshToken, 0, TimeUnit.SECONDS);
}

@Override
public String getAuthorizerAccessToken(String appId) {
return redisOps.getValue(this.getKey(this.authorizerAccessTokenKey, appId));
}

@Override
public boolean isAuthorizerAccessTokenExpired(String appId) {
Long expire = redisOps.getExpire(this.getKey(this.authorizerAccessTokenKey, appId));
return expire == null || expire < 2;
}

@Override
public void expireAuthorizerAccessToken(String appId) {
redisOps.expire(this.getKey(this.authorizerAccessTokenKey, appId), 0, TimeUnit.SECONDS);
}

@Override
public void updateAuthorizerAccessToken(String appId, String authorizerAccessToken, int expiresInSeconds) {
redisOps.setValue(this.getKey(this.authorizerAccessTokenKey, appId), authorizerAccessToken, expiresInSeconds - 200, TimeUnit.SECONDS);
}

@Override
public String getJsapiTicket(String appId) {
return redisOps.getValue(this.getKey(this.jsapiTicketKey, appId));
}

@Override
public boolean isJsapiTicketExpired(String appId) {
Long expire = redisOps.getExpire(this.getKey(this.jsapiTicketKey, appId));
return expire == null || expire < 2;
}

@Override
public void expireJsapiTicket(String appId) {
redisOps.expire(this.getKey(this.jsapiTicketKey, appId), 0, TimeUnit.SECONDS);
}

@Override
public void updateJsapiTicket(String appId, String jsapiTicket, int expiresInSeconds) {
redisOps.setValue(this.getKey(this.jsapiTicketKey, appId), jsapiTicket, expiresInSeconds - 200, TimeUnit.SECONDS);
}

@Override
public String getCardApiTicket(String appId) {
return redisOps.getValue(this.getKey(this.cardApiTicket, appId));
}

@Override
public boolean isCardApiTicketExpired(String appId) {
Long expire = redisOps.getExpire(this.getKey(this.cardApiTicket, appId));
return expire == null || expire < 2;
}

@Override
public void expireCardApiTicket(String appId) {
redisOps.expire(this.getKey(this.cardApiTicket, appId), 0, TimeUnit.SECONDS);
}

@Override
public void updateCardApiTicket(String appId, String cardApiTicket, int expiresInSeconds) {
redisOps.setValue(this.getKey(this.cardApiTicket, appId), cardApiTicket, expiresInSeconds - 200, TimeUnit.SECONDS);
}

@Override
public Lock getLockByKey(String key) {
return redisOps.getLock(key);
}
}
Loading