2024-11-20 01:17:47 +00:00
|
|
|
import { defineStore } from 'pinia'
|
|
|
|
import {
|
|
|
|
ServeRemoveRecords,
|
|
|
|
ServeRevokeRecords,
|
|
|
|
ServePublishMessage,
|
2025-01-07 10:57:29 +00:00
|
|
|
ServeCollectEmoticon,
|
2025-02-05 08:22:32 +00:00
|
|
|
ServeEmptyMessage,
|
2024-11-20 01:17:47 +00:00
|
|
|
} from '@/api/chat/index'
|
|
|
|
import { ServeGetGroupMembers } from '@/api/group/index'
|
|
|
|
import { useEditorStore } from './editor'
|
2024-12-20 08:59:58 +00:00
|
|
|
import { useDialogueListStore } from './dialogueList'
|
2025-01-23 08:45:49 +00:00
|
|
|
import { useAuth } from '../auth/index'
|
2024-11-20 01:17:47 +00:00
|
|
|
|
|
|
|
// 键盘消息事件定时器
|
|
|
|
// let keyboardTimeout = null
|
|
|
|
|
|
|
|
export const useDialogueStore = defineStore('dialogue', {
|
|
|
|
state: () => {
|
|
|
|
return {
|
|
|
|
// 对话索引(聊天对话的唯一索引)
|
|
|
|
index_name: '',
|
|
|
|
|
|
|
|
// 对话节点
|
|
|
|
talk: {
|
|
|
|
username: '',
|
|
|
|
talk_type: 0, // 对话来源[1:私聊;2:群聊]
|
2025-01-07 10:57:29 +00:00
|
|
|
receiver_id: 0,
|
2024-11-20 01:17:47 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// 好友是否正在输入文字
|
|
|
|
keyboard: false,
|
|
|
|
|
|
|
|
// 对方是否在线
|
|
|
|
online: false,
|
|
|
|
|
|
|
|
// 聊天记录
|
|
|
|
records: [],
|
|
|
|
|
|
|
|
// 新消息提示
|
|
|
|
unreadBubble: 0,
|
|
|
|
|
|
|
|
// 是否开启多选操作模式
|
|
|
|
isOpenMultiSelect: false,
|
|
|
|
|
|
|
|
// 是否显示编辑器
|
|
|
|
isShowEditor: false,
|
|
|
|
|
|
|
|
// 是否显示会话列表
|
|
|
|
isShowSessionList: true,
|
|
|
|
|
|
|
|
// 群成员列表
|
|
|
|
members: [],
|
|
|
|
|
|
|
|
// 对话记录
|
|
|
|
items: {
|
|
|
|
'1_1': {
|
|
|
|
talk_type: 1, // 对话类型
|
|
|
|
receiver_id: 0, // 接收者ID
|
|
|
|
read_sequence: 0, // 当前已读的最后一条记录
|
2025-01-07 10:57:29 +00:00
|
|
|
records: [],
|
|
|
|
},
|
2024-12-19 03:02:47 +00:00
|
|
|
},
|
|
|
|
// 转发消息类型
|
|
|
|
forwardType: 1,
|
|
|
|
// 合并转发消息
|
2025-01-07 10:57:29 +00:00
|
|
|
forwardMessages: [],
|
2024-11-20 01:17:47 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
getters: {
|
|
|
|
// 多选列表
|
|
|
|
selectItems: (state) => state.records.filter((item) => item.isCheck),
|
|
|
|
// 当前对话是否是群聊
|
2025-01-07 10:57:29 +00:00
|
|
|
isGroupTalk: (state) => state.talk.talk_type === 2,
|
|
|
|
//获取被禁言的成员列表
|
|
|
|
getSilenceMember: (state) =>
|
|
|
|
state.members.filter((item) => item.is_mute === 1),
|
|
|
|
//获取群管理员
|
2025-01-23 08:45:49 +00:00
|
|
|
getAdminList: (state) =>
|
|
|
|
state.members.filter((item) => item.leader === 1 || item.leader === 2),
|
2024-11-20 01:17:47 +00:00
|
|
|
},
|
|
|
|
actions: {
|
|
|
|
// 更新在线状态
|
|
|
|
setOnlineStatus(status) {
|
|
|
|
this.online = status
|
|
|
|
},
|
|
|
|
|
|
|
|
// 更新对话信息
|
|
|
|
setDialogue(data = {}) {
|
|
|
|
this.online = data.is_online == 1
|
|
|
|
this.talk = {
|
|
|
|
username: data.remark || data.name,
|
|
|
|
talk_type: data.talk_type,
|
2025-01-07 10:57:29 +00:00
|
|
|
receiver_id: data.receiver_id,
|
2024-11-20 01:17:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.index_name = `${data.talk_type}_${data.receiver_id}`
|
|
|
|
this.records = []
|
|
|
|
this.unreadBubble = 0
|
|
|
|
this.isShowEditor = data?.is_robot === 0
|
|
|
|
|
|
|
|
this.members = []
|
|
|
|
if (data.talk_type == 2) {
|
|
|
|
this.updateGroupMembers()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// 更新提及列表
|
|
|
|
async updateGroupMembers() {
|
|
|
|
let { code, data } = await ServeGetGroupMembers({
|
2025-01-07 10:57:29 +00:00
|
|
|
group_id: this.talk.receiver_id,
|
2024-11-20 01:17:47 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if (code != 200) return
|
|
|
|
|
|
|
|
this.members = data.items.map((o) => ({
|
|
|
|
id: o.user_id,
|
|
|
|
nickname: o.nickname,
|
|
|
|
avatar: o.avatar,
|
|
|
|
gender: o.gender,
|
|
|
|
leader: o.leader,
|
|
|
|
remark: o.remark,
|
|
|
|
online: false,
|
2025-01-07 10:57:29 +00:00
|
|
|
value: o.nickname,
|
2025-01-16 10:25:21 +00:00
|
|
|
key: o.key,
|
|
|
|
erp_user_id: o.erp_user_id,
|
2025-01-20 11:03:57 +00:00
|
|
|
is_mute: o.is_mute,
|
2025-02-05 08:22:32 +00:00
|
|
|
is_mine:
|
|
|
|
useAuth()?.userInfo?.value?.ID === o?.erp_user_id ? true : false,
|
2024-11-20 01:17:47 +00:00
|
|
|
}))
|
|
|
|
},
|
|
|
|
|
|
|
|
// 清空对话记录
|
|
|
|
clearDialogueRecord() {
|
|
|
|
this.records = []
|
|
|
|
},
|
|
|
|
|
|
|
|
// 数组头部压入对话记录
|
|
|
|
unshiftDialogueRecord(records) {
|
|
|
|
this.records.unshift(...records)
|
|
|
|
},
|
|
|
|
|
|
|
|
// 推送对话记录
|
|
|
|
addDialogueRecord(record) {
|
|
|
|
// TOOD 需要通过 sequence 排序,保证消息一致性
|
|
|
|
// this.records.splice(index, 0, record)
|
|
|
|
|
|
|
|
this.records.push(record)
|
|
|
|
},
|
|
|
|
|
|
|
|
// 更新对话记录
|
|
|
|
updateDialogueRecord(params) {
|
|
|
|
const { msg_id = '' } = params
|
|
|
|
|
|
|
|
const item = this.records.find((item) => item.msg_id === msg_id)
|
|
|
|
|
|
|
|
item && Object.assign(item, params)
|
|
|
|
},
|
|
|
|
|
|
|
|
// 批量删除对话记录
|
|
|
|
batchDelDialogueRecord(msgIds = []) {
|
|
|
|
msgIds.forEach((msgid) => {
|
|
|
|
const index = this.records.findIndex((item) => item.msg_id === msgid)
|
|
|
|
|
|
|
|
if (index >= 0) this.records.splice(index, 1)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
// 自增好友键盘输入事件
|
|
|
|
// triggerKeyboard() {
|
|
|
|
// this.keyboard = true
|
|
|
|
|
|
|
|
// clearTimeout(keyboardTimeout)
|
|
|
|
|
|
|
|
// keyboardTimeout = setTimeout(() => (this.keyboard = false), 2000)
|
|
|
|
// },
|
|
|
|
|
|
|
|
setUnreadBubble(value) {
|
|
|
|
if (value === 0) {
|
|
|
|
this.unreadBubble = 0
|
|
|
|
} else {
|
|
|
|
this.unreadBubble++
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2024-12-19 03:02:47 +00:00
|
|
|
// 设置多选模式
|
|
|
|
setMultiSelect(value) {
|
|
|
|
this.isOpenMultiSelect = value
|
|
|
|
},
|
|
|
|
|
2024-11-20 01:17:47 +00:00
|
|
|
// 关闭多选模式
|
|
|
|
closeMultiSelect() {
|
|
|
|
this.isOpenMultiSelect = false
|
|
|
|
|
|
|
|
for (const item of this.selectItems) {
|
|
|
|
if (item.isCheck) {
|
|
|
|
item.isCheck = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// 删除聊天记录
|
|
|
|
ApiDeleteRecord(msgIds = []) {
|
2024-12-20 08:59:58 +00:00
|
|
|
const { batchDelDialogueRecord } = useDialogueListStore()
|
2024-11-20 01:17:47 +00:00
|
|
|
ServeRemoveRecords({
|
|
|
|
talk_type: this.talk.talk_type,
|
|
|
|
receiver_id: this.talk.receiver_id,
|
2025-01-07 10:57:29 +00:00
|
|
|
msg_ids: msgIds,
|
2024-11-20 01:17:47 +00:00
|
|
|
}).then((res) => {
|
|
|
|
if (res.code == 200) {
|
|
|
|
this.batchDelDialogueRecord(msgIds)
|
2024-12-20 08:59:58 +00:00
|
|
|
batchDelDialogueRecord(msgIds)
|
2024-11-20 01:17:47 +00:00
|
|
|
} else {
|
2024-11-28 08:55:45 +00:00
|
|
|
message.warning(res.message)
|
2024-11-20 01:17:47 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2025-02-05 08:22:32 +00:00
|
|
|
//清空聊天记录
|
|
|
|
apiClearRecord() {
|
2025-02-06 09:01:28 +00:00
|
|
|
const { clearDialogueRecord } = useDialogueListStore()
|
2025-02-05 08:22:32 +00:00
|
|
|
ServeEmptyMessage({
|
|
|
|
talk_type: this.talk.talk_type,
|
|
|
|
receiver_id: this.talk.receiver_id,
|
|
|
|
}).then((res) => {
|
|
|
|
if (res.code == 200) {
|
2025-02-06 09:01:28 +00:00
|
|
|
clearDialogueRecord()
|
2025-02-05 08:22:32 +00:00
|
|
|
} else {
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2024-11-20 01:17:47 +00:00
|
|
|
// 撤销聊天记录
|
|
|
|
ApiRevokeRecord(msg_id = '') {
|
|
|
|
ServeRevokeRecords({ msg_id }).then((res) => {
|
|
|
|
if (res.code == 200) {
|
|
|
|
this.updateDialogueRecord({ msg_id, is_revoke: 1 })
|
|
|
|
} else {
|
2024-11-28 08:55:45 +00:00
|
|
|
message.warning(res.message)
|
2024-11-20 01:17:47 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
// 转发聊天记录
|
|
|
|
ApiForwardRecord(options) {
|
|
|
|
let data = {
|
|
|
|
type: 'forward',
|
|
|
|
receiver: {
|
|
|
|
talk_type: this.talk.talk_type,
|
2025-01-07 10:57:29 +00:00
|
|
|
receiver_id: this.talk.receiver_id,
|
2024-11-20 01:17:47 +00:00
|
|
|
},
|
2025-01-07 10:57:29 +00:00
|
|
|
...options,
|
2024-11-20 01:17:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ServePublishMessage(data).then((res) => {
|
|
|
|
if (res.code == 200) {
|
2024-12-19 03:02:47 +00:00
|
|
|
// this.closeMultiSelect()
|
|
|
|
message.success('转发成功')
|
2024-11-20 01:17:47 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
ApiCollectImage(options) {
|
|
|
|
const { msg_id } = options
|
|
|
|
|
|
|
|
ServeCollectEmoticon({ msg_id }).then(() => {
|
|
|
|
useEditorStore().loadUserEmoticon()
|
2024-11-28 08:55:45 +00:00
|
|
|
message && message.success('收藏成功')
|
2024-11-20 01:17:47 +00:00
|
|
|
})
|
2024-12-19 03:02:47 +00:00
|
|
|
},
|
|
|
|
// 设置转发消息类型
|
|
|
|
setForwardType(type) {
|
|
|
|
this.forwardType = type
|
|
|
|
},
|
|
|
|
// 设置合并转发消息
|
|
|
|
setForwardMessages(messages) {
|
|
|
|
this.forwardMessages = messages
|
2025-01-07 10:57:29 +00:00
|
|
|
},
|
|
|
|
},
|
2024-11-20 01:17:47 +00:00
|
|
|
})
|