|
@@ -416,3 +416,171 @@ export class CloudQuery {
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+export class CloudUser extends CloudObject {
|
|
|
+ constructor() {
|
|
|
+ super("_User");
|
|
|
+
|
|
|
+ let userCacheStr = localStorage.getItem("NCloud/dev/User")
|
|
|
+ if (userCacheStr) {
|
|
|
+ let userData = JSON.parse(userCacheStr)
|
|
|
+
|
|
|
+ this.id = userData?.objectId;
|
|
|
+ this.sessionToken = userData?.sessionToken;
|
|
|
+ this.data = userData;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ sessionToken: string | null = ""
|
|
|
+
|
|
|
+ async current() {
|
|
|
+ if (!this.sessionToken) {
|
|
|
+ console.error("用户未登录");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return this;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ async login(username: string, password: string): Promise<CloudUser | null> {
|
|
|
+ const response = await fetch(`http://dev.fmode.cn:1337/parse/login`, {
|
|
|
+ headers: {
|
|
|
+ "x-parse-application-id": "dev",
|
|
|
+ "Content-Type": "application/json"
|
|
|
+ },
|
|
|
+ body: JSON.stringify({ username, password }),
|
|
|
+ method: "POST"
|
|
|
+ });
|
|
|
+
|
|
|
+ const result = await response?.json();
|
|
|
+ if (result?.error) {
|
|
|
+ console.error(result?.error);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ this.id = result?.objectId;
|
|
|
+ this.sessionToken = result?.sessionToken;
|
|
|
+ this.data = result;
|
|
|
+
|
|
|
+ console.log(result)
|
|
|
+ localStorage.setItem("NCloud/dev/User", JSON.stringify(result))
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ async logout() {
|
|
|
+ if (!this.sessionToken) {
|
|
|
+ console.error("用户未登录");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ const response = await fetch(`http://dev.fmode.cn:1337/parse/logout`, {
|
|
|
+ headers: {
|
|
|
+ "x-parse-application-id": "dev",
|
|
|
+ "x-parse-session-token": this.sessionToken
|
|
|
+ },
|
|
|
+ method: "POST"
|
|
|
+ });
|
|
|
+
|
|
|
+ const result = await response?.json();
|
|
|
+ if (result?.error) {
|
|
|
+ console.error(result?.error);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ localStorage.removeItem("NCloud/dev/User")
|
|
|
+ this.id = null;
|
|
|
+ this.sessionToken = null;
|
|
|
+ this.data = {};
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ async signUp(username: string, password: string, additionalData: Record<string, any> = {}) {
|
|
|
+ const userData = {
|
|
|
+ username,
|
|
|
+ password,
|
|
|
+ ...additionalData
|
|
|
+ };
|
|
|
+
|
|
|
+ const response = await fetch(`http://dev.fmode.cn:1337/parse/users`, {
|
|
|
+ headers: {
|
|
|
+ "x-parse-application-id": "dev",
|
|
|
+ "Content-Type": "application/json"
|
|
|
+ },
|
|
|
+ body: JSON.stringify(userData),
|
|
|
+ method: "POST"
|
|
|
+ });
|
|
|
+
|
|
|
+ const result = await response?.json();
|
|
|
+ if (result?.error) {
|
|
|
+ console.error(result?.error);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ console.log(result)
|
|
|
+ localStorage.setItem("NCloud/dev/User", JSON.stringify(result))
|
|
|
+ this.id = result?.objectId;
|
|
|
+ this.sessionToken = result?.sessionToken;
|
|
|
+ this.data = result;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ override async save() {
|
|
|
+ let method = "POST";
|
|
|
+ let url = `http://dev.fmode.cn:1337/parse/users`;
|
|
|
+
|
|
|
+
|
|
|
+ if (this.id) {
|
|
|
+ url += `/${this.id}`;
|
|
|
+ method = "PUT";
|
|
|
+ }
|
|
|
+
|
|
|
+ let data: any = JSON.parse(JSON.stringify(this.data))
|
|
|
+ delete data.createdAt
|
|
|
+ delete data.updatedAt
|
|
|
+ delete data.ACL
|
|
|
+ delete data.objectId
|
|
|
+ const body = JSON.stringify(data);
|
|
|
+ let headersOptions: any = {
|
|
|
+ "content-type": "application/json;charset=UTF-8",
|
|
|
+ "x-parse-application-id": "dev",
|
|
|
+ "x-parse-session-token": this.sessionToken,
|
|
|
+ }
|
|
|
+ const response = await fetch(url, {
|
|
|
+ headers: headersOptions,
|
|
|
+ body: body,
|
|
|
+ method: method,
|
|
|
+ mode: "cors",
|
|
|
+ credentials: "omit"
|
|
|
+ });
|
|
|
+
|
|
|
+ const result = await response?.json();
|
|
|
+ if (result?.error) {
|
|
|
+ console.error(result?.error);
|
|
|
+ }
|
|
|
+ if (result?.objectId) {
|
|
|
+ this.id = result?.objectId;
|
|
|
+ }
|
|
|
+ localStorage.setItem("NCloud/dev/User", JSON.stringify(this.data))
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+}
|