cyxncloud.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. export class CloudQuery {
  2. className = "";
  3. constructor(className) {
  4. this.className = className;
  5. }
  6. async getAll() {
  7. let response = await fetch(
  8. "http://1.94.237.145:1338/parsecyx/classes/" + this.className,
  9. {
  10. headers: {
  11. accept: "*/*",
  12. "accept-language":
  13. "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6,zh-TW;q=0.5,ja;q=0.4",
  14. "if-none-match": 'W/"f36-Q0ech+Jn3bu0uYx1n7/k1+XDYr0"',
  15. "x-parse-application-id": "cyx",
  16. },
  17. referrer: "http://127.0.0.1:4040/",
  18. referrerPolicy: "strict-origin-when-cross-origin",
  19. body: null,
  20. method: "GET",
  21. mode: "cors",
  22. credentials: "omit",
  23. }
  24. );
  25. let json = [];
  26. if (response) {
  27. json = await response.json();
  28. }
  29. return json || [];
  30. }
  31. async getBy(id) {
  32. let response = await fetch(
  33. "http://1.94.237.145:1338/parsecyx/classes/" + this.className + "/" + id,
  34. {
  35. headers: {
  36. accept: "*/*",
  37. "accept-language":
  38. "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6,zh-TW;q=0.5,ja;q=0.4",
  39. "if-none-match": 'W/"f36-Q0ech+Jn3bu0uYx1n7/k1+XDYr0"',
  40. "x-parse-application-id": "cyx",
  41. },
  42. referrer: "http://127.0.0.1:4040/",
  43. referrerPolicy: "strict-origin-when-cross-origin",
  44. body: null,
  45. method: "GET",
  46. mode: "cors",
  47. credentials: "omit",
  48. }
  49. );
  50. let json = {};
  51. if (response) {
  52. json = await response.json();
  53. }
  54. return json || null;
  55. }
  56. }
  57. // fetch("http://1.94.237.145:1338/parsecyx/classes/_User?", {
  58. // headers: {
  59. // accept: "*/*",
  60. // "accept-language":
  61. // "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6,zh-TW;q=0.5,ja;q=0.4",
  62. // "if-none-match": 'W/"fa-azg+Ndlb0QPHwwjUHnRWOyZVfK8"',
  63. // "x-parse-application-id": "cyx",
  64. // "x-parse-session-token": "r:59067722e551c61a2146dc890bc4a888",
  65. // },
  66. // referrer: "http://127.0.0.1:4040/",
  67. // referrerPolicy: "strict-origin-when-cross-origin",
  68. // body: null,
  69. // method: "GET",
  70. // mode: "cors",
  71. // credentials: "omit",
  72. // });
  73. // user操作
  74. export class CloudUser {
  75. constructor() {}
  76. objectId: string = "";
  77. sessionToken: string = "";
  78. data: Record<string, any> = {};
  79. // 获取当前用户信息
  80. async current() {
  81. let url = "http://1.94.237.145:1338/parsecyx/users/me";
  82. let response = await fetch(url, {
  83. method: "GET",
  84. headers: {
  85. "X-Parse-Application-Id": "cyx",
  86. "X-Parse-Session-Token": this.sessionToken,
  87. },
  88. });
  89. return response.json();
  90. }
  91. // 用户登录
  92. async login(username: string, password: string): Promise<CloudUser | null> {
  93. const response = await fetch(`http://1.94.237.145:1338/parsecyx/login`, {
  94. method: "POST",
  95. headers: {
  96. "X-Parse-Application-Id": "cyx",
  97. "Content-Type": "application/json",
  98. },
  99. body: JSON.stringify({ username, password }),
  100. });
  101. const result = await response?.json();
  102. if (result?.error) {
  103. console.error(result?.error);
  104. return null;
  105. }
  106. // 设置用户信息
  107. this.objectId = result.objectId;
  108. this.sessionToken = result.sessionToken;
  109. this.data = result; // 保存用户数据
  110. // 缓存用户信息
  111. // localStorage.setItem("NCloud/dev/User", JSON.stringify(result));
  112. return this;
  113. }
  114. // 用户登出
  115. async logout() {
  116. let url = "http://1.94.237.145:1338/parsecyx/logout";
  117. return await fetch(url, {
  118. method: "POST",
  119. headers: {
  120. "X-Parse-Application-Id": "cyx",
  121. "X-Parse-Session-Token": this.sessionToken,
  122. },
  123. })
  124. .then((response) => {
  125. if (!response.ok) {
  126. throw new Error("Logout failed");
  127. }
  128. return response.json();
  129. })
  130. .then(() => {
  131. this.sessionToken = "";
  132. this.objectId = "";
  133. this.data = {};
  134. console.log("User logged out successfully");
  135. })
  136. .catch((error) => {
  137. console.error("Error logging out:", error);
  138. throw error;
  139. });
  140. }
  141. // 用户注册
  142. async register(username, password, phone) {
  143. let url = "http://1.94.237.145:1338/parsecyx/users";
  144. return await fetch(url, {
  145. method: "POST",
  146. headers: {
  147. "X-Parse-Application-Id": "cyx",
  148. "Content-Type": "application/json",
  149. },
  150. body: JSON.stringify({ username, password, phone }),
  151. })
  152. .then((response) => {
  153. if (!response.ok) {
  154. throw new Error("Registration failed");
  155. }
  156. return response.json();
  157. })
  158. .then((data) => {
  159. this.objectId = data.objectId;
  160. this.sessionToken = data.sessionToken;
  161. this.data = data;
  162. })
  163. .catch((error) => {
  164. console.error("Error registering user:", error);
  165. throw error;
  166. });
  167. }
  168. }