|
@@ -40,65 +40,61 @@ export function useDataSource () {
|
|
|
const userStore = useUserStore()
|
|
|
const loginType = useLoginType()
|
|
|
// 最近会话数据源
|
|
|
- if (!WKSDK.shared().config.provider.syncConversationsCallback) {
|
|
|
- WKSDK.shared().config.provider.syncConversationsCallback = async () => {
|
|
|
- const query = {
|
|
|
- msg_count: 100
|
|
|
- }
|
|
|
- if (loginType.loginType === 'enterprise') {
|
|
|
- Object.assign(query, { enterpriseId: userStore.baseInfo.enterpriseId })
|
|
|
- }
|
|
|
- const resultConversations = []
|
|
|
- const resp = await getConversationSync(query)
|
|
|
- // console.log(resp)
|
|
|
- const conversationList = resp
|
|
|
- if (conversationList) {
|
|
|
- conversationList.forEach(conversation => {
|
|
|
- conversation.channel = new Channel(conversation.channel_id, conversation.channel_type)
|
|
|
- conversation.unread = +(conversation.unread || 0)
|
|
|
- resultConversations.push(conversation)
|
|
|
- })
|
|
|
- }
|
|
|
- return resultConversations
|
|
|
+ WKSDK.shared().config.provider.syncConversationsCallback = async () => {
|
|
|
+ const query = {
|
|
|
+ msg_count: 100
|
|
|
}
|
|
|
+ if (loginType.loginType === 'enterprise') {
|
|
|
+ Object.assign(query, { enterpriseId: userStore.baseInfo.enterpriseId })
|
|
|
+ }
|
|
|
+ const resultConversations = []
|
|
|
+ const resp = await getConversationSync(query)
|
|
|
+ // console.log(resp)
|
|
|
+ const conversationList = resp
|
|
|
+ if (conversationList) {
|
|
|
+ conversationList.forEach(conversation => {
|
|
|
+ conversation.channel = new Channel(conversation.channel_id, conversation.channel_type)
|
|
|
+ conversation.unread = +(conversation.unread || 0)
|
|
|
+ resultConversations.push(conversation)
|
|
|
+ })
|
|
|
+ }
|
|
|
+ return resultConversations
|
|
|
}
|
|
|
- if (!WKSDK.shared().config.provider.syncMessagesCallback) {
|
|
|
// 同步频道消息数据源
|
|
|
- WKSDK.shared().config.provider.syncMessagesCallback = async function(channel) {
|
|
|
- // 后端提供的获取频道消息列表的接口数据 然后构建成 Message对象数组返回
|
|
|
- let resultMessages = new Array()
|
|
|
- const {
|
|
|
- startMessageSeq: start_message_seq,
|
|
|
- endMessageSeq: end_message_seq,
|
|
|
- limit,
|
|
|
- pullMode: pull_mode
|
|
|
- } = HISTORY_QUERY
|
|
|
- const query = {
|
|
|
- channel_id: channel.channelID,
|
|
|
- channel_type: channel.channelType,
|
|
|
- start_message_seq,
|
|
|
- end_message_seq,
|
|
|
- limit,
|
|
|
- pull_mode,
|
|
|
- }
|
|
|
- if (loginType.loginType === 'enterprise') {
|
|
|
- Object.assign(query, { enterpriseId: userStore.baseInfo.enterpriseId })
|
|
|
- }
|
|
|
- const resp = await getMessageSync(query)
|
|
|
- const messageList = resp && resp["messages"]
|
|
|
- if (messageList) {
|
|
|
- messageList.forEach((msg) => {
|
|
|
- // const message = Convert.toMessage(msg);
|
|
|
- // msg.channel = new Channel(msg.channel_id, msg.channel_type)
|
|
|
- msg.payload = JSON.parse(Base64.decode(msg.payload))
|
|
|
- resultMessages.push(msg);
|
|
|
- });
|
|
|
- }
|
|
|
- const more = resp.more === 1
|
|
|
- return {
|
|
|
- more,
|
|
|
- resultMessages
|
|
|
- }
|
|
|
+ WKSDK.shared().config.provider.syncMessagesCallback = async function(channel) {
|
|
|
+ // 后端提供的获取频道消息列表的接口数据 然后构建成 Message对象数组返回
|
|
|
+ let resultMessages = new Array()
|
|
|
+ const {
|
|
|
+ startMessageSeq: start_message_seq,
|
|
|
+ endMessageSeq: end_message_seq,
|
|
|
+ limit,
|
|
|
+ pullMode: pull_mode
|
|
|
+ } = HISTORY_QUERY
|
|
|
+ const query = {
|
|
|
+ channel_id: channel.channelID,
|
|
|
+ channel_type: channel.channelType,
|
|
|
+ start_message_seq,
|
|
|
+ end_message_seq,
|
|
|
+ limit,
|
|
|
+ pull_mode,
|
|
|
+ }
|
|
|
+ if (loginType.loginType === 'enterprise') {
|
|
|
+ Object.assign(query, { enterpriseId: userStore.baseInfo.enterpriseId })
|
|
|
+ }
|
|
|
+ const resp = await getMessageSync(query)
|
|
|
+ const messageList = resp && resp["messages"]
|
|
|
+ if (messageList) {
|
|
|
+ messageList.forEach((msg) => {
|
|
|
+ // const message = Convert.toMessage(msg);
|
|
|
+ // msg.channel = new Channel(msg.channel_id, msg.channel_type)
|
|
|
+ msg.payload = JSON.parse(Base64.decode(msg.payload))
|
|
|
+ resultMessages.push(msg);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ const more = resp.more === 1
|
|
|
+ return {
|
|
|
+ more,
|
|
|
+ resultMessages
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -194,6 +190,8 @@ async function getKey () {
|
|
|
}
|
|
|
|
|
|
export const useIM = () => {
|
|
|
+ useDataSource()
|
|
|
+ const key = ref(0)
|
|
|
const IM = useIMStore()
|
|
|
const unreadCount = ref(0)
|
|
|
const connected = ref(0)
|
|
@@ -208,14 +206,11 @@ export const useIM = () => {
|
|
|
// 认证信息
|
|
|
WKSDK.shared().config.uid = uid // 用户uid(需要在悟空通讯端注册过)
|
|
|
WKSDK.shared().config.token = token // 用户token (需要在悟空通讯端注册过)
|
|
|
- // console.log('1')
|
|
|
// 连接状态监听
|
|
|
WKSDK.shared().connectManager.addConnectStatusListener(connectStatusListener)
|
|
|
- // console.log('2')
|
|
|
// 常规消息监听
|
|
|
WKSDK.shared().chatManager.addMessageListener(messageListen)
|
|
|
// 连接
|
|
|
- // console.log('连接')
|
|
|
WKSDK.shared().connectManager.connect()
|
|
|
})
|
|
|
onUnmounted(() => {
|
|
@@ -229,15 +224,29 @@ export const useIM = () => {
|
|
|
async function messageListen (message) {
|
|
|
console.log('收到消息', message)
|
|
|
IM.setFromChannel(message.channel.channelID)
|
|
|
- const count = WKSDK.shared().conversationManager.getAllUnreadCount()
|
|
|
- IM.setNewMsg(count)
|
|
|
- unreadCount.value = count
|
|
|
- // console.log('未读消息数', count)
|
|
|
- // 创建通道
|
|
|
+ setUnreadCount()
|
|
|
}
|
|
|
+
|
|
|
async function connectStatusListener (status) {
|
|
|
// console.log('连接状态', status === ConnectStatus.Connected)
|
|
|
+ // 连接成功 获取点击数
|
|
|
connected.value = status === ConnectStatus.Connected
|
|
|
+ if (connected.value) {
|
|
|
+ await syncConversation()
|
|
|
+ setUnreadCount()
|
|
|
+ // setTimeout(() => {
|
|
|
+ // setUnreadCount()
|
|
|
+ // console.log('连接成功, 获取总数')
|
|
|
+ // }, 0)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ function setUnreadCount () {
|
|
|
+ const count = WKSDK.shared().conversationManager.getAllUnreadCount()
|
|
|
+ key.value++
|
|
|
+ IM.setNewMsg(key.value)
|
|
|
+ unreadCount.value = count
|
|
|
+ console.log('未读消息总数', count)
|
|
|
}
|
|
|
|
|
|
return {
|
|
@@ -247,17 +256,18 @@ export const useIM = () => {
|
|
|
}
|
|
|
|
|
|
export function initConnect (callback = () => {}) {
|
|
|
+ useDataSource()
|
|
|
const IM = useIMStore()
|
|
|
const conversationList = ref([])
|
|
|
const messageItems = ref([])
|
|
|
|
|
|
watch(
|
|
|
() => IM.newMsg,
|
|
|
- () => {
|
|
|
+ async () => {
|
|
|
// 未读消息变化
|
|
|
- syncConversation()
|
|
|
+ const res = await syncConversation()
|
|
|
+ conversationList.value = res
|
|
|
// 拉取最新消息 查看是否是自己的数据
|
|
|
-
|
|
|
},
|
|
|
{
|
|
|
deep: true,
|
|
@@ -283,35 +293,14 @@ export function initConnect (callback = () => {}) {
|
|
|
// 发送成功
|
|
|
console.log('发送成功')
|
|
|
// 添加一组成功数据
|
|
|
- callback()
|
|
|
+ callback(true)
|
|
|
} else {
|
|
|
// 发送失败
|
|
|
console.log('发送失败')
|
|
|
// 添加一组失败数据
|
|
|
- // callback()
|
|
|
+ callback(false)
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- // 常规消息监听
|
|
|
- // async function messageListen (message) {
|
|
|
- // console.log('收到消息', message)
|
|
|
- // syncConversation()
|
|
|
- // }
|
|
|
- // 同步最近会话
|
|
|
- async function syncConversation () {
|
|
|
- const res = await WKSDK.shared().conversationManager.sync()
|
|
|
- conversationList.value = res
|
|
|
- return res
|
|
|
- // return new Promise((resolve, reject) => {
|
|
|
- // WKSDK.shared().conversationManager.sync().then(res => {
|
|
|
- // conversationList.value = res
|
|
|
- // resolve(res)
|
|
|
- // }).catch(error => {
|
|
|
- // reject(error)
|
|
|
- // })
|
|
|
- // })
|
|
|
- }
|
|
|
-
|
|
|
return {
|
|
|
// connected,
|
|
|
conversationList,
|
|
@@ -320,6 +309,12 @@ export function initConnect (callback = () => {}) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+// 同步最近会话
|
|
|
+async function syncConversation () {
|
|
|
+ const res = await WKSDK.shared().conversationManager.sync()
|
|
|
+ return res
|
|
|
+}
|
|
|
+
|
|
|
// export async function getRecentMessages (channel) {
|
|
|
// const { resultMessages, more } = await WKSDK.shared().chatManager.syncMessages(channel, HISTORY_QUERY)
|
|
|
// // console.log(res)
|