|
@@ -2,7 +2,7 @@
|
|
|
|
|
|
|
|
|
|
import { ref, onMounted, onUnmounted, watch } from 'vue';
|
|
import { ref, onMounted, onUnmounted, watch } from 'vue';
|
|
-import { getConversationSync, getMessageSync, getChatKey } from '@/api/common'
|
|
|
|
|
|
+import { getConversationSync, getMessageSync, getChatKey, setUnread, deleteConversation } from '@/api/common'
|
|
import { Base64 } from 'js-base64'
|
|
import { Base64 } from 'js-base64'
|
|
|
|
|
|
import { useUserStore } from '@/store/user'
|
|
import { useUserStore } from '@/store/user'
|
|
@@ -10,7 +10,6 @@ import { useLoginType } from '@/store/loginType'
|
|
import { useIMStore } from '@/store/im'
|
|
import { useIMStore } from '@/store/im'
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
// 配置悟空IM
|
|
// 配置悟空IM
|
|
import {
|
|
import {
|
|
MessageText,
|
|
MessageText,
|
|
@@ -18,9 +17,41 @@ import {
|
|
WKSDK,
|
|
WKSDK,
|
|
ChannelTypePerson,
|
|
ChannelTypePerson,
|
|
// Conversation,
|
|
// Conversation,
|
|
|
|
+ MessageContent,
|
|
// Message, StreamItem, ChannelTypeGroup, MessageStatus, SyncOptions, MessageExtra, MessageContent
|
|
// Message, StreamItem, ChannelTypeGroup, MessageStatus, SyncOptions, MessageExtra, MessageContent
|
|
} from "wukongimjssdk"
|
|
} from "wukongimjssdk"
|
|
|
|
|
|
|
|
+// 注册消息体
|
|
|
|
+class ObjectContent extends MessageContent {
|
|
|
|
+ constructor(text) {
|
|
|
|
+ super();
|
|
|
|
+ this.content = text;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ get conversationDigest() {
|
|
|
|
+ // 这里需要实现具体的逻辑
|
|
|
|
+ return this.content
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ get contentType() {
|
|
|
|
+ // 这里需要实现具体的逻辑
|
|
|
|
+ return 101; // 示例实现
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ decodeJSON(content) {
|
|
|
|
+ this.content = content.text;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ encodeJSON() {
|
|
|
|
+ return {
|
|
|
|
+ content: this.content
|
|
|
|
+ };
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+// 注册101类型为面试
|
|
|
|
+WKSDK.shared().register(101, () => new ObjectContent())
|
|
|
|
+
|
|
|
|
+
|
|
const HISTORY_QUERY = {
|
|
const HISTORY_QUERY = {
|
|
limit: 30,
|
|
limit: 30,
|
|
startMessageSeq: 0,
|
|
startMessageSeq: 0,
|
|
@@ -88,9 +119,13 @@ export function useDataSource () {
|
|
// const message = Convert.toMessage(msg);
|
|
// const message = Convert.toMessage(msg);
|
|
// msg.channel = new Channel(msg.channel_id, msg.channel_type)
|
|
// msg.channel = new Channel(msg.channel_id, msg.channel_type)
|
|
msg.payload = JSON.parse(Base64.decode(msg.payload))
|
|
msg.payload = JSON.parse(Base64.decode(msg.payload))
|
|
|
|
+ if (msg.payload.type === 101) {
|
|
|
|
+ msg.payload.content = JSON.parse(msg.payload.content ?? '{}')
|
|
|
|
+ }
|
|
resultMessages.push(msg);
|
|
resultMessages.push(msg);
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
+ console.log(resultMessages)
|
|
const more = resp.more === 1
|
|
const more = resp.more === 1
|
|
return {
|
|
return {
|
|
more,
|
|
more,
|
|
@@ -99,81 +134,6 @@ export function useDataSource () {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-// function toConversation (conversationMap) {
|
|
|
|
-// const conversation = new Conversation()
|
|
|
|
-// conversation.channel = new Channel(conversationMap['channel_id'], conversationMap['channel_type'])
|
|
|
|
-// conversation.unread = conversationMap['unread'] || 0;
|
|
|
|
-// conversation.timestamp = conversationMap['timestamp'] || 0;
|
|
|
|
-// // let recents = conversationMap["recents"];
|
|
|
|
-// // if (recents && recents.length > 0) {
|
|
|
|
-// // const messageModel = toMessage(recents[0]);
|
|
|
|
-// // conversation.lastMessage = messageModel
|
|
|
|
-// // }
|
|
|
|
-// conversation.extra = {}
|
|
|
|
-
|
|
|
|
-// return conversation
|
|
|
|
-// }
|
|
|
|
-// function toMessage(msgMap) {
|
|
|
|
-// const message = new Message();
|
|
|
|
-// if (msgMap['message_idstr']) {
|
|
|
|
-// message.messageID = msgMap['message_idstr'];
|
|
|
|
-// }
|
|
|
|
-// // else {
|
|
|
|
-// // message.messageID = new BigNumber(msgMap['message_id']).toString();
|
|
|
|
-// // }
|
|
|
|
-// if (msgMap["header"]) {
|
|
|
|
-// message.header.reddot = msgMap["header"]["red_dot"] === 1 ? true : false
|
|
|
|
-// }
|
|
|
|
-// // if (msgMap["setting"]) {
|
|
|
|
-// // message.setting = Setting.fromUint8(msgMap["setting"])
|
|
|
|
-// // }
|
|
|
|
-// if (msgMap["revoke"]) {
|
|
|
|
-// message.remoteExtra.revoke = msgMap["revoke"] === 1 ? true : false
|
|
|
|
-// }
|
|
|
|
-// if(msgMap["message_extra"]) {
|
|
|
|
-// const messageExtra = msgMap["message_extra"]
|
|
|
|
-// message.remoteExtra = this.toMessageExtra(messageExtra)
|
|
|
|
-// }
|
|
|
|
-
|
|
|
|
-// message.clientSeq = msgMap["client_seq"]
|
|
|
|
-// message.channel = new Channel(msgMap['channel_id'], msgMap['channel_type']);
|
|
|
|
-// message.messageSeq = msgMap["message_seq"]
|
|
|
|
-// message.clientMsgNo = msgMap["client_msg_no"]
|
|
|
|
-// message.streamNo = msgMap["stream_no"]
|
|
|
|
-// message.streamFlag = msgMap["stream_flag"]
|
|
|
|
-// message.fromUID = msgMap["from_uid"]
|
|
|
|
-// message.timestamp = msgMap["timestamp"]
|
|
|
|
-// message.status = MessageStatus.Normal
|
|
|
|
-
|
|
|
|
-// const contentObj = JSON.parse(Base64.decode(msgMap["payload"]))
|
|
|
|
-// // const contentObj = JSON.parse(decodedBuffer.toString('utf8'))
|
|
|
|
-// let contentType = 0
|
|
|
|
-// if (contentObj) {
|
|
|
|
-// contentType = contentObj.type
|
|
|
|
-// }
|
|
|
|
-// const messageContent = WKSDK.shared().getMessageContent(contentType)
|
|
|
|
-// if (contentObj) {
|
|
|
|
-// messageContent.decode(this.stringToUint8Array(JSON.stringify(contentObj)))
|
|
|
|
-// }
|
|
|
|
-// message.content = messageContent
|
|
|
|
-
|
|
|
|
-// message.isDeleted = msgMap["is_deleted"] === 1
|
|
|
|
-
|
|
|
|
-// const streamMaps = msgMap["streams"]
|
|
|
|
-// if(streamMaps && streamMaps.length>0) {
|
|
|
|
-// const streams = []
|
|
|
|
-// for (const streamMap of streamMaps) {
|
|
|
|
-// const streamItem = new StreamItem()
|
|
|
|
-// streamItem.clientMsgNo = streamMap["client_msg_no"]
|
|
|
|
-// streamItem.streamSeq = streamMap["stream_seq"]
|
|
|
|
-// streams.push(streamItem)
|
|
|
|
-// }
|
|
|
|
-// message.streams = streams
|
|
|
|
-// }
|
|
|
|
-
|
|
|
|
-// return message
|
|
|
|
-// }
|
|
|
|
-
|
|
|
|
async function getKey () {
|
|
async function getKey () {
|
|
const userStore = useUserStore()
|
|
const userStore = useUserStore()
|
|
const loginType = useLoginType()
|
|
const loginType = useLoginType()
|
|
@@ -193,8 +153,6 @@ export const useIM = () => {
|
|
useDataSource()
|
|
useDataSource()
|
|
const key = ref(0)
|
|
const key = ref(0)
|
|
const IM = useIMStore()
|
|
const IM = useIMStore()
|
|
- const unreadCount = ref(0)
|
|
|
|
- const connected = ref(0)
|
|
|
|
|
|
|
|
onMounted( async () => {
|
|
onMounted( async () => {
|
|
|
|
|
|
@@ -230,14 +188,12 @@ export const useIM = () => {
|
|
async function connectStatusListener (status) {
|
|
async function connectStatusListener (status) {
|
|
// console.log('连接状态', status === ConnectStatus.Connected)
|
|
// console.log('连接状态', status === ConnectStatus.Connected)
|
|
// 连接成功 获取点击数
|
|
// 连接成功 获取点击数
|
|
- connected.value = status === ConnectStatus.Connected
|
|
|
|
- if (connected.value) {
|
|
|
|
|
|
+ const connected = status === ConnectStatus.Connected
|
|
|
|
+ IM.setConnected(connected)
|
|
|
|
+ if (connected) {
|
|
|
|
+ // 必须同步最近会话才能获取未读总数
|
|
await syncConversation()
|
|
await syncConversation()
|
|
setUnreadCount()
|
|
setUnreadCount()
|
|
- // setTimeout(() => {
|
|
|
|
- // setUnreadCount()
|
|
|
|
- // console.log('连接成功, 获取总数')
|
|
|
|
- // }, 0)
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -245,14 +201,9 @@ export const useIM = () => {
|
|
const count = WKSDK.shared().conversationManager.getAllUnreadCount()
|
|
const count = WKSDK.shared().conversationManager.getAllUnreadCount()
|
|
key.value++
|
|
key.value++
|
|
IM.setNewMsg(key.value)
|
|
IM.setNewMsg(key.value)
|
|
- unreadCount.value = count
|
|
|
|
|
|
+ IM.setUnreadCount(count)
|
|
console.log('未读消息总数', count)
|
|
console.log('未读消息总数', count)
|
|
}
|
|
}
|
|
-
|
|
|
|
- return {
|
|
|
|
- unreadCount,
|
|
|
|
- connected
|
|
|
|
- }
|
|
|
|
}
|
|
}
|
|
|
|
|
|
export function initConnect (callback = () => {}) {
|
|
export function initConnect (callback = () => {}) {
|
|
@@ -265,8 +216,7 @@ export function initConnect (callback = () => {}) {
|
|
() => IM.newMsg,
|
|
() => IM.newMsg,
|
|
async () => {
|
|
async () => {
|
|
// 未读消息变化
|
|
// 未读消息变化
|
|
- const res = await syncConversation()
|
|
|
|
- conversationList.value = res
|
|
|
|
|
|
+ updateConversation()
|
|
// 拉取最新消息 查看是否是自己的数据
|
|
// 拉取最新消息 查看是否是自己的数据
|
|
},
|
|
},
|
|
{
|
|
{
|
|
@@ -301,8 +251,42 @@ export function initConnect (callback = () => {}) {
|
|
callback(false)
|
|
callback(false)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ async function updateConversation () {
|
|
|
|
+ const res = await syncConversation()
|
|
|
|
+ conversationList.value = res
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ function updateUnreadCount () {
|
|
|
|
+ const count = WKSDK.shared().conversationManager.getAllUnreadCount()
|
|
|
|
+ IM.setUnreadCount(count)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ async function deleteConversations (channel, enterpriseId) {
|
|
|
|
+ const query = {
|
|
|
|
+ channel_id: channel.channelID,
|
|
|
|
+ channel_type: channel.channelType,
|
|
|
|
+ enterpriseId
|
|
|
|
+ }
|
|
|
|
+ await deleteConversation(query)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ async function resetUnread (channel, enterpriseId) {
|
|
|
|
+ const query = {
|
|
|
|
+ channel_id: channel.channelID,
|
|
|
|
+ channel_type: channel.channelType,
|
|
|
|
+ enterpriseId,
|
|
|
|
+ unread: 0
|
|
|
|
+ }
|
|
|
|
+ const res = await setUnread(query)
|
|
|
|
+ return res
|
|
|
|
+ }
|
|
|
|
+
|
|
return {
|
|
return {
|
|
- // connected,
|
|
|
|
|
|
+ resetUnread,
|
|
|
|
+ deleteConversations,
|
|
|
|
+ updateConversation,
|
|
|
|
+ updateUnreadCount,
|
|
conversationList,
|
|
conversationList,
|
|
messageItems,
|
|
messageItems,
|
|
// channel
|
|
// channel
|
|
@@ -315,15 +299,6 @@ async function syncConversation () {
|
|
return res
|
|
return res
|
|
}
|
|
}
|
|
|
|
|
|
-// export async function getRecentMessages (channel) {
|
|
|
|
-// const { resultMessages, more } = await WKSDK.shared().chatManager.syncMessages(channel, HISTORY_QUERY)
|
|
|
|
-// // console.log(res)
|
|
|
|
-// // return resultMessages.map(_e => {
|
|
|
|
-// // _e.payload = JSON.parse(Base64.decode(_e.payload))
|
|
|
|
-// // return _e
|
|
|
|
-// // })
|
|
|
|
-// }
|
|
|
|
-
|
|
|
|
// 发起聊天
|
|
// 发起聊天
|
|
export async function initChart (userId, enterpriseId) {
|
|
export async function initChart (userId, enterpriseId) {
|
|
const channel = ref()
|
|
const channel = ref()
|
|
@@ -362,12 +337,47 @@ export async function getMoreMessages (pageSize, channel) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-export function send (text, _channel) {
|
|
|
|
-
|
|
|
|
- const _text = new MessageText(text) // 文本消息
|
|
|
|
- console.log('发送', _text, _channel)
|
|
|
|
|
|
+/**
|
|
|
|
+ *
|
|
|
|
+ * @param {*} text
|
|
|
|
+ * @param {*} _channel
|
|
|
|
+ * @param { Number } type : 101 面试主体
|
|
|
|
+ * @returns
|
|
|
|
+ */
|
|
|
|
+export function send (text, _channel, type) {
|
|
|
|
+ let _text
|
|
|
|
+ if (type === 101) {
|
|
|
|
+ _text = new ObjectContent(text)
|
|
|
|
+ WKSDK.shared().chatManager.send(_text, _channel)
|
|
|
|
+ console.log(_text)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ _text = new MessageText(text)
|
|
|
|
+ console.log(_text)
|
|
WKSDK.shared().chatManager.send(_text, _channel)
|
|
WKSDK.shared().chatManager.send(_text, _channel)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// 对话开场白
|
|
|
|
+export async function prologue ({userId, enterpriseId, text}) {
|
|
|
|
+ const { channel } = await checkConversation(userId, enterpriseId)
|
|
|
|
+ send(text, channel)
|
|
|
|
+}
|
|
|
|
|
|
-
|
|
|
|
|
|
+// 检测是否存在频道
|
|
|
|
+export async function checkConversation (userId, enterpriseId) {
|
|
|
|
+ const query = {
|
|
|
|
+ userId,
|
|
|
|
+ enterpriseId
|
|
|
|
+ }
|
|
|
|
+ // 创建聊天频道
|
|
|
|
+ const { uid } = await getChatKey(query)
|
|
|
|
+ const _channel = new Channel(uid, ChannelTypePerson)
|
|
|
|
+ const conversation = WKSDK.shared().conversationManager.findConversation(_channel)
|
|
|
|
+ if(!conversation) {
|
|
|
|
+ // 如果最近会话不存在,则创建一个空的会话
|
|
|
|
+ WKSDK.shared().conversationManager.createEmptyConversation(_channel)
|
|
|
|
+ }
|
|
|
|
+ return {
|
|
|
|
+ channel: _channel
|
|
|
|
+ }
|
|
|
|
+}
|