123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- /** @format */
- import {
- createNewChatApi,
- getBrandConversationApi,
- getContactListApi,
- getContactMessagesApi,
- getNewMessageListApi,
- sendMessageApi,
- } from '@/api/model/message'
- const messageList = ref<any[]>([])
- const contacts = ref<any[]>([])
- const selectedContact = ref<any>({})
- const isScrollBottom = ref<boolean>(false)
- const currentPage = ref<number>(1)
- const loading = ref<boolean>(false)
- const noMore = ref<boolean>(false)
- const timer = ref<any>(null)
- export function useMessage() {
- /**
- * 创建客服
- */
- const createServer = async () => {
- try {
- await createNewChatApi({
- sessionType: '1',
- })
- }
- catch (error) {
- console.log(error)
- }
- }
- /**
- * 创建品牌对话
- */
- const createBrandConversation = async (brandId: any) => {
- try {
- const { id } = await createNewChatApi({
- brandId,
- sessionType: '2',
- })
- selectedContact.value.sessionId = id
- }
- catch (error) {
- console.log(error)
- }
- }
- /**
- * 获取和品牌的对话
- */
- const getBrandConversation = async (brandId: any) => {
- try {
- await getBrandConversationApi({
- brandId,
- sessionType: '2',
- })
- }
- catch (error) {
- console.log(error)
- }
- }
- /**
- * 获取联系列表
- */
- const getContactList = async () => {
- try {
- const res: any = await getContactListApi()
- contacts.value = res.records
- }
- catch (error) {
- console.log(error)
- }
- }
- /**
- * 获取最新消息列表
- */
- const getNewMessageList = async () => {
- try {
- const params = {
- messageId: messageList.value[messageList.value.length - 1].id,
- sessionId: selectedContact.value.sessionId,
- }
- const result: any = await getNewMessageListApi(params)
- messageList.value = [...messageList.value, ...result.reverse()]
- }
- catch (error) {
- console.log(error)
- }
- }
- /**
- * 获取当前联系人的聊天记录
- */
- const getContactMessages = async (pageNo = 1, pageSize = 10) => {
- try {
- const params = {
- sessionId: selectedContact.value.sessionId,
- pageNo,
- pageSize,
- }
- loading.value = true
- const { records }: any = await getContactMessagesApi(params)
- messageList.value = [...records.reverse(), ...messageList.value]
- loading.value = false
- if (records.length < pageSize)
- noMore.value = true
- }
- catch (error) {
- console.log(error)
- }
- }
- const onScrollTop = async () => {
- currentPage.value++
- await getContactMessages(currentPage.value)
- }
- /**
- * 选择联系人
- */
- const selectContact = async (contact: any) => {
- if (selectedContact.value.sessionId === contact.sessionId)
- return
- isScrollBottom.value = false
- selectedContact.value = contact
- messageList.value = []
- currentPage.value = 1
- noMore.value = false
- await getContactMessages(currentPage.value)
- isScrollBottom.value = true
- }
- // 循环调用新聊天内容接口
- const setRoundNewMessage = () => {
- timer.value = setInterval(() => {
- if (messageList.value.length > 0)
- getNewMessageList()
- else
- clearInterval(timer.value)
- }, 5000)
- }
- /**
- * 发送的消息
- */
- const sendMessage = async (message: any, type: string) => {
- try {
- isScrollBottom.value = false
- const params: any = {
- sessionId: selectedContact.value.sessionId,
- type,
- }
- if (type === '1')
- params.content = message
- else
- params.imageUrl = message
- await sendMessageApi(params)
- if (currentPage.value === 1 && messageList.value.length === 0) {
- await getContactMessages()
- // 轮询调用接口
- setRoundNewMessage()
- }
- else {
- await getNewMessageList()
- }
- isScrollBottom.value = true
- }
- catch (error) {
- console.log(error)
- }
- }
- const handleClick = (tab: any) => {
- console.log(tab, 'tab')
- }
- return {
- getContactList,
- handleClick,
- setRoundNewMessage,
- messageList,
- timer,
- contacts,
- onScrollTop,
- noMore,
- createServer,
- selectContact,
- selectedContact,
- getNewMessageList,
- getContactMessages,
- isScrollBottom,
- createBrandConversation,
- getBrandConversation,
- loading,
- sendMessage,
- }
- }
|