| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432 | 
							
- import { ref, onMounted, onUnmounted, watch } from 'vue';
 
- import { getConversationSync, getMessageSync, getChatKey, setUnread, deleteConversation } from '@/api/common'
 
- import { Base64 } from 'js-base64'
 
- import { userStore } from '@/store/user'
 
- import { useIMStore } from '@/store/im'
 
- // 配置悟空IM
 
- import {
 
-   MessageText,
 
-   Channel,
 
-   WKSDK,
 
-   ChannelTypePerson,
 
-   MessageContent,
 
- } from "wukongimjssdk"
 
- // 默认招呼语
 
- export const defaultText = '您好,关注到您发布该职位信息,请问有机会与您进一步沟通吗?'
 
- // 企业默认招呼语
 
- // export const defaultTextEnt = '您好,我们正在寻找充满激情、勇于挑战的您,快来和我聊一聊吧~'
 
- const { ObjectContent } = initRegister(101)
 
- const { ObjectContent: ObjectContent2 } = initRegister(102)
 
- const { ObjectContent: ObjectContent3 } = initRegister(103)
 
- const { ObjectContent: ObjectContent4 } = initRegister(104)
 
- const { ObjectContent: ObjectContent5 } = initRegister(105) // 发送简历
 
- const contentType = {
 
-   101: ObjectContent,
 
-   102: ObjectContent2,
 
-   103: ObjectContent3,
 
-   104: ObjectContent4,
 
-   105: ObjectContent5, // 发送简历
 
- }
 
- // 注册消息体
 
- function initRegister (type) {
 
-   class ObjectContent extends MessageContent {
 
-     constructor(text) {
 
-       super();
 
-       this.content = text
 
-     }
 
-     get conversationDigest() {
 
-         // 这里需要实现具体的逻辑
 
-         return this.content
 
-     }
 
-     get contentType() {
 
-         // 这里需要实现具体的逻辑
 
-         return type; // 示例实现
 
-     }
 
-     decodeJSON(content) {
 
-         this.content = content.text;
 
-     }
 
-     encodeJSON() {
 
-         return {
 
-           content: this.content
 
-         };
 
-     }
 
-   }
 
-   // 注册101类型为面试
 
-   WKSDK.shared().register(type, () => new ObjectContent(''))
 
-   return {
 
-     ObjectContent
 
-   }
 
- }
 
- const HISTORY_QUERY = {
 
-   limit: 20,
 
-   startMessageSeq: 0,
 
-   endMessageSeq: 0,
 
-   pullMode: 1
 
- }
 
- const ConnectStatus = {
 
-   Disconnect: 0, // 断开连接
 
-   Connected: 1, // 连接成功
 
-   Connecting: 2, // 连接中
 
-   ConnectFail: 3, // 连接错误
 
-   ConnectKick: 4, // 连接被踢,服务器要求客户端断开(一般是账号在其他地方登录,被踢)
 
- }
 
- // api 接入
 
- export function useDataSource () {
 
-   // 最近会话数据源
 
-   WKSDK.shared().config.provider.syncConversationsCallback  = async () => {
 
-     const query = {
 
-       msg_count: 1
 
-     }
 
-     const resultConversations = []
 
-     const resp = await getConversationSync(query)
 
-     const { data: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.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,
 
-     }
 
-     const { data } = await getMessageSync(query)
 
-     const resp = data
 
-     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))
 
-         if (contentType[msg.payload.type]) {
 
-           msg.payload.content = JSON.parse(msg.payload.content ?? '{}')
 
-         }
 
-         resultMessages.push(msg)
 
-       })
 
-     }
 
-     // console.log(resultMessages)
 
-     const more = resp.more === 1
 
-     return {
 
-       more,
 
-       resultMessages
 
-     }
 
-   }
 
- }
 
- export function toChannel (channelID, channelType) {
 
-   return new Channel(channelID, channelType)
 
- }
 
- async function getKey () {
 
-   const useUserStore = userStore()
 
-   if (!useUserStore.accountInfo?.userId) {
 
-     return {}
 
-   }
 
-   const keyQuery = {
 
-     userId: useUserStore.accountInfo?.userId
 
-   }
 
-   const { data } = await getChatKey(keyQuery)
 
-   return {
 
-     ...data
 
-   }
 
- }
 
