chatroomMsgs.js
3.33 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
import store from '../'
import config from '../../configs'
import util from '../../utils'
import {formatMsg} from './msgs'
export function onChatroomMsgs (msgs) {
if (!Array.isArray(msgs)) {
msgs = [msgs]
}
msgs = msgs.map(msg => {
return formatMsg(msg)
})
if (store.state.currChatroomId) {
store.commit('updateCurrChatroomMsgs', {
type: 'put',
msgs
})
}
}
function onSendMsgDone (error, msg) {
store.dispatch('hideLoading')
if (error) {
alert(error.message)
return
}
onChatroomMsgs([msg])
}
export function sendChatroomMsg ({state, commit}, obj) {
const chatroom = state.currChatroom
obj = obj || {}
let type = obj.type || ''
store.dispatch('showLoading')
switch (type) {
case 'text':
chatroom.sendText({
text: obj.text,
done: onSendMsgDone
})
break
case 'custom':
chatroom.sendCustomMsg({
content: JSON.stringify(obj.content),
pushContent: obj.pushContent,
done: onSendMsgDone
})
}
}
export function sendChatroomRobotMsg ({state, commit}, obj) {
const chatroom = state.currChatroom
let {type, robotAccid, content, params, target, body} = obj
if (type === 'text') {
chatroom.sendRobotMsg({
robotAccid,
content: {
type: 'text',
content,
},
body,
done: onSendMsgDone
})
} else if (type === 'welcome') {
chatroom.sendRobotMsg({
robotAccid,
content: {
type: 'welcome',
},
body,
done: onSendMsgDone
})
} else if (type === 'link') {
chatroom.sendRobotMsg({
robotAccid,
content: {
type: 'link',
params,
target
},
body,
done: onSendMsgDone
})
}
}
export function sendChatroomFileMsg ({state, commit}, obj) {
const chatroom = state.currChatroom
let {fileInput} = Object.assign({}, obj)
let type = 'file'
if (/\.(png|jpg|bmp|jpeg|gif)$/i.test(fileInput.value)) {
type = 'image'
} else if (/\.(mov|mp4|ogg|webm)$/i.test(fileInput.value)) {
type = 'video'
}
store.dispatch('showLoading')
chatroom.sendFile({
type,
fileInput,
uploadprogress: function (data) {
// console.log(data.percentageText)
},
uploaderror: function () {
console && console.log('上传失败')
},
uploaddone: function(error, file) {
// console.log(error);
// console.log(file);
},
beforesend: function (msg) {
// console && console.log('正在发送消息, id=', msg);
},
done: function (error, msg) {
onSendMsgDone (error, msg)
}
})
}
export function getChatroomHistoryMsgs ({state, commit}, obj) {
const chatroom = state.currChatroom
if (chatroom) {
let {timetag} = Object.assign({}, obj)
let options = {
timetag,
limit: config.localMsglimit || 20,
done: function getChatroomHistoryMsgsDone (error, obj) {
if (obj.msgs) {
if (obj.msgs.length === 0) {
commit('setNoMoreHistoryMsgs')
} else {
let msgs = obj.msgs.map(msg => {
return formatMsg(msg)
})
commit('updateCurrChatroomMsgs', {
type: 'concat',
msgs
})
}
}
store.dispatch('hideLoading')
}
}
store.dispatch('showLoading')
nim.getHistoryMsgs(options)
}
}