|
|
@@ -1,9 +1,11 @@
|
|
|
+import * as Parse from 'parse';
|
|
|
+
|
|
|
// CloudObject.ts
|
|
|
export class CloudObject {
|
|
|
className: string;
|
|
|
id: string | null = null;
|
|
|
- createdAt:any;
|
|
|
- updatedAt:any;
|
|
|
+ createdAt: any;
|
|
|
+ updatedAt: any;
|
|
|
data: Record<string, any> = {};
|
|
|
|
|
|
constructor(className: string) {
|
|
|
@@ -16,7 +18,8 @@ export class CloudObject {
|
|
|
|
|
|
set(json: Record<string, any>) {
|
|
|
Object.keys(json).forEach(key => {
|
|
|
- if (["objectId", "id", "createdAt", "updatedAt"].indexOf(key) > -1) {
|
|
|
+ if (["objectId", "id", "createdAt", "updatedAt", "ACL"].indexOf(key) > -1) {
|
|
|
+ if (key === "objectId") this.id = json[key];
|
|
|
return;
|
|
|
}
|
|
|
this.data[key] = json[key];
|
|
|
@@ -31,7 +34,6 @@ export class CloudObject {
|
|
|
let method = "POST";
|
|
|
let url = `https://dev.fmode.cn/parse/classes/${this.className}`;
|
|
|
|
|
|
- // 更新
|
|
|
if (this.id) {
|
|
|
url += `/${this.id}`;
|
|
|
method = "PUT";
|
|
|
@@ -51,7 +53,7 @@ export class CloudObject {
|
|
|
|
|
|
const result = await response?.json();
|
|
|
if (result?.error) {
|
|
|
- console.error(result?.error);
|
|
|
+ throw new Error(result.error);
|
|
|
}
|
|
|
if (result?.objectId) {
|
|
|
this.id = result?.objectId;
|
|
|
@@ -65,29 +67,33 @@ export class CloudObject {
|
|
|
headers: {
|
|
|
"x-parse-application-id": "dev"
|
|
|
},
|
|
|
- body: null,
|
|
|
method: "DELETE",
|
|
|
mode: "cors",
|
|
|
credentials: "omit"
|
|
|
});
|
|
|
|
|
|
const result = await response?.json();
|
|
|
- if (result) {
|
|
|
- this.id = null;
|
|
|
+ if (result?.error) {
|
|
|
+ throw new Error(result.error);
|
|
|
}
|
|
|
+ this.id = null;
|
|
|
return true;
|
|
|
}
|
|
|
+
|
|
|
+ protected setParseObject(obj: Parse.Object) {
|
|
|
+ this.parseObject = obj;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-// CloudQuery.ts
|
|
|
export class CloudQuery {
|
|
|
+ private query: Parse.Query;
|
|
|
className: string;
|
|
|
queryParams: Record<string, any> = {};
|
|
|
|
|
|
constructor(className: string) {
|
|
|
this.className = className;
|
|
|
}
|
|
|
-
|
|
|
+ // 作用是将查询参数转换为对象
|
|
|
include(...fileds:string[]) {
|
|
|
this.queryParams["include"] = fileds;
|
|
|
}
|
|
|
@@ -112,12 +118,11 @@ export class CloudQuery {
|
|
|
}
|
|
|
|
|
|
equalTo(key: string, value: any) {
|
|
|
- if (!this.queryParams["where"]) this.queryParams["where"] = {};
|
|
|
this.queryParams["where"][key] = value;
|
|
|
}
|
|
|
|
|
|
async get(id: string) {
|
|
|
- const url = `https://dev.fmode.cn/parse/classes/${this.className}/${id}?`;
|
|
|
+ const url = `http://dev.fmode.cn:1337/parse/classes/${this.className}/${id}?`;
|
|
|
|
|
|
const response = await fetch(url, {
|
|
|
headers: {
|
|
|
@@ -131,52 +136,32 @@ export class CloudQuery {
|
|
|
});
|
|
|
|
|
|
const json = await response?.json();
|
|
|
- if (json) {
|
|
|
- let existsObject = this.dataToObj(json)
|
|
|
+ // return json || {};
|
|
|
+ const exists = json?.results?.[0] || null;
|
|
|
+ if (exists) {
|
|
|
+ let existsObject = this.dataToObj(exists)
|
|
|
return existsObject;
|
|
|
}
|
|
|
return null
|
|
|
- }
|
|
|
|
|
|
- async find():Promise<Array<CloudObject>> {
|
|
|
- let url = `https://dev.fmode.cn/parse/classes/${this.className}?`;
|
|
|
-
|
|
|
- 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: {
|
|
|
- "if-none-match": "W/\"1f0-ghxH2EwTk6Blz0g89ivf2adBDKY\"",
|
|
|
- "x-parse-application-id": "dev"
|
|
|
- },
|
|
|
- body: null,
|
|
|
- method: "GET",
|
|
|
- mode: "cors",
|
|
|
- credentials: "omit"
|
|
|
- });
|
|
|
-
|
|
|
- const json = await response?.json();
|
|
|
- let list = json?.results || []
|
|
|
- let objList = list.map((item:any)=>this.dataToObj(item))
|
|
|
- return objList || [];
|
|
|
}
|
|
|
|
|
|
+ async find(): Promise<CloudObject[]> {
|
|
|
+ try {
|
|
|
+ const results = await this.query.find();
|
|
|
+ return results.map((result: Parse.Object) => {
|
|
|
+ const obj = new CloudObject(this.className);
|
|
|
+ (obj as any).parseObject = result; // 使用类型断言
|
|
|
+ return obj;
|
|
|
+ });
|
|
|
+ } catch (error) {
|
|
|
+ console.error('Query failed:', error);
|
|
|
+ throw error;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
async first() {
|
|
|
- let url = `https://dev.fmode.cn/parse/classes/${this.className}?`;
|
|
|
+ let url = `http://dev.fmode.cn:1337/parse/classes/${this.className}?`;
|
|
|
|
|
|
if (Object.keys(this.queryParams["where"]).length) {
|
|
|
const whereStr = JSON.stringify(this.queryParams["where"]);
|
|
|
@@ -185,36 +170,26 @@ export class CloudQuery {
|
|
|
|
|
|
const response = await fetch(url, {
|
|
|
headers: {
|
|
|
- "if-none-match": "W/\"1f0-ghxH2EwTk6Blz0g89ivf2adBDKY\"",
|
|
|
"x-parse-application-id": "dev"
|
|
|
- },
|
|
|
- body: null,
|
|
|
- method: "GET",
|
|
|
- mode: "cors",
|
|
|
- credentials: "omit"
|
|
|
+ }
|
|
|
});
|
|
|
|
|
|
const json = await response?.json();
|
|
|
- const exists = json?.results?.[0] || null;
|
|
|
- if (exists) {
|
|
|
- let existsObject = this.dataToObj(exists)
|
|
|
- return existsObject;
|
|
|
- }
|
|
|
- return null
|
|
|
- }
|
|
|
-
|
|
|
- dataToObj(exists:any):CloudObject{
|
|
|
- let existsObject = new CloudObject(this.className);
|
|
|
- existsObject.set(exists);
|
|
|
- existsObject.id = exists.objectId;
|
|
|
- existsObject.createdAt = exists.createdAt;
|
|
|
- existsObject.updatedAt = exists.updatedAt;
|
|
|
- return existsObject;
|
|
|
+ // const exists = json?.results?.[0] || null;
|
|
|
+ // if (exists) {
|
|
|
+ // let existsObject = this.dataToObj(exists)
|
|
|
+ // return existsObject;
|
|
|
+ // }
|
|
|
+ // return null
|
|
|
+ let list = json?.results || []
|
|
|
+ let objList = list.map((item:any)=>this.dataToObj(item))
|
|
|
+ return objList || [];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-// CloudUser.ts
|
|
|
export class CloudUser extends CloudObject {
|
|
|
+ sessionToken: string | null = null;
|
|
|
+
|
|
|
constructor() {
|
|
|
super("_User"); // 假设用户类在Parse中是"_User"
|
|
|
// 读取用户缓存信息
|
|
|
@@ -236,7 +211,7 @@ export class CloudUser extends CloudObject {
|
|
|
return null;
|
|
|
}
|
|
|
return this;
|
|
|
- // const response = await fetch(`https://dev.fmode.cn/parse/users/me`, {
|
|
|
+ // 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进行身份验证
|
|
|
@@ -254,74 +229,58 @@ export class CloudUser extends CloudObject {
|
|
|
|
|
|
/** 登录 */
|
|
|
async login(username: string, password: string):Promise<CloudUser|null> {
|
|
|
- const response = await fetch(`https://dev.fmode.cn/parse/login`, {
|
|
|
+ const response = await fetch(`http://dev.fmode.cn:1337/parse/login`, {
|
|
|
headers: {
|
|
|
- "x-parse-application-id": "dev",
|
|
|
- "Content-Type": "application/json"
|
|
|
+ 'Content-Type': 'application/json',
|
|
|
+ 'X-Parse-Application-Id': 'dev'
|
|
|
},
|
|
|
- body: JSON.stringify({ username, password }),
|
|
|
- method: "POST"
|
|
|
+ body: JSON.stringify({ username, password })
|
|
|
});
|
|
|
|
|
|
- const result = await response?.json();
|
|
|
+ const result = await response.json();
|
|
|
if (result?.error) {
|
|
|
- console.error(result?.error);
|
|
|
- return null;
|
|
|
+ throw new Error(result.error);
|
|
|
}
|
|
|
-
|
|
|
- // 设置用户信息
|
|
|
- this.id = result?.objectId;
|
|
|
- this.sessionToken = result?.sessionToken;
|
|
|
- this.data = result; // 保存用户数据
|
|
|
- // 缓存用户信息
|
|
|
- console.log(result)
|
|
|
- localStorage.setItem("NCloud/dev/User",JSON.stringify(result))
|
|
|
+
|
|
|
+ this.id = result.objectId;
|
|
|
+ this.sessionToken = result.sessionToken;
|
|
|
+ this.data = result;
|
|
|
+ localStorage.setItem("NCloud/dev/User", JSON.stringify(result));
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
- /** 登出 */
|
|
|
- async logout() {
|
|
|
+ async logOut() {
|
|
|
if (!this.sessionToken) {
|
|
|
- console.error("用户未登录");
|
|
|
- return;
|
|
|
+ throw new Error('User not logged in');
|
|
|
}
|
|
|
|
|
|
const response = await fetch(`https://dev.fmode.cn/parse/logout`, {
|
|
|
headers: {
|
|
|
- "x-parse-application-id": "dev",
|
|
|
- "x-parse-session-token": this.sessionToken
|
|
|
+ 'X-Parse-Application-Id': 'dev',
|
|
|
+ 'X-Parse-Session-Token': this.sessionToken
|
|
|
},
|
|
|
- method: "POST"
|
|
|
+ method: 'POST'
|
|
|
});
|
|
|
|
|
|
- let result = await response?.json();
|
|
|
-
|
|
|
+ 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/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 // 合并额外的用户数据
|
|
|
+ ...additionalData // 合并额外的用户��据
|
|
|
};
|
|
|
|
|
|
const response = await fetch(`https://dev.fmode.cn/parse/users`, {
|
|
|
@@ -335,23 +294,19 @@ export class CloudUser extends CloudObject {
|
|
|
|
|
|
const result = await response?.json();
|
|
|
if (result?.error) {
|
|
|
- console.error(result?.error);
|
|
|
- return null;
|
|
|
+ throw new Error(result.error);
|
|
|
}
|
|
|
|
|
|
- // 设置用户信息
|
|
|
- // 缓存用户信息
|
|
|
- console.log(result)
|
|
|
- localStorage.setItem("NCloud/dev/User",JSON.stringify(result))
|
|
|
this.id = result?.objectId;
|
|
|
this.sessionToken = result?.sessionToken;
|
|
|
- this.data = result; // 保存用户数据
|
|
|
+ this.data = result;
|
|
|
+ localStorage.setItem("NCloud/dev/User", JSON.stringify(result));
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
override async save() {
|
|
|
let method = "POST";
|
|
|
- let url = `https://dev.fmode.cn/parse/users`;
|
|
|
+ let url = `http://dev.fmode.cn:1337/parse/users`;
|
|
|
|
|
|
// 更新用户信息
|
|
|
if (this.id) {
|
|
|
@@ -371,21 +326,23 @@ export class CloudUser extends CloudObject {
|
|
|
"x-parse-session-token": this.sessionToken, // 添加sessionToken以进行身份验证
|
|
|
}
|
|
|
const response = await fetch(url, {
|
|
|
- headers: headersOptions,
|
|
|
- body: body,
|
|
|
- method: method,
|
|
|
+ headers: {
|
|
|
+ "content-type": "application/json;charset=UTF-8",
|
|
|
+ "x-parse-application-id": "dev",
|
|
|
+ "x-parse-session-token": this.sessionToken
|
|
|
+ },
|
|
|
+ body: JSON.stringify(data),
|
|
|
+ method: "PUT",
|
|
|
mode: "cors",
|
|
|
credentials: "omit"
|
|
|
});
|
|
|
-
|
|
|
+
|
|
|
const result = await response?.json();
|
|
|
if (result?.error) {
|
|
|
- console.error(result?.error);
|
|
|
+ throw new Error(result.error);
|
|
|
}
|
|
|
- if (result?.objectId) {
|
|
|
- this.id = result?.objectId;
|
|
|
- }
|
|
|
- localStorage.setItem("NCloud/dev/User",JSON.stringify(this.data))
|
|
|
+
|
|
|
+ localStorage.setItem("NCloud/dev/User", JSON.stringify(this.data));
|
|
|
return this;
|
|
|
}
|
|
|
}
|