Skip to content

Commit a22c689

Browse files
refactor(open,channel): 消除 redis 配置存储与媒体下载执行器中的重复代码
open: 三个 WxOpenInRedis*ConfigStorage 的 20 个方法逐字符相同,抽取 AbstractWxOpenInRedisOpsConfigStorage 统一实现,子类仅保留构造函数。 channel: 两个媒体下载执行器中的 uri 拼参与临时文件命名逻辑上移到 ChannelMediaDownloadRequestExecutor。 Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent 6014c83 commit a22c689

7 files changed

Lines changed: 186 additions & 399 deletions

File tree

‎weixin-java-channel/src/main/java/me/chanjar/weixin/channel/executor/ApacheHttpChannelMediaDownloadRequestExecutor.java‎

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
import me.chanjar.weixin.common.util.http.ResponseHandler;
99
import me.chanjar.weixin.common.util.http.apache.InputStreamResponseHandler;
1010
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
11-
import org.apache.commons.io.FilenameUtils;
12-
import org.apache.commons.lang3.StringUtils;
1311
import org.apache.http.Header;
1412
import org.apache.http.HttpHost;
1513
import org.apache.http.client.config.RequestConfig;
@@ -30,14 +28,7 @@ public ApacheHttpChannelMediaDownloadRequestExecutor(RequestHttp<CloseableHttpCl
3028

3129
@Override
3230
public ChannelImageResponse execute(String uri, String data, WxType wxType) throws WxErrorException, IOException {
33-
if (data != null) {
34-
if (uri.indexOf('?') == -1) {
35-
uri += '?';
36-
}
37-
uri += uri.endsWith("?") ? data : '&' + data;
38-
}
39-
40-
HttpGet httpGet = new HttpGet(uri);
31+
HttpGet httpGet = new HttpGet(this.appendDataToUri(uri, data));
4132
if (requestHttp.getRequestHttpProxy() != null) {
4233
RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build();
4334
httpGet.setConfig(config);
@@ -56,20 +47,7 @@ public ChannelImageResponse execute(String uri, String data, WxType wxType) thro
5647
}
5748
}
5849

59-
String fileName = this.getFileName(response);
60-
if (StringUtils.isBlank(fileName)) {
61-
fileName = String.valueOf(System.currentTimeMillis());
62-
}
63-
64-
String baseName = FilenameUtils.getBaseName(fileName);
65-
if (StringUtils.isBlank(fileName) || baseName.length() < 3) {
66-
baseName = String.valueOf(System.currentTimeMillis());
67-
}
68-
String extension = FilenameUtils.getExtension(fileName);
69-
if (StringUtils.isBlank(extension)) {
70-
extension = "unknown";
71-
}
72-
File file = createTmpFile(inputStream, baseName, extension, tmpDirFile);
50+
File file = this.saveTmpFile(inputStream, this.getFileName(response));
7351
return new ChannelImageResponse(file, contentType);
7452
}
7553
}

‎weixin-java-channel/src/main/java/me/chanjar/weixin/channel/executor/ChannelMediaDownloadRequestExecutor.java‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import me.chanjar.weixin.channel.bean.image.ChannelImageResponse;
44
import me.chanjar.weixin.common.util.http.RequestExecutor;
55
import me.chanjar.weixin.common.util.http.RequestHttp;
6+
import org.apache.commons.io.FilenameUtils;
67
import org.apache.commons.io.IOUtils;
8+
import org.apache.commons.lang3.StringUtils;
79

810
import java.io.File;
911
import java.io.IOException;
@@ -64,6 +66,45 @@ public static File createTmpFile(InputStream inputStream, String name, String ex
6466
return resultFile;
6567
}
6668

69+
/**
70+
* 把请求参数拼接到请求地址上
71+
*
72+
* @param uri 请求地址
73+
* @param data 请求参数,可为空
74+
* @return 拼接后的请求地址
75+
*/
76+
protected String appendDataToUri(String uri, String data) {
77+
if (data == null) {
78+
return uri;
79+
}
80+
String result = uri;
81+
if (result.indexOf('?') == -1) {
82+
result += '?';
83+
}
84+
return result + (result.endsWith("?") ? data : '&' + data);
85+
}
86+
87+
/**
88+
* 按照响应中的文件名把媒体内容写入临时文件
89+
*
90+
* @param inputStream 媒体内容
91+
* @param fileName 响应中解析出的文件名,可为空
92+
* @return 临时文件
93+
*/
94+
protected File saveTmpFile(InputStream inputStream, String fileName) throws IOException {
95+
String name = StringUtils.isBlank(fileName) ? String.valueOf(System.currentTimeMillis()) : fileName;
96+
97+
String baseName = FilenameUtils.getBaseName(name);
98+
if (StringUtils.isBlank(baseName) || baseName.length() < 3) {
99+
baseName = String.valueOf(System.currentTimeMillis());
100+
}
101+
String extension = FilenameUtils.getExtension(name);
102+
if (StringUtils.isBlank(extension)) {
103+
extension = "unknown";
104+
}
105+
return createTmpFile(inputStream, baseName, extension, tmpDirFile);
106+
}
107+
67108
protected String createDefaultFileName() {
68109
return UUID.randomUUID().toString();
69110
}

