wlzboy
2026-01-24 b2bd9fb71ee17d0ec73429f03dc87c87a0a38325
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/QyWechatServiceImpl.java
@@ -1,5 +1,6 @@
package com.ruoyi.system.service.impl;
import com.ruoyi.common.config.WechatConfig;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.QyWechatArticle;
@@ -38,6 +39,8 @@
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private WechatConfig wechatConfig;
    /**
     * 发送企业微信消息
     */
@@ -59,6 +62,35 @@
            // 发送文本消息
            return sendTextMessage(qyUserId, title, content, notifyUrl);
        } catch (Exception e) {
            log.error("企业微信消息发送异常,userId={}", userId, e);
            return false;
        }
    }
    @Override
    public boolean sendNotifyMessageWithDefaultAppId(Long userId, String title, String content, String businessUrl){
        String appId=wechatConfig.getAppId();
        return sendNotifyMessage(userId,title,content,appId,businessUrl);
    }
    @Override
    public boolean sendNotifyMessage(Long userId, String title, String content, String appId, String businessUrl) {
        try {
            // 检查服务是否启用
            if (!isEnabled()) {
                log.info("企业微信服务未启用,跳过消息发送");
                return false;
            }
            // 获取用户的企业微信ID
            String qyUserId = getQyUserIdByUserId(userId);
            if (StringUtils.isEmpty(qyUserId)) {
                log.warn("用户{}未绑定企业微信ID,无法发送消息", userId);
                return false;
            }
            // 发送文本消息
            return sendTextMessage(qyUserId, title, content, appId, businessUrl);
        } catch (Exception e) {
            log.error("企业微信消息发送异常,userId={}", userId, e);
            return false;
@@ -101,6 +133,7 @@
            article.setTitle(title);
            article.setDescription(content);
            article.setUrl(notifyUrl);
            // 设置默认图片URL,您可以根据需要修改
            
@@ -141,6 +174,81 @@
        }
    }
    @Override
    public boolean sendTextMessage(String qyUserId, String title, String content, String appId, String businessUrl) {
        try {
            // 检查服务是否启用
            if (!isEnabled()) {
                log.info("企业微信服务未启用,跳过消息发送");
                return false;
            }
            // 获取企业微信配置
            String corpId = configService.selectConfigByKey("qy_wechat.corp_id");
            String corpSecret = configService.selectConfigByKey("qy_wechat.miniprogram_secret");
            if (StringUtils.isEmpty(corpId) || StringUtils.isEmpty(corpSecret)) {
                log.error("企业微信配置不完整,缺少corpId或corpSecret");
                return false;
            }
            // 获取AccessToken
            String accessToken = qyWechatAccessTokenService.getQyMiniAccessToken(corpId, corpSecret);
            if (StringUtils.isEmpty(accessToken)) {
                log.error("获取企业微信AccessToken失败");
                return false;
            }
            // 构造请求URL
            String url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + accessToken;
            // 构造文章对象
            QyWechatArticle article = new QyWechatArticle();
            article.setTitle(title);
            article.setDescription(content);
            article.setAppid(appId);
            article.setPagepath(businessUrl);
            // 设置默认图片URL,您可以根据需要修改
            // 构造请求参数
            Map<String, Object> params = new HashMap<>();
            params.put("touser", qyUserId);
            params.put("msgtype", "news");
            params.put("agentid", Integer.parseInt(configService.selectConfigByKey("qywechat.miniprogram.agentid")));
            // 构造文章列表
            List<QyWechatArticle> articles = new ArrayList<>();
            articles.add(article);
            params.put("news", Collections.singletonMap("articles", articles));
            // 发送HTTP POST请求
            String response = sendHttpPostRequest(url, params);
            if (StringUtils.isEmpty(response)) {
                log.error("发送企业微信消息失败,响应为空");
                return false;
            }
            // 解析响应结果
            QyWechatResponse result = parseResponse(response);
            if (result != null && result.getErrcode() == 0) {
                log.info("企业微信消息发送成功,用户ID: {}", qyUserId);
                return true;
            } else {
                log.error("企业微信消息发送失败,错误码: {}, 错误信息: {}",
                        result != null ? result.getErrcode() : "unknown",
                        result != null ? result.getErrmsg() : response);
                return false;
            }
        } catch (Exception e) {
            log.error("企业微信文本消息发送异常,qyUserId={}", qyUserId, e);
            return false;
        }
    }
    /**
     * 获取用户的企业微信ID
     */