|
@@ -63,8 +63,8 @@ export class CloudObject {
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
- async destroy(): Promise<boolean> {
|
|
|
- if (!this.id) return false;
|
|
|
+ async destroy(){
|
|
|
+ if (!this.id) return;
|
|
|
|
|
|
const response = await fetch(`http://1.94.237.145:1339/parse/classes/${this.className}/${this.id}`, {
|
|
|
headers: {
|
|
@@ -92,36 +92,38 @@ export class CloudObject {
|
|
|
export class CloudQuery {
|
|
|
className: string;
|
|
|
whereOptions: Record<string, any> = {};
|
|
|
-
|
|
|
constructor(className: string) {
|
|
|
this.className = className;
|
|
|
}
|
|
|
-
|
|
|
+ include(...fileds:string[]) {
|
|
|
+ this.whereOptions["include"] = fileds;
|
|
|
+ }
|
|
|
greaterThan(key: string, value: any) {
|
|
|
- if (!this.whereOptions[key]) this.whereOptions[key] = {};
|
|
|
- this.whereOptions[key]["$gt"] = value;
|
|
|
+ if (!this.whereOptions["where"][key]) this.whereOptions["where"][key] = {};
|
|
|
+ this.whereOptions["where"][key]["$gt"] = value;
|
|
|
}
|
|
|
|
|
|
greaterThanAndEqualTo(key: string, value: any) {
|
|
|
- if (!this.whereOptions[key]) this.whereOptions[key] = {};
|
|
|
- this.whereOptions[key]["$gte"] = value;
|
|
|
+ if (!this.whereOptions["where"][key]) this.whereOptions["where"][key] = {};
|
|
|
+ this.whereOptions["where"][key]["$gte"] = value;
|
|
|
}
|
|
|
|
|
|
lessThan(key: string, value: any) {
|
|
|
- if (!this.whereOptions[key]) this.whereOptions[key] = {};
|
|
|
- this.whereOptions[key]["$lt"] = value;
|
|
|
+ if (!this.whereOptions["where"][key]) this.whereOptions["where"][key] = {};
|
|
|
+ this.whereOptions["where"][key]["$lt"] = value;
|
|
|
}
|
|
|
|
|
|
lessThanAndEqualTo(key: string, value: any) {
|
|
|
- if (!this.whereOptions[key]) this.whereOptions[key] = {};
|
|
|
- this.whereOptions[key]["$lte"] = value;
|
|
|
+ if (!this.whereOptions["where"][key]) this.whereOptions["where"][key] = {};
|
|
|
+ this.whereOptions["where"][key]["$lte"] = value;
|
|
|
}
|
|
|
|
|
|
equalTo(key: string, value: any) {
|
|
|
- this.whereOptions[key] = value;
|
|
|
+ if (!this.whereOptions["where"]) this.whereOptions["where"] = {};
|
|
|
+ this.whereOptions["where"][key] = value;
|
|
|
}
|
|
|
|
|
|
- async get(id: string): Promise<Record<string, any>> {
|
|
|
+ async get(id: string) {
|
|
|
const url = `http://1.94.237.145.1339/parse/classes/${this.className}/${id}?`;
|
|
|
const response = await fetch(url, {
|
|
|
headers: {
|
|
@@ -138,16 +140,28 @@ export class CloudQuery {
|
|
|
credentials: "omit"
|
|
|
});
|
|
|
const json = await response?.json();
|
|
|
- return json || {};
|
|
|
+ if (json) {
|
|
|
+ let existsObject = this.dataToObj(json)
|
|
|
+ return existsObject;
|
|
|
+ }
|
|
|
+ return null
|
|
|
}
|
|
|
|
|
|
async find(): Promise<CloudObject[]> {
|
|
|
let url = `http://1.94.237.145:1339/parse/classes/${this.className}?`;
|
|
|
|
|
|
- if (Object.keys(this.whereOptions).length) {
|
|
|
- const whereStr = JSON.stringify(this.whereOptions);
|
|
|
- url += `where=${whereStr}`;
|
|
|
- }
|
|
|
+ let whereStr = ``
|
|
|
+ Object.keys(this.whereOptions).forEach(key=>{
|
|
|
+ let paramStr = JSON.stringify(this.whereOptions[key]);
|
|
|
+ if(key=="include"){
|
|
|
+ paramStr = this.whereOptions[key]?.join(",")
|
|
|
+ }
|
|
|
+ if(whereStr) {
|
|
|
+ url += `${key}=${paramStr}`;
|
|
|
+ }else{
|
|
|
+ url += `&${key}=${paramStr}`;
|
|
|
+ }
|
|
|
+ })
|
|
|
|
|
|
const response = await fetch(url, {
|
|
|
headers: {
|
|
@@ -170,11 +184,11 @@ export class CloudQuery {
|
|
|
return objList || [];
|
|
|
}
|
|
|
|
|
|
- async first(): Promise<CloudObject | null> {
|
|
|
+ async first() {
|
|
|
let url = `http://1.94.237.145:1339/parse/classes/${this.className}?`;
|
|
|
|
|
|
- if (Object.keys(this.whereOptions).length) {
|
|
|
- const whereStr = JSON.stringify(this.whereOptions);
|
|
|
+ if (Object.keys(this.whereOptions["where"]).length) {
|
|
|
+ const whereStr = JSON.stringify(this.whereOptions["where"]);
|
|
|
url += `where=${whereStr}`;
|
|
|
}
|
|
|
|
|
@@ -202,6 +216,8 @@ export class CloudQuery {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
dataToObj(exists:any):CloudObject{
|
|
|
let existsObject = new CloudObject(this.className);
|
|
|
existsObject.set(exists);
|
|
@@ -295,15 +311,22 @@ export class CloudUser extends CloudObject {
|
|
|
const result = await response?.json();
|
|
|
if (result?.error) {
|
|
|
console.error(result?.error);
|
|
|
+ if(result?.error=="Invalid session token"){
|
|
|
+ this.clearUserCache()
|
|
|
+ return true;
|
|
|
+ }
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
+ this.clearUserCache()
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ clearUserCache(){
|
|
|
// 清除用户信息
|
|
|
- localStorage.removeItem("NCloud/ylj/User")
|
|
|
+ localStorage.removeItem("NCloud/dev/User")
|
|
|
this.id = null;
|
|
|
this.sessionToken = null;
|
|
|
this.data = {};
|
|
|
- return true;
|
|
|
}
|
|
|
|
|
|
/** 注册 */
|
|
@@ -338,4 +361,73 @@ export class CloudUser extends CloudObject {
|
|
|
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
|
|
|
+ }
|
|
|
}
|