wlzboy
5 天以前 cfe0b79fbea0fb1d7a5a796e71ada7d3b7812046
ruoyi-common/src/main/java/com/ruoyi/common/utils/WechatUtils.java
@@ -200,4 +200,87 @@
        }
        return userAgent.toLowerCase().contains("micromessenger");
    }
    /**
     * 截断thing类型字段,微信订阅消息thing类型最长20个字符
     *
     * @param value 原始字符串
     * @return 截断后的字符串(最长20字符)
     */
    public static String truncateThingValue(String value) {
        if (StringUtils.isEmpty(value)) {
            return value;
        }
        // 微信thing类型最长20个字符
        final int MAX_THING_LENGTH = 20;
        if (value.length() <= MAX_THING_LENGTH) {
            return value;
        }
        // 截断并添加省略号,确保总长度不超过20
        return value.substring(0, MAX_THING_LENGTH - 1) + "…";
    }
    /**
     * 发送小程序订阅消息
     *
     * @param accessToken 微信接口调用凭证
     * @param touser 接收人openId
     * @param templateId 模板ID
     * @param page 小程序跳转页面
     * @param data 模板数据,key为字段名(如thing1、time27),value为字段值
     * @return 微信返回结果JSON
     */
    public static JSONObject sendSubscribeMessage(String accessToken,
                                                  String touser,
                                                  String templateId,
                                                  String page,
                                                  Map<String, String> data) {
        try {
            if (StringUtils.isEmpty(accessToken) || StringUtils.isEmpty(touser) || StringUtils.isEmpty(templateId)) {
                log.error("发送订阅消息参数不完整,accessToken={}, touser={}, templateId={}", accessToken, touser, templateId);
                return null;
            }
            String url = WECHAT_API_BASE_URL_SERVER + "/cgi-bin/message/subscribe/send?access_token=" + accessToken;
            Map<String, Object> body = new HashMap<>();
            body.put("touser", touser);
            body.put("template_id", templateId);
            if (StringUtils.isNotEmpty(page)) {
                body.put("page", page);
            }
             body.put("miniprogram_state", "formal");
            Map<String, Object> dataNode = new HashMap<>();
            if (data != null && !data.isEmpty()) {
                for (Map.Entry<String, String> entry : data.entrySet()) {
                    Map<String, String> valueNode = new HashMap<>();
                    valueNode.put("value", entry.getValue());
                    dataNode.put(entry.getKey(), valueNode);
                }
            }
            body.put("data", dataNode);
            String jsonBody = JSON.toJSONString(body);
            String response = HttpUtils.sendPost(url, jsonBody);
            JSONObject jsonObject = JSON.parseObject(response);
            if (jsonObject == null) {
                log.error("发送订阅消息返回为空");
                return null;
            }
            if (jsonObject.getIntValue("errcode") != 0) {
                log.error("发送订阅消息失败: {}", jsonObject.toJSONString());
            } else {
                log.info("发送订阅消息成功,touser={}, templateId={}", touser, templateId);
            }
            return jsonObject;
        } catch (Exception e) {
            log.error("发送订阅消息异常: {}", e.getMessage(), e);
            return null;
        }
    }
}