export class CloudQuery { className = ""; constructor(className) { this.className = className; } async getAll() { let response = await fetch( "http://1.94.237.145:1338/parsecyx/classes/" + this.className, { headers: { accept: "*/*", "accept-language": "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", "if-none-match": 'W/"f36-Q0ech+Jn3bu0uYx1n7/k1+XDYr0"', "x-parse-application-id": "cyx", }, referrer: "http://127.0.0.1:4040/", referrerPolicy: "strict-origin-when-cross-origin", body: null, method: "GET", mode: "cors", credentials: "omit", } ); let json = []; if (response) { json = await response.json(); } return json || []; } async getBy(id) { let response = await fetch( "http://1.94.237.145:1338/parsecyx/classes/" + this.className + "/" + id, { headers: { accept: "*/*", "accept-language": "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", "if-none-match": 'W/"f36-Q0ech+Jn3bu0uYx1n7/k1+XDYr0"', "x-parse-application-id": "cyx", }, referrer: "http://127.0.0.1:4040/", referrerPolicy: "strict-origin-when-cross-origin", body: null, method: "GET", mode: "cors", credentials: "omit", } ); let json = {}; if (response) { json = await response.json(); } return json || null; } } // fetch("http://1.94.237.145:1338/parsecyx/classes/_User?", { // headers: { // accept: "*/*", // "accept-language": // "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", // "if-none-match": 'W/"fa-azg+Ndlb0QPHwwjUHnRWOyZVfK8"', // "x-parse-application-id": "cyx", // "x-parse-session-token": "r:59067722e551c61a2146dc890bc4a888", // }, // referrer: "http://127.0.0.1:4040/", // referrerPolicy: "strict-origin-when-cross-origin", // body: null, // method: "GET", // mode: "cors", // credentials: "omit", // }); // user操作 export class CloudUser { constructor() {} objectId: string = ""; sessionToken: string = ""; data: Record = {}; // 获取当前用户信息 async current() { let url = "http://1.94.237.145:1338/parsecyx/users/me"; let response = await fetch(url, { method: "GET", headers: { "X-Parse-Application-Id": "cyx", "X-Parse-Session-Token": this.sessionToken, }, }); return response.json(); } // 用户登录 async login(username: string, password: string): Promise { const response = await fetch(`http://1.94.237.145:1338/parsecyx/login`, { method: "POST", headers: { "X-Parse-Application-Id": "cyx", "Content-Type": "application/json", }, body: JSON.stringify({ username, password }), }); const result = await response?.json(); if (result?.error) { console.error(result?.error); return null; } // 设置用户信息 this.objectId = result.objectId; this.sessionToken = result.sessionToken; this.data = result; // 保存用户数据 // 缓存用户信息 // localStorage.setItem("NCloud/dev/User", JSON.stringify(result)); return this; } // 用户登出 async logout() { let url = "http://1.94.237.145:1338/parsecyx/logout"; return await fetch(url, { method: "POST", headers: { "X-Parse-Application-Id": "cyx", "X-Parse-Session-Token": this.sessionToken, }, }) .then((response) => { if (!response.ok) { throw new Error("Logout failed"); } return response.json(); }) .then(() => { this.sessionToken = ""; this.objectId = ""; this.data = {}; console.log("User logged out successfully"); }) .catch((error) => { console.error("Error logging out:", error); throw error; }); } // 用户注册 async register(username, password, phone) { let url = "http://1.94.237.145:1338/parsecyx/users"; return await fetch(url, { method: "POST", headers: { "X-Parse-Application-Id": "cyx", "Content-Type": "application/json", }, body: JSON.stringify({ username, password, phone }), }) .then((response) => { if (!response.ok) { throw new Error("Registration failed"); } return response.json(); }) .then((data) => { this.objectId = data.objectId; this.sessionToken = data.sessionToken; this.data = data; }) .catch((error) => { console.error("Error registering user:", error); throw error; }); } }