增加聊天记录的存储和读取

This commit is contained in:
chen wei bo
2025-09-10 19:15:43 +08:00
parent 0cbb163740
commit 37bd1b0449
7 changed files with 158 additions and 3 deletions
@@ -315,6 +315,7 @@ export class ChatAIService {
const reqData = {
girlId,
ChatMsg: msgList,
msgId: new Date().getTime(),
};
console.log("请求上报聊天数据的请求数据:", reqData);
let res = await ChatService.I.reqChatMsg(reqData);
+84 -1
View File
@@ -11,24 +11,33 @@ export class GirlChatData extends BaseData {
private chatTotalCount: number;
// 剩余聊天次数, < 0: 不限制; = 0 聊天次数已经用完; > 0 还剩余多少次聊天次数
private chatRemainCount: number;
// 聊天记录
private chatRecord: Map<string, proto.cs.IChatMsg>;
private chatRecordIds: number[];
constructor() {
super();
this.id = 0;
this.chatTotalCount = 0;
this.chatRemainCount = 0;
this.chatRecord = new Map<string, proto.cs.IChatMsg>(),
this.chatRecordIds = [];
}
public reset(): void {
this.id = 0;
this.chatTotalCount = 0;
this.chatRemainCount = 0;
this.chatRecord.clear();
this.chatRecordIds = [];
}
public clear(): void {
this.id = 0;
this.chatTotalCount = 0;
this.chatRemainCount = 0;
this.chatRecord.clear();
this.chatRecordIds = [];
}
public destroy(): void {
@@ -36,6 +45,8 @@ export class GirlChatData extends BaseData {
this.id = null;
this.chatTotalCount = null;
this.chatRemainCount = null;
this.chatRecord = null;
this.chatRecordIds = null;
}
/** 获取 id */
@@ -75,5 +86,77 @@ export class GirlChatData extends BaseData {
if (this.chatRemainCount > 0) {
this.setChatRemainCount(Math.max(0, this.chatRemainCount - times));
}
}
}
// 解析聊天记录数据,一个
private parseOneChatRecord(data: proto.cs.IChatMsg): proto.cs.IChatMsg | null {
if (!data) return null;
const oneData: proto.cs.IChatMsg = {
msgId: data.msgId, // id,使用时间戳(精确到毫秒)
msg: data.msg, // 聊天内容
isAi: data.isAi, // 是否是 ai 聊天数据
};
return oneData;
}
// 解析聊天记录数据,多个
private parseAllChatRecord(data: proto.cs.IChatMsg[]): proto.cs.IChatMsg[] {
if (!data) return [];
let allData = [];
for (const value of data) {
const oneData: proto.cs.IChatMsg = this.parseOneChatRecord(value);
if (oneData) {
allData.push(oneData);
}
}
return allData;
}
/** 保存聊天记录 */
public setChatRecord(data: proto.cs.IChatMsg[]): void {
// 如果传入的记录为空,直接返回
if (!data || data.length === 0) return;
// 解析数据
let allData = this.parseAllChatRecord(data);
if (!allData) return;
// 使用 Map 来进行去重,key 为 msgId
const existingMsgIds = new Set(this.chatRecordIds);
// 过滤掉已经存在的消息(通过 msgId)
const newChatRecords = allData.filter(msg => !existingMsgIds.has(msg.msgId));
// 将新的聊天记录添加到 Map 中
newChatRecords.forEach(msg => {
this.chatRecord.set(msg.msgId.toString(), msg);
this.chatRecordIds.push(Number(msg.msgId));
});
// 排序:根据 msgId 从大到小排序
this.chatRecordIds.sort((a, b) => b - a);
console.log("当前技师 ", this.id, " ,聊天记录:", this.chatRecord);
}
/** 获取聊天记录的所有 id */
public getChatRecordIds(): number[] {
return this.chatRecordIds;
}
/** 获取聊天记录,根据 id */
private getChatRecordById(id: number): proto.cs.IChatMsg | null {
return this.chatRecord.get(id.toString()) ?? null;
}
/** 获取聊天记录,根据 id,是否是 ai 聊天数据 */
public getChatRecordIsAi(id: number): boolean {
const chatRecord = this.getChatRecordById(id);
return chatRecord ? chatRecord.isAi : false;
}
/** 获取聊天记录,根据 id,聊天内容 */
public getChatRecordMsg(id: number): string {
const chatRecord = this.getChatRecordById(id);
return chatRecord ? chatRecord.msg : "";
}
}
+28
View File
@@ -809,7 +809,35 @@ export class GirlData extends BaseData {
return true;
}
return false;
}
/** 保存聊天记录 */
public setChatRecord(categoryId: string, girlId: number, data: proto.cs.IChatMsg[]): void {
let oneData = this.getGrilChat(categoryId, girlId);
if (!oneData) return;
oneData.setChatRecord(data);
}
/** 获取聊天记录的所有 id */
public getChatRecordIds(categoryId: string, girlId: number): number[] {
let oneData = this.getGrilChat(categoryId, girlId);
if (!oneData) return [];
return oneData.getChatRecordIds();
}
/** 获取聊天记录,根据 id,是否是 ai 聊天数据 */
public getChatRecordIsAi(categoryId: string, girlId: number, id: number): boolean {
let oneData = this.getGrilChat(categoryId, girlId);
if (!oneData) return false;
return oneData.getChatRecordIsAi(id);
}
/** 获取聊天记录,根据 id,聊天内容 */
public getChatRecordMsg(categoryId: string, girlId: number, id: number): string {
let oneData = this.getGrilChat(categoryId, girlId);
if (!oneData) return "";
return oneData.getChatRecordMsg(id);
}
/** --------------------------------------------------------- 技师的好感度数据 --------------------------------------------------------- */
+6
View File
@@ -2080,6 +2080,9 @@ static getTypeUrl(typeUrlPrefix?: string): string;
/** ChatMsg isAi */
isAi?: (boolean|null);
/** ChatMsg msgId */
msgId?: (number|Long|null);
}
/** Represents a ChatMsg. */
@@ -2097,6 +2100,9 @@ msg: string;
/** ChatMsg isAi. */
isAi: boolean;
/** ChatMsg msgId. */
msgId: (number|Long);
/**
* Creates a new ChatMsg instance using the specified properties.
* @param [properties] Properties to set
+37
View File
@@ -4818,6 +4818,7 @@ $root.cs = (function() {
* @interface IChatMsg
* @property {string|null} [msg] ChatMsg msg
* @property {boolean|null} [isAi] ChatMsg isAi
* @property {number|Long|null} [msgId] ChatMsg msgId
*/
/**
@@ -4851,6 +4852,14 @@ $root.cs = (function() {
*/
ChatMsg.prototype.isAi = false;
/**
* ChatMsg msgId.
* @member {number|Long} msgId
* @memberof cs.ChatMsg
* @instance
*/
ChatMsg.prototype.msgId = $util.Long ? $util.Long.fromBits(0,0,false) : 0;
/**
* Creates a new ChatMsg instance using the specified properties.
* @function create
@@ -4879,6 +4888,8 @@ $root.cs = (function() {
writer.uint32(/* id 1, wireType 2 =*/10).string(message.msg);
if (message.isAi != null && Object.hasOwnProperty.call(message, "isAi"))
writer.uint32(/* id 2, wireType 0 =*/16).bool(message.isAi);
if (message.msgId != null && Object.hasOwnProperty.call(message, "msgId"))
writer.uint32(/* id 3, wireType 0 =*/24).int64(message.msgId);
return writer;
};
@@ -4923,6 +4934,10 @@ $root.cs = (function() {
message.isAi = reader.bool();
break;
}
case 3: {
message.msgId = reader.int64();
break;
}
default:
reader.skipType(tag & 7);
break;
@@ -4964,6 +4979,9 @@ $root.cs = (function() {
if (message.isAi != null && message.hasOwnProperty("isAi"))
if (typeof message.isAi !== "boolean")
return "isAi: boolean expected";
if (message.msgId != null && message.hasOwnProperty("msgId"))
if (!$util.isInteger(message.msgId) && !(message.msgId && $util.isInteger(message.msgId.low) && $util.isInteger(message.msgId.high)))
return "msgId: integer|Long expected";
return null;
};
@@ -4983,6 +5001,15 @@ $root.cs = (function() {
message.msg = String(object.msg);
if (object.isAi != null)
message.isAi = Boolean(object.isAi);
if (object.msgId != null)
if ($util.Long)
(message.msgId = $util.Long.fromValue(object.msgId)).unsigned = false;
else if (typeof object.msgId === "string")
message.msgId = parseInt(object.msgId, 10);
else if (typeof object.msgId === "number")
message.msgId = object.msgId;
else if (typeof object.msgId === "object")
message.msgId = new $util.LongBits(object.msgId.low >>> 0, object.msgId.high >>> 0).toNumber();
return message;
};
@@ -5002,11 +5029,21 @@ $root.cs = (function() {
if (options.defaults) {
object.msg = "";
object.isAi = false;
if ($util.Long) {
var long = new $util.Long(0, 0, false);
object.msgId = options.longs === String ? long.toString() : options.longs === Number ? long.toNumber() : long;
} else
object.msgId = options.longs === String ? "0" : 0;
}
if (message.msg != null && message.hasOwnProperty("msg"))
object.msg = message.msg;
if (message.isAi != null && message.hasOwnProperty("isAi"))
object.isAi = message.isAi;
if (message.msgId != null && message.hasOwnProperty("msgId"))
if (typeof message.msgId === "number")
object.msgId = options.longs === String ? String(message.msgId) : message.msgId;
else
object.msgId = options.longs === String ? $util.Long.prototype.toString.call(message.msgId) : options.longs === Number ? new $util.LongBits(message.msgId.low >>> 0, message.msgId.high >>> 0).toNumber() : message.msgId;
return object;
};