2024-11-20 01:17:47 +00:00
|
|
|
import Base from './base'
|
|
|
|
import { nextTick } from 'vue'
|
|
|
|
import ws from '@/connect'
|
|
|
|
import { parseTime } from '@/utils/datetime'
|
|
|
|
import * as message from '@/constant/message'
|
|
|
|
import { formatTalkItem, palyMusic, formatTalkRecord } from '@/utils/talk'
|
|
|
|
// import { isElectronMode } from '@/utils/common'
|
2025-03-24 12:03:52 +00:00
|
|
|
import {
|
|
|
|
ServeClearTalkUnreadNum,
|
|
|
|
ServeCreateTalkList,
|
|
|
|
} from '@/api/chat/index.js'
|
|
|
|
import {
|
|
|
|
useTalkStore,
|
|
|
|
useDialogueStore,
|
|
|
|
useDialogueListStore,
|
|
|
|
useGroupStore,
|
|
|
|
} from '@/store'
|
2024-11-20 01:17:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 好友状态事件
|
|
|
|
*/
|
|
|
|
class Talk extends Base {
|
|
|
|
/**
|
|
|
|
* @var resource 资源
|
|
|
|
*/
|
|
|
|
resource
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 发送者ID
|
|
|
|
*/
|
|
|
|
sender_id = 0
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 接收者ID
|
|
|
|
*/
|
|
|
|
receiver_id = 0
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 聊天类型[1:私聊;2:群聊;]
|
|
|
|
*/
|
|
|
|
talk_type = 0
|
|
|
|
|
2025-03-15 08:44:40 +00:00
|
|
|
/**
|
|
|
|
* 文件上传唯一随机值
|
|
|
|
*/
|
|
|
|
fileNum = ''
|
|
|
|
|
2024-11-20 01:17:47 +00:00
|
|
|
/**
|
|
|
|
* 初始化构造方法
|
|
|
|
*
|
|
|
|
* @param {Object} resource Socket消息
|
|
|
|
*/
|
|
|
|
constructor(resource) {
|
|
|
|
super()
|
|
|
|
|
|
|
|
this.sender_id = resource.sender_id
|
|
|
|
this.receiver_id = resource.receiver_id
|
|
|
|
this.talk_type = resource.talk_type
|
2025-03-15 08:44:40 +00:00
|
|
|
// this.fileNum = resource.file_num
|
2025-03-24 12:03:52 +00:00
|
|
|
if (resource.file_num) {
|
2025-03-15 08:44:40 +00:00
|
|
|
resource.data.file_num = resource.file_num
|
|
|
|
}
|
2024-11-20 01:17:47 +00:00
|
|
|
this.resource = resource.data
|
|
|
|
|
|
|
|
this.handle()
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 判断消息发送者是否来自于我
|
|
|
|
* @returns
|
|
|
|
*/
|
|
|
|
isCurrSender() {
|
|
|
|
return this.sender_id == this.getAccountId()
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取对话索引
|
|
|
|
*
|
|
|
|
* @return String
|
|
|
|
*/
|
|
|
|
getIndexName() {
|
|
|
|
if (this.talk_type == 2) {
|
|
|
|
return `${this.talk_type}_${this.receiver_id}`
|
|
|
|
}
|
|
|
|
|
|
|
|
let receiver_id = this.isCurrSender() ? this.receiver_id : this.sender_id
|
|
|
|
|
|
|
|
return `${this.talk_type}_${receiver_id}`
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取聊天列表左侧的对话信息
|
|
|
|
*/
|
|
|
|
getTalkText() {
|
|
|
|
let text = ''
|
|
|
|
if (this.resource.msg_type != message.ChatMsgTypeText) {
|
|
|
|
text = message.ChatMsgTypeMapping[this.resource.msg_type]
|
|
|
|
} else {
|
|
|
|
text = this.resource.extra.content.replace(/<img .*?>/g, '')
|
|
|
|
}
|
|
|
|
|
|
|
|
return text
|
|
|
|
}
|
|
|
|
|
|
|
|
// 播放提示音
|
|
|
|
play() {
|
|
|
|
// 客户端有消息提示
|
|
|
|
// if (isElectronMode()) return
|
|
|
|
// useSettingsStore().isPromptTone && palyMusic()
|
|
|
|
}
|
|
|
|
|
|
|
|
handle() {
|
|
|
|
// 不是自己发送的消息则需要播放提示音
|
|
|
|
if (!this.isCurrSender()) {
|
|
|
|
this.play()
|
|
|
|
}
|
|
|
|
|
|
|
|
// 判断会话列表是否存在,不存在则创建
|
|
|
|
if (useTalkStore().findTalkIndex(this.getIndexName()) == -1) {
|
|
|
|
return this.addTalkItem()
|
|
|
|
}
|
|
|
|
|
|
|
|
// 判断当前是否正在和好友对话
|
|
|
|
if (this.isTalk(this.talk_type, this.receiver_id, this.sender_id)) {
|
|
|
|
this.insertTalkRecord()
|
|
|
|
} else {
|
|
|
|
this.updateTalkItem()
|
2025-03-24 12:03:52 +00:00
|
|
|
if (typeof plus !== 'undefined') {
|
|
|
|
let OAWebView = plus.webview.all()
|
2025-03-25 05:12:06 +00:00
|
|
|
OAWebView.forEach((webview) => {
|
|
|
|
if (webview.id === 'webviewId1') {
|
|
|
|
webview.evalJS(`updateUnreadMsgNumAdd()`)
|
|
|
|
}
|
|
|
|
})
|
2025-03-24 12:03:52 +00:00
|
|
|
} else {
|
|
|
|
document.addEventListener('plusready', () => {
|
|
|
|
let OAWebView = plus.webview.all()
|
2025-03-25 05:12:06 +00:00
|
|
|
OAWebView.forEach((webview) => {
|
|
|
|
if (webview.id === 'webviewId1') {
|
|
|
|
webview.evalJS(`updateUnreadMsgNumAdd()`)
|
|
|
|
}
|
|
|
|
})
|
2025-03-24 12:03:52 +00:00
|
|
|
})
|
|
|
|
}
|
2024-11-20 01:17:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 显示消息提示
|
|
|
|
* @returns
|
|
|
|
*/
|
|
|
|
showMessageNocice() {
|
|
|
|
// if (useSettingsStore().isLeaveWeb) {
|
|
|
|
// const notification = new Notification('LumenIM 在线聊天', {
|
|
|
|
// dir: 'auto',
|
|
|
|
// lang: 'zh-CN',
|
|
|
|
// body: '您有新的消息请注意查收'
|
|
|
|
// })
|
|
|
|
// notification.onclick = () => {
|
|
|
|
// notification.close()
|
|
|
|
// }
|
|
|
|
// } else {
|
|
|
|
// window['$notification'].create({
|
|
|
|
// title: '消息通知',
|
|
|
|
// content: '您有新的消息请注意查收',
|
|
|
|
// duration: 3000
|
|
|
|
// })
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 加载对接节点
|
|
|
|
*/
|
|
|
|
addTalkItem() {
|
|
|
|
let receiver_id = this.sender_id
|
|
|
|
let talk_type = this.talk_type
|
|
|
|
|
|
|
|
if (talk_type == 1 && this.receiver_id != this.getAccountId()) {
|
|
|
|
receiver_id = this.receiver_id
|
|
|
|
} else if (talk_type == 2) {
|
|
|
|
receiver_id = this.receiver_id
|
|
|
|
}
|
|
|
|
|
|
|
|
ServeCreateTalkList({
|
|
|
|
talk_type,
|
2025-03-24 12:03:52 +00:00
|
|
|
receiver_id,
|
2024-11-20 01:17:47 +00:00
|
|
|
}).then(({ code, data }) => {
|
|
|
|
if (code == 200) {
|
|
|
|
let item = formatTalkItem(data)
|
|
|
|
item.unread_num = 1
|
|
|
|
useTalkStore().addItem(item)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 插入对话记录
|
|
|
|
*/
|
|
|
|
insertTalkRecord() {
|
|
|
|
let record = this.resource
|
2025-03-24 12:03:52 +00:00
|
|
|
let newRecord = formatTalkRecord(this.getAccountId(), this.resource)
|
|
|
|
const { addDialogueRecord, addChatRecord } = useDialogueListStore()
|
2024-11-20 01:17:47 +00:00
|
|
|
// 群成员变化的消息,需要更新群成员列表
|
2025-03-21 09:01:34 +00:00
|
|
|
if ([1102, 1103, 1104, 1115].includes(record.msg_type)) {
|
2024-11-20 01:17:47 +00:00
|
|
|
useDialogueStore().updateGroupMembers()
|
|
|
|
}
|
2025-03-22 06:22:09 +00:00
|
|
|
if ([1116].includes(record.msg_type)) {
|
|
|
|
// 更新会话信息
|
|
|
|
useDialogueStore().setDialogue({
|
|
|
|
name: record.extra.group_name,
|
|
|
|
talk_type: record.talk_type,
|
|
|
|
receiver_id: record.receiver_id,
|
|
|
|
})
|
|
|
|
// 更新群聊信息
|
|
|
|
useGroupStore().updateGroupInfo({
|
|
|
|
group_name: record.extra.group_name,
|
|
|
|
avatar: record.extra.group_avatar,
|
|
|
|
})
|
|
|
|
// 更新会话列表中的会话信息
|
2025-03-24 12:03:52 +00:00
|
|
|
const dialogue = useDialogueListStore().getDialogueList(
|
|
|
|
`${record.talk_type}_${record.receiver_id}`,
|
|
|
|
)
|
2025-03-22 06:22:09 +00:00
|
|
|
if (dialogue) {
|
|
|
|
dialogue.talk.username = record.extra.group_name
|
|
|
|
}
|
|
|
|
}
|
2025-03-24 12:03:52 +00:00
|
|
|
addDialogueRecord([newRecord], 'add')
|
|
|
|
addChatRecord(this.getIndexName(), newRecord)
|
2024-12-06 08:55:15 +00:00
|
|
|
useDialogueStore().addDialogueRecord(newRecord)
|
2024-11-20 01:17:47 +00:00
|
|
|
|
|
|
|
if (!this.isCurrSender()) {
|
|
|
|
// 推送已读消息
|
|
|
|
setTimeout(() => {
|
|
|
|
ws.emit('im.message.read', {
|
|
|
|
receiver_id: this.sender_id,
|
2025-03-24 12:03:52 +00:00
|
|
|
msg_ids: [this.resource.msg_id],
|
2024-11-20 01:17:47 +00:00
|
|
|
})
|
|
|
|
}, 1000)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 获取聊天面板元素节点
|
|
|
|
const el = document.getElementById('imChatPanel')
|
|
|
|
if (!el) return
|
|
|
|
|
|
|
|
// 判断的滚动条是否在底部
|
2025-03-24 12:03:52 +00:00
|
|
|
const isBottom =
|
|
|
|
Math.ceil(el.scrollTop) + el.clientHeight >= el.scrollHeight
|
2024-11-20 01:17:47 +00:00
|
|
|
|
|
|
|
if (isBottom || record.user_id == this.getAccountId()) {
|
|
|
|
nextTick(() => {
|
|
|
|
el.scrollTop = el.scrollHeight + 1000
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
useDialogueStore().setUnreadBubble()
|
|
|
|
}
|
|
|
|
|
|
|
|
useTalkStore().updateItem({
|
|
|
|
index_name: this.getIndexName(),
|
|
|
|
msg_text: this.getTalkText(),
|
2025-03-24 12:03:52 +00:00
|
|
|
updated_at: parseTime(new Date()),
|
2024-11-20 01:17:47 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if (this.talk_type == 1 && this.getAccountId() !== this.sender_id) {
|
2025-03-24 12:03:52 +00:00
|
|
|
//不在此处维护未读消息数量
|
|
|
|
// ServeClearTalkUnreadNum({
|
|
|
|
// talk_type: 1,
|
|
|
|
// receiver_id: this.sender_id,
|
|
|
|
// })
|
2024-11-20 01:17:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 更新对话列表记录
|
|
|
|
*/
|
|
|
|
updateTalkItem() {
|
|
|
|
useTalkStore().updateMessage({
|
|
|
|
index_name: this.getIndexName(),
|
|
|
|
msg_text: this.getTalkText(),
|
2025-03-24 12:03:52 +00:00
|
|
|
updated_at: parseTime(new Date()),
|
2024-11-20 01:17:47 +00:00
|
|
|
})
|
2025-03-24 12:03:52 +00:00
|
|
|
if (this.resource.msg_type == 1116) {
|
2025-03-22 06:22:09 +00:00
|
|
|
// 更新会话列表中的会话信息
|
2025-03-24 12:03:52 +00:00
|
|
|
const dialogue = useDialogueListStore().getDialogueList(
|
|
|
|
`${this.resource.talk_type}_${this.resource.receiver_id}`,
|
|
|
|
)
|
2025-03-22 06:22:09 +00:00
|
|
|
if (dialogue) {
|
|
|
|
dialogue.talk.username = this.resource.extra.group_name
|
|
|
|
}
|
|
|
|
useTalkStore().loadTalkList()
|
|
|
|
}
|
2024-11-20 01:17:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Talk
|