123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- 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<string, any> = {};
- // 获取当前用户信息
- 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<CloudUser | null> {
- 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;
- });
- }
- }
|