| | |
| | | } |
| | | } |
| | | |
| | | @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; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 发送企业微信文本消息 |
| | | */ |
| | |
| | | article.setTitle(title); |
| | | article.setDescription(content); |
| | | article.setUrl(notifyUrl); |
| | | |
| | | // 设置默认图片URL,您可以根据需要修改 |
| | | |
| | | |
| | |
| | | } |
| | | } |
| | | |
| | | @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 |
| | | */ |