|
@@ -82,34 +82,37 @@ export class CloudObject {
|
|
// CloudQuery.ts
|
|
// CloudQuery.ts
|
|
export class CloudQuery {
|
|
export class CloudQuery {
|
|
className: string;
|
|
className: string;
|
|
- whereOptions: Record<string, any> = {};
|
|
|
|
|
|
+ queryParams: Record<string, any> = {};
|
|
|
|
|
|
constructor(className: string) {
|
|
constructor(className: string) {
|
|
this.className = className;
|
|
this.className = className;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ include(...fileds:string[]) {
|
|
|
|
+ this.queryParams["include"] = fileds;
|
|
|
|
+ }
|
|
greaterThan(key: string, value: any) {
|
|
greaterThan(key: string, value: any) {
|
|
- if (!this.whereOptions[key]) this.whereOptions[key] = {};
|
|
|
|
- this.whereOptions[key]["$gt"] = value;
|
|
|
|
|
|
+ if (!this.queryParams["where"][key]) this.queryParams["where"][key] = {};
|
|
|
|
+ this.queryParams["where"][key]["$gt"] = value;
|
|
}
|
|
}
|
|
|
|
|
|
greaterThanAndEqualTo(key: string, value: any) {
|
|
greaterThanAndEqualTo(key: string, value: any) {
|
|
- if (!this.whereOptions[key]) this.whereOptions[key] = {};
|
|
|
|
- this.whereOptions[key]["$gte"] = value;
|
|
|
|
|
|
+ if (!this.queryParams["where"][key]) this.queryParams["where"][key] = {};
|
|
|
|
+ this.queryParams["where"][key]["$gte"] = value;
|
|
}
|
|
}
|
|
|
|
|
|
lessThan(key: string, value: any) {
|
|
lessThan(key: string, value: any) {
|
|
- if (!this.whereOptions[key]) this.whereOptions[key] = {};
|
|
|
|
- this.whereOptions[key]["$lt"] = value;
|
|
|
|
|
|
+ if (!this.queryParams["where"][key]) this.queryParams["where"][key] = {};
|
|
|
|
+ this.queryParams["where"][key]["$lt"] = value;
|
|
}
|
|
}
|
|
|
|
|
|
lessThanAndEqualTo(key: string, value: any) {
|
|
lessThanAndEqualTo(key: string, value: any) {
|
|
- if (!this.whereOptions[key]) this.whereOptions[key] = {};
|
|
|
|
- this.whereOptions[key]["$lte"] = value;
|
|
|
|
|
|
+ if (!this.queryParams["where"][key]) this.queryParams["where"][key] = {};
|
|
|
|
+ this.queryParams["where"][key]["$lte"] = value;
|
|
}
|
|
}
|
|
|
|
|
|
equalTo(key: string, value: any) {
|
|
equalTo(key: string, value: any) {
|
|
- this.whereOptions[key] = value;
|
|
|
|
|
|
+ this.queryParams["where"][key] = value;
|
|
}
|
|
}
|
|
|
|
|
|
async get(id: string) {
|
|
async get(id: string) {
|
|
@@ -133,10 +136,21 @@ export class CloudQuery {
|
|
async find() {
|
|
async find() {
|
|
let url = `http://dev.fmode.cn:1337/parse/classes/${this.className}?`;
|
|
let url = `http://dev.fmode.cn:1337/parse/classes/${this.className}?`;
|
|
|
|
|
|
- if (Object.keys(this.whereOptions).length) {
|
|
|
|
- const whereStr = JSON.stringify(this.whereOptions);
|
|
|
|
- url += `where=${whereStr}`;
|
|
|
|
- }
|
|
|
|
|
|
+ let queryStr = ``
|
|
|
|
+ Object.keys(this.queryParams).forEach(key=>{
|
|
|
|
+ let paramStr = JSON.stringify(this.queryParams[key]);
|
|
|
|
+ if(key=="include"){
|
|
|
|
+ paramStr = this.queryParams[key]?.join(",")
|
|
|
|
+ }
|
|
|
|
+ if(queryStr) {
|
|
|
|
+ url += `${key}=${paramStr}`;
|
|
|
|
+ }else{
|
|
|
|
+ url += `&${key}=${paramStr}`;
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ // if (Object.keys(this.queryParams["where"]).length) {
|
|
|
|
+
|
|
|
|
+ // }
|
|
|
|
|
|
const response = await fetch(url, {
|
|
const response = await fetch(url, {
|
|
headers: {
|
|
headers: {
|
|
@@ -158,8 +172,8 @@ export class CloudQuery {
|
|
async first() {
|
|
async first() {
|
|
let url = `http://dev.fmode.cn:1337/parse/classes/${this.className}?`;
|
|
let url = `http://dev.fmode.cn:1337/parse/classes/${this.className}?`;
|
|
|
|
|
|
- if (Object.keys(this.whereOptions).length) {
|
|
|
|
- const whereStr = JSON.stringify(this.whereOptions);
|
|
|
|
|
|
+ if (Object.keys(this.queryParams["where"]).length) {
|
|
|
|
+ const whereStr = JSON.stringify(this.queryParams["where"]);
|
|
url += `where=${whereStr}`;
|
|
url += `where=${whereStr}`;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -191,4 +205,133 @@ export class CloudQuery {
|
|
existsObject.updatedAt = exists.updatedAt;
|
|
existsObject.updatedAt = exists.updatedAt;
|
|
return existsObject;
|
|
return existsObject;
|
|
}
|
|
}
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// CloudUser.ts
|
|
|
|
+export class CloudUser extends CloudObject {
|
|
|
|
+ constructor() {
|
|
|
|
+ super("_User"); // 假设用户类在Parse中是"_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;
|
|
|
|
+ // const response = await fetch(`http://dev.fmode.cn:1337/parse/users/me`, {
|
|
|
|
+ // headers: {
|
|
|
|
+ // "x-parse-application-id": "dev",
|
|
|
|
+ // "x-parse-session-token": this.sessionToken // 使用sessionToken进行身份验证
|
|
|
|
+ // },
|
|
|
|
+ // method: "GET"
|
|
|
|
+ // });
|
|
|
|
+
|
|
|
|
+ // const result = await response?.json();
|
|
|
|
+ // if (result?.error) {
|
|
|
|
+ // console.error(result?.error);
|
|
|
|
+ // return null;
|
|
|
|
+ // }
|
|
|
|
+ // return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /** 登录 */
|
|
|
|
+ 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;
|
|
|
|
+ }
|
|
}
|
|
}
|