Commit 6945ad0a by zhangxingmin

push

parent 0868fc62
package com.yd.notice.service.config;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@RequiredArgsConstructor
public class WecomConfig {
private final WecomProperties wecomProperties;
@Bean
public WxCpService wxCpService() {
WxCpDefaultConfigImpl config = new WxCpDefaultConfigImpl();
config.setCorpId(wecomProperties.getCorpId());
config.setAgentId(wecomProperties.getAgentId());
config.setCorpSecret(wecomProperties.getSecret());
WxCpServiceImpl wxCpService = new WxCpServiceImpl();
wxCpService.setWxCpConfigStorage(config);
return wxCpService;
}
}
\ No newline at end of file
//package com.yd.notice.service.config;
//
//import lombok.RequiredArgsConstructor;
//import me.chanjar.weixin.cp.api.WxCpService;
//import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
//import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//
//@Configuration
//@RequiredArgsConstructor
//public class WecomConfig {
// private final WecomProperties wecomProperties;
//
// @Bean
// public WxCpService wxCpService() {
// WxCpDefaultConfigImpl config = new WxCpDefaultConfigImpl();
// config.setCorpId(wecomProperties.getCorpId());
// config.setAgentId(wecomProperties.getAgentId());
// config.setCorpSecret(wecomProperties.getSecret());
//
// WxCpServiceImpl wxCpService = new WxCpServiceImpl();
// wxCpService.setWxCpConfigStorage(config);
// return wxCpService;
// }
//}
\ No newline at end of file
package com.yd.notice.service.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Data
@Configuration
@ConfigurationProperties(prefix = "wecom")
public class WecomProperties {
private String corpId;
private Integer agentId;
private String secret;
}
\ No newline at end of file
//package com.yd.notice.service.config;
//
//import lombok.Data;
//import org.springframework.boot.context.properties.ConfigurationProperties;
//import org.springframework.context.annotation.Configuration;
//
//@Data
//@Configuration
//@ConfigurationProperties(prefix = "wecom")
//public class WecomProperties {
// private String corpId;
// private Integer agentId;
// private String secret;
//}
\ No newline at end of file
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 com.yd.notice.service.model.NotificationTask;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.message.WxCpMessage;
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
import org.springframework.stereotype.Service;
@Slf4j
@Service
@RequiredArgsConstructor
public class WecomMessageSender {
private final WxCpService wxCpService;
private final IChannelConfigService channelConfigService;
/**
* 发送文本消息
* @param task 消息任务实体
* @return true-发送成功,false-发送失败
* 发送文本消息(动态获取渠道配置)
*/
public boolean sendTextMessage(NotificationTask task) {
try {
// 构建消息体
WxCpMessage message = WxCpMessage.TEXT()
.agentId(wxCpService.getWxCpConfigStorage().getAgentId())
.toUser(task.getReceiver()) // 接收人UserID,多个用"|"分隔,或使用"@all"
.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);
// 1. 根据 channelBizId 查询渠道配置
ChannelConfig config = channelConfigService.getByChannelBizId(task.getChannelBizId());
if (config == null || config.getStatus() != 1) {
log.error("渠道配置不存在或已禁用,channelBizId: {}", task.getChannelBizId());
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");
/**
* 发送图文卡片消息(带跳转链接)
*/
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();
// 3. 动态创建 WxCpService(每次新建,可增加缓存优化)
WxCpDefaultConfigImpl wxConfig = new WxCpDefaultConfigImpl();
wxConfig.setCorpId(corpId);
wxConfig.setAgentId(agentId);
wxConfig.setCorpSecret(secret);
WxCpService wxCpService = new WxCpServiceImpl();
wxCpService.setWxCpConfigStorage(wxConfig);
// 构建 WxCpMessage 消息体
WxCpMessage message = WxCpMessage.NEWS()
.agentId(wxCpService.getWxCpConfigStorage().getAgentId())
// 4. 构建消息并发送
WxCpMessage message = WxCpMessage.TEXT()
.agentId(agentId)
.toUser(task.getReceiver())
.addArticle(article) // 使用NewArticle对象
.content(task.getContent())
.build();
// 发送消息
wxCpService.getMessageService().send(message);
log.info("企业微信图文消息发送成功,taskBizId: {}", task.getTaskBizId());
log.info("企业微信消息发送成功,taskBizId: {}, channelBizId: {}", task.getTaskBizId(), task.getChannelBizId());
return true;
} catch (Exception e) {
log.error("企业微信图文消息发送失败,taskBizId: {}", task.getTaskBizId(), e);
log.error("企业微信消息发送失败,taskBizId: {}, error: {}", task.getTaskBizId(), e.getMessage(), e);
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;
*/
public interface IChannelConfigService extends IService<ChannelConfig> {
ChannelConfig getByChannelBizId(String channelBizId);
}
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.dao.ChannelConfigMapper;
import com.yd.notice.service.service.IChannelConfigService;
......@@ -17,4 +18,10 @@ import org.springframework.stereotype.Service;
@Service
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