class CloudObject { id: string ; className: string; data: Record = {}; createdAt: string | null = null; // 添加 createdAt 属性 updatedAt: string | null = null; // 添加 updatedAt 属性 constructor(className: string) { this.className = className; } toPointer(): { __type: string, className: string, objectId: string } { return { "__type": "Pointer", "className": this.className, "objectId": this.id }; } set(json: Record): void { Object.keys(json).forEach(key => { if (["objectId", "id", "createdAt", "updatedAt", "ACL"].indexOf(key) > -1) { return; } this.data[key] = json[key]; }); } get(key: string): any { return this.data[key] || null; } async save(): Promise { let method: string = "POST"; let url: string = `http://dev.fmode.cn:1337/parse/classes/${this.className}`; // 更新 if (this.id) { url += `/${this.id}`; method = "PUT"; } let body: string = JSON.stringify(this.data); let response: Response = await fetch(url, { "headers": { "content-type": "application/json;charset=UTF-8", "x-parse-application-id": "dev" }, "body": body, "method": method, "mode": "cors", "credentials": "omit" }); let result: any = await response.json(); if (result.error) { console.error(result.error); } if (result.objectId) { this.id = result.objectId; } return this; } async destroy(): Promise { if (!this.id) return false; let response: Response = await fetch(`http://dev.fmode.cn:1337/parse/classes/Doctor/${this.id}`, { "headers": { "x-parse-application-id": "dev" }, "body": null, "method": "DELETE", "mode": "cors", "credentials": "omit" }); let result: any = await response.json(); if (result) { // this.id = null; } return true; } } class CloudQuery { className: string; whereOptions: Record = {}; constructor(className: string) { this.className = className; } greaterThan(key: string, value: any): void { if (!this.whereOptions[key]) this.whereOptions[key] = {}; this.whereOptions[key]["$gt"] = value; } greaterThanAndEqualTo(key: string, value: any): void { if (!this.whereOptions[key]) this.whereOptions[key] = {}; this.whereOptions[key]["$gte"] = value; } lessThan(key: string, value: any): void { if (!this.whereOptions[key]) this.whereOptions[key] = {}; this.whereOptions[key]["$lt"] = value; } lessThanAndEqualTo(key: string, value: any): void { if (!this.whereOptions[key]) this.whereOptions[key] = {}; this.whereOptions[key]["$lte"] = value; } equalTo(key: string, value: any): void { this.whereOptions[key] = value; } async get(id: string): Promise> { let url: string = `http://dev.fmode.cn:1337/parse/classes/${this.className}/${id}?`; let response: Response = await fetch(url, { "headers": { "if-none-match": "W/\"1f0-ghxH2EwTk6Blz0g89ivf2adBDKY\"", "x-parse-application-id": "dev" }, "body": null, "method": "GET", "mode": "cors", "credentials": "omit" }); let json: any = await response.json(); return json || {}; } async fetchData(url: string): Promise { try { let response: Response = await fetch(url, { "headers": { "if-none-match": "W/\"1f0-ghxH2EwTk6Blz0g89ivf2adBDKY\"", "x-parse-application-id": "dev" }, "body": null, "method": "GET", "mode": "cors", "credentials": "omit" }); return await response.json(); } catch (error) { console.error("Fetch error:", error); return null; } } async find(): Promise[]> { let url: string = `http://dev.fmode.cn:1337/parse/classes/${this.className}?`; if (Object.keys(this.whereOptions).length) { let whereStr: string = JSON.stringify(this.whereOptions); url += `where=${whereStr}`; } let json: any = await this.fetchData(url); return json?.results || []; } async first(): Promise { let url: string = `http://dev.fmode.cn:1337/parse/classes/${this.className}?`; if (Object.keys(this.whereOptions).length) { let whereStr: string = JSON.stringify(this.whereOptions); url += `where=${whereStr}`; } let json: any = await this.fetchData(url); let exists: Record | null = json?.results?.[0] || null; if (exists) { let existsObject: CloudObject = new CloudObject(this.className); existsObject.set(exists); existsObject.id = exists.objectId; existsObject.createdAt = exists.createdAt; existsObject.updatedAt = exists.updatedAt; return existsObject; } return null; } } export { CloudObject, CloudQuery };