// 消息实体类 class Message implements JsonSerializable { private fields: string[] = ['ver', 'header', 'payload', 'timestamp', 'token']; private ver: string = '1.0.0'; private header!: Header; private payload?: Payload; private timestamp: number = Date.now(); private token: string = ''; constructor() { this.header = new Header(); this.payload = new Payload(); } // // 序列化接口实现 // jsonSerialize(): object { // const data: Record = {}; // this.fields.forEach(key => { // const value = (this as any)[key]; // if (value !== undefined && value !== null) { // data[key] = value instanceof JsonSerializable // ? value.jsonSerialize() // : value; // } // }); // return data; // } // 反序列化:JSON → Message static fromJson(json: string): Message { try { const data = JSON.parse(json); return Message.fromObject(data); } catch (e) { throw new Error('Invalid JSON structure'); } } // 反序列化:对象 → Message static fromObject(data: Record): Message { const message = new Message(); message.fields.forEach(key => { if (data[key] !== undefined && data[key] !== null) { if (key === 'header') { message.header = Header.fromObject(data.header); } else if (key === 'payload') { message.payload = Payload.fromObject(data.payload); } else { (message as any)[key] = data[key]; } } }); return message; } // Getter/Setter 方法 getVer(): string { return this.ver; } setVer(value: string): void { this.ver = value; } getHeader(): Header { return this.header; } setHeader(value: Header): void { this.header = value; } getPayload(): Payload | undefined { return this.payload; } setPayload(value: Payload): void { this.payload = value; } getTimestamp(): number { return this.timestamp; } setTimestamp(value: number): void { this.timestamp = value; } getToken(): string { return this.token; } setToken(value: string): void { this.token = value; } } // 消息头信息 class Header implements JsonSerializable { private fields: string[] = ['id', 'frame', 'type', 'target', 'from', 'to']; private id: string = ''; private frame: string = ''; private type: string = ''; private target: string = ''; private from: any = null; private to: any = null; constructor() { this.id = 'msg_' + this.generateShortId(); } // 生成短ID(替代PHP的uniqid+md5) private generateShortId(): string { return Math.random().toString(36).substring(2, 10) + Date.now().toString(36).substring(4, 8); } jsonSerialize(): object { const data: Record = {}; this.fields.forEach(key => { const value = (this as any)[key]; if (value !== undefined && value !== null) { data[key] = value; } }); return data; } static fromJson(json: string): Header { try { const data = JSON.parse(json); return Header.fromObject(data); } catch (e) { throw new Error('Invalid JSON structure'); } } static fromObject(data: Record): Header { const header = new Header(); header.fields.forEach(key => { if (data[key] !== undefined && data[key] !== null) { (header as any)[key] = data[key]; } }); return header; } // Getter/Setter 方法 getId(): string { return this.id; } setId(value: string): void { this.id = value; } getFrame(): string { return this.frame; } setFrame(value: string): void { this.frame = value; } getType(): string { return this.type; } setType(value: string): void { this.type = value; } getTarget(): string { return this.target; } setTarget(value: string): void { this.target = value; } getFrom(): any { return this.from; } setFrom(value: any): void { this.from = value; } getTo(): any { return this.to; } setTo(value: any): void { this.to = value; } } // 消息扩展信息 class Payload implements JsonSerializable { private fields: string[] = ['name', 'content', 'url', 'width', 'height', 'duration', 'size', 'reply', 'mention']; private name: string = ''; private content: any = null; private url: string = ''; private width: number = 0; private height: number = 0; private duration: number = 0; private size: number = 0; private reply: string = ''; private mention: any[] = []; jsonSerialize(): object { const data: Record = {}; this.fields.forEach(key => { const value = (this as any)[key]; if (value !== undefined && value !== null) { data[key] = value; } }); return data; } static fromJson(json: string): Payload { try { const data = JSON.parse(json); return Payload.fromObject(data); } catch (e) { throw new Error('Invalid JSON structure'); } } static fromObject(data: Record): Payload { const payload = new Payload(); payload.fields.forEach(key => { if (data[key] !== undefined && data[key] !== null) { (payload as any)[key] = data[key]; } }); return payload; } // Getter/Setter 方法 getName(): string { return this.name; } setName(value: string): void { this.name = value; } getContent(): any { return this.content; } setContent(value: any): void { this.content = value; } getUrl(): string { return this.url; } setUrl(value: string): void { this.url = value; } getWidth(): number { return this.width; } setWidth(value: number): void { this.width = value; } getHeight(): number { return this.height; } setHeight(value: number): void { this.height = value; } getDuration(): number { return this.duration; } setDuration(value: number): void { this.duration = value; } getSize(): number { return this.size; } setSize(value: number): void { this.size = value; } getReply(): string { return this.reply; } setReply(value: string): void { this.reply = value; } getMention(): any[] { return this.mention; } setMention(value: any[]): void { this.mention = value; } } // 序列化接口 interface JsonSerializable { jsonSerialize(): object; } //import {Message, Header, Payload} from "@/utlis/messageStruct.uts" // 使用示例 const exampleUsage = () => { // 创建消息对象 const message = new Message(); message.setToken('abc123xyz'); // 设置头部信息 const header = message.getHeader(); header.setFrame('msg'); header.setType('chat'); header.setFrom('user123'); header.setTo('user456'); // 设置负载信息 const payload = message.getPayload() || new Payload(); payload.setName('image.jpg'); payload.setUrl('https://example.com/images/image.jpg'); payload.setWidth(800); payload.setHeight(600); payload.setSize(102400); payload.setMention(['user789']); message.setPayload(payload); // 序列化为JSON const jsonStr = JSON.stringify(message.jsonSerialize()); console.log('Serialized JSON:', jsonStr); // 反序列化 try { const parsed = Message.fromJson(jsonStr); console.log('Deserialized message:', parsed); const headerObj = parsed.getHeader(); console.log('Header type:', headerObj.getType()); const payloadObj = parsed.getPayload(); if (payloadObj) { console.log('Payload URL:', payloadObj.getUrl()); } } catch (e) { console.error('Deserialization failed:', e); } }; // 导出类 export { Message, Header, Payload };