FinanceService.java
11.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
package com.tencent.wework;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.linkwechat.common.config.RuoYiConfig;
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.io.InputStream;
import java.security.PrivateKey;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Consumer;
/**
* @author danmo
* @description
* @date 2020/12/2 16:01
**/
@Slf4j
public class FinanceService{
/**
* NewSdk返回的sdk指针
*/
private long sdk = 0;
/**
* 超时时间,单位秒
*/
private final long timeout = 5 * 60;
/**
* 企业id
*/
private String corpId;
/**
* 会话存档密钥
*/
private String secret;
/**
* 会话私钥
*/
private String privateKey;
/**
* 代理
*/
private String proxy;
/**
* 代理密码
*/
private String passwd;
/**
* 一次拉取的消息条数,最大值1000条,超过1000条会返回错误
*/
private final long LIMIT = 1_000L;
private RuoYiConfig ruoYiConfig;
public FinanceService(String corpId, String secret, String privateKey){
this(corpId,secret,privateKey,"","");
}
/**
* 构造器
* @param corpId 企业id
* @param secret 会话存档密钥
* @param privateKey 会话私钥
*/
public FinanceService(String corpId, String secret, String privateKey, String proxy, String passwd){
this.corpId = corpId;
this.secret = secret;
this.privateKey = privateKey;
this.proxy = proxy;
this.passwd = passwd;
ruoYiConfig = SpringUtils.getBean(RuoYiConfig.class);
//初始化
initSDK();
}
/**
* 初始化
*/
private void initSDK() {
if (0 == sdk) {
sdk = Finance.NewSdk();
Finance.Init(sdk, corpId, secret);
}
}
/**
* 拉取聊天记录
*
* @param seq 消息的seq值,标识消息的序号
*/
public void getChatData(long seq, Consumer<JSONObject> consumer) {
long slice = Finance.NewSlice();
int ret = Finance.GetChatData(sdk, seq, LIMIT, proxy, passwd, timeout, slice);
if (ret != 0) {
log.info("getChatData ret " + ret);
return;
}
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 -> {
LocalSEQ.set(data.getLong("seq"));
JSONObject jsonObject = decryptChatRecord(sdk, data.getString("encrypt_random_key"), data.getString("encrypt_chat_msg"));
if (jsonObject != null) {
jsonObject.put("seq", LocalSEQ.get());
consumer.accept(jsonObject);
}
});
log.info("数据解析完成:------------");
}
Finance.FreeSlice(slice);
}
/**
* @param sdk 初始化时候获取到的值
* @param encryptRandomKey 企业微信返回的随机密钥
* @param encryptChatMsg 企业微信返回的单条记录的密文消息
* @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 JSONObject decryptChatRecord(Long sdk, String encryptRandomKey, String encryptChatMsg) {
Long msg = null;
try {
//获取私钥
PrivateKey privateKeyObj = RSAUtil.getPrivateKey(privateKey);
String str = RSAUtil.decryptRSA(encryptRandomKey, privateKeyObj);
//初始化参数slice
msg = Finance.NewSlice();
//解密
Finance.DecryptData(sdk, str, encryptChatMsg, 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 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 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 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 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 void setMediaImageData(JSONObject realJsonData, String msgType) {
String fileName = IdUtils.simpleUUID() + ".jpg";
getPath(realJsonData, msgType, fileName);
}
private void setMediaVoiceData(JSONObject realJsonData, String msgType) {
String fileName = IdUtils.simpleUUID() + ".amr";
getPath(realJsonData, msgType, fileName);
}
private void setMediaVideoData(JSONObject realJsonData, String msgType) {
String fileName = IdUtils.simpleUUID() + ".mp4";
getPath(realJsonData, msgType, fileName);
}
private 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 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);
if(ruoYiConfig.getFile().isStartCosUpload()) {
//开启云上传开关则云上传,不然上传本地
InputStream inputStream = new FileInputStream(new File(filePath,fileName));
String fileFullName = FileUploadUtils.upload2Cos(inputStream, FileUtil.getSuffix(fileName), ruoYiConfig.getFile().getCos());
String imgUrlPrefix = ruoYiConfig.getFile().getCos().getCosImgUrlPrefix();
data.put("attachment", imgUrlPrefix + fileFullName);
}else {
data.put("attachment", filePath + "/" + fileName);
}
} catch (Exception e) {
e.printStackTrace();
}
if (realJsonData.containsKey("content")) {
realJsonData.put("content", data);
} else {
realJsonData.put(msgType, data);
}
}
private String getFilePath(String msgType) {
return RuoYiConfig.getProfile() + "/" + msgType + "/" + DateUtils.getDate();
}
private void getMediaData(String sdkFileId, String filePath, String fileName) {
String indexbuf = "";
while (true) {
long mediaData = Finance.NewMediaData();
int ret = Finance.GetMediaData(sdk, indexbuf, sdkFileId, proxy, passwd, timeout, mediaData);
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(mediaData));
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
if (Finance.IsMediaDataFinish(mediaData) == 1) {
Finance.FreeMediaData(mediaData);
break;
} else {
indexbuf = Finance.GetOutIndexBuf(mediaData);
Finance.FreeMediaData(mediaData);
}
}
}
}