refactor: friend basic function refactory complete

This commit is contained in:
2025-12-16 18:09:06 +08:00
parent b2bcfdf1a9
commit a4bed485a5
33 changed files with 1269 additions and 822 deletions

View File

@@ -1,5 +1,5 @@
import { defineStore } from 'pinia'
import { connectWebSocket, disconnectWebSocket, sendMessage, setIsManualClose } from '@/socket/onlineSocket'
import { connectWebSocket, disconnectWebSocket, sendMessage, setIsManualClose } from '@/websocket/onlineSocket'
import { messageStore } from './message'
const message = messageStore()
@@ -8,17 +8,17 @@ export const onlineSocketStore = defineStore('onlineSocket', {
state: () => ({
isConnected: false,
hasGotMessage: false,
u_id: ''
id: ''
}),
actions: {
connect(u_id) {
this.u_id = u_id;
connect(id) {
this.id = id;
if (this.isConnected === true) return
connectWebSocket();
this.isConnected = true;
if (!this.hasGotMessage) {
message.loadMessagesHistory(this.u_id)
message.loadMessagesHistory(this.id)
this.hasGotMessage = true
}
},
@@ -27,11 +27,11 @@ export const onlineSocketStore = defineStore('onlineSocket', {
disconnectWebSocket();
this.isConnected = false;
if (this.hasGotMessage) {
message.saveMessagesHistory(this.u_id)
message.saveMessagesHistory(this.id)
}
},
send(message) {
sendMessage(message);
sendMessage(JSON.stringify(message));
}
}
})

View File

@@ -1,4 +1,4 @@
import { connectVoicesocket, disconnectVoicesocket, sendMessage, hangup } from "@/socket/voiceSocket";
import { connectVoicesocket, disconnectVoicesocket, sendMessage, hangup } from "@/websocket/voiceSocket";
import { defineStore } from "pinia";
export const voiceStore = defineStore("voice", {

0
src/store/friends.ts Normal file
View File

194
src/store/message.ts Normal file
View File

@@ -0,0 +1,194 @@
import { defineStore } from "pinia";
import {
saveMessages,
loadMessages,
deleteMessages,
} from "@/functions/historyMessages";
import {
saveGroupMessages,
loadGroupMessages,
deleteGroupMessages,
} from "@/functions/groupHistoryMessage";
import { toRaw } from "vue";
interface Message {
cmd: string;
from: number;
to: number;
content: string;
time: string;
}
export const messageStore = defineStore("messageStore", {
// 定义一个响应式数组来存储聊天消息
state: () => ({
historymessages: [],
messages: [],
from: 0,
to: 0,
corresponding: [],
}),
// 定义操作消息的函数
actions: {
// 添加消息到数组
addMessage(message: Message) {
this.messages.push(message);
},
// 清空所有消息
clearMessages() {
this.messages = [];
},
setCorresponding() {
// 过滤出当前聊天中的消息
this.corresponding = this.messages.filter(
(msg: Message) =>
(msg.from === this.from && msg.to === this.to) ||
(msg.from === this.to && msg.to === this.from)
);
const historymessages = this.historymessages.filter(
(msg: Message) =>
(msg.from === this.from && msg.to === this.to) ||
(msg.from === this.to && msg.to === this.from)
);
this.corresponding = [...historymessages, ...this.corresponding];
},
// 清除当前登录的聊天数据,保存到本地
async saveMessagesHistory(id: number) {
const messages = toRaw(this.messages);
await saveMessages(id, messages);
},
// 加载本地聊天数据
async loadMessagesHistory(id: number) {
try {
this.historymessages = await loadMessages(id);
// 确保历史消息是数组类型
if (!Array.isArray(this.historymessages)) {
console.error("历史消息数据无效:", this.historymessages);
this.historymessages = []; // 如果数据无效,设置为空数组
}
} catch (error) {
console.log("加载历史消息时出错" + error);
}
},
async deleteMessagesHistory(id: number, f_id: number) {
this.historymessages = this.historymessages.filter(
(msg: Message) =>
!(
(msg.from === f_id && msg.to === id) ||
(msg.from === id && msg.to === f_id)
)
);
this.messages = this.messages.filter(
(msg: Message) =>
!(
(msg.from === f_id && msg.to === id) ||
(msg.from === id && msg.to === f_id)
)
);
if (f_id === this.from) {
console.log("清除对应聊天数据展示栈");
this.corresponding = [];
}
const messages = JSON.parse(JSON.stringify(toRaw(this.historymessages)));
try {
await deleteMessages(id, messages);
} catch (error) {
console.log("删除历史消息时出错" + error);
}
},
},
// 定义计算属性
getters: {
// 获取消息数量
messageCount: (state) => state.messages.length,
},
});
export const messageSignStore = defineStore("messageSignStore", {
state: () => ({
sign: [],
}),
actions: {
addSign(sign) {
// 检查是否已经存在相同的值
const exists = this.sign.some(
(item) =>
item.sender === sign.sender && item.sender_name === sign.sender_name
);
if (!exists) {
this.sign.push(sign);
} else {
console.warn(`Sign "${sign}" already exists and will not be added.`);
}
},
clearSign() {
this.sign = [];
},
removeSign(sign) {
// 找到相同值的索引
const index = this.sign.indexOf(sign);
if (index !== -1) {
// 如果存在,删除该值
this.sign.splice(index, 1);
} else {
console.warn(`Sign "${sign}" not found.`);
}
},
},
});
export const groupMessageStore = defineStore("groupMessageStore", {
state: () => ({
g_id: "",
historymessages: [],
messages: [],
corresponding: [],
}),
actions: {
addMessage(message) {
this.messages.push(message);
},
clearMessages() {
this.messages = [];
},
async initMessages() {
this.historymessages = [...this.historymessages, ...this.messages];
this.messages = [];
},
async getHistoryMessages(u_id, g_id) {
this.g_id = g_id;
const key = `${u_id}-${g_id}`;
try {
this.historymessages = await loadGroupMessages(key);
console.log("缓存到历史消息:");
console.log(this.historymessages);
// 确保历史消息是数组类型
if (!Array.isArray(this.historymessages)) {
console.error("历史消息数据无效:", this.historymessages);
this.historymessages = []; // 如果数据无效,设置为空数组
}
} catch (error) {
console.log("加载历史消息时出错" + error);
}
},
async saveMessagesHistory(u_id, g_id) {
const key = `${u_id}-${g_id}`;
console.log(key);
const messages = toRaw(this.messages);
this.messages = [];
this.historymessages = [];
await saveGroupMessages(key, messages);
},
async deleteMessagesHistory(u_id, g_id) {
const key = `${u_id}-${g_id}`;
this.historymessages = [];
this.messages = [];
await deleteGroupMessages(key, []);
},
},
});

View File

@@ -48,9 +48,9 @@ export const messageStore = defineStore("messageStore", {
this.corresponding = [...historymessages, ...this.corresponding];
},
// 清楚当前登录的聊天数据,保存到本地
async saveMessagesHistory(u_id) {
async saveMessagesHistory(id) {
const messages = toRaw(this.messages);
await saveMessages(u_id, messages);
await saveMessages(id, messages);
},
// 加载本地聊天数据
async loadMessagesHistory(u_id) {

18
src/store/message_sign.ts Normal file
View File

@@ -0,0 +1,18 @@
import { defineStore } from "pinia";
import { ref } from "vue";
export const messageSignStore = defineStore("messageSignStore", ()=>{
const messageSign = ref(false);
const setMessageSign = (sign: boolean)=>{
messageSign.value = sign;
}
const getMessageSign = (): boolean =>{
return messageSign.value;
}
return {
setMessageSign,
getMessageSign
}
});

90
src/store/user.ts Normal file
View File

@@ -0,0 +1,90 @@
import { ref, reactive } from "vue";
import { defineStore } from "pinia";
interface User {
id: number;
u_id: string;
u_name: string;
u_avatar: string;
u_account: string;
u_introduction: string;
}
interface Code {
c_id: string;
code: string;
}
export const userInfoStore = defineStore(
"userInfoStore",
() => {
const token = ref<string | null>(null);
const account = ref<string | null>(null);
const user = reactive<User>({
id: 0,
u_id: "",
u_name: "",
u_avatar: "",
u_account: "",
u_introduction: "",
});
const clearUserInfo = (): void => {
token.value = null;
account.value = null;
user.id = 0;
user.u_id = "";
user.u_name = "";
user.u_avatar = "";
user.u_account = "";
user.u_introduction = "";
};
// const setAccount = (acc: string | null): void => {
// account.value = acc;
// };
// const getAccount = (): string | null => {
// return account.value;
// };
return {
token,
user,
clearUserInfo,
account,
// setAccount,
// getAccount,
};
},
{
persist: {
key: "userInfo",
storage: localStorage,
pick: ["token", "account", "user"],
},
}
);
export const codeStore = defineStore(
"codeStore",
() => {
const codes = reactive<Code>({
c_id: "",
code: "",
});
const setID = (id: string): void => {
codes.c_id = id;
};
const getID = (): string => {
return codes.c_id;
}
return {
setID,
getID
};
},
);