1.更换变量,修改外链打包后不生效问题
This commit is contained in:
2026-03-12 16:10:15 +08:00
parent f269c42a58
commit 687fb37beb
10 changed files with 248 additions and 10 deletions
+236
View File
@@ -0,0 +1,236 @@
// 数据库管理类
class ConversationDB {
constructor() {
this.dbName = 'TIMConversationDB';
this.version = 1;
this.db = null;
}
// 打开数据库
async openDB() {
return new Promise((resolve, reject) => {
const request = indexedDB.open(this.dbName, this.version);
request.onerror = () => reject(request.error);
request.onsuccess = () => {
this.db = request.result;
resolve(this.db);
};
request.onupgradeneeded = (event) => {
const db = event.target.result;
// 创建会话存储空间
if (!db.objectStoreNames.contains('conversations')) {
const store = db.createObjectStore('conversations', {
keyPath: 'conversationID',
});
// 创建索引
store.createIndex('lastMessageTime', 'lastMessageTime', { unique: false });
store.createIndex('isTop', 'isTop', { unique: false });
store.createIndex('status', 'status', { unique: false });
store.createIndex('type', 'type', { unique: false });
}
// 创建消息存储空间(可选)
if (!db.objectStoreNames.contains('messages')) {
const messageStore = db.createObjectStore('messages', {
keyPath: 'ID',
});
messageStore.createIndex('conversationID', 'conversationID', { unique: false });
messageStore.createIndex('time', 'time', { unique: false });
}
};
});
}
// 保存或更新会话
async saveConversation(conversation) {
if (!this.db) await this.openDB();
return new Promise((resolve, reject) => {
const transaction = this.db.transaction(['conversations'], 'readwrite');
const store = transaction.objectStore('conversations');
// 添加更新时间
conversation.updatedTime = Date.now();
const request = store.put(conversation);
request.onerror = () => reject(request.error);
request.onsuccess = () => resolve(request.result);
});
}
// 批量保存会话
async saveConversations(conversations) {
if (!this.db) await this.openDB();
return new Promise((resolve, reject) => {
const transaction = this.db.transaction(['conversations'], 'readwrite');
const store = transaction.objectStore('conversations');
conversations.forEach((conversation) => {
conversation.updatedTime = Date.now();
store.put(conversation);
});
transaction.oncomplete = () => resolve();
transaction.onerror = () => reject(transaction.error);
});
}
// 获取会话列表(支持排序和分页)
async getConversationList(options = {}) {
if (!this.db) await this.openDB();
const {
page = 1,
pageSize = 50,
status = 0, // 0:活跃 1:已结束
sortBy = 'lastMessageTime',
sortOrder = 'desc',
} = options;
return new Promise((resolve, reject) => {
const transaction = this.db.transaction(['conversations'], 'readonly');
const store = transaction.objectStore('conversations');
const index = store.index(sortBy);
let conversations = [];
let count = 0;
let cursor = index.openCursor(null, sortOrder === 'desc' ? 'prev' : 'next');
cursor.onsuccess = (event) => {
const cursor = event.target.result;
if (cursor) {
const conversation = cursor.value;
// 过滤条件
if (conversation.status === status) {
count++;
// 分页计算
const startIndex = (page - 1) * pageSize;
const endIndex = page * pageSize;
if (count > startIndex && count <= endIndex) {
conversations.push(conversation);
}
}
if (count < endIndex) {
cursor.continue();
} else {
resolve({
conversations,
total: count,
page,
pageSize,
});
}
} else {
resolve({
conversations,
total: count,
page,
pageSize,
});
}
};
cursor.onerror = () => reject(cursor.error);
});
}
// 根据会话ID获取会话
async getConversation(conversationID) {
if (!this.db) await this.openDB();
return new Promise((resolve, reject) => {
const transaction = this.db.transaction(['conversations'], 'readonly');
const store = transaction.objectStore('conversations');
const request = store.get(conversationID);
request.onerror = () => reject(request.error);
request.onsuccess = () => resolve(request.result);
});
}
// 删除会话
async deleteConversation(conversationID) {
if (!this.db) await this.openDB();
return new Promise((resolve, reject) => {
const transaction = this.db.transaction(['conversations'], 'readwrite');
const store = transaction.objectStore('conversations');
const request = store.delete(conversationID);
request.onerror = () => reject(request.error);
request.onsuccess = () => resolve();
});
}
// 标记会话为已结束
async markConversationEnded(conversationID, reason = '') {
const conversation = await this.getConversation(conversationID);
if (conversation) {
conversation.status = 1;
conversation.endReason = reason;
conversation.endTime = Date.now();
await this.saveConversation(conversation);
}
}
// 更新会话最后消息
async updateLastMessage(conversationID, message) {
const conversation = await this.getConversation(conversationID);
if (conversation) {
conversation.lastMessage = this.getMessageSummary(message);
conversation.lastMessageType = message.type;
conversation.lastMessageTime = message.time || Date.now();
// 如果会话已结束但收到新消息,重新激活
if (conversation.status === 1) {
conversation.status = 0;
conversation.endReason = '';
conversation.endTime = 0;
}
await this.saveConversation(conversation);
}
}
// 获取消息摘要
getMessageSummary(message) {
switch (message.type) {
case 'TIMTextElem':
return message.payload.text;
case 'TIMImageElem':
return '[图片]';
case 'TIMSoundElem':
return '[语音]';
case 'TIMCustomElem':
return this.parseCustomMessage(message);
default:
return '[未知消息]';
}
}
// 解析自定义消息
parseCustomMessage(message) {
try {
const data = JSON.parse(message.payload.data);
if (data.type === 'END_CONVERSATION') {
return `[会话结束: ${data.reason}]`;
}
return '[自定义消息]';
} catch {
return '[自定义消息]';
}
}
}
// 单例模式
export const conversationDB = new ConversationDB();