- export const useIM = () => {
 
-   useDataSource()
 
-   const key = ref(0)
 
-   const IM = useIMStore()
 
-   
 
-   onMounted( async () => {
 
-     await resetConfig()
 
-     // 连接状态监听
 
-     WKSDK.shared().connectManager.addConnectStatusListener(connectStatusListener)
 
-     // 常规消息监听
 
-     WKSDK.shared().chatManager.addMessageListener(messageListen)
 
-     // 连接
 
-     WKSDK.shared().connectManager.connect()
 
-   })
 
-   onUnmounted(() => {
 
-     WKSDK.shared().connectManager.removeConnectStatusListener(connectStatusListener)
 
-     // 常规消息监听移除
 
-     WKSDK.shared().chatManager.removeMessageListener(messageListen)
 
-     // 连接状态监听移除
 
-     WKSDK.shared().connectManager.disconnect()
 
-   })
 
-   
 
-   async function messageListen (message) {
 
-     // console.log('收到消息', message)
 
-     IM.setFromChannel(message.channel.channelID)
 
-     setUnreadCount()
 
-   }
 
-   async function connectStatusListener (status) {
 
-     // console.log('连接状态', status === ConnectStatus.Connected)
 
-     // 连接成功 获取点击数
 
-     const connected = status === ConnectStatus.Connected
 
-     IM.setConnected(connected)
 
-     if (connected) {
 
-       // 必须同步最近会话才能获取未读总数
 
-       await syncConversation()
 
-       setUnreadCount()
 
-     }
 
-   }
 
-   function setUnreadCount () {
 
-     const count = WKSDK.shared().conversationManager.getAllUnreadCount()
 
-     key.value++
 
-     IM.setNewMsg(key.value)
 
-     IM.setUnreadCount(count)
 
-     console.log('未读消息总数', count)
 
-   }
 
-   async function resetConfig () {
 
-     try {
 
-       const { uid, wssUrl, token } = await getKey()
 
-       IM.setUid(uid)
 
-       // 单机模式可以直接设置地址
 
-       WKSDK.shared().config.addr = 'wss://' + wssUrl// 默认端口为5200 + wsUrl 
 
-       // 认证信息
 
-       WKSDK.shared().config.uid = uid // 用户uid(需要在悟空通讯端注册过)
 
-       WKSDK.shared().config.token = token // 用户token (需要在悟空通讯端注册过)
 
-     } catch (error) {
 
-       console.log(error)
 
-     }
 
-   }
 
-   return {
 
-     resetConfig
 
-   }
 
- }
 
