FinanceUtils.java
14.1 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package com.tencent.wework;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.linkwechat.common.config.CosConfig;
import com.linkwechat.common.config.RuoYiConfig;
import com.linkwechat.common.constant.Constants;
import com.linkwechat.common.constant.WeConstans;
import com.linkwechat.common.core.redis.RedisCache;
import com.linkwechat.common.utils.DateUtils;
import com.linkwechat.common.utils.StringUtils;
import com.linkwechat.common.utils.file.FileUploadUtils;
import com.linkwechat.common.utils.spring.SpringUtils;
import com.linkwechat.common.utils.uuid.IdUtils;
import com.linkwechat.common.utils.wecom.RSAUtil;
import lombok.extern.slf4j.Slf4j;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.PrivateKey;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicLong;
/**
* @author danmo
* @description
* @date 2020/12/2 16:01
**/
@Slf4j
public class FinanceUtils {
/**
* NewSdk返回的sdk指针
*/
private static volatile long sdk = 0;
/**
* 超时时间,单位秒
*/
private final static long timeout = 5 * 60;
private static String downloadWeWorkPath = RuoYiConfig.getDownloadWeWorkPath();
/**
* 初始化
*
* @param corpId 企业id
* @param secret 会话存档密钥
*/
public static void initSDK(String corpId, String secret) {
if (sdk == 0) {
sdk = Finance.NewSdk();
Finance.Init(sdk, corpId, secret);
}
}
private final static String privateKey = "-----BEGIN RSA PRIVATE KEY-----\n" +
"MIIEpAIBAAKCAQEAjGwIQIMGDJonMQdX3OJzZN0YVoYbORK1FU/8vhwdI9ixxSGW\n" +
"nrAQc6r8k3g1QTXzmKAxdVlAa1OGZCpl5X5A6C9JwDhFpuN+SKJxfxtHNv1oBk0r\n" +
"LnDftsdlibZFQn3fiMomxKyU8eujWBe31+pLQXaTSdSHcNhb+NrBa7AtNUr/c7bK\n" +
"nnFUZHDpXFB4Z0BhK1cRh2NN7ppLNnIJXpduGrwGtLdx++LsBaX2vjO4qW/DKP2Z\n" +
"3gZON1tGoofS1IPhCg0naoRN3lL7ctFAyxOr2ualGOc6hpvBGfjnBKU42aoaKQEi\n" +
"7tVJHChonZXMRcm4mdPu5371IzPeR3NYlP3ESQIDAQABAoIBAEux8MDV4GzqrDXB\n" +
"v5tSduHoTTZXFFWvv+29MIFKpmqsZSfe81KXlkbD5WOZaRu9+ZJMyFx5wEAaxlc4\n" +
"g7UyWcrPsvHrY6CTAYAmEQhzq+/4qhq7fouKTQ46boHNv4gYUmdqkXtzjNbcLzFN\n" +
"l8zMf8TKJpul9VzfRLThN2LVlYkkETcCukzZrK+aSCi6E/dL+noL1ndZYmWF5kbB\n" +
"lk881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RIdv5sVkE24gNG7sawg==\n" +
"-----END RSA PRIVATE KEY-----";
/**
* 拉取聊天记录
*
* @param seq 消息的seq值,标识消息的序号
* @param proxy 代理
* @param passwd 密码
*/
public static List<JSONObject> getChatData(long seq, String proxy, String passwd, RedisCache redisCache) {
List<JSONObject> resList = new ArrayList<>();
long slice = Finance.NewSlice();
int ret = Finance.GetChatData(sdk, seq, WeConstans.LIMIT, proxy, passwd, timeout, slice);
if (ret != 0) {
log.info("getChatData ret " + ret);
return null;
}
String content = Finance.GetContentFromSlice(slice);
JSONArray chatdataArr = JSONObject.parseObject(content).getJSONArray("chatdata");
log.info("开始执行数据解析:------------");
AtomicLong LocalSEQ = new AtomicLong();
if (CollectionUtil.isNotEmpty(chatdataArr)) {
chatdataArr.stream().map(data -> (JSONObject) data).forEach(data -> {
try {
LocalSEQ.set(data.getLong("seq"));
JSONObject jsonObject = decryptChatRecord(sdk, data.getString("encrypt_random_key"),
data.getString("encrypt_chat_msg"), privateKey);
if (jsonObject == null) {
return;
}
jsonObject.put("seq", LocalSEQ.get());
resList.add(jsonObject);
} catch (Exception e) {
e.printStackTrace();
}
});
log.info("数据解析完成:------------");
}
Finance.FreeSlice(slice);
redisCache.setCacheObject(WeConstans.CONTACT_SEQ_KEY, LocalSEQ.get());
return resList;
}
/**
* @param sdk 初始化时候获取到的值
* @param ncrypt_random_key 企业微信返回的随机密钥
* @param encrypt_chat_msg 企业微信返回的单条记录的密文消息
* @param privateKey 企业微信管理后台设置的私钥,!!!版本记得对应上!!!
* @return JSONObject 返回不同格式的聊天数据,格式有二十来种
* 详情请看官网 https://open.work.weixin.qq.com/api/doc/90000/90135/91774#%E6%B6%88%E6%81%AF%E6%A0%BC%E5%BC%8F
*/
private static JSONObject decryptChatRecord(Long sdk, String ncrypt_random_key, String encrypt_chat_msg, String privateKey) {
Long msg = null;
try {
//获取私钥
PrivateKey privateKeyObj = RSAUtil.getPrivateKey(privateKey);
String str = RSAUtil.decryptRSA(ncrypt_random_key, privateKeyObj);
//初始化参数slice
msg = Finance.NewSlice();
//解密
Finance.DecryptData(sdk, str, encrypt_chat_msg, msg);
String jsonDataStr = Finance.GetContentFromSlice(msg);
JSONObject realJsonData = JSONObject.parseObject(jsonDataStr);
String msgType = realJsonData.getString("msgtype");
if (StringUtils.isNotEmpty(msgType)) {
getSwitchType(realJsonData, msgType);
}
log.info("数据解析:------------" + realJsonData.toJSONString());
return realJsonData;
} catch (Exception e) {
log.error("解析密文失败");
return null;
} finally {
if (msg != null) {
//释放参数slice
Finance.FreeSlice(msg);
}
}
}
private static void getSwitchType(JSONObject realJsonData, String msgType) {
switch (msgType) {
case "image":
setMediaImageData(realJsonData, msgType);
break;
case "voice":
setMediaVoiceData(realJsonData, msgType);
break;
case "video":
setMediaVideoData(realJsonData, msgType);
break;
case "emotion":
setMediaEmotionData(realJsonData, msgType);
break;
case "file":
setMediaFileData(realJsonData, msgType);
break;
case "mixed":
setMediaMixedData(realJsonData, msgType);
break;
case "meeting_voice_call":
case "voip_doc_share":
setMediaMeetingVoiceCallData(realJsonData, msgType);
break;
default:
break;
}
}
private static void setMediaMeetingVoiceCallData(JSONObject realJsonData, String msgType) {
JSONObject meetingVoiceCall = Optional.ofNullable(realJsonData.getJSONObject(msgType))
.orElse(realJsonData.getJSONObject("content"));
String fileName = meetingVoiceCall.getString("filename");
getPath(realJsonData, msgType, fileName);
}
private static void setMediaMixedData(JSONObject realJsonData, String msgType) {
JSONObject mixedData = realJsonData.getJSONObject(msgType);
JSONArray items = mixedData.getJSONArray("item");
items.stream().map(item -> (JSONObject) item).forEach(item -> {
getSwitchType(item, item.getString("type"));
});
}
private static void setMediaFileData(JSONObject realJsonData, String msgType) {
JSONObject emotionData = Optional.ofNullable(realJsonData.getJSONObject(msgType))
.orElse(realJsonData.getJSONObject("content"));
String filename = emotionData.getString("filename");
//String fileext = emotionData.getString("fileext");
//String fileName = filename+"."+fileext;
getPath(realJsonData, msgType, filename);
}
private static void setMediaImageData(JSONObject realJsonData, String msgType) {
String fileName = IdUtils.simpleUUID() + ".jpg";
getPath(realJsonData, msgType, fileName);
}
private static void setMediaVoiceData(JSONObject realJsonData, String msgType) {
String fileName = IdUtils.simpleUUID() + ".mp3";
getPath(realJsonData, msgType, fileName);
}
private static void setMediaVideoData(JSONObject realJsonData, String msgType) {
String fileName = IdUtils.simpleUUID() + ".mp4";
getPath(realJsonData, msgType, fileName);
}
private static void setMediaEmotionData(JSONObject realJsonData, String msgType) {
String fileName = "";
JSONObject emotionData = realJsonData.getJSONObject(msgType);
Integer type = emotionData.getInteger("type");
switch (type) {
case 1:
fileName = IdUtils.simpleUUID() + ".gif";
break;
case 2:
fileName = IdUtils.simpleUUID() + ".png";
break;
default:
break;
}
getPath(realJsonData, msgType, fileName);
}
/*//本地存储
private static void getPath(JSONObject realJsonData, String msgType, String fileName) {
String filePath = getFilePath(msgType);
JSONObject data = Optional.ofNullable(realJsonData.getJSONObject(msgType))
.orElse(realJsonData.getJSONObject("content"));
String sdkfileid = data.getString("sdkfileid");
try {
getMediaData(sdkfileid, "", "", filePath, fileName);
} catch (Exception e) {
e.printStackTrace();
}
String currentDir = StringUtils.substring(filePath,downloadWeWorkPath.length()+1);
String pathFileName;
if(org.apache.commons.lang3.StringUtils.isBlank(currentDir)){
pathFileName = Constants.RESOURCE_PREFIX + "/" +fileName;
}else {
pathFileName = Constants.RESOURCE_PREFIX + "/" + currentDir + "/" + fileName;
}
data.put("attachment", pathFileName);
if (realJsonData.containsKey("content")) {
realJsonData.put("content", data);
} else {
realJsonData.put(msgType, data);
}
}*/
//云存储
private static void getPath(JSONObject realJsonData, String msgType, String fileName) {
String filePath = getFilePath(msgType);
JSONObject data = Optional.ofNullable(realJsonData.getJSONObject(msgType))
.orElse(realJsonData.getJSONObject("content"));
String sdkfileid = data.getString("sdkfileid");
try {
getMediaData(sdkfileid, "", "", filePath, fileName);
CosConfig cosConfig = SpringUtils.getBean(CosConfig.class);
String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
StringBuilder cosUrl = new StringBuilder(cosConfig.getImgUrlPrefix());
String cosFilePath = FileUploadUtils.upload2Cos(new FileInputStream(new File(filePath, fileName)), suffix, cosConfig);
cosUrl.append(cosFilePath);
data.put("attachment", cosUrl.toString());
} catch (Exception e) {
e.printStackTrace();
}
if (realJsonData.containsKey("content")) {
realJsonData.put("content", data);
} else {
realJsonData.put(msgType, data);
}
}
private static String getFilePath(String msgType) {
return StringUtils.format(downloadWeWorkPath, msgType, DateUtils.getDate());
}
private static void getMediaData(String sdkFileid, String proxy, String passwd, String filePath, String fileName) {
String indexbuf = "";
while (true) {
long media_data = Finance.NewMediaData();
int ret = Finance.GetMediaData(sdk, indexbuf, sdkFileid, proxy, passwd, timeout, media_data);
log.info("getMediaData ret:" + ret);
if (ret != 0) {
return;
}
try {
File f = new File(filePath);
if (!f.exists()) {
f.mkdirs();
}
File file = new File(filePath, fileName);
if (!file.isDirectory()) {
file.createNewFile();
}
FileOutputStream outputStream = new FileOutputStream(file, true);
outputStream.write(Finance.GetData(media_data));
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
if (Finance.IsMediaDataFinish(media_data) == 1) {
Finance.FreeMediaData(media_data);
break;
} else {
indexbuf = Finance.GetOutIndexBuf(media_data);
Finance.FreeMediaData(media_data);
}
}
}
}