‎weixin-java-channel/src/main/java/me/chanjar/weixin/channel/executor/HttpComponentsChannelMediaDownloadRequestExecutor.java‎

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
import me.chanjar.weixin.common.util.http.ResponseHandler;
99
import me.chanjar.weixin.common.util.http.hc.InputStreamResponseHandler;
1010
import me.chanjar.weixin.common.util.http.hc.Utf8ResponseHandler;
11-
import org.apache.commons.io.FilenameUtils;
12-
import org.apache.commons.lang3.StringUtils;
1311
import org.apache.hc.client5.http.ClientProtocolException;
1412
import org.apache.hc.client5.http.classic.methods.HttpGet;
1513
import org.apache.hc.client5.http.config.RequestConfig;
@@ -32,14 +30,7 @@ public HttpComponentsChannelMediaDownloadRequestExecutor(RequestHttp<CloseableHt
3230

3331
@Override
3432
public ChannelImageResponse execute(String uri, String data, WxType wxType) throws WxErrorException, IOException {
35-
if (data != null) {
36-
if (uri.indexOf('?') == -1) {
37-
uri += '?';
38-
}
39-
uri += uri.endsWith("?") ? data : '&' + data;
40-
}
41-
42-
HttpGet httpGet = new HttpGet(uri);
33+
HttpGet httpGet = new HttpGet(this.appendDataToUri(uri, data));
4334
if (requestHttp.getRequestHttpProxy() != null) {
4435
RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build();
4536
httpGet.setConfig(config);
@@ -58,20 +49,7 @@ public ChannelImageResponse execute(String uri, String data, WxType wxType) thro
5849
}
5950
}
6051

61-
String fileName = this.getFileName(response);
62-
if (StringUtils.isBlank(fileName)) {
63-
fileName = String.valueOf(System.currentTimeMillis());
64-
}
65-
66-
String baseName = FilenameUtils.getBaseName(fileName);
67-
if (StringUtils.isBlank(fileName) || baseName.length() < 3) {
68-
baseName = String.valueOf(System.currentTimeMillis());
69-
}
70-
String extension = FilenameUtils.getExtension(fileName);
71-
if (StringUtils.isBlank(extension)) {
72-
extension = "unknown";
73-
}
74-
File file = createTmpFile(inputStream, baseName, extension, tmpDirFile);
52+
File file = this.saveTmpFile(inputStream, this.getFileName(response));
7553
return new ChannelImageResponse(file, contentType);
7654
} catch (HttpException httpException) {
7755
throw new ClientProtocolException(httpException.getMessage(), httpException);
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package me.chanjar.weixin.open.api.impl;
2+
3+
import lombok.NonNull;
4+
import me.chanjar.weixin.common.redis.WxRedisOps;
5+
6+
import java.util.concurrent.TimeUnit;
7+
import java.util.concurrent.locks.Lock;
8+
9+
/**
10+
* 基于 {@link WxRedisOps} 的开放平台 redis 配置存储公共实现,
11+
* 各具体实现类只需提供对应的 {@link WxRedisOps}。
12+
*
13+
* @author yangyidian
14+
*/
15+
public abstract class AbstractWxOpenInRedisOpsConfigStorage extends AbstractWxOpenInRedisConfigStorage {
16+
17+
/**
18+
* redis 操作实现
19+
*/
20+
protected final WxRedisOps redisOps;
21+
22+
protected AbstractWxOpenInRedisOpsConfigStorage(@NonNull WxRedisOps redisOps, String keyPrefix) {
23+
this.redisOps = redisOps;
24+
this.keyPrefix = keyPrefix;
25+
}
26+
27+
@Override
28+
public String getComponentVerifyTicket() {
29+
return redisOps.getValue(this.componentVerifyTicketKey);
30+
}
31+
32+
@Override
33+
public void setComponentVerifyTicket(String componentVerifyTicket) {
34+
redisOps.setValue(this.componentVerifyTicketKey, componentVerifyTicket, 43200, TimeUnit.SECONDS);
35+
}
36+
37+
@Override
38+
public String getComponentAccessToken() {
39+
return redisOps.getValue(this.componentAccessTokenKey);
40+
}
41+
42+
@Override
43+
public boolean isComponentAccessTokenExpired() {
44+
Long expire = redisOps.getExpire(this.componentAccessTokenKey);
45+
return expire == null || expire < 2;
46+
}
47+
48+
@Override
49+
public void expireComponentAccessToken() {
50+
redisOps.expire(this.componentAccessTokenKey, 0, TimeUnit.SECONDS);
51+
}
52+
53+
@Override
54+
public void updateComponentAccessToken(String componentAccessToken, int expiresInSeconds) {
55+
redisOps.setValue(this.componentAccessTokenKey, componentAccessToken, expiresInSeconds - 200, TimeUnit.SECONDS);
56+
}
57+
58+
@Override
59+
public String getAuthorizerRefreshToken(String appId) {
60+
return redisOps.getValue(this.getKey(this.authorizerRefreshTokenKey, appId));
61+
}
62+
63+
@Override
64+
public void setAuthorizerRefreshToken(String appId, String authorizerRefreshToken) {
65+
redisOps.setValue(this.getKey(this.authorizerRefreshTokenKey, appId), authorizerRefreshToken, 0, TimeUnit.SECONDS);
66+
}
67+
68+
@Override
69+
public String getAuthorizerAccessToken(String appId) {
70+
return redisOps.getValue(this.getKey(this.authorizerAccessTokenKey, appId));
71+
}
72+
73+
@Override
74+
public boolean isAuthorizerAccessTokenExpired(String appId) {
75+
Long expire = redisOps.getExpire(this.getKey(this.authorizerAccessTokenKey, appId));
76+
return expire == null || expire < 2;
77+
}
78+
79+
@Override
80+
public void expireAuthorizerAccessToken(String appId) {
81+
redisOps.expire(this.getKey(this.authorizerAccessTokenKey, appId), 0, TimeUnit.SECONDS);
82+
}
83+
84+
@Override
85+
public void updateAuthorizerAccessToken(String appId, String authorizerAccessToken, int expiresInSeconds) {
86+
redisOps.setValue(this.getKey(this.authorizerAccessTokenKey, appId), authorizerAccessToken, expiresInSeconds - 200, TimeUnit.SECONDS);
87+
}
88+
89+
@Override
90+
public String getJsapiTicket(String appId) {
91+
return redisOps.getValue(this.getKey(this.jsapiTicketKey, appId));
92+
}
93+
94+
@Override
95+
public boolean isJsapiTicketExpired(String appId) {
96+
Long expire = redisOps.getExpire(this.getKey(this.jsapiTicketKey, appId));
97+
return expire == null || expire < 2;
98+
}
99+
100+
@Override
101+
public void expireJsapiTicket(String appId) {
102+
redisOps.expire(this.getKey(this.jsapiTicketKey, appId), 0, TimeUnit.SECONDS);
103+
}
104+
105+
@Override
106+
public void updateJsapiTicket(String appId, String jsapiTicket, int expiresInSeconds) {
107+
redisOps.setValue(this.getKey(this.jsapiTicketKey, appId), jsapiTicket, expiresInSeconds - 200, TimeUnit.SECONDS);
108+
}
109+
110+
@Override
111+
public String getCardApiTicket(String appId) {
112+
return redisOps.getValue(this.getKey(this.cardApiTicket, appId));
113+
}
114+
115+
@Override
116+
public boolean isCardApiTicketExpired(String appId) {
117+
Long expire = redisOps.getExpire(this.getKey(this.cardApiTicket, appId));
118+
return expire == null || expire < 2;
119+
}
120+
121+
@Override
122+
public void expireCardApiTicket(String appId) {
123+
redisOps.expire(this.getKey(this.cardApiTicket, appId), 0, TimeUnit.SECONDS);
124+
}
125+
126+
@Override
127+
public void updateCardApiTicket(String appId, String cardApiTicket, int expiresInSeconds) {
128+
redisOps.setValue(this.getKey(this.cardApiTicket, appId), cardApiTicket, expiresInSeconds - 200, TimeUnit.SECONDS);
129+
}
130+
131+
@Override
132+
public Lock getLockByKey(String key) {
133+
return redisOps.getLock(key);
134+
}
135+
}

0 commit comments

Comments
 (0)