|
|
@@ -1,4 +1,5 @@
|
|
|
-// CloudObject.ts
|
|
|
+
|
|
|
+//CloudObject.ts
|
|
|
export class CloudObject {
|
|
|
className: string;
|
|
|
id: string | null = null;
|
|
|
@@ -29,7 +30,7 @@ export class CloudObject {
|
|
|
|
|
|
async save() {
|
|
|
let method = "POST";
|
|
|
- let url = `https://dev.fmode.cn/parse/classes/${this.className}`;
|
|
|
+ let url = `http://dev.fmode.cn:1337/parse/classes/${this.className}`;
|
|
|
|
|
|
// 更新
|
|
|
if (this.id) {
|
|
|
@@ -61,7 +62,7 @@ export class CloudObject {
|
|
|
|
|
|
async destroy() {
|
|
|
if (!this.id) return;
|
|
|
- const response = await fetch(`https://dev.fmode.cn/parse/classes/${this.className}/${this.id}`, {
|
|
|
+ const response = await fetch(`http://dev.fmode.cn:1337/parse/classes/${this.className}/${this.id}`, {
|
|
|
headers: {
|
|
|
"x-parse-application-id": "dev"
|
|
|
},
|
|
|
@@ -82,41 +83,38 @@ export class CloudObject {
|
|
|
// CloudQuery.ts
|
|
|
export class CloudQuery {
|
|
|
className: string;
|
|
|
- queryParams: Record<string, any> = {};
|
|
|
+ whereOptions: Record<string, any> = {};
|
|
|
|
|
|
constructor(className: string) {
|
|
|
this.className = className;
|
|
|
}
|
|
|
|
|
|
- include(...fileds:string[]) {
|
|
|
- this.queryParams["include"] = fileds;
|
|
|
- }
|
|
|
greaterThan(key: string, value: any) {
|
|
|
- if (!this.queryParams["where"][key]) this.queryParams["where"][key] = {};
|
|
|
- this.queryParams["where"][key]["$gt"] = value;
|
|
|
+ if (!this.whereOptions[key]) this.whereOptions[key] = {};
|
|
|
+ this.whereOptions[key]["$gt"] = value;
|
|
|
}
|
|
|
|
|
|
greaterThanAndEqualTo(key: string, value: any) {
|
|
|
- if (!this.queryParams["where"][key]) this.queryParams["where"][key] = {};
|
|
|
- this.queryParams["where"][key]["$gte"] = value;
|
|
|
+ if (!this.whereOptions[key]) this.whereOptions[key] = {};
|
|
|
+ this.whereOptions[key]["$gte"] = value;
|
|
|
}
|
|
|
|
|
|
lessThan(key: string, value: any) {
|
|
|
- if (!this.queryParams["where"][key]) this.queryParams["where"][key] = {};
|
|
|
- this.queryParams["where"][key]["$lt"] = value;
|
|
|
+ if (!this.whereOptions[key]) this.whereOptions[key] = {};
|
|
|
+ this.whereOptions[key]["$lt"] = value;
|
|
|
}
|
|
|
|
|
|
lessThanAndEqualTo(key: string, value: any) {
|
|
|
- if (!this.queryParams["where"][key]) this.queryParams["where"][key] = {};
|
|
|
- this.queryParams["where"][key]["$lte"] = value;
|
|
|
+ if (!this.whereOptions[key]) this.whereOptions[key] = {};
|
|
|
+ this.whereOptions[key]["$lte"] = value;
|
|
|
}
|
|
|
|
|
|
equalTo(key: string, value: any) {
|
|
|
- this.queryParams["where"][key] = value;
|
|
|
+ this.whereOptions[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: {
|
|
|
@@ -134,23 +132,12 @@ export class CloudQuery {
|
|
|
}
|
|
|
|
|
|
async find() {
|
|
|
- let url = `https://dev.fmode.cn/parse/classes/${this.className}?`;
|
|
|
+ let url = `http://dev.fmode.cn:1337/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) {
|
|
|
-
|
|
|
- // }
|
|
|
+ if (Object.keys(this.whereOptions).length) {
|
|
|
+ const whereStr = JSON.stringify(this.whereOptions);
|
|
|
+ url += `where=${whereStr}`;
|
|
|
+ }
|
|
|
|
|
|
const response = await fetch(url, {
|
|
|
headers: {
|
|
|
@@ -170,10 +157,10 @@ export class CloudQuery {
|
|
|
}
|
|
|
|
|
|
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"]);
|
|
|
+ if (Object.keys(this.whereOptions).length) {
|
|
|
+ const whereStr = JSON.stringify(this.whereOptions);
|
|
|
url += `where=${whereStr}`;
|
|
|
}
|
|
|
|
|
|
@@ -197,6 +184,7 @@ export class CloudQuery {
|
|
|
return null
|
|
|
}
|
|
|
|
|
|
+ //使得页面上获得的数据是由CloudObject构成的
|
|
|
dataToObj(exists:any):CloudObject{
|
|
|
let existsObject = new CloudObject(this.className);
|
|
|
existsObject.set(exists);
|
|
|
@@ -209,129 +197,182 @@ export class CloudQuery {
|
|
|
|
|
|
// CloudUser.ts
|
|
|
export class CloudUser extends CloudObject {
|
|
|
+ sessionToken: string | null = ""; // 用户的 sessionToken,用于身份验证
|
|
|
+
|
|
|
constructor() {
|
|
|
- super("_User"); // 假设用户类在Parse中是"_User"
|
|
|
+ super("FilmUser"); // 调用父类构造函数,指定类名为 "FilmUser"
|
|
|
+
|
|
|
// 读取用户缓存信息
|
|
|
- let userCacheStr = localStorage.getItem("NCloud/dev/User")
|
|
|
- if(userCacheStr){
|
|
|
- let userData = JSON.parse(userCacheStr)
|
|
|
+ let userCacheStr = localStorage.getItem("NCloud/dev/User");
|
|
|
+ if (userCacheStr) {
|
|
|
+ let userData = JSON.parse(userCacheStr);
|
|
|
// 设置用户信息
|
|
|
- this.id = userData?.objectId;
|
|
|
- this.sessionToken = userData?.sessionToken;
|
|
|
+ this.id = userData?.objectId; // 设置用户 ID
|
|
|
+ this.sessionToken = userData?.sessionToken; // 设置 sessionToken
|
|
|
this.data = userData; // 保存用户数据
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- sessionToken:string|null = ""
|
|
|
/** 获取当前用户信息 */
|
|
|
async current() {
|
|
|
+ // 检查用户是否登录
|
|
|
if (!this.sessionToken) {
|
|
|
console.error("用户未登录");
|
|
|
- return null;
|
|
|
+ return null; // 如果未登录,返回 null
|
|
|
}
|
|
|
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;
|
|
|
- }
|
|
|
+
|
|
|
+ // // 发送 GET 请求以获取当前用户信息
|
|
|
+ // 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(`https://dev.fmode.cn/parse/login`, {
|
|
|
+ async login(username: string, password: string): Promise<CloudUser | null> {
|
|
|
+ // 发送 POST 请求以进行用户登录
|
|
|
+ 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 }),
|
|
|
+ body: JSON.stringify({ username, password }), // 登录凭据
|
|
|
method: "POST"
|
|
|
});
|
|
|
|
|
|
- const result = await response?.json();
|
|
|
+ const result = await response?.json(); // 解析响应
|
|
|
if (result?.error) {
|
|
|
- console.error(result?.error);
|
|
|
+ console.error(result?.error); // 处理错误
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
// 设置用户信息
|
|
|
- this.id = result?.objectId;
|
|
|
- this.sessionToken = result?.sessionToken;
|
|
|
+ this.id = result?.objectId; // 设置用户 ID
|
|
|
+ this.sessionToken = result?.sessionToken; // 设置 sessionToken
|
|
|
this.data = result; // 保存用户数据
|
|
|
- // 缓存用户信息
|
|
|
- console.log(result)
|
|
|
- localStorage.setItem("NCloud/dev/User",JSON.stringify(result))
|
|
|
- return this;
|
|
|
+
|
|
|
+ // 缓存用户信息到 localStorage
|
|
|
+ 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(`https://dev.fmode.cn/parse/logout`, {
|
|
|
+ // 发送 POST 请求以进行用户登出
|
|
|
+ const response = await fetch(`http://dev.fmode.cn:1337/parse/logout`, {
|
|
|
headers: {
|
|
|
"x-parse-application-id": "dev",
|
|
|
- "x-parse-session-token": this.sessionToken
|
|
|
+ "x-parse-session-token": this.sessionToken // 使用 sessionToken 进行身份验证
|
|
|
},
|
|
|
method: "POST"
|
|
|
});
|
|
|
|
|
|
- const result = await response?.json();
|
|
|
+ const result = await response?.json(); // 解析响应
|
|
|
if (result?.error) {
|
|
|
- console.error(result?.error);
|
|
|
- return false;
|
|
|
+ console.error(result?.error); // 处理错误
|
|
|
+ return false; // 登出失败
|
|
|
}
|
|
|
|
|
|
// 清除用户信息
|
|
|
- localStorage.removeItem("NCloud/dev/User")
|
|
|
- this.id = null;
|
|
|
- this.sessionToken = null;
|
|
|
- this.data = {};
|
|
|
- return true;
|
|
|
+ localStorage.removeItem("NCloud/dev/User"); // 从 localStorage 中移除用户信息
|
|
|
+ this.id = null; // 清除用户 ID
|
|
|
+ this.sessionToken = null; // 清除 sessionToken
|
|
|
+ this.data = {}; // 清空用户数据
|
|
|
+ return true; // 登出成功
|
|
|
}
|
|
|
|
|
|
/** 注册 */
|
|
|
async signUp(username: string, password: string, additionalData: Record<string, any> = {}) {
|
|
|
+ // 构建用户数据
|
|
|
const userData = {
|
|
|
username,
|
|
|
password,
|
|
|
...additionalData // 合并额外的用户数据
|
|
|
};
|
|
|
|
|
|
- const response = await fetch(`https://dev.fmode.cn/parse/users`, {
|
|
|
+ // 发送 POST 请求以进行用户注册
|
|
|
+ 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),
|
|
|
+ body: JSON.stringify(userData), // 注册凭据
|
|
|
method: "POST"
|
|
|
});
|
|
|
|
|
|
- const result = await response?.json();
|
|
|
+ const result = await response?.json(); // 解析响应
|
|
|
if (result?.error) {
|
|
|
- console.error(result?.error);
|
|
|
- return null;
|
|
|
+ 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.id = result?.objectId; // 设置用户 ID
|
|
|
+ this.sessionToken = result?.sessionToken; // 设置 sessionToken
|
|
|
this.data = result; // 保存用户数据
|
|
|
+ return this; // 返回当前用户实例
|
|
|
+ }
|
|
|
+
|
|
|
+ //覆盖save方法
|
|
|
+ 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, // 添加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;
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
}
|