Some checks are pending
Check / lint (push) Waiting to run
Check / typecheck (push) Waiting to run
Check / build (build, 18.x, ubuntu-latest) (push) Waiting to run
Check / build (build, 18.x, windows-latest) (push) Waiting to run
Check / build (build:app, 18.x, ubuntu-latest) (push) Waiting to run
Check / build (build:app, 18.x, windows-latest) (push) Waiting to run
Check / build (build:mp-weixin, 18.x, ubuntu-latest) (push) Waiting to run
Check / build (build:mp-weixin, 18.x, windows-latest) (push) Waiting to run
183 lines
5.2 KiB
JavaScript
183 lines
5.2 KiB
JavaScript
import { defineStore } from 'pinia'
|
||
import { ServeGetTalkList, ServeCreateTalkList } from '@/api/chat/index'
|
||
import { formatTalkItem, ttime, KEY_INDEX_NAME } from '@/utils/talk'
|
||
import { useEditorDraftStore } from './editor-draft'
|
||
// import { ISession } from '@/types/chat'
|
||
|
||
export const useTalkStore = defineStore('talk', {
|
||
state: () => {
|
||
return {
|
||
// 加载状态[1:未加载;2:加载中;3:加载完成;4:加载失败;]
|
||
loadStatus: 2,
|
||
// 会话列表
|
||
items: [],
|
||
}
|
||
},
|
||
getters: {
|
||
// 过滤所有置顶对话列表
|
||
topItems: (state) => {
|
||
return state.items.filter((item) => item.is_top == 1)
|
||
},
|
||
// 过滤所有免打扰对话列表
|
||
disturbItems: (state) => {
|
||
return state.items.filter((item) => item.is_disturb == 1)
|
||
},
|
||
|
||
// 对话列表
|
||
talkItems: (state) => {
|
||
let topList = state.items.filter((item) => item.is_top == 1)
|
||
let listT = state.items.filter((v) => v.is_top !== 1)
|
||
let listP = topList.sort((a, b) => {
|
||
return ttime(b.updated_at) - ttime(a.updated_at)
|
||
})
|
||
listT = listT.sort((a, b) => {
|
||
return ttime(b.updated_at) - ttime(a.updated_at)
|
||
})
|
||
return [...listP, ...listT]
|
||
},
|
||
|
||
// 消息未读数总计
|
||
talkUnreadNum: (state) => {
|
||
return state.items.reduce((total, item) => {
|
||
return total + item.unread_num
|
||
}, 0)
|
||
},
|
||
},
|
||
actions: {
|
||
findItem(index_name) {
|
||
return this.items.find((item) => item.index_name === index_name)
|
||
},
|
||
|
||
// 更新对话节点
|
||
updateItem(params) {
|
||
const item = this.items.find(
|
||
(item) => item.index_name === params.index_name,
|
||
)
|
||
|
||
item && Object.assign(item, params)
|
||
},
|
||
|
||
// 新增对话节点
|
||
addItem(params) {
|
||
this.items = [params, ...this.items]
|
||
},
|
||
|
||
// 移除对话节点
|
||
delItem(index_name) {
|
||
const i = this.items.findIndex((item) => item.index_name === index_name)
|
||
|
||
if (i >= 0) {
|
||
this.items.splice(i, 1)
|
||
}
|
||
|
||
this.items = [...this.items]
|
||
},
|
||
|
||
// 更新对话消息
|
||
updateMessage(params) {
|
||
const item = this.items.find(
|
||
(item) => item.index_name === params.index_name,
|
||
)
|
||
if (item) {
|
||
item.unread_num++
|
||
item.msg_text = params.msg_text
|
||
item.updated_at = params.updated_at
|
||
}
|
||
},
|
||
|
||
// 更新联系人备注
|
||
setRemark(params) {
|
||
const item = this.items.find(
|
||
(item) => item.index_name === `1_${params.user_id}`,
|
||
)
|
||
|
||
item && (item.remark = params.remark)
|
||
},
|
||
|
||
// 加载会话列表
|
||
loadTalkList() {
|
||
this.loadStatus = 2
|
||
const resp = ServeGetTalkList()
|
||
|
||
// 返回 Promise 对象,使调用方可以使用 then/catch
|
||
return resp.then(({ code, data }) => {
|
||
if (code == 200) {
|
||
//向OA的webview通信,改变未读消息数量
|
||
if (typeof plus !== 'undefined') {
|
||
let OAWebView = plus.webview.all()
|
||
OAWebView.forEach((webview) => {
|
||
if (webview.id === 'webviewId1') {
|
||
webview.evalJS(`doUpdateUnreadNum('${data.unread_num}')`)
|
||
}
|
||
})
|
||
} else {
|
||
document.addEventListener('plusready', () => {
|
||
let OAWebView = plus.webview.all()
|
||
OAWebView.forEach((webview) => {
|
||
if (webview.id === 'webviewId1') {
|
||
webview.evalJS(`doUpdateUnreadNum('${data.unread_num}')`)
|
||
}
|
||
})
|
||
})
|
||
}
|
||
this.items = data.items.map((item) => {
|
||
const value = formatTalkItem(item)
|
||
|
||
const draft = useEditorDraftStore().items[value.index_name]
|
||
if (draft) {
|
||
value.draft_text = JSON.parse(draft).text || ''
|
||
}
|
||
|
||
if (value.is_robot == 1) {
|
||
value.is_online = 1
|
||
}
|
||
|
||
return value
|
||
})
|
||
|
||
this.loadStatus = 3
|
||
return this.items // 返回处理后的数据
|
||
} else {
|
||
this.loadStatus = 4
|
||
return Promise.reject(new Error('加载会话列表失败')) // 返回拒绝的 Promise
|
||
}
|
||
}).catch((error) => {
|
||
this.loadStatus = 4
|
||
return Promise.reject(error) // 继续向上传递错误
|
||
})
|
||
},
|
||
|
||
findTalkIndex(index_name) {
|
||
return this.items.findIndex((item) => item.index_name === index_name)
|
||
},
|
||
|
||
toTalk(talk_type, receiver_id, erp_user_id) {
|
||
if (this.findTalkIndex(`${talk_type}_${receiver_id}`) >= 0) {
|
||
sessionStorage.setItem(KEY_INDEX_NAME, `${talk_type}_${receiver_id}`)
|
||
uni.reLaunch({
|
||
url: '/pages/index/index?openSessionIndexName=' + `${talk_type}_${receiver_id}`,
|
||
})
|
||
return
|
||
}
|
||
|
||
ServeCreateTalkList({
|
||
talk_type,
|
||
receiver_id,
|
||
erp_user_id,
|
||
}).then(({ code, data, message }) => {
|
||
if (code == 200) {
|
||
if (this.findTalkIndex(`${talk_type}_${receiver_id}`) === -1) {
|
||
this.addItem(formatTalkItem(data))
|
||
}
|
||
sessionStorage.setItem(KEY_INDEX_NAME, `${talk_type}_${receiver_id}`)
|
||
uni.reLaunch({
|
||
url: '/pages/index/index?openSessionIndexName=' + `${talk_type}_${receiver_id}`,
|
||
})
|
||
} else {
|
||
message.warning(message)
|
||
}
|
||
})
|
||
},
|
||
},
|
||
})
|