You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
132 lines
3.6 KiB
132 lines
3.6 KiB
import { defineStore } from 'pinia';
|
|
import { io, Socket } from 'socket.io-client';
|
|
import { getToken } from '@/utils/token-util';
|
|
import { ChatConversation, ChatMessage } from '@/api/system/chat/model';
|
|
import {
|
|
pageChatConversation,
|
|
updateChatConversation
|
|
} from '@/uni_modules/ws-common/api/system/chat';
|
|
// import { emitter } from '@/uni_modules/ws-common/utils/common';
|
|
|
|
const SOCKET_URL: string = 'wss://server.jimeigroup.cn';
|
|
|
|
interface ConnectionOptions {
|
|
token: string;
|
|
userId: number;
|
|
isAdmin: boolean;
|
|
}
|
|
|
|
export interface ChatState {
|
|
socket: Socket | undefined;
|
|
conversations: ChatConversation[];
|
|
}
|
|
|
|
export const useChatStore = defineStore({
|
|
id: 'chat',
|
|
state: (): ChatState => ({
|
|
socket: undefined,
|
|
conversations: []
|
|
}),
|
|
getters: {
|
|
unReadLetter(): number {
|
|
return this.conversations.reduce((count, item) => count + item.unRead, 0);
|
|
},
|
|
unReadConversations(): ChatConversation[] {
|
|
return this.conversations
|
|
.filter((item) => item.unRead > 0)
|
|
.sort((a, b) => {
|
|
return (
|
|
new Date(b.updateTime).getTime() - new Date(a.updateTime).getTime()
|
|
);
|
|
});
|
|
}
|
|
},
|
|
actions: {
|
|
readConversation(id) {
|
|
const index = this.conversations.findIndex((item) => item.id === id);
|
|
if (index >= 0) {
|
|
updateChatConversation({
|
|
id: this.conversations[index].id,
|
|
unRead: 0
|
|
});
|
|
this.conversations.splice(index, 1);
|
|
}
|
|
},
|
|
async connectSocketIO(userId: number) {
|
|
console.log(
|
|
'---------------------------------connectSocketIO----------------------------------'
|
|
);
|
|
const options: ConnectionOptions = {
|
|
token: getToken() || '',
|
|
userId: userId,
|
|
isAdmin: true
|
|
};
|
|
|
|
const socket: Socket = io(SOCKET_URL, {
|
|
query: options,
|
|
transports: ['websocket', 'polling'],
|
|
timeout: 5000
|
|
});
|
|
|
|
socket.on('connect', () => {
|
|
this.socket = socket;
|
|
console.log(
|
|
'---------------------------------socket connect----------------------------------'
|
|
);
|
|
// 获取聊天列表
|
|
pageChatConversation({
|
|
keywords: '',
|
|
status: 1,
|
|
page: 1,
|
|
limit: 100,
|
|
onlyFake: true
|
|
}).then((res) => {
|
|
if (res?.list) {
|
|
this.conversations = res.list;
|
|
}
|
|
});
|
|
});
|
|
console.log(
|
|
'---------------------------------socket----------------------------------',
|
|
socket
|
|
);
|
|
// 收到新消息
|
|
socket.on('message', (data: ChatMessage) => {
|
|
const index = this.conversations.findIndex(
|
|
(item) =>
|
|
item.friendId === data.formUserId && item.userId === data.toUserId
|
|
);
|
|
let content = '';
|
|
if (data.type == 'image') {
|
|
content = '图片';
|
|
} else if (data.type === 'card') {
|
|
content = '卡片';
|
|
} else {
|
|
content = data.content;
|
|
}
|
|
if (index >= 0) {
|
|
this.conversations[index].unRead++;
|
|
this.conversations[index].content = content;
|
|
this.conversations[index].updateTime = Date.now();
|
|
} else {
|
|
this.conversations.push({
|
|
content: content,
|
|
friendInfo: data.formUserInfo,
|
|
userInfo: data.toUserInfo,
|
|
messages: [],
|
|
unRead: 1,
|
|
updateTime: Date.now(),
|
|
userId: data.toUserId,
|
|
friendId: data.formUserId
|
|
});
|
|
}
|
|
|
|
// emitter.emit('message', data);
|
|
});
|
|
|
|
socket.on('connect_error', () => {
|
|
console.log('connect_error');
|
|
});
|
|
}
|
|
}
|
|
});
|