Commit 26324834 by 林永健

refactor(wechat-push): 优化企微推送接口并支持动态URL构建

- 统一推送方法参数,改用实体对象传递,简化调用代码
- 新增WechatPushParam封装推送参数,增强代码可读性和维护性
- 实现动态URL构建,支持门店ID、名称、业务类型等URL参数
- 增加业务类型转换工具方法,统一业务类型中文及URL参数映射
- 对中文参数进行URL编码,保证动态链接兼容性
- 迁移业务类型获取逻辑至MetricsValueUtils,去除重复代码
- 细化日志信息,完善异常捕获和错误提示
- 调整相关调用代码,调用新接口减少冗余参数传递
1 parent afe5559e
...@@ -186,8 +186,7 @@ public class MerchantBiDailyPushJob implements SimpleJob { ...@@ -186,8 +186,7 @@ public class MerchantBiDailyPushJob implements SimpleJob {
return; return;
} }
wechatPushService.pushDailySummaryMsg(follow.getMerchantName(), getBusinessTypeName(follow.getBusinessType()), wechatPushService.pushDailySummaryMsg(follow, count, targetUuids, false);
count, targetUuids, false);
} }
/** /**
...@@ -209,8 +208,7 @@ public class MerchantBiDailyPushJob implements SimpleJob { ...@@ -209,8 +208,7 @@ public class MerchantBiDailyPushJob implements SimpleJob {
return; return;
} }
wechatPushService.pushDailySummaryMsg(follow.getMerchantName(), getBusinessTypeName(follow.getBusinessType()), wechatPushService.pushDailySummaryMsg(follow, count, followerUuids, true);
count, followerUuids, true);
} }
private void pushToOperators(MerchantBiFollow follow, int count) { private void pushToOperators(MerchantBiFollow follow, int count) {
...@@ -234,8 +232,7 @@ public class MerchantBiDailyPushJob implements SimpleJob { ...@@ -234,8 +232,7 @@ public class MerchantBiDailyPushJob implements SimpleJob {
return; return;
} }
wechatPushService.pushDailySummaryMsg(follow.getMerchantName(), getBusinessTypeName(follow.getBusinessType()), wechatPushService.pushDailySummaryMsg(follow, count, targetUuids, false);
count, targetUuids, false);
} }
private void pushToFollowers(MerchantBiFollow follow, int count) { private void pushToFollowers(MerchantBiFollow follow, int count) {
...@@ -254,8 +251,7 @@ public class MerchantBiDailyPushJob implements SimpleJob { ...@@ -254,8 +251,7 @@ public class MerchantBiDailyPushJob implements SimpleJob {
log.info("【{}-{}】门店无关注人, id:{}", follow.getMerchantId(), follow.getBusinessType(), follow.getId()); log.info("【{}-{}】门店无关注人, id:{}", follow.getMerchantId(), follow.getBusinessType(), follow.getId());
return; return;
} }
wechatPushService.pushDailySummaryMsg(follow.getMerchantName(), getBusinessTypeName(follow.getBusinessType()), wechatPushService.pushDailySummaryMsg(follow, count, followerUuids, true);
count, followerUuids, true);
} }
private String getBusinessTypeName(Byte businessType) { private String getBusinessTypeName(Byte businessType) {
......
...@@ -100,8 +100,7 @@ public class MerchantBiCommentService { ...@@ -100,8 +100,7 @@ public class MerchantBiCommentService {
// 3. 企微推送 (@ 提醒) // 3. 企微推送 (@ 提醒)
try { try {
wechatPushService.pushMentionMsg(request.getStaffName(), request.getMerchantName(), wechatPushService.pushMentionMsg(request, uuids);
getBusinessTypeName(request.getBusinessType()), uuids);
} catch (Exception e) { } catch (Exception e) {
log.error("触发留言 @ 推送异常", e); log.error("触发留言 @ 推送异常", e);
} }
......
...@@ -618,8 +618,7 @@ public class MerchantBiFollowService { ...@@ -618,8 +618,7 @@ public class MerchantBiFollowService {
} }
log.info("触发关注推送:merchantId={}, biz={}, 触发人={}, 推送目标={}", log.info("触发关注推送:merchantId={}, biz={}, 触发人={}, 推送目标={}",
request.getMerchantId(), request.getBusinessType(), request.getStaffName(), targetUuids); request.getMerchantId(), request.getBusinessType(), request.getStaffName(), targetUuids);
wechatPushService.pushFollowMsg(request.getStaffName(), request.getMerchantName(), wechatPushService.pushFollowMsg(request, targetUuids);
getBusinessTypeName(request.getBusinessType()), targetUuids);
} catch (Exception e) { } catch (Exception e) {
log.error("触发关注推送异常", e); log.error("触发关注推送异常", e);
} }
...@@ -639,6 +638,24 @@ public class MerchantBiFollowService { ...@@ -639,6 +638,24 @@ public class MerchantBiFollowService {
} }
} }
/**
* 将业务类型转换为 URL 参数格式
* 1->yanbao, 2->qinggai, 3->quanyi
*/
private String convertBizTypeToParam(Byte businessType) {
if (businessType == null) return "";
switch (businessType) {
case 1:
return "yanbao";
case 2:
return "qinggai";
case 3:
return "quanyi";
default:
return "";
}
}
private MerchantBiFollow getOrCreateFollow(MerchantBiFollowReq request) { private MerchantBiFollow getOrCreateFollow(MerchantBiFollowReq request) {
MerchantBiFollow follow = followMapper.findFollowByMerchantAndBiz(request.getMerchantId(), request.getBusinessType()); MerchantBiFollow follow = followMapper.findFollowByMerchantAndBiz(request.getMerchantId(), request.getBusinessType());
......
...@@ -3,6 +3,11 @@ package cn.com.glsx.dj.peanalysis.service; ...@@ -3,6 +3,11 @@ package cn.com.glsx.dj.peanalysis.service;
import cn.com.glsx.dj.mindworkplate.dto.bianalyse.CommonPushWeChatMsgDTO; import cn.com.glsx.dj.mindworkplate.dto.bianalyse.CommonPushWeChatMsgDTO;
import cn.com.glsx.dj.mindworkplate.enums.CommonErrorEnum; import cn.com.glsx.dj.mindworkplate.enums.CommonErrorEnum;
import cn.com.glsx.dj.mindworkplate.remote.BiAnalysisRemote; import cn.com.glsx.dj.mindworkplate.remote.BiAnalysisRemote;
import cn.com.glsx.dj.peanalysis.dto.WechatPushParam;
import cn.com.glsx.dj.peanalysis.dto.analysis.MerchantBiCommentReq;
import cn.com.glsx.dj.peanalysis.dto.analysis.MerchantBiFollowReq;
import cn.com.glsx.dj.peanalysis.model.MerchantBiFollow;
import cn.com.glsx.dj.peanalysis.utils.MetricsValueUtils;
import cn.com.glsx.framework.core.beans.rpc.RpcResponse; import cn.com.glsx.framework.core.beans.rpc.RpcResponse;
import cn.com.glsx.framework.core.exception.RpcServiceException; import cn.com.glsx.framework.core.exception.RpcServiceException;
import cn.com.glsx.scrm.wechat.enums.WorkWechatAppMessageTypeEnum; import cn.com.glsx.scrm.wechat.enums.WorkWechatAppMessageTypeEnum;
...@@ -11,6 +16,10 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -11,6 +16,10 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
...@@ -26,54 +35,150 @@ public class WechatPushService { ...@@ -26,54 +35,150 @@ public class WechatPushService {
@Autowired @Autowired
private BiAnalysisRemote biAnalysisRemote; private BiAnalysisRemote biAnalysisRemote;
@Autowired @Autowired
private Environment environment; private Environment environment;
/** /**
* 关注行为推送 * 关注行为推送
*/ */
public void pushFollowMsg(String staffName, String merchantName, String businessType, List<String> targetUuids) { public void pushFollowMsg(MerchantBiFollowReq req, List<String> targetUuids) {
String content = String.format("「%s」关注了你负责的「%s」门店:「%s」,请及时查看。", staffName, businessType, merchantName); String content = String.format("「%s」关注了你负责的「%s」门店:「%s」,请及时查看。",
sendPush("重要提醒", content, targetUuids); req.getStaffName(), MetricsValueUtils.getBusinessTypeName(req.getBusinessType()), req.getMerchantName());
WechatPushParam param = WechatPushParam.builder()
.title("重要提醒")
.content(content)
.storeId(req.getMerchantId())
.storeName(req.getMerchantName())
.businessTypeCode(req.getBusinessType())
.targetUuids(targetUuids)
.build();
sendPush(param);
} }
/** /**
* @ 提醒推送 * @ 提醒推送
*/ */
public void pushMentionMsg(String staffName, String merchantName, String businessType, List<String> targetUuids) { public void pushMentionMsg(MerchantBiCommentReq req, List<String> targetUuids) {
String content = String.format("「%s」关注了你负责的「%s」门店:「%s」 添加了留言并@你,请及时查看。", staffName, businessType, merchantName); String content = String.format("「%s」关注了你负责的「%s」门店:「%s」添加了留言并@你,请及时查看。",
sendPush("重要提醒", content, targetUuids); req.getStaffName(), req.getBusinessType(), req.getMerchantName());
WechatPushParam param = WechatPushParam.builder()
.title("重要提醒")
.content(content)
.storeId(req.getMerchantId())
.storeName(req.getMerchantName())
.businessTypeCode(req.getBusinessType())
.targetUuids(targetUuids)
.build();
sendPush(param);
} }
/** /**
* 每日动态汇总推送 * 每日动态汇总推送
*/ */
public void pushDailySummaryMsg(String merchantName, String businessType, int count, List<String> targetUuids, boolean isFollower) { public void pushDailySummaryMsg(MerchantBiFollow req, int count, List<String> targetUuids, boolean isFollower) {
String prefix = isFollower ? "你关注的" : "你负责的"; String prefix = isFollower ? "你关注的" : "你负责的";
String content = String.format("%s「%s」门店:「%s」昨日共有「%d」条动态,请及时查看。", prefix, businessType, merchantName, count); String content = String.format("%s「%s」门店:「%s」昨日共有「%d」条动态,请及时查看。",
sendPush("日常跟进提醒", content, targetUuids); prefix, MetricsValueUtils.getBusinessTypeName(req.getBusinessType()), req.getMerchantName(), count);
WechatPushParam param = WechatPushParam.builder()
.title("日常跟进提醒")
.content(content)
.storeName(req.getMerchantName())
.businessTypeCode(req.getBusinessType())
.targetUuids(targetUuids)
.build();
sendPush(param);
} }
private void sendPush(String title, String content, List<String> uuids) { /**
* 发送推送
*/
private void sendPush(WechatPushParam param) {
try { try {
String url = Optional.ofNullable(environment.getProperty("dj.pe.analysis.follow.work.wechat.message.text.card.url")) // 构建 URL 参数
.orElseThrow(() -> new RpcServiceException(CommonErrorEnum.NOT_FOUND_CONFIG, "未找到url配置项")); String url = buildUrl(param);
CommonPushWeChatMsgDTO msg = CommonPushWeChatMsgDTO.builder() CommonPushWeChatMsgDTO msg = CommonPushWeChatMsgDTO.builder()
.uuids(new HashSet<>(uuids)) .uuids(new HashSet<>(param.getTargetUuids()))
.url(url) .url(url)
.title(title) .title(param.getTitle())
.btnTxt("查看详情") .btnTxt("查看详情")
.msgType(WorkWechatAppMessageTypeEnum.TEXTCARD.getValue()) .msgType(WorkWechatAppMessageTypeEnum.TEXTCARD.getValue())
.description(content) .description(param.getContent())
.build(); .build();
log.info("发送企微推送: msg:{}", msg); log.info("发送企微推送:msg:{}", msg);
RpcResponse<Boolean> response = biAnalysisRemote.pushWeChatMsg(msg); RpcResponse<Boolean> response = biAnalysisRemote.pushWeChatMsg(msg);
if (response == null || !response.isSuccess()) { if (response == null || !response.isSuccess()) {
log.error("企微推送失败: {}", response != null ? response.getMessage() : "null"); log.error("企微推送失败:{}", response != null ? response.getMessage() : "null");
} }
} catch (Exception e) { } catch (Exception e) {
log.error("企微推送异常", e); log.error("企微推送异常", e);
} }
} }
/**
* 构建动态 URL
*/
private String buildUrl(WechatPushParam param) {
// 如果没有传入日期,默认计算最近一个月的日期范围
String startDay = param.getStartDay();
String endDay = param.getEndDay();
if (startDay == null || endDay == null) {
LocalDate endDate = LocalDate.now().minusDays(1); // 昨天
LocalDate startDate = endDate.minusMonths(1).plusDays(1); // 上个月的同一天
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
startDay = startDate.format(formatter);
endDay = endDate.format(formatter);
}
String baseUrl = Optional.ofNullable(environment.getProperty("dj.pe.analysis.follow.work.wechat.message.text.card.url"))
.orElseThrow(() -> new RpcServiceException(CommonErrorEnum.NOT_FOUND_CONFIG, "未找到url配置项"));
// 构建动态 URL,对中文参数进行 URL 编码
return baseUrl +
"?storeId=" + (param.getStoreId() != null ? param.getStoreId() : "") +
"&storeName=" + urlEncode(param.getStoreName()) +
"&businessType=" + convertBizTypeToParam(param.getBusinessTypeCode()) +
"&queryType=" + (param.getQueryType() != null ? param.getQueryType() : "month") +
"&startDay=" + startDay +
"&endDay=" + endDay +
"&tab=" + (param.getTab() != null ? param.getTab() : "message");
}
/**
* 将业务类型转换为 URL 参数格式
* 1->yanbao, 2->qinggai, 3->quanyi
*/
private String convertBizTypeToParam(Byte businessTypeCode) {
if (businessTypeCode == null) {
return "";
}
switch (businessTypeCode) {
case 1:
return "yanbao";
case 2:
return "qinggai";
case 3:
return "quanyi";
default:
return "";
}
}
/**
* URL 编码处理中文
*/
private String urlEncode(String str) {
if (str == null || str.isEmpty()) {
return "";
}
try {
return URLEncoder.encode(str, "UTF-8");
} catch (UnsupportedEncodingException e) {
// UTF-8 是 Java 标准编码,不会抛出此异常
return str;
}
}
} }
...@@ -88,4 +88,18 @@ public final class MetricsValueUtils { ...@@ -88,4 +88,18 @@ public final class MetricsValueUtils {
log.error("不支持的bizs参数: {}", bizs); log.error("不支持的bizs参数: {}", bizs);
return -1; return -1;
} }
public static String getBusinessTypeName(Byte businessType) {
if (businessType == null) return "未知业务";
switch (businessType) {
case 1:
return "延保";
case 2:
return "轻改";
case 3:
return "权益";
default:
return "未知业务";
}
}
} }
  • @wangxw1
    commented
    Master

    你好,我是本次的代码审查工程师。本次提交主要涉及企微推送逻辑的重构,引入了 WechatPushParam 封装参数并支持了动态 URL 构建。以下是针对本次代码变更的审查意见:

    一、关键问题与优化建议(Top 3)

    1. 功能实现错误:pushMentionMsg 消息内容逻辑回归(重要性:高)

    • 问题描述:在 WechatPushService.java 的 pushMentionMsg 方法中,拼接推送内容时直接使用了 req.getBusinessType()(Byte 类型),而未调用工具类转换。
    • 影响分析:这会导致发送给用户的企微消息中,业务类型显示为原始数字(如 "1", "2")而非中文名称(如 "延保", "轻改"),影响用户体验且与重构前的逻辑不一致。
    • 优化建议:统一使用 MetricsValueUtils.getBusinessTypeName() 进行转换。 java // 修正后 String content = String.format("「%s」关注了你负责的「%s」门店:「%s」添加了留言并@你,请及时查看。", req.getStaffName(), MetricsValueUtils.getBusinessTypeName(req.getBusinessType()), // 需修正此处 req.getMerchantName());

    2. 代码冗余与维护性风险:业务类型转换逻辑重复定义(重要性:中)

    • 问题描述:convertBizTypeToParam 方法在 MerchantBiFollowService 和 WechatPushService 中各实现了一次,逻辑完全一致。
    • 影响分析:违反了 DRY(Don't Repeat Yourself)原则。未来若增加业务类型(如增加“保养”),开发者极易漏改其中一处,导致系统行为不一致,增加维护成本。
    • 优化建议:将此转换逻辑下沉到 MetricsValueUtils 或定义在业务类型枚举类中,实现全局复用。

    3. 缺乏配置健壮性:URL 参数拼接未处理潜在冲突(重要性:低)

    • 问题描述:在 buildUrl 方法中,通过 environment.getProperty 获取的 baseUrl 若末尾已包含 ? 或其他参数,直接拼接 ?storeId=... 会导致 URL 格式非法。
    • 影响分析:虽然当前配置可能没问题,但代码缺乏健壮性,配置项的微小变动可能导致推送链接失效。
    • 优化建议:在拼接前判断 baseUrl 是否已包含 ?。 java String separator = baseUrl.contains("?") ? "&" : "?"; return baseUrl + separator + "storeId=" + ...

    二、评分明细

    评分维度 分数 理由
    功能正确性与健壮性 34/40 发现一处逻辑回归 bug(pushMentionMsg 业务名显示错误),其余逻辑正确。
    安全性与潜在风险 30/30 正确使用了 URLEncoder 对中文参数进行编码,有效规避了 URL 解析异常风险。
    最佳实践与可维护性 16/20 引入 DTO 简化了参数传递,值得肯定;但存在代码重复(转换逻辑)及硬编码(Case 1, 2, 3)。
    性能与资源利用 5/5 推送逻辑不涉及复杂计算或资源竞争,性能良好。
    提交信息质量 5/5 Commit 信息非常规范,清晰描述了重构内容、原因及作者,符合工程规范。

    综合评分建议

    本次重构方向正确,显著提升了推送模块的可扩展性,但细节处存在逻辑疏忽和代码重复。修复上述 Top 3 问题后,代码质量将达到较高水平。

    总分:90分

Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!