zhuo
2025-04-24 89536a3b40beed6619d02306bcfc3c96b3c3ec9c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package com.ruoyi.common.utils;
 
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import me.chanjar.weixin.cp.bean.message.WxCpMessage;
import me.chanjar.weixin.cp.bean.message.WxCpMessageSendResult;
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
 
import java.io.*;
import java.net.MalformedURLException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.List;
 
/**
 * @Author: chenxiangfeng
 * @Date: 2021/7/20 10:35
 */
public class WxCpUtils {
 
    public static final String CORP_ID = "wwa423654b975441ac";
    public static final String CORP_SECRET = "snXq8qwA5tGu0YN1PlSDQqr6u9x3A0c_jQDmt8CN8Vs";
    public static final Integer AGENT_ID = 1000515;
 
    /**
     * @param user      例:ZT-033268|ZT-028629,多个中间用|隔开
     * @param content   传文本内容
     * @param imageFile 图片
     * @throws Exception
     */
    public static void inform(String user, String content, File imageFile) throws Exception {
        WxCpDefaultConfigImpl config = new WxCpDefaultConfigImpl();
        // 注册的企业信息
        config.setCorpId(CORP_ID);
        config.setCorpSecret(CORP_SECRET);
 
        WxCpServiceImpl wxCpService = new WxCpServiceImpl();
        wxCpService.setWxCpConfigStorage(config);
 
        WxCpMessage message;
        WxCpMessageSendResult result;
        // 发送文本
        if (StringUtils.isNotBlank(content)) {
            message = WxCpMessage.TEXT()
                    .content(content)
                    // 用户id
                    .toUser(user)
                    // 应用id
                    .agentId(AGENT_ID)
                    .build();
            result = wxCpService.getMessageService().send(message);
        }
        // 发送文件
        if (imageFile != null) {
            // 获取文件id
            WxMediaUploadResult imgResult = wxCpService.getMediaService().upload("file", imageFile);
            message = WxCpMessage.IMAGE()
                    .mediaId(imgResult.getMediaId())
                    // 用户id
                    .toUser(user)
                    // 应用id
                    .agentId(AGENT_ID)
                    .build();
            result = wxCpService.getMessageService().send(message);
        }
    }
 
    /**
     * 推送群消息
     *
     * @param webHookList 企业微信机器人地址
     * @param file        图片
     * @throws Exception
     */
    public static void informWebHook(List<String> webHookList, File file) throws Exception {
        JSONObject json = new JSONObject();
        json.putOpt("msgtype", "image");
        JSONObject image = new JSONObject();
        image.putOpt("base64", getBase64(file));
        image.putOpt("md5", getMd5(file));
        json.putOpt("image", image);
 
        webHookList.stream().forEach(webHook -> {
            String result = HttpRequest.post(webHook)
                    .header("Content-Type", "application/json")
                    .body(json.toString())
                    .execute().body();
        });
    }
 
    /**
     * todo:推送群消息
     *
     * @param webHook 企业微信机器人地址
     * @param content 数据
     * @throws Exception
     */
    public static String informWebHook(String webHook, String content) {
        JSONObject jsonObject = new JSONObject()
                .accumulate("msgtype", "text")
                .accumulate("text", new JSONObject()
                        .accumulate("content", content));
        return HttpRequest.post(webHook).header("Content-Type", "application/json").body(jsonObject.toString()).execute().body();
    }
 
    public static String getMd5(File file) {
        String name = "";
        try {
            InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
            byte[] bytes = new byte[1024];
            int len = 0;
            MessageDigest messagedigest = MessageDigest.getInstance("MD5");
            while ((len = inputStream.read(bytes)) > 0) {
                messagedigest.update(bytes, 0, len);
            }
            name = Md5Util.bufferToHex(messagedigest.digest());
            inputStream.close();
        } catch (MalformedURLException e) {
        } catch (IOException e) {
        } catch (NoSuchAlgorithmException e) {
        }
        return name;
 
    }
 
    public static String getBase64(File file) {
        FileInputStream inputStream = null;
        String base64Str = "";
        try {
            inputStream = new FileInputStream(file);
            Base64.Encoder encoder = Base64.getEncoder();
            int available = inputStream.available();
            byte[] bytes = new byte[available];
            inputStream.read(bytes);
            base64Str = encoder.encodeToString(bytes);
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return base64Str;
    }
 
}