|
@@ -16,7 +16,7 @@ export class CloudObject {
|
|
|
|
|
|
set(json: Record<string, any>) {
|
|
|
Object.keys(json).forEach(key => {
|
|
|
- if (["objectId", "id", "createdAt", "updatedAt", "ACL"].indexOf(key) > -1) {
|
|
|
+ if (["objectId", "id", "createdAt", "updatedAt"].indexOf(key) > -1) {
|
|
|
return;
|
|
|
}
|
|
|
this.data[key] = json[key];
|
|
@@ -79,37 +79,46 @@ export class CloudObject {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
// CloudQuery.ts
|
|
|
export class CloudQuery {
|
|
|
className: string;
|
|
|
- whereOptions: Record<string, any> = {};
|
|
|
+ queryParams: Record<string, any> = {};
|
|
|
|
|
|
constructor(className: string) {
|
|
|
this.className = className;
|
|
|
}
|
|
|
|
|
|
+ include(...fileds:string[]) {
|
|
|
+ this.queryParams["include"] = fileds;
|
|
|
+ }
|
|
|
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) {
|
|
|
- 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) {
|
|
|
- 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) {
|
|
|
- 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) {
|
|
|
- this.whereOptions[key] = value;
|
|
|
+ if (!this.queryParams["where"]) this.queryParams["where"] = {};
|
|
|
+ this.queryParams["where"][key] = value;
|
|
|
}
|
|
|
|
|
|
async get(id: string) {
|
|
@@ -130,13 +139,24 @@ export class CloudQuery {
|
|
|
return json || {};
|
|
|
}
|
|
|
|
|
|
- async find() {
|
|
|
+ async find():Promise<Array<CloudObject>> {
|
|
|
let url = `https://dev.fmode.cn/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, {
|
|
|
headers: {
|
|
@@ -158,8 +178,8 @@ export class CloudQuery {
|
|
|
async first() {
|
|
|
let url = `https://dev.fmode.cn/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}`;
|
|
|
}
|
|
|
|
|
@@ -192,6 +212,14 @@ export class CloudQuery {
|
|
|
return existsObject;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
// CloudUser.ts
|
|
|
export class CloudUser extends CloudObject {
|
|
|
constructor() {
|
|
@@ -214,21 +242,21 @@ export class CloudUser extends CloudObject {
|
|
|
console.error("用户未登录");
|
|
|
return null;
|
|
|
}
|
|
|
-
|
|
|
- const response = await fetch(`https://dev.fmode.cn/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;
|
|
|
+ return this;
|
|
|
+ // const response = await fetch(`https://dev.fmode.cn/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;
|
|
|
}
|
|
|
|
|
|
/** 登录 */
|
|
@@ -287,7 +315,8 @@ export class CloudUser extends CloudObject {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
- /** 注册 */
|
|
|
+
|
|
|
+ /** 注册 */
|
|
|
async signUp(username: string, password: string, additionalData: Record<string, any> = {}) {
|
|
|
const userData = {
|
|
|
username,
|
|
@@ -311,9 +340,81 @@ export class CloudUser extends CloudObject {
|
|
|
}
|
|
|
|
|
|
// 设置用户信息
|
|
|
+ // 缓存用户信息
|
|
|
+ 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 = `https://dev.fmode.cn/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, // 添加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;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+export class CloudApi{
|
|
|
+ async fetch(path:string,body:any,options?:{
|
|
|
+ method:string
|
|
|
+ body:any
|
|
|
+ }){
|
|
|
+
|
|
|
+ let reqOpts:any = {
|
|
|
+ headers: {
|
|
|
+ "x-parse-application-id": "dev",
|
|
|
+ "Content-Type": "application/json"
|
|
|
+ },
|
|
|
+ method: options?.method || "POST",
|
|
|
+ mode: "cors",
|
|
|
+ credentials: "omit"
|
|
|
+ }
|
|
|
+ if(body||options?.body){
|
|
|
+ reqOpts.body = JSON.stringify(body || options?.body);
|
|
|
+ reqOpts.json = true;
|
|
|
+ }
|
|
|
+ let host = `https://dev.fmode.cn`
|
|
|
+ // host = `http://127.0.0.1:1337`
|
|
|
+ let url = `${host}/api/`+path
|
|
|
+ console.log(url,reqOpts)
|
|
|
+ const response = await fetch(url,reqOpts);
|
|
|
+ let json = await response.json();
|
|
|
+ return json
|
|
|
+ }
|
|
|
}
|