wechat.js
4.22 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
/**
* routes/wechat.js — 微信数据导入 REST 路由
*
* POST /api/wechat/import-contacts body: { limit? }
* → 调用 WechatImporter.importContacts(),返回导入统计
*
* POST /api/wechat/import-chat body: { username, limit? }
* → 调用 WechatImporter.importChatHistory()
*
* GET /api/wechat/contacts query: ?q=搜索词&limit=50
* → 调用 WechatReader.getContacts() 然后本地过滤
*
* GET /api/wechat/sessions query: ?limit=100
* → 返回最近会话列表
*/
const express = require('express');
const router = express.Router();
const { WechatReader } = require('../services/wechat/wechat-reader');
const { WechatImporter } = require('../services/wechat/wechat-importer');
// 懒加载单例
let _reader = null;
let _importer = null;
function reader() {
if (!_reader) _reader = new WechatReader();
return _reader;
}
function importer() {
if (!_importer) _importer = new WechatImporter();
return _importer;
}
/**
* POST /api/wechat/import-contacts
* 批量导入微信联系人到图谱
*/
router.post('/import-contacts', (req, res) => {
try {
const limit = Math.min(parseInt(req.body?.limit) || 200, 20000);
const result = importer().importContacts(limit);
res.json({ ok: true, ...result });
} catch (e) {
console.error('[wechat] import-contacts error:', e);
res.status(500).json({ ok: false, error: e.message });
}
});
/**
* POST /api/wechat/import-chat
* 导入指定联系人的聊天记录
*/
router.post('/import-chat', (req, res) => {
const { username, limit } = req.body || {};
if (!username) {
return res.status(400).json({ ok: false, error: '缺少 username 参数' });
}
try {
const msgLimit = parseInt(limit) || 99999;
const result = importer().importChatHistory(username, msgLimit);
res.json({ ok: true, ...result });
} catch (e) {
console.error('[wechat] import-chat error:', e);
res.status(500).json({ ok: false, error: e.message });
}
});
/**
* GET /api/wechat/contacts?q=搜索词&limit=50
* 从 wechat_plain 搜索联系人(不写图谱)
*/
router.get('/contacts', (req, res) => {
try {
const q = (req.query.q || '').trim().toLowerCase();
const limit = Math.min(parseInt(req.query.limit) || 50, 1000);
// 读取足够多联系人以便本地过滤
const fetchLimit = q ? 5000 : limit;
let contacts = reader().getContacts(fetchLimit);
if (q) {
contacts = contacts.filter(c =>
c.nickName.toLowerCase().includes(q) ||
c.remark.toLowerCase().includes(q) ||
c.alias.toLowerCase().includes(q) ||
c.username.toLowerCase().includes(q)
).slice(0, limit);
} else {
contacts = contacts.slice(0, limit);
}
res.json({ ok: true, total: contacts.length, contacts });
} catch (e) {
console.error('[wechat] contacts error:', e);
res.status(500).json({ ok: false, error: e.message });
}
});
/**
* GET /api/wechat/messages/:username?page=1&pageSize=200&q=关键词
* 分页 + 搜索聊天记录(直接读 SQLite,不写图谱)
*/
router.get('/messages/:username', (req, res) => {
const { username } = req.params;
const page = Math.max(1, parseInt(req.query.page) || 1);
const pageSize = Math.min(Math.max(1, parseInt(req.query.pageSize) || 200), 500);
const q = (req.query.q || '').trim();
try {
const result = reader().getMessagesPaged(username, page, pageSize, q);
res.json({ ok: true, ...result });
} catch (e) {
console.error('[wechat] messages error:', e);
res.status(500).json({ ok: false, error: e.message });
}
});
/**
* GET /api/wechat/sessions?limit=100
* 获取最近会话列表
*/
router.get('/sessions', (req, res) => {
try {
const limit = Math.min(parseInt(req.query.limit) || 100, 500);
const sessions = reader().getSessions(limit);
res.json({ ok: true, total: sessions.length, sessions });
} catch (e) {
console.error('[wechat] sessions error:', e);
res.status(500).json({ ok: false, error: e.message });
}
});
module.exports = router;