|
@@ -2,7 +2,14 @@
|
|
|
|
|
|
|
|
|
import { ref, onMounted, onUnmounted } from 'vue';
|
|
|
-import { getConversationSync } from '@/api/common'
|
|
|
+import { getConversationSync, getMessageSync, getChatKey } from '@/api/common'
|
|
|
+import { Base64 } from 'js-base64'
|
|
|
+
|
|
|
+import { useUserStore } from '@/store/user'
|
|
|
+import { useLoginType } from '@/store/loginType'
|
|
|
+
|
|
|
+const userStore = useUserStore()
|
|
|
+const loginType = useLoginType()
|
|
|
|
|
|
// 配置悟空IM
|
|
|
import {
|
|
@@ -12,11 +19,13 @@ import {
|
|
|
ChannelTypePerson,
|
|
|
// ChannelTypeGroup
|
|
|
} from "wukongimjssdk"
|
|
|
-import Snackbar from '@/plugins/snackbar'
|
|
|
-
|
|
|
-import { useUserStore } from '@/store/user'
|
|
|
|
|
|
-const userStore = useUserStore()
|
|
|
+const HISTORY_QUERY = {
|
|
|
+ limit: 15,
|
|
|
+ startMessageSeq: 0,
|
|
|
+ endMessageSeq: 0,
|
|
|
+ pullMode: 1
|
|
|
+}
|
|
|
|
|
|
const ConnectStatus = {
|
|
|
Disconnect: 0, // 断开连接
|
|
@@ -26,115 +35,181 @@ const ConnectStatus = {
|
|
|
ConnectKick: 4, // 连接被踢,服务器要求客户端断开(一般是账号在其他地方登录,被踢)
|
|
|
}
|
|
|
|
|
|
-
|
|
|
initDataSource()
|
|
|
// api 接入
|
|
|
function initDataSource () {
|
|
|
// 最近会话数据源
|
|
|
WKSDK.shared().config.provider.syncConversationsCallback = async () => {
|
|
|
- const res = await getConversationSync({ msg_count: 100 })
|
|
|
+ const query = {
|
|
|
+ msg_count: 100
|
|
|
+ }
|
|
|
+ if (loginType.loginType === 'enterprise') {
|
|
|
+ Object.assign(query, { enterpriseId: userStore.baseInfo.enterpriseId })
|
|
|
+ }
|
|
|
+ const res = await getConversationSync(query)
|
|
|
return res
|
|
|
}
|
|
|
+ // 同步频道消息数据源
|
|
|
+ WKSDK.shared().config.provider.syncMessagesCallback = async function(channel, opts) {
|
|
|
+ // 后端提供的获取频道消息列表的接口数据 然后构建成 Message对象数组返回
|
|
|
+ let resultMessages = new Array()
|
|
|
+ const query = {
|
|
|
+ channel_id: channel.channelID,
|
|
|
+ channel_type: channel.channelType,
|
|
|
+ start_message_seq: opts.startMessageSeq,
|
|
|
+ end_message_seq: opts.endMessageSeq,
|
|
|
+ limit: opts.limit,
|
|
|
+ pull_mode: opts.pullMode,
|
|
|
+ }
|
|
|
+ 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);
|
|
|
+ resultMessages.push(msg);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ return resultMessages
|
|
|
+ }
|
|
|
}
|
|
|
-export function initKey () {
|
|
|
- return new Promise((resolve) => {
|
|
|
- userStore.getChatKey().then(() => {
|
|
|
- resolve()
|
|
|
- }).catch((error) => {
|
|
|
- // 报错重连
|
|
|
- console.log(error.msg)
|
|
|
- setTimeout(() => {
|
|
|
- initKey()
|
|
|
- }, 3000)
|
|
|
- })
|
|
|
- })
|
|
|
+
|
|
|
+export const initKey = async () => {
|
|
|
+ // 通过自身userId和企业id获取token和uid
|
|
|
+ const keyQuery = {
|
|
|
+ userId: userStore.userInfo.id
|
|
|
+ }
|
|
|
+ if (loginType.loginType === 'enterprise') {
|
|
|
+ Object.assign(keyQuery, { enterpriseId: userStore.baseInfo.enterpriseId })
|
|
|
+ }
|
|
|
+ const { uid, wsUrl, token } = await getChatKey(keyQuery)
|
|
|
+ // 单机模式可以直接设置地址
|
|
|
+ WKSDK.shared().config.addr = 'ws://' + wsUrl // 默认端口为5200
|
|
|
+ // 认证信息
|
|
|
+ WKSDK.shared().config.uid = uid // 用户uid(需要在悟空通讯端注册过)
|
|
|
+ WKSDK.shared().config.token = token // 用户token (需要在悟空通讯端注册过)
|
|
|
+ return {
|
|
|
+ uid
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-export function initConnect () {
|
|
|
+export function initConnect (userId, enterpriseId, callback) {
|
|
|
+
|
|
|
const connected = ref(false)
|
|
|
const conversationList = ref([])
|
|
|
- // 单机模式可以直接设置地址
|
|
|
- WKSDK.shared().config.addr = 'ws://' + userStore.IMConfig.wsUrl // 默认端口为5200
|
|
|
- // 认证信息
|
|
|
- WKSDK.shared().config.uid = userStore.IMConfig.uid // 用户uid(需要在悟空通讯端注册过)
|
|
|
- WKSDK.shared().config.token = userStore.IMConfig.token // 用户token (需要在悟空通讯端注册过)
|
|
|
+ const messageItems = ref([])
|
|
|
+ const toUid = ref()
|
|
|
+ const channel = ref()
|
|
|
// 连接
|
|
|
+ console.log(WKSDK.shared().config)
|
|
|
onMounted(() => {
|
|
|
// 连接状态监听
|
|
|
- WKSDK.shared().connectManager.addConnectStatusListener(
|
|
|
- async (status, reasonCode) => {
|
|
|
- if (status === ConnectStatus.Connected) {
|
|
|
- console.log('连接成功')
|
|
|
- connected.value = true
|
|
|
- // 获取列表
|
|
|
- const res = await syncConversation()
|
|
|
- conversationList.value = [...res]
|
|
|
- } else {
|
|
|
- console.log('reasonCode', reasonCode)
|
|
|
- connected.value = false
|
|
|
- conversationList.value = []
|
|
|
- }
|
|
|
- }
|
|
|
- )
|
|
|
-
|
|
|
+ WKSDK.shared().connectManager.addConnectStatusListener(connectStatusListener)
|
|
|
// 消息发送状态监听
|
|
|
WKSDK.shared().chatManager.addMessageStatusListener(statusListen)
|
|
|
// 常规消息监听
|
|
|
WKSDK.shared().chatManager.addMessageListener(messageListen)
|
|
|
-
|
|
|
- connect()
|
|
|
+ // 连接
|
|
|
+ WKSDK.shared().connectManager.connect()
|
|
|
})
|
|
|
onUnmounted(() => {
|
|
|
- // 连接状态监听移除
|
|
|
- WKSDK.shared().connectManager.disconnect()
|
|
|
+ WKSDK.shared().connectManager.removeConnectStatusListener(connectStatusListener)
|
|
|
// 消息发送状态监听移除
|
|
|
WKSDK.shared().chatManager.removeMessageStatusListener(statusListen)
|
|
|
// 常规消息监听移除
|
|
|
WKSDK.shared().chatManager.removeMessageListener(messageListen)
|
|
|
+ // 连接状态监听移除
|
|
|
+ WKSDK.shared().connectManager.disconnect()
|
|
|
})
|
|
|
- return { connected, conversationList }
|
|
|
-}
|
|
|
|
|
|
-// 消息发送状态监听
|
|
|
-function statusListen (packet) {
|
|
|
- if (packet.reasonCode === 1) {
|
|
|
- // 发送成功
|
|
|
- } else {
|
|
|
- // 发送失败
|
|
|
+ const connectStatusListener = async (status) => {
|
|
|
+ console.log('连接状态', status)
|
|
|
+ if (status === ConnectStatus.Connected) {
|
|
|
+ console.log('连接成功')
|
|
|
+ connected.value = true
|
|
|
+ // 连接成功 创建channel
|
|
|
+ // const { message } = await syncConversation()
|
|
|
+ // conversationList.value = message
|
|
|
+ if (!userId) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ await initChart(userId, enterpriseId)
|
|
|
+
|
|
|
+ callback()
|
|
|
+ } else {
|
|
|
+ connected.value = false
|
|
|
+ conversationList.value = []
|
|
|
+ toUid.value = null
|
|
|
+ }
|
|
|
}
|
|
|
-}
|
|
|
|
|
|
-// 常规消息监听
|
|
|
-function messageListen (message) {
|
|
|
- console.log('收到消息', message)
|
|
|
- // message.content // 消息内容
|
|
|
- // message.channel // 消息频道
|
|
|
- // message.fromUID // 消息发送者
|
|
|
-}
|
|
|
+ // 消息发送状态监听
|
|
|
+ function statusListen (packet) {
|
|
|
+ if (packet.reasonCode === 1) {
|
|
|
+ // 发送成功
|
|
|
+ console.log('发送成功')
|
|
|
+ } else {
|
|
|
+ // 发送失败
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
+ // 常规消息监听
|
|
|
+ async function messageListen (message) {
|
|
|
+ console.log('收到消息', message)
|
|
|
+ const { channel: _channel } = message
|
|
|
+ const conversation = WKSDK.shared().conversationManager.findConversation(_channel)
|
|
|
+ if(!conversation) {
|
|
|
+ // 如果最近会话不存在,则创建一个空的会话
|
|
|
+ WKSDK.shared().conversationManager.createEmptyConversation(_channel)
|
|
|
+ }
|
|
|
+ channel.value = _channel
|
|
|
+ getMessage(_channel)
|
|
|
+ // 创建通道
|
|
|
+ }
|
|
|
|
|
|
-export function connect () {
|
|
|
- WKSDK.shared().connectManager.connect()
|
|
|
-}
|
|
|
+ async function getMessage (channel) {
|
|
|
+ const res = await WKSDK.shared().chatManager.syncMessages(channel, HISTORY_QUERY)
|
|
|
+ // console.log(res)
|
|
|
+ messageItems.value = res.map(_e => {
|
|
|
+ _e.payload = JSON.parse(Base64.decode(_e.payload))
|
|
|
+ return _e
|
|
|
+ })
|
|
|
+ }
|
|
|
|
|
|
-export function send (user, text) {
|
|
|
- // 例如发送文本消息hello给用户u10001
|
|
|
- const _text = new MessageText(text) // 文本消息
|
|
|
- WKSDK.shared().chatManager.send(_text, new Channel(user, ChannelTypePerson))
|
|
|
+ // 发起聊天
|
|
|
+ async function initChart (userId, enterpriseId) {
|
|
|
+ const { uid } = await getChatKey({userId, enterpriseId})
|
|
|
+ const _channel = new Channel(uid, ChannelTypePerson)
|
|
|
+ channel.value = _channel
|
|
|
+ }
|
|
|
|
|
|
- // 例如发送文本消息hello给群频道g10001
|
|
|
- // WKSDK.shared().chatManager.send(_text, new Channel(user,ChannelTypeGroup))
|
|
|
+ // 同步最近会话
|
|
|
+ // async function syncConversation () {
|
|
|
+ // return new Promise((resolve, reject) => {
|
|
|
+ // WKSDK.shared().conversationManager.sync().then(res => {
|
|
|
+ // resolve(res)
|
|
|
+ // }).catch(error => {
|
|
|
+ // reject(error)
|
|
|
+ // })
|
|
|
+ // })
|
|
|
+ // }
|
|
|
+
|
|
|
+ return {
|
|
|
+ connected,
|
|
|
+ conversationList,
|
|
|
+ messageItems,
|
|
|
+ channel
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-// 同步最近会话
|
|
|
-export async function syncConversation () {
|
|
|
- return new Promise((resolve) => {
|
|
|
- WKSDK.shared().conversationManager.sync().then(res => {
|
|
|
- resolve(res)
|
|
|
- }).catch(error => {
|
|
|
- Snackbar.error(error.msg)
|
|
|
- })
|
|
|
- })
|
|
|
+
|
|
|
+export function send (text, _channel) {
|
|
|
+ const _text = new MessageText(text) // 文本消息
|
|
|
+ console.log('发送', _text)
|
|
|
+ WKSDK.shared().chatManager.send(_text, _channel)
|
|
|
}
|
|
|
|
|
|
|
|
|
+
|