chore(config): 初始化项目配置文件
- 添加 .editorconfig 文件统一代码风格 - 配置 .env.development 环境变量文件 - 创建 .env.example 环境变量示例文件 - 设置 .eslintignore 忽略检查规则 - 配置 .eslintrc.js 代码检查规则 - 添加 .gitignore 文件忽略版本控制 - 设置 .prettierignore 忽略格式化规则 - 新增隐私政策HTML页面文件 - 创建API密钥编辑组件基础结构
This commit is contained in:
134
src/store/modules/chat.ts
Normal file
134
src/store/modules/chat.ts
Normal file
@@ -0,0 +1,134 @@
|
||||
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 '@/api/system/chat';
|
||||
import { emitter } from '@/utils/common';
|
||||
|
||||
const SOCKET_URL: string = 'wss://server.websoft.top';
|
||||
|
||||
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
|
||||
);
|
||||
console.log('收到socket消息>>>');
|
||||
// 收到新消息
|
||||
socket.on('message', (data: ChatMessage) => {
|
||||
console.log('收到socket消息>>>');
|
||||
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');
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user