user.js
2.08 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
import tim from '../../tim'
const user = {
state: {
currentUserProfile: {},
isLogin: false,
isSDKReady: false, // TIM SDK 是否 ready
userID: 0,
userSig: '',
sdkAppID: 0,
},
mutations: {
updateCurrentUserProfile(state, userProfile) {
state.currentUserProfile = userProfile
},
toggleIsLogin(state, isLogin) {
state.isLogin = typeof isLogin === 'undefined' ? !state.isLogin : isLogin
},
toggleIsSDKReady(state, isSDKReady) {
state.isSDKReady = typeof isSDKReady === 'undefined' ? !state.isSDKReady : isSDKReady
},
reset(state) {
Object.assign(state, {
currentUserProfile: {},
isLogin: false,
isSDKReady: false // TIM SDK 是否 ready
})
},
GET_USER_INFO(state, payload) {
state.userID = payload.userID
state.userSig = payload.userSig
state.sdkAppID = payload.sdkAppID
},
},
actions: {
login(context, userID) {
tim.login({
userID,
userSig: window.genTestUserSig(userID).userSig
}).then(() => {
context.commit('toggleIsLogin', true)
context.commit('startComputeCurrent')
window.$message({type: 'success', message: '登录成功'})
}).catch(imError => {
window.$message.error(imError.message)
})
},
logout(context) {
// 若有当前会话,在退出登录时已读上报
if (context.rootState.conversation.currentConversation.conversationID) {
tim.setMessageRead({conversationID: context.rootState.conversation.currentConversation.conversationID})
}
tim.logout().then(() => {
context.commit('toggleIsLogin')
context.commit('stopComputeCurrent')
context.commit('reset')
window.close()
})
},
}
}
export default user