-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.js
197 lines (177 loc) · 5.85 KB
/
renderer.js
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
var nodejieba = window.require('nodejieba');
const si = require('search-index')
var SDK = require('./lib/NIM_Web_SDK_v8.3.0_test')
let searchDB
si({
name: 'ELECTRON-FULLTEXT-SEARCH-DB',
storeVectors: true
}).then((result) => {
searchDB = result;
})
function doLog(err, obj) {
console.log('receive: ', err, obj)
}
const ignoreChars = " \t\r\n~!@#$%^&*()_+-=【】、{}|;':\",。、《》?αβγδεζηθικλμνξοπρστυφχψωΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩ。,、;:?!…—·ˉ¨‘’“”々~‖∶"'`|〃〔〕〈〉《》「」『』.〖〗【】()[]{}ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫ⒈⒉⒊⒋⒌⒍⒎⒏⒐⒑⒒⒓⒔⒕⒖⒗⒘⒙⒚⒛㈠㈡㈢㈣㈤㈥㈦㈧㈨㈩①②③④⑤⑥⑦⑧⑨⑩⑴⑵⑶⑷⑸⑹⑺⑻⑼⑽⑾⑿⒀⒁⒂⒃⒄⒅⒆⒇≈≡≠=≤≥<>≮≯∷±+-×÷/∫∮∝∞∧∨∑∏∪∩∈∵∴⊥∥∠⌒⊙≌∽√§№☆★○●◎◇◆□℃‰€■△▲※→←↑↓〓¤°#&@\︿_ ̄―♂♀┌┍┎┐┑┒┓─┄┈├┝┞┟┠┡┢┣│┆┊┬┭┮┯┰┱┲┳┼┽┾┿╀╁╂╃└┕┖┗┘┙┚┛━┅┉┤┥┦┧┨┩┪┫┃┇┋┴┵┶┷┸┹┺┻╋╊╉╈╇╆╅╄";
async function putFts(msgs) {
if (Object.prototype.toString.call(msgs) !== '[object Array]') {
msgs = [msgs]
}
// 分词,并过滤无意义的符号
var fts = msgs.filter(msg => msg.text && msg.idClient).map(msg => ({
idx: nodejieba.cut(msg.text).filter(word => !ignoreChars.includes(word)),
_id: msg.idClient
}))
await searchDB.PUT(fts);
console.log('search-index save success ', fts)
}
async function queryFts(text, limit = 100) {
var searchParams = nodejieba.cut(text).filter(word => !ignoreChars.includes(word))
let records = await searchDB.QUERY({
SEARCH: searchParams
})
let idClients = records.RESULT.map(item => item._id).slice(0, limit);
if (!idClients) {
console.log('查询本地消息,无匹配词')
return;
}
window.nim.getLocalMsgsByIdClients({
idClients,
done: function (err, obj) {
console.log('查询本地消息' + (!err ? '成功' : '失败'), err, obj);
}
})
}
async function deleteFts(_ids) {
if (Object.prototype.toString.call(_ids) !== '[object Array]') {
_ids = [_ids]
}
await searchDB.DELETE(_ids)
console.log('成功删除:', _ids)
}
window.nim = SDK.NIM.getInstance({
debug: true,
appKey: 'fe416640c8e8a72734219e1847ad2547',
account: 'cs6',
token: 'e10adc3949ba59abbe56e057f20f883e',
// db: form.db,
// syncSessionUnread: form.syncSessionUnread,
// autoMarkRead: form.syncSessionUnread,
reconnectionDelay: 1000, // 在第一次尝试重连之前最初等待多长时间
reconnectionDelayMax: 60000, // 重新连接之间等待的最大时间
reconnectionJitter: 0, // 重连等待时间振荡值
onconnect(obj) {
console.log('连接建立成功', obj);
// if (loginInfo) {
// 连接上以后更新uid
// commit('updateUserUID', loginInfo)
// }
},
onerror() {
// alert(JSON.stringify(event))
// debugger
console.error('error');
// location.href = config.loginUrl
},
onwillreconnect(obj) {
console.log(obj)
},
ondisconnect: function onDisconnect (error) {
let map = {
PC: '电脑版',
Web: '网页版',
Android: '手机版',
iOS: '手机版',
WindowsPhone: '手机版'
}
let str = error.from
let errorMsg = `你的帐号于${new Date()}被${(map[str] || '其他端')}踢出下线,请确定帐号信息安全!`
switch (error.code) {
// 账号或者密码错误, 请跳转到登录页面并提示错误
case 302:
console.log('帐号或密码错误');
break
// 被踢, 请提示错误后跳转到登录页面
case 'kicked':
console.log('被踢')
break
default:
console.error(error);
break
}
},
/* 关系(静默,黑名单)及好友,同步及更新 */
onfriends: doLog,
onsyncfriendaction: doLog,
// onmutelist: doLog,
// onsyncmarkinmutelist: doLog,
onblacklist: doLog,
onsyncmarkinblacklist: doLog,
/* 用户信息/名片,同步及更新 */
onmyinfo: doLog,
onupdatemyinfo: doLog,
onusers: doLog,
onupdateuser: doLog,
/* 群组信息,同步及更新 */
onteams: doLog,
onsynccreateteam: doLog,
onteammembers: doLog,
onCreateTeam: doLog,
onDismissTeam: doLog,
onUpdateTeam: doLog,
onAddTeamMembers: doLog,
onRemoveTeamMembers: doLog,
onUpdateTeamManagers: doLog,
onupdateteammember: doLog,
onUpdateTeamMembersMute: doLog,
onTeamMsgReceipt: doLog,
/* 超级群,同步及更新 */
onSuperTeams: doLog,
onSyncCreateSuperTeam: doLog,
onUpdateSuperTeam: doLog,
onUpdateSuperTeamMember: doLog,
onAddSuperTeamMembers: doLog,
onRemoveSuperTeamMembers: doLog,
onDismissSuperTeam: doLog,
// onTransferSuperTeam: doLog,
onUpdateSuperTeamMembersMute: doLog,
/* 会话 */
onsessions: doLog,
onupdatesession: doLog,
/* 消息 */
onroamingmsgs: function(obj) {
putFts(obj)
},
onofflinemsgs: function(obj) {
putFts(obj)
},
onmsg: function(obj) {
putFts(obj)
},
/* 系统通知 */
onsysmsg: doLog,
onofflinesysmsgs: doLog,
onupdatesysmsg: doLog,
onsysmsgunread: doLog,
onupdatesysmsgunread: doLog,
onofflinecustomsysmsgs: doLog,
oncustomsysmsg: doLog,
onStickTopSessions: function(session) {
console.log('收到置顶会话列表', session);
},
/* 同步完成 */
onsyncdone: function onSyncDone () {
// store.commit('setLoading', false)
console.log('onsyncdone')
}
});
// window.nim.sendText({
// scene: 'p2p',
// to: 'cs2',
// text: '春眠不觉晓',
// done(err, obj) {
// if (err) return
// // 发送失败的时候可能无 idClient
// if (!obj.idClient) return
// putFts(obj)
// }
// })