Commit 6945ad0a by zhangxingmin

push

parent 0868fc62
package com.yd.notice.service.config; //package com.yd.notice.service.config;
//
import lombok.RequiredArgsConstructor; //import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.cp.api.WxCpService; //import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl; //import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl; //import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
import org.springframework.context.annotation.Bean; //import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; //import org.springframework.context.annotation.Configuration;
//
@Configuration //@Configuration
@RequiredArgsConstructor //@RequiredArgsConstructor
public class WecomConfig { //public class WecomConfig {
private final WecomProperties wecomProperties; // private final WecomProperties wecomProperties;
//
@Bean // @Bean
public WxCpService wxCpService() { // public WxCpService wxCpService() {
WxCpDefaultConfigImpl config = new WxCpDefaultConfigImpl(); // WxCpDefaultConfigImpl config = new WxCpDefaultConfigImpl();
config.setCorpId(wecomProperties.getCorpId()); // config.setCorpId(wecomProperties.getCorpId());
config.setAgentId(wecomProperties.getAgentId()); // config.setAgentId(wecomProperties.getAgentId());
config.setCorpSecret(wecomProperties.getSecret()); // config.setCorpSecret(wecomProperties.getSecret());
//
WxCpServiceImpl wxCpService = new WxCpServiceImpl(); // WxCpServiceImpl wxCpService = new WxCpServiceImpl();
wxCpService.setWxCpConfigStorage(config); // wxCpService.setWxCpConfigStorage(config);
return wxCpService; // return wxCpService;
} // }
} //}
\ No newline at end of file \ No newline at end of file
package com.yd.notice.service.config; //package com.yd.notice.service.config;
//
import lombok.Data; //import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties; //import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration; //import org.springframework.context.annotation.Configuration;
//
@Data //@Data
@Configuration //@Configuration
@ConfigurationProperties(prefix = "wecom") //@ConfigurationProperties(prefix = "wecom")
public class WecomProperties { //public class WecomProperties {
private String corpId; // private String corpId;
private Integer agentId; // private Integer agentId;
private String secret; // private String secret;
} //}
\ No newline at end of file \ No newline at end of file
package com.yd.notice.service.send; package com.yd.notice.service.send;
import com.alibaba.fastjson.JSONObject;
import com.yd.notice.service.model.ChannelConfig;
import com.yd.notice.service.service.IChannelConfigService;
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import me.chanjar.weixin.cp.bean.article.NewArticle; import me.chanjar.weixin.cp.bean.article.NewArticle;
import com.yd.notice.service.model.NotificationTask; import com.yd.notice.service.model.NotificationTask;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.cp.api.WxCpService; import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.message.WxCpMessage; import me.chanjar.weixin.cp.bean.message.WxCpMessage;
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@Slf4j @Slf4j
@Service @Service
@RequiredArgsConstructor @RequiredArgsConstructor
public class WecomMessageSender { public class WecomMessageSender {
private final WxCpService wxCpService;
private final IChannelConfigService channelConfigService;
/** /**
* 发送文本消息 * 发送文本消息(动态获取渠道配置)
* @param task 消息任务实体
* @return true-发送成功,false-发送失败
*/ */
public boolean sendTextMessage(NotificationTask task) { public boolean sendTextMessage(NotificationTask task) {
try { try {
// 构建消息体 // 1. 根据 channelBizId 查询渠道配置
WxCpMessage message = WxCpMessage.TEXT() ChannelConfig config = channelConfigService.getByChannelBizId(task.getChannelBizId());
.agentId(wxCpService.getWxCpConfigStorage().getAgentId()) if (config == null || config.getStatus() != 1) {
.toUser(task.getReceiver()) // 接收人UserID,多个用"|"分隔,或使用"@all" log.error("渠道配置不存在或已禁用,channelBizId: {}", task.getChannelBizId());
.content(task.getContent()) // 消息内容
.build();
// 发送消息
wxCpService.getMessageService().send(message);
log.info("企业微信消息发送成功,taskBizId: {}, receiver: {}",
task.getTaskBizId(), task.getReceiver());
return true;
} catch (Exception e) {
log.error("企业微信消息发送失败,taskBizId: {}, error: {}",
task.getTaskBizId(), e.getMessage(), e);
return false; return false;
} }
if (!"wecom".equals(config.getChannelType())) {
log.error("渠道类型不是企业微信,channelBizId: {}", task.getChannelBizId());
return false;
} }
// 2. 解析 config_value(JSON格式,需解密)
String configValueJson = config.getConfigValue();
// TODO: 若 config_value 是加密的,此处需要调用解密工具解密
JSONObject wecomConfig = JSONObject.parseObject(configValueJson);
String corpId = wecomConfig.getString("corpId");
Integer agentId = wecomConfig.getInteger("agentId");
String secret = wecomConfig.getString("secret");
/** // 3. 动态创建 WxCpService(每次新建,可增加缓存优化)
* 发送图文卡片消息(带跳转链接) WxCpDefaultConfigImpl wxConfig = new WxCpDefaultConfigImpl();
*/ wxConfig.setCorpId(corpId);
public boolean sendNewsMessage(NotificationTask task, String url, String description) { wxConfig.setAgentId(agentId);
try { wxConfig.setCorpSecret(secret);
// 使用 builder() 构建 NewArticle 对象 WxCpService wxCpService = new WxCpServiceImpl();
NewArticle article = NewArticle.builder() wxCpService.setWxCpConfigStorage(wxConfig);
.title(task.getTitle()) // 消息标题
.description(description) // 消息描述
.url(url) // 点击后跳转的链接
// .picUrl("https://your-domain.com/cover.jpg") // 可选:封面图片
// .btnText("查看详情") // 可选:按钮文字
.build();
// 构建 WxCpMessage 消息体 // 4. 构建消息并发送
WxCpMessage message = WxCpMessage.NEWS() WxCpMessage message = WxCpMessage.TEXT()
.agentId(wxCpService.getWxCpConfigStorage().getAgentId()) .agentId(agentId)
.toUser(task.getReceiver()) .toUser(task.getReceiver())
.addArticle(article) // 使用NewArticle对象 .content(task.getContent())
.build(); .build();
// 发送消息
wxCpService.getMessageService().send(message); wxCpService.getMessageService().send(message);
log.info("企业微信图文消息发送成功,taskBizId: {}", task.getTaskBizId()); log.info("企业微信消息发送成功,taskBizId: {}, channelBizId: {}", task.getTaskBizId(), task.getChannelBizId());
return true; return true;
} catch (Exception e) { } catch (Exception e) {
log.error("企业微信图文消息发送失败,taskBizId: {}", task.getTaskBizId(), e); log.error("企业微信消息发送失败,taskBizId: {}, error: {}", task.getTaskBizId(), e.getMessage(), e);
return false; return false;
} }
} }
// /**
// * 发送图文卡片消息(带跳转链接)
// */
// public boolean sendNewsMessage(NotificationTask task, String url, String description) {
// try {
// // 使用 builder() 构建 NewArticle 对象
// NewArticle article = NewArticle.builder()
// .title(task.getTitle()) // 消息标题
// .description(description) // 消息描述
// .url(url) // 点击后跳转的链接
// // .picUrl("https://your-domain.com/cover.jpg") // 可选:封面图片
// // .btnText("查看详情") // 可选:按钮文字
// .build();
//
// // 构建 WxCpMessage 消息体
// WxCpMessage message = WxCpMessage.NEWS()
// .agentId(wxCpService.getWxCpConfigStorage().getAgentId())
// .toUser(task.getReceiver())
// .addArticle(article) // 使用NewArticle对象
// .build();
//
// // 发送消息
// wxCpService.getMessageService().send(message);
// log.info("企业微信图文消息发送成功,taskBizId: {}", task.getTaskBizId());
// return true;
//
// } catch (Exception e) {
// log.error("企业微信图文消息发送失败,taskBizId: {}", task.getTaskBizId(), e);
// return false;
// }
// }
} }
\ No newline at end of file
...@@ -13,4 +13,5 @@ import com.baomidou.mybatisplus.extension.service.IService; ...@@ -13,4 +13,5 @@ import com.baomidou.mybatisplus.extension.service.IService;
*/ */
public interface IChannelConfigService extends IService<ChannelConfig> { public interface IChannelConfigService extends IService<ChannelConfig> {
ChannelConfig getByChannelBizId(String channelBizId);
} }
package com.yd.notice.service.service.impl; package com.yd.notice.service.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yd.notice.service.model.ChannelConfig; import com.yd.notice.service.model.ChannelConfig;
import com.yd.notice.service.dao.ChannelConfigMapper; import com.yd.notice.service.dao.ChannelConfigMapper;
import com.yd.notice.service.service.IChannelConfigService; import com.yd.notice.service.service.IChannelConfigService;
...@@ -17,4 +18,10 @@ import org.springframework.stereotype.Service; ...@@ -17,4 +18,10 @@ import org.springframework.stereotype.Service;
@Service @Service
public class ChannelConfigServiceImpl extends ServiceImpl<ChannelConfigMapper, ChannelConfig> implements IChannelConfigService { public class ChannelConfigServiceImpl extends ServiceImpl<ChannelConfigMapper, ChannelConfig> implements IChannelConfigService {
@Override
public ChannelConfig getByChannelBizId(String channelBizId) {
return this.getOne(new LambdaQueryWrapper<ChannelConfig>()
.eq(ChannelConfig::getChannelBizId, channelBizId)
.eq(ChannelConfig::getIsDeleted, 0));
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment