session.js
2.31 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
/*
* 会话列表
*/
import store from '../'
// 如果会话对象不是好友,需要更新好友名片
function updateSessionAccount (sessions) {
let accountsNeedSearch = []
sessions.forEach(item => {
if (item.scene === 'p2p') {
// 如果不存在缓存资料
if (!store.state.userInfos[item.to]) {
accountsNeedSearch.push(item.to)
}
}
})
if (accountsNeedSearch.length > 0) {
store.dispatch('searchUsers', {
accounts: accountsNeedSearch
})
}
}
// onSessions只在初始化完成后回调
export function onSessions (sessions) {
updateSessionAccount(sessions)
store.commit('updateSessions', sessions)
}
export function onUpdateSession (session) {
let sessions = [session]
updateSessionAccount(sessions)
store.commit('updateSessions', sessions)
}
export function deleteSession ({state, commit}, sessionId) {
const nim = state.nim
sessionId = sessionId || ''
let scene = null
let account = null
if (/^p2p-/.test(sessionId)) {
scene = 'p2p'
account = sessionId.replace(/^p2p-/, '')
} else if (/^team-/.test(sessionId)) {
scene = 'team'
account = sessionId.replace(/^team-/, '')
}
if (account && scene) {
nim.deleteSession({
scene,
to: account,
done: function deleteServerSessionDone (error, obj) {
if (error) {
alert(error)
return
}
nim.deleteLocalSession({
id: sessionId,
done: function deleteLocalSessionDone (error, obj) {
if (error) {
alert(error)
return
}
commit('deleteSessions', [sessionId])
}
})
}
})
}
}
export function setCurrSession ({state, commit, dispatch}, sessionId) {
const nim = state.nim
if (sessionId) {
commit('updateCurrSessionId', {
type: 'init',
sessionId
})
if (nim) {
// 如果在聊天页面刷新,此时还没有nim实例,需要在onSessions里同步
nim.setCurrSession(sessionId)
commit('updateCurrSessionMsgs', {
type: 'init',
sessionId
})
// 发送已读回执
dispatch('sendMsgReceipt')
}
}
}
export function resetCurrSession ({state, commit}) {
const nim = state.nim
nim.resetCurrSession()
commit('updateCurrSessionMsgs', {
type: 'destroy'
})
}