|
@@ -1,9 +1,8 @@
|
|
-// CloudObject.ts
|
|
|
|
export class CloudObject {
|
|
export class CloudObject {
|
|
id: string | null = null;
|
|
id: string | null = null;
|
|
className: string;
|
|
className: string;
|
|
- createdAt:any;
|
|
|
|
- updatedAt:any;
|
|
|
|
|
|
+ createdAt: any;
|
|
|
|
+ updatedAt: any;
|
|
data: Record<string, any> = {};
|
|
data: Record<string, any> = {};
|
|
|
|
|
|
constructor(className: string) {
|
|
constructor(className: string) {
|
|
@@ -27,9 +26,10 @@ export class CloudObject {
|
|
return this.data[key] || null;
|
|
return this.data[key] || null;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ // 修改 save 方法:根据新的接口 URL
|
|
async save(): Promise<this> {
|
|
async save(): Promise<this> {
|
|
let method = "POST";
|
|
let method = "POST";
|
|
- let url = `http://1.94.237.145:1339/parse/classes/${this.className}`;
|
|
|
|
|
|
+ let url = `http://dev.fmode.cn:1337/parse/classes/${this.className}`;
|
|
|
|
|
|
// 更新
|
|
// 更新
|
|
if (this.id) {
|
|
if (this.id) {
|
|
@@ -41,9 +41,9 @@ export class CloudObject {
|
|
const response = await fetch(url, {
|
|
const response = await fetch(url, {
|
|
headers: {
|
|
headers: {
|
|
"accept": "*/*",
|
|
"accept": "*/*",
|
|
- "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
|
|
|
|
|
|
+ "accept-language": "zh-CN,zh;q=0.9",
|
|
"content-type": "text/plain;charset=UTF-8",
|
|
"content-type": "text/plain;charset=UTF-8",
|
|
- "x-parse-application-id": "ylj",
|
|
|
|
|
|
+ "x-parse-application-id": "dev",
|
|
"Referer": "http://127.0.0.1:4040/",
|
|
"Referer": "http://127.0.0.1:4040/",
|
|
"Referrer-Policy": "strict-origin-when-cross-origin"
|
|
"Referrer-Policy": "strict-origin-when-cross-origin"
|
|
},
|
|
},
|
|
@@ -63,14 +63,15 @@ export class CloudObject {
|
|
return this;
|
|
return this;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ // 修改 destroy 方法:根据新的接口 URL
|
|
async destroy(): Promise<boolean> {
|
|
async destroy(): Promise<boolean> {
|
|
if (!this.id) return false;
|
|
if (!this.id) return false;
|
|
|
|
|
|
- const response = await fetch(`http://1.94.237.145:1339/parse/classes/${this.className}/${this.id}`, {
|
|
|
|
|
|
+ const response = await fetch(`http://dev.fmode.cn:1337/parse/classes/${this.className}/${this.id}`, {
|
|
headers: {
|
|
headers: {
|
|
"accept": "*/*",
|
|
"accept": "*/*",
|
|
- "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
|
|
|
|
- "x-parse-application-id": "ylj",
|
|
|
|
|
|
+ "accept-language": "zh-CN,zh;q=0.9",
|
|
|
|
+ "x-parse-application-id": "dev",
|
|
"Referer": "http://127.0.0.1:4040/",
|
|
"Referer": "http://127.0.0.1:4040/",
|
|
"Referrer-Policy": "strict-origin-when-cross-origin"
|
|
"Referrer-Policy": "strict-origin-when-cross-origin"
|
|
},
|
|
},
|
|
@@ -88,7 +89,7 @@ export class CloudObject {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-// CloudQuery.ts
|
|
|
|
|
|
+
|
|
export class CloudQuery {
|
|
export class CloudQuery {
|
|
className: string;
|
|
className: string;
|
|
whereOptions: Record<string, any> = {};
|
|
whereOptions: Record<string, any> = {};
|
|
@@ -121,14 +122,14 @@ export class CloudQuery {
|
|
this.whereOptions[key] = value;
|
|
this.whereOptions[key] = value;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ // 修改 get 方法:根据新的接口 URL
|
|
async get(id: string): Promise<Record<string, any>> {
|
|
async get(id: string): Promise<Record<string, any>> {
|
|
- const url = `http://1.94.237.145.1339/parse/classes/${this.className}/${id}?`;
|
|
|
|
|
|
+ const url = `http://dev.fmode.cn:1337/parse/classes/${this.className}/${id}?`;
|
|
const response = await fetch(url, {
|
|
const response = await fetch(url, {
|
|
headers: {
|
|
headers: {
|
|
"accept": "*/*",
|
|
"accept": "*/*",
|
|
- "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
|
|
|
|
- "if-none-match": "W/\"127-p6+dUp7xlGAxzyvVsfuvVhMxjPo\"",
|
|
|
|
- "x-parse-application-id": "ylj",
|
|
|
|
|
|
+ "accept-language": "zh-CN,zh;q=0.9",
|
|
|
|
+ "x-parse-application-id": "dev",
|
|
"Referer": "http://127.0.0.1:4040/",
|
|
"Referer": "http://127.0.0.1:4040/",
|
|
"Referrer-Policy": "strict-origin-when-cross-origin"
|
|
"Referrer-Policy": "strict-origin-when-cross-origin"
|
|
},
|
|
},
|
|
@@ -141,8 +142,9 @@ export class CloudQuery {
|
|
return json || {};
|
|
return json || {};
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ // 修改 find 方法:根据新的接口 URL
|
|
async find(): Promise<CloudObject[]> {
|
|
async find(): Promise<CloudObject[]> {
|
|
- let url = `http://1.94.237.145:1339/parse/classes/${this.className}?`;
|
|
|
|
|
|
+ let url = `http://dev.fmode.cn:1337/parse/classes/${this.className}?`;
|
|
|
|
|
|
if (Object.keys(this.whereOptions).length) {
|
|
if (Object.keys(this.whereOptions).length) {
|
|
const whereStr = JSON.stringify(this.whereOptions);
|
|
const whereStr = JSON.stringify(this.whereOptions);
|
|
@@ -152,9 +154,8 @@ export class CloudQuery {
|
|
const response = await fetch(url, {
|
|
const response = await fetch(url, {
|
|
headers: {
|
|
headers: {
|
|
"accept": "*/*",
|
|
"accept": "*/*",
|
|
- "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
|
|
|
|
- "if-none-match": "W/\"127-p6+dUp7xlGAxzyvVsfuvVhMxjPo\"",
|
|
|
|
- "x-parse-application-id": "ylj",
|
|
|
|
|
|
+ "accept-language": "zh-CN,zh;q=0.9",
|
|
|
|
+ "x-parse-application-id": "dev",
|
|
"Referer": "http://127.0.0.1:4040/",
|
|
"Referer": "http://127.0.0.1:4040/",
|
|
"Referrer-Policy": "strict-origin-when-cross-origin"
|
|
"Referrer-Policy": "strict-origin-when-cross-origin"
|
|
},
|
|
},
|
|
@@ -163,15 +164,16 @@ export class CloudQuery {
|
|
mode: "cors",
|
|
mode: "cors",
|
|
credentials: "omit"
|
|
credentials: "omit"
|
|
});
|
|
});
|
|
-
|
|
|
|
|
|
+
|
|
const json = await response?.json();
|
|
const json = await response?.json();
|
|
- let list=json?.results || [];
|
|
|
|
- let objList=list.map((item:any)=>this.dataToObj(item));
|
|
|
|
|
|
+ let list = json?.results || [];
|
|
|
|
+ let objList = list.map((item: any) => this.dataToObj(item));
|
|
return objList || [];
|
|
return objList || [];
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ // 修改 first 方法:根据新的接口 URL
|
|
async first(): Promise<CloudObject | null> {
|
|
async first(): Promise<CloudObject | null> {
|
|
- let url = `http://1.94.237.145:1339/parse/classes/${this.className}?`;
|
|
|
|
|
|
+ let url = `http://dev.fmode.cn:1337/parse/classes/${this.className}?`;
|
|
|
|
|
|
if (Object.keys(this.whereOptions).length) {
|
|
if (Object.keys(this.whereOptions).length) {
|
|
const whereStr = JSON.stringify(this.whereOptions);
|
|
const whereStr = JSON.stringify(this.whereOptions);
|
|
@@ -181,9 +183,8 @@ export class CloudQuery {
|
|
const response = await fetch(url, {
|
|
const response = await fetch(url, {
|
|
headers: {
|
|
headers: {
|
|
"accept": "*/*",
|
|
"accept": "*/*",
|
|
- "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
|
|
|
|
- "if-none-match": "W/\"127-p6+dUp7xlGAxzyvVsfuvVhMxjPo\"",
|
|
|
|
- "x-parse-application-id": "ylj",
|
|
|
|
|
|
+ "accept-language": "zh-CN,zh;q=0.9",
|
|
|
|
+ "x-parse-application-id": "dev",
|
|
"Referer": "http://127.0.0.1:4040/",
|
|
"Referer": "http://127.0.0.1:4040/",
|
|
"Referrer-Policy": "strict-origin-when-cross-origin"
|
|
"Referrer-Policy": "strict-origin-when-cross-origin"
|
|
},
|
|
},
|
|
@@ -202,44 +203,49 @@ export class CloudQuery {
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
- dataToObj(exists:any):CloudObject{
|
|
|
|
|
|
+ dataToObj(exists: any): CloudObject {
|
|
let existsObject = new CloudObject(this.className);
|
|
let existsObject = new CloudObject(this.className);
|
|
- existsObject.set(exists);
|
|
|
|
- existsObject.id = exists.objectId;
|
|
|
|
- existsObject.createdAt = exists.createdAt;
|
|
|
|
- existsObject.updatedAt = exists.updatedAt;
|
|
|
|
- return existsObject;
|
|
|
|
- }
|
|
|
|
|
|
+ existsObject.set(exists);
|
|
|
|
+ existsObject.id = exists.objectId;
|
|
|
|
+ existsObject.createdAt = exists.createdAt;
|
|
|
|
+ existsObject.updatedAt = exists.updatedAt;
|
|
|
|
+ return existsObject;
|
|
}
|
|
}
|
|
-// CloudUser.ts
|
|
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
export class CloudUser extends CloudObject {
|
|
export class CloudUser extends CloudObject {
|
|
|
|
+ sessionToken: string | null = "";
|
|
|
|
+
|
|
constructor() {
|
|
constructor() {
|
|
- super("_User"); // 假设用户类在Parse中是"_User"
|
|
|
|
- // 读取用户缓存信息
|
|
|
|
- let userCacheStr = localStorage.getItem("NCloud/ylj/User")
|
|
|
|
- if(userCacheStr){
|
|
|
|
- let userData = JSON.parse(userCacheStr)
|
|
|
|
- // 设置用户信息
|
|
|
|
|
|
+ super("_User");
|
|
|
|
+ let userCacheStr = localStorage.getItem("NCloud/dev/User");
|
|
|
|
+ if (userCacheStr) {
|
|
|
|
+ let userData = JSON.parse(userCacheStr);
|
|
this.id = userData?.objectId;
|
|
this.id = userData?.objectId;
|
|
this.sessionToken = userData?.sessionToken;
|
|
this.sessionToken = userData?.sessionToken;
|
|
- this.data = userData; // 保存用户数据
|
|
|
|
|
|
+ this.data = userData;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- sessionToken:string|null = ""
|
|
|
|
- /** 获取当前用户信息 */
|
|
|
|
|
|
+ // 获取当前用户信息
|
|
async current() {
|
|
async current() {
|
|
if (!this.sessionToken) {
|
|
if (!this.sessionToken) {
|
|
console.error("用户未登录");
|
|
console.error("用户未登录");
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
return this;
|
|
return this;
|
|
- /*const response = await fetch(`http://1.94.237.145:1337/parse/users/me`, {
|
|
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 登录
|
|
|
|
+ async login(username: string, password: string): Promise<CloudUser | null> {
|
|
|
|
+ const response = await fetch(`http://dev.fmode.cn:1337/parse/login`, {
|
|
headers: {
|
|
headers: {
|
|
- "x-parse-application-id": "hcx",
|
|
|
|
- "x-parse-session-token": this.sessionToken // 使用sessionToken进行身份验证
|
|
|
|
|
|
+ "x-parse-application-id": "dev",
|
|
|
|
+ "Content-Type": "application/json"
|
|
},
|
|
},
|
|
- method: "GET"
|
|
|
|
|
|
+ body: JSON.stringify({ username, password }),
|
|
|
|
+ method: "POST"
|
|
});
|
|
});
|
|
|
|
|
|
const result = await response?.json();
|
|
const result = await response?.json();
|
|
@@ -247,17 +253,25 @@ export class CloudUser extends CloudObject {
|
|
console.error(result?.error);
|
|
console.error(result?.error);
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
- return result;*/
|
|
|
|
|
|
+ // 登录成功后保存用户信息和 sessionToken
|
|
|
|
+ this.id = result?.objectId;
|
|
|
|
+ this.sessionToken = result?.sessionToken;
|
|
|
|
+ this.data = result;
|
|
|
|
+
|
|
|
|
+ // 将用户信息保存到 localStorage 方便下次访问
|
|
|
|
+ localStorage.setItem("NCloud/dev/User", JSON.stringify(result));
|
|
|
|
+
|
|
|
|
+ return this;
|
|
}
|
|
}
|
|
|
|
|
|
- /** 登录 */
|
|
|
|
- async login(username: string, password: string):Promise<CloudUser|null> {
|
|
|
|
- const response = await fetch(`http://1.94.237.145:1339/parse/login`, {
|
|
|
|
|
|
+ // 注册
|
|
|
|
+ async signUp(username: string, password: string, email: string): Promise<CloudUser | null> {
|
|
|
|
+ const response = await fetch(`http://dev.fmode.cn:1337/parse/users`, {
|
|
headers: {
|
|
headers: {
|
|
- "x-parse-application-id": "ylj",
|
|
|
|
|
|
+ "x-parse-application-id": "dev",
|
|
"Content-Type": "application/json"
|
|
"Content-Type": "application/json"
|
|
},
|
|
},
|
|
- body: JSON.stringify({ username, password }),
|
|
|
|
|
|
+ body: JSON.stringify({ username, password, email }),
|
|
method: "POST"
|
|
method: "POST"
|
|
});
|
|
});
|
|
|
|
|
|
@@ -266,30 +280,34 @@ export class CloudUser extends CloudObject {
|
|
console.error(result?.error);
|
|
console.error(result?.error);
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
-
|
|
|
|
- // 设置用户信息
|
|
|
|
- this.id = result?.objectId;
|
|
|
|
- this.sessionToken = result?.sessionToken;
|
|
|
|
- this.data = result; // 保存用户数据
|
|
|
|
- // 缓存用户信息
|
|
|
|
- console.log(result)
|
|
|
|
- localStorage.setItem("NCloud/ylj/User",JSON.stringify(result))
|
|
|
|
- return this;
|
|
|
|
|
|
+ // 注册成功后自动登录
|
|
|
|
+ return await this.login(username, password);
|
|
}
|
|
}
|
|
|
|
|
|
- /** 登出 */
|
|
|
|
- async logout() {
|
|
|
|
|
|
+ // 注销
|
|
|
|
+ async logOut(): Promise<void> {
|
|
|
|
+ // 删除用户信息和 sessionToken
|
|
|
|
+ localStorage.removeItem("NCloud/dev/User");
|
|
|
|
+ this.id = null;
|
|
|
|
+ this.sessionToken = null;
|
|
|
|
+ this.data = {};
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 修改密码
|
|
|
|
+ async changePassword(newPassword: string): Promise<boolean> {
|
|
if (!this.sessionToken) {
|
|
if (!this.sessionToken) {
|
|
console.error("用户未登录");
|
|
console.error("用户未登录");
|
|
- return;
|
|
|
|
|
|
+ return false;
|
|
}
|
|
}
|
|
|
|
|
|
- const response = await fetch(`http://1.94.237.145:1339/parse/logout`, {
|
|
|
|
|
|
+ const response = await fetch(`http://dev.fmode.cn:1337/parse/users/${this.id}`, {
|
|
headers: {
|
|
headers: {
|
|
- "x-parse-application-id": "ylj",
|
|
|
|
- "x-parse-session-token": this.sessionToken
|
|
|
|
|
|
+ "x-parse-application-id": "dev",
|
|
|
|
+ "x-parse-session-token": this.sessionToken!,
|
|
|
|
+ "Content-Type": "application/json"
|
|
},
|
|
},
|
|
- method: "POST"
|
|
|
|
|
|
+ body: JSON.stringify({ password: newPassword }),
|
|
|
|
+ method: "PUT"
|
|
});
|
|
});
|
|
|
|
|
|
const result = await response?.json();
|
|
const result = await response?.json();
|
|
@@ -298,44 +316,38 @@ export class CloudUser extends CloudObject {
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
- // 清除用户信息
|
|
|
|
- localStorage.removeItem("NCloud/ylj/User")
|
|
|
|
- this.id = null;
|
|
|
|
- this.sessionToken = null;
|
|
|
|
- this.data = {};
|
|
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
- /** 注册 */
|
|
|
|
- async signUp(username: string, password: string, additionalData: Record<string, any> = {}) {
|
|
|
|
- const userData = {
|
|
|
|
- username,
|
|
|
|
- password,
|
|
|
|
- ...additionalData // 合并额外的用户数据
|
|
|
|
- };
|
|
|
|
|
|
+ // 修改用户信息
|
|
|
|
+ async updateUserInfo(newData: Record<string, any>): Promise<boolean> {
|
|
|
|
+ if (!this.sessionToken) {
|
|
|
|
+ console.error("用户未登录");
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
|
|
- const response = await fetch(`http://1.94.237.145:1339/parse/users`, {
|
|
|
|
|
|
+ const response = await fetch(`http://dev.fmode.cn:1337/parse/users/${this.id}`, {
|
|
headers: {
|
|
headers: {
|
|
- "x-parse-application-id": "ylj",
|
|
|
|
|
|
+ "x-parse-application-id": "dev",
|
|
|
|
+ "x-parse-session-token": this.sessionToken!,
|
|
"Content-Type": "application/json"
|
|
"Content-Type": "application/json"
|
|
},
|
|
},
|
|
- body: JSON.stringify(userData),
|
|
|
|
- method: "POST"
|
|
|
|
|
|
+ body: JSON.stringify(newData),
|
|
|
|
+ method: "PUT"
|
|
});
|
|
});
|
|
|
|
|
|
const result = await response?.json();
|
|
const result = await response?.json();
|
|
if (result?.error) {
|
|
if (result?.error) {
|
|
console.error(result?.error);
|
|
console.error(result?.error);
|
|
- return null;
|
|
|
|
|
|
+ return false;
|
|
}
|
|
}
|
|
|
|
|
|
- // 设置用户信息
|
|
|
|
- // 缓存用户信息
|
|
|
|
- console.log(result)
|
|
|
|
- localStorage.setItem("NCloud/ylj/User",JSON.stringify(result))
|
|
|
|
- this.id = result?.objectId;
|
|
|
|
- this.sessionToken = result?.sessionToken;
|
|
|
|
- this.data = result; // 保存用户数据
|
|
|
|
- return this;
|
|
|
|
|
|
+ // 更新本地缓存
|
|
|
|
+ this.data = { ...this.data, ...newData };
|
|
|
|
+ localStorage.setItem("NCloud/dev/User", JSON.stringify(this.data));
|
|
|
|
+
|
|
|
|
+ return true;
|
|
}
|
|
}
|
|
-}
|
|
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|