Skip to content

refactor(open,channel): 消除 redis 配置存储与媒体下载执行器中的重复代码 - #4084

Open
devin-ai-integration[bot] wants to merge 1 commit into
developfrom
devin/1785512623-refactor-open-channel-dup
Open

refactor(open,channel): 消除 redis 配置存储与媒体下载执行器中的重复代码#4084
devin-ai-integration[bot] wants to merge 1 commit into
developfrom
devin/1785512623-refactor-open-channel-dup

Conversation

@devin-ai-integration

Copy link
Copy Markdown

Summary

#4082,继续清理 CPD 扫描出的模块内重复(均不改变对外 API 与行为)。

weixin-java-openWxOpenInRedisConfigStorageWxOpenInRedisTemplateConfigStorageWxOpenInRedissonConfigStorage 三个类各 140 行,除构造函数外的 20 个方法(component ticket / access token、authorizer token、jsapi ticket、card api ticket、lock)逐字符相同,都只是在操作 WxRedisOps。抽取:

public abstract class AbstractWxOpenInRedisOpsConfigStorage extends AbstractWxOpenInRedisConfigStorage {
  protected final WxRedisOps redisOps;
  protected AbstractWxOpenInRedisOpsConfigStorage(@NonNull WxRedisOps redisOps, String keyPrefix) { ... }
  // 原三份重复的 20 个方法
}

三个实现类只剩构造函数(super(redisOps, keyPrefix)),公共构造函数签名、可见性与 @NonNull 语义全部保留;原 private final WxRedisOps redisOps 字段本就不对外暴露。

weixin-java-channelApacheHttpChannelMediaDownloadRequestExecutorHttpComponentsChannelMediaDownloadRequestExecutor 使用不同的 HttpClient 类型(httpclient4 / hc5),无法直接合并,但其中与 HTTP 库无关的两段逻辑重复,上移到公共父类 ChannelMediaDownloadRequestExecutor

  • appendDataToUri(uri, data):请求参数拼接(? / & 判断)。
  • saveTmpFile(inputStream, fileName):文件名为空取时间戳、baseName 少于 3 字符取时间戳、扩展名为空取 unknown,再调用 createTmpFile

原代码中 if (StringUtils.isBlank(fileName) || baseName.length() < 3) 的前半段判断在该位置恒为 false(fileName 上一行已被兜底成时间戳),新实现改为等价且更直观的 StringUtils.isBlank(baseName) || baseName.length() < 3,结果一致。

验证

  • mvn -pl weixin-java-open,weixin-java-channel -am -DskipTests install 编译通过。
  • 本地启动 redis 后用 TestNG 直接运行 WxOpenInRedisConfigStorageTest(根 POM surefire <skip>true</skip>):改动前后同为 9 个用例 8 通过 1 失败,失败用例 testGetComponentVerifyTicket 是既有的用例间顺序依赖问题(被 test_ticket_for_expiration 覆盖),与本次改动无关,已在 develop 基线上复现确认。
  • channel 模块无对应单元测试,仅编译验证。

Link to Devin session: https://app.devin.ai/sessions/65a1cece5efb4304a7e6b85aaa562d7a
Requested by: @binarywang

open: 三个 WxOpenInRedis*ConfigStorage 的 20 个方法逐字符相同,抽取
AbstractWxOpenInRedisOpsConfigStorage 统一实现,子类仅保留构造函数。
channel: 两个媒体下载执行器中的 uri 拼参与临时文件命名逻辑上移到
ChannelMediaDownloadRequestExecutor。

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@binarywang binarywang self-assigned this Jul 31, 2026
@devin-ai-integration

Copy link
Copy Markdown
Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment, CI, and merge conflict monitoring

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant