class CloudObject {.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. class CloudObject {
  2. id: string ;
  3. className: string;
  4. data: Record<string, any> = {};
  5. createdAt: string | null = null; // 添加 createdAt 属性
  6. updatedAt: string | null = null; // 添加 updatedAt 属性
  7. constructor(className: string) {
  8. this.className = className;
  9. }
  10. toPointer(): { __type: string, className: string, objectId: string } {
  11. return { "__type": "Pointer", "className": this.className, "objectId": this.id };
  12. }
  13. set(json: Record<string, any>): void {
  14. Object.keys(json).forEach(key => {
  15. if (["objectId", "id", "createdAt", "updatedAt", "ACL"].indexOf(key) > -1) {
  16. return;
  17. }
  18. this.data[key] = json[key];
  19. });
  20. }
  21. get(key: string): any {
  22. return this.data[key] || null;
  23. }
  24. async save(): Promise<this> {
  25. let method: string = "POST";
  26. let url: string = `http://dev.fmode.cn:1337/parse/classes/${this.className}`;
  27. // 更新
  28. if (this.id) {
  29. url += `/${this.id}`;
  30. method = "PUT";
  31. }
  32. let body: string = JSON.stringify(this.data);
  33. let response: Response = await fetch(url, {
  34. "headers": {
  35. "content-type": "application/json;charset=UTF-8",
  36. "x-parse-application-id": "dev"
  37. },
  38. "body": body,
  39. "method": method,
  40. "mode": "cors",
  41. "credentials": "omit"
  42. });
  43. let result: any = await response.json();
  44. if (result.error) {
  45. console.error(result.error);
  46. }
  47. if (result.objectId) {
  48. this.id = result.objectId;
  49. }
  50. return this;
  51. }
  52. async destroy(): Promise<boolean> {
  53. if (!this.id) return false;
  54. let response: Response = await fetch(`http://dev.fmode.cn:1337/parse/classes/Doctor/${this.id}`, {
  55. "headers": {
  56. "x-parse-application-id": "dev"
  57. },
  58. "body": null,
  59. "method": "DELETE",
  60. "mode": "cors",
  61. "credentials": "omit"
  62. });
  63. let result: any = await response.json();
  64. if (result) {
  65. // this.id = null;
  66. }
  67. return true;
  68. }
  69. }
  70. class CloudQuery {
  71. className: string;
  72. whereOptions: Record<string, any> = {};
  73. constructor(className: string) {
  74. this.className = className;
  75. }
  76. greaterThan(key: string, value: any): void {
  77. if (!this.whereOptions[key]) this.whereOptions[key] = {};
  78. this.whereOptions[key]["$gt"] = value;
  79. }
  80. greaterThanAndEqualTo(key: string, value: any): void {
  81. if (!this.whereOptions[key]) this.whereOptions[key] = {};
  82. this.whereOptions[key]["$gte"] = value;
  83. }
  84. lessThan(key: string, value: any): void {
  85. if (!this.whereOptions[key]) this.whereOptions[key] = {};
  86. this.whereOptions[key]["$lt"] = value;
  87. }
  88. lessThanAndEqualTo(key: string, value: any): void {
  89. if (!this.whereOptions[key]) this.whereOptions[key] = {};
  90. this.whereOptions[key]["$lte"] = value;
  91. }
  92. equalTo(key: string, value: any): void {
  93. this.whereOptions[key] = value;
  94. }
  95. async get(id: string): Promise<Record<string, any>> {
  96. let url: string = `http://dev.fmode.cn:1337/parse/classes/${this.className}/${id}?`;
  97. let response: Response = await fetch(url, {
  98. "headers": {
  99. "if-none-match": "W/\"1f0-ghxH2EwTk6Blz0g89ivf2adBDKY\"",
  100. "x-parse-application-id": "dev"
  101. },
  102. "body": null,
  103. "method": "GET",
  104. "mode": "cors",
  105. "credentials": "omit"
  106. });
  107. let json: any = await response.json();
  108. return json || {};
  109. }
  110. async fetchData(url: string): Promise<any> {
  111. try {
  112. let response: Response = await fetch(url, {
  113. "headers": {
  114. "if-none-match": "W/\"1f0-ghxH2EwTk6Blz0g89ivf2adBDKY\"",
  115. "x-parse-application-id": "dev"
  116. },
  117. "body": null,
  118. "method": "GET",
  119. "mode": "cors",
  120. "credentials": "omit"
  121. });
  122. return await response.json();
  123. } catch (error) {
  124. console.error("Fetch error:", error);
  125. return null;
  126. }
  127. }
  128. async find(): Promise<Record<string, any>[]> {
  129. let url: string = `http://dev.fmode.cn:1337/parse/classes/${this.className}?`;
  130. if (Object.keys(this.whereOptions).length) {
  131. let whereStr: string = JSON.stringify(this.whereOptions);
  132. url += `where=${whereStr}`;
  133. }
  134. let json: any = await this.fetchData(url);
  135. return json?.results || [];
  136. }
  137. async first(): Promise<CloudObject | null> {
  138. let url: string = `http://dev.fmode.cn:1337/parse/classes/${this.className}?`;
  139. if (Object.keys(this.whereOptions).length) {
  140. let whereStr: string = JSON.stringify(this.whereOptions);
  141. url += `where=${whereStr}`;
  142. }
  143. let json: any = await this.fetchData(url);
  144. let exists: Record<string, any> | null = json?.results?.[0] || null;
  145. if (exists) {
  146. let existsObject: CloudObject = new CloudObject(this.className);
  147. existsObject.set(exists);
  148. existsObject.id = exists.objectId;
  149. existsObject.createdAt = exists.createdAt;
  150. existsObject.updatedAt = exists.updatedAt;
  151. return existsObject;
  152. }
  153. return null;
  154. }
  155. }
  156. export { CloudObject, CloudQuery };