- export function initConnect (callback = () => {}, mounted = () => {}) {
 
-   useDataSource()
 
-   const IM = useIMStore()
 
-   const conversationList = ref([])
 
-   const messageItems = ref([])
 
-   watch(
 
-     () => IM.newMsg,
 
-     async () => {
 
-       // 未读消息变化
 
-       updateConversation()
 
-       // 拉取最新消息 查看是否是自己的数据
 
-     },
 
-     {
 
-       deep: true,
 
-       immediate: true
 
-     }
 
-   )
 
-   onMounted(async () => {
 
-     // 消息发送状态监听
 
-     WKSDK.shared().chatManager.addMessageStatusListener(statusListen)
 
-     // 常规消息监听
 
-     // WKSDK.shared().chatManager.addMessageListener(messageListen)
 
-     mounted()
 
-   })
 
-   onUnmounted(() => {
 
-     // 消息发送状态监听移除
 
-     WKSDK.shared().chatManager.removeMessageStatusListener(statusListen)
 
-     // 常规消息监听移除
 
-     // WKSDK.shared().chatManager.removeMessageListener(messageListen)
 
-   })
 
-   // 消息发送状态监听
 
-   function statusListen (packet) {
 
-     console.log('发送状态', packet)
 
-     if (packet.reasonCode === 1) {
 
-       // 发送成功
 
-       console.log('发送成功')
 
-       // 添加一组成功数据
 
-       callback(true)
 
-     } else {
 
-       // 发送失败
 
-       console.log('发送失败')
 
-       // 添加一组失败数据
 
-       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 {
 
-     resetUnread,
 
-     deleteConversations,
 
-     updateConversation,
 
-     updateUnreadCount,
 
-     conversationList,
 
-     messageItems,
 
-     // channel
 
-   }
 
- }
 
- // 同步最近会话
 
- async function syncConversation () {
 
-   const res = await WKSDK.shared().conversationManager.sync()
 
-   return res
 
- }
 
- // 发起聊天
 
- export async function initChart (userId, enterpriseId) {
 
-   try {
 
-     const channel = ref()
 
-     // const list = ref([])
 
-     const query = {
 
-       userId,
 
-       enterpriseId
 
-     }
 
-     // 创建聊天频道
 
-     const { data } = await getChatKey(query)
 
-     // console.log(data, 'data')
 
-     const { uid } = data
 
-     const _channel = new Channel(uid, ChannelTypePerson)
 
-     channel.value = _channel
 
-     const conversation = WKSDK.shared().conversationManager.findConversation(_channel)
 
-     if(!conversation) {
 
-       // 如果最近会话不存在,则创建一个空的会话
 
-       WKSDK.shared().conversationManager.createEmptyConversation(_channel)
 
-     }
 
-     const res = await getMoreMessages(1, _channel)
 
-     return {
 
-       channel,
 
-       ...res
 
-     }
 
-   } catch (error) {
 
-     console.log(error)
 
-   }
 
- }
 
- // 翻页
 
- export async function getMoreMessages (pageSize, channel) {
 
-   const list = ref([])
 
-   Object.assign(HISTORY_QUERY, {
 
-     startMessageSeq: (pageSize - 1) * HISTORY_QUERY.limit
 
-   })
 
-   const { resultMessages, more } = await WKSDK.shared().chatManager.syncMessages(channel)
 
-   list.value = resultMessages
 
-   return {
 
-     list,
 
-     more
 
-   }
 
- }
 
- /**
 
-  * 
 
-  * @param {*} text 
 
-  * @param {*} _channel 
 
-  * @param { Number } type : 101 面试主体 
 
-  * @returns 
 
-  */
 
-   // 发送职位使用101
 
- export function send (text, _channel, type) {
 
-   let _text
 
-   if (contentType[type]) {
 
-     _text = new contentType[type](text)
 
-     WKSDK.shared().chatManager.send(_text, _channel)
 
-     return
 
-   }
 
-   _text = new MessageText(text)
 
-   console.log(WKSDK.shared().chatManager, 111111)
 
-   WKSDK.shared().chatManager.send(_text, _channel)
 
- }
 
- // 对话开场白 用户 to 企业
 
- export async function prologue ({userId, enterpriseId, text}) {
 
-   const { channel } = await checkConversation(userId, enterpriseId)
 
-   send(text, channel, 102)
 
-   return channel
 
- }
 
- // 企业 to 用户
 
- export async function talkToUser ({userId, text}) {
 
-   const { channel, isNewTalk } = await checkConversation(userId)
 
-   if (!isNewTalk) send(text, channel)
 
- }
 
- // 检测是否存在频道
 
- export async function checkConversation (userId, enterpriseId) {
 
-   const query = {
 
-     userId,
 
-     enterpriseId
 
-   }
 
-   // 创建聊天频道
 
-   const { data } = await getChatKey(query)
 
-   const { uid } = data
 
-   const _channel = new Channel(uid, ChannelTypePerson)
 
-   console.log('生成channel', _channel)
 
-   const conversation = WKSDK.shared().conversationManager.findConversation(_channel)
 
-   const isNewTalk = ref(false)
 
-   if(!conversation) {
 
-     // 如果最近会话不存在,则创建一个空的会话
 
-     WKSDK.shared().conversationManager.createEmptyConversation(_channel)
 
-     isNewTalk.value = true
 
-   }
 
-   return {
 
-     channel: _channel,
 
-     isNewTalk: isNewTalk.value
 
-   }
 
- }
 
 
  |