|
@@ -2,8 +2,8 @@
|
|
export class CloudObject {
|
|
export class CloudObject {
|
|
className: string;
|
|
className: string;
|
|
id: string | null = null;
|
|
id: string | null = null;
|
|
- createdAt:any;
|
|
|
|
- updatedAt:any;
|
|
|
|
|
|
+ createdAt: any;
|
|
|
|
+ updatedAt: any;
|
|
data: Record<string, any> = {};
|
|
data: Record<string, any> = {};
|
|
|
|
|
|
constructor(className: string) {
|
|
constructor(className: string) {
|
|
@@ -88,7 +88,7 @@ export class CloudQuery {
|
|
this.className = className;
|
|
this.className = className;
|
|
}
|
|
}
|
|
|
|
|
|
- include(...fileds:string[]) {
|
|
|
|
|
|
+ include(...fileds: string[]) {
|
|
this.queryParams["include"] = fileds;
|
|
this.queryParams["include"] = fileds;
|
|
}
|
|
}
|
|
greaterThan(key: string, value: any) {
|
|
greaterThan(key: string, value: any) {
|
|
@@ -112,6 +112,10 @@ export class CloudQuery {
|
|
}
|
|
}
|
|
|
|
|
|
equalTo(key: string, value: any) {
|
|
equalTo(key: string, value: any) {
|
|
|
|
+ // 确保 this.queryParams["where"] 已经初始化为对象
|
|
|
|
+ if (!this.queryParams["where"]) {
|
|
|
|
+ this.queryParams["where"] = {}; // 防止未初始化的情况
|
|
|
|
+ }
|
|
this.queryParams["where"][key] = value;
|
|
this.queryParams["where"][key] = value;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -137,19 +141,19 @@ export class CloudQuery {
|
|
let url = `http://dev.fmode.cn:1337/parse/classes/${this.className}?`;
|
|
let url = `http://dev.fmode.cn:1337/parse/classes/${this.className}?`;
|
|
|
|
|
|
let queryStr = ``
|
|
let queryStr = ``
|
|
- Object.keys(this.queryParams).forEach(key=>{
|
|
|
|
|
|
+ Object.keys(this.queryParams).forEach(key => {
|
|
let paramStr = JSON.stringify(this.queryParams[key]);
|
|
let paramStr = JSON.stringify(this.queryParams[key]);
|
|
- if(key=="include"){
|
|
|
|
|
|
+ if (key == "include") {
|
|
paramStr = this.queryParams[key]?.join(",")
|
|
paramStr = this.queryParams[key]?.join(",")
|
|
}
|
|
}
|
|
- if(queryStr) {
|
|
|
|
|
|
+ if (queryStr) {
|
|
url += `${key}=${paramStr}`;
|
|
url += `${key}=${paramStr}`;
|
|
- }else{
|
|
|
|
|
|
+ } else {
|
|
url += `&${key}=${paramStr}`;
|
|
url += `&${key}=${paramStr}`;
|
|
}
|
|
}
|
|
})
|
|
})
|
|
// if (Object.keys(this.queryParams["where"]).length) {
|
|
// if (Object.keys(this.queryParams["where"]).length) {
|
|
-
|
|
|
|
|
|
+
|
|
// }
|
|
// }
|
|
|
|
|
|
const response = await fetch(url, {
|
|
const response = await fetch(url, {
|
|
@@ -165,7 +169,7 @@ export class CloudQuery {
|
|
|
|
|
|
const json = await response?.json();
|
|
const json = await response?.json();
|
|
let list = json?.results || []
|
|
let list = json?.results || []
|
|
- let objList = list.map((item:any)=>this.dataToObj(item))
|
|
|
|
|
|
+ let objList = list.map((item: any) => this.dataToObj(item))
|
|
return objList || [];
|
|
return objList || [];
|
|
}
|
|
}
|
|
|
|
|
|
@@ -197,7 +201,7 @@ 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.set(exists);
|
|
existsObject.id = exists.objectId;
|
|
existsObject.id = exists.objectId;
|
|
@@ -213,7 +217,7 @@ export class CloudUser extends CloudObject {
|
|
super("_User"); // 假设用户类在Parse中是"_User"
|
|
super("_User"); // 假设用户类在Parse中是"_User"
|
|
// 读取用户缓存信息
|
|
// 读取用户缓存信息
|
|
let userCacheStr = localStorage.getItem("NCloud/dev/User")
|
|
let userCacheStr = localStorage.getItem("NCloud/dev/User")
|
|
- if(userCacheStr){
|
|
|
|
|
|
+ if (userCacheStr) {
|
|
let userData = JSON.parse(userCacheStr)
|
|
let userData = JSON.parse(userCacheStr)
|
|
// 设置用户信息
|
|
// 设置用户信息
|
|
this.id = userData?.objectId;
|
|
this.id = userData?.objectId;
|
|
@@ -222,7 +226,7 @@ export class CloudUser extends CloudObject {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- sessionToken:string|null = ""
|
|
|
|
|
|
+ sessionToken: string | null = ""
|
|
/** 获取当前用户信息 */
|
|
/** 获取当前用户信息 */
|
|
async current() {
|
|
async current() {
|
|
if (!this.sessionToken) {
|
|
if (!this.sessionToken) {
|
|
@@ -246,8 +250,44 @@ export class CloudUser extends CloudObject {
|
|
// return result;
|
|
// return result;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /** 获取特定用户信息 */
|
|
|
|
+ async getUserByUsername(objectId: string) {
|
|
|
|
+ const url = `http://dev.fmode.cn:1337/parse/classes/_User?where=${JSON.stringify({ objectId })}`;
|
|
|
|
+
|
|
|
|
+ const response = await fetch(url, {
|
|
|
|
+ headers: {
|
|
|
|
+ "accept": "*/*",
|
|
|
|
+ "x-parse-application-id": "dev",
|
|
|
|
+ "x-parse-master-key": "devmk", // 使用您的 Master Key
|
|
|
|
+ "Content-Type": "application/json"
|
|
|
|
+ },
|
|
|
|
+ method: "GET",
|
|
|
|
+ mode: "cors",
|
|
|
|
+ credentials: "omit"
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ const result = await response?.json();
|
|
|
|
+ if (result?.error) {
|
|
|
|
+ console.error(result?.error);
|
|
|
|
+ return null; // 返回 null 表示未找到用户或发生错误
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 处理查询结果
|
|
|
|
+ if (result.results && result.results.length > 0) {
|
|
|
|
+ const userData = result.results[0]; // 获取第一个匹配的用户
|
|
|
|
+ const user = new CloudUser();
|
|
|
|
+ user.set(userData);
|
|
|
|
+ user.id = userData.objectId;
|
|
|
|
+ user.createdAt = userData.createdAt;
|
|
|
|
+ user.updatedAt = userData.updatedAt;
|
|
|
|
+ return user; // 返回找到的用户对象
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return null; // 如果没有找到用户,返回 null
|
|
|
|
+ }
|
|
|
|
+
|
|
/** 登录 */
|
|
/** 登录 */
|
|
- async login(username: string, password: string):Promise<CloudUser|null> {
|
|
|
|
|
|
+ async login(username: string, password: string): Promise<CloudUser | null> {
|
|
const response = await fetch(`http://dev.fmode.cn:1337/parse/login`, {
|
|
const response = await fetch(`http://dev.fmode.cn:1337/parse/login`, {
|
|
headers: {
|
|
headers: {
|
|
"x-parse-application-id": "dev",
|
|
"x-parse-application-id": "dev",
|
|
@@ -262,14 +302,14 @@ export class CloudUser extends CloudObject {
|
|
console.error(result?.error);
|
|
console.error(result?.error);
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
// 设置用户信息
|
|
// 设置用户信息
|
|
this.id = result?.objectId;
|
|
this.id = result?.objectId;
|
|
this.sessionToken = result?.sessionToken;
|
|
this.sessionToken = result?.sessionToken;
|
|
this.data = result; // 保存用户数据
|
|
this.data = result; // 保存用户数据
|
|
// 缓存用户信息
|
|
// 缓存用户信息
|
|
console.log(result)
|
|
console.log(result)
|
|
- localStorage.setItem("NCloud/dev/User",JSON.stringify(result))
|
|
|
|
|
|
+ localStorage.setItem("NCloud/dev/User", JSON.stringify(result))
|
|
return this;
|
|
return this;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -328,7 +368,7 @@ export class CloudUser extends CloudObject {
|
|
// 设置用户信息
|
|
// 设置用户信息
|
|
// 缓存用户信息
|
|
// 缓存用户信息
|
|
console.log(result)
|
|
console.log(result)
|
|
- localStorage.setItem("NCloud/dev/User",JSON.stringify(result))
|
|
|
|
|
|
+ localStorage.setItem("NCloud/dev/User", JSON.stringify(result))
|
|
this.id = result?.objectId;
|
|
this.id = result?.objectId;
|
|
this.sessionToken = result?.sessionToken;
|
|
this.sessionToken = result?.sessionToken;
|
|
this.data = result; // 保存用户数据
|
|
this.data = result; // 保存用户数据
|
|
@@ -338,20 +378,20 @@ export class CloudUser extends CloudObject {
|
|
override async save() {
|
|
override async save() {
|
|
let method = "POST";
|
|
let method = "POST";
|
|
let url = `http://dev.fmode.cn:1337/parse/users`;
|
|
let url = `http://dev.fmode.cn:1337/parse/users`;
|
|
-
|
|
|
|
|
|
+
|
|
// 更新用户信息
|
|
// 更新用户信息
|
|
if (this.id) {
|
|
if (this.id) {
|
|
url += `/${this.id}`;
|
|
url += `/${this.id}`;
|
|
method = "PUT";
|
|
method = "PUT";
|
|
}
|
|
}
|
|
-
|
|
|
|
- let data:any = JSON.parse(JSON.stringify(this.data))
|
|
|
|
|
|
+
|
|
|
|
+ let data: any = JSON.parse(JSON.stringify(this.data))
|
|
delete data.createdAt
|
|
delete data.createdAt
|
|
delete data.updatedAt
|
|
delete data.updatedAt
|
|
delete data.ACL
|
|
delete data.ACL
|
|
delete data.objectId
|
|
delete data.objectId
|
|
const body = JSON.stringify(data);
|
|
const body = JSON.stringify(data);
|
|
- let headersOptions:any = {
|
|
|
|
|
|
+ let headersOptions: any = {
|
|
"content-type": "application/json;charset=UTF-8",
|
|
"content-type": "application/json;charset=UTF-8",
|
|
"x-parse-application-id": "dev",
|
|
"x-parse-application-id": "dev",
|
|
"x-parse-session-token": this.sessionToken, // 添加sessionToken以进行身份验证
|
|
"x-parse-session-token": this.sessionToken, // 添加sessionToken以进行身份验证
|
|
@@ -363,7 +403,7 @@ export class CloudUser extends CloudObject {
|
|
mode: "cors",
|
|
mode: "cors",
|
|
credentials: "omit"
|
|
credentials: "omit"
|
|
});
|
|
});
|
|
-
|
|
|
|
|
|
+
|
|
const result = await response?.json();
|
|
const result = await response?.json();
|
|
if (result?.error) {
|
|
if (result?.error) {
|
|
console.error(result?.error);
|
|
console.error(result?.error);
|
|
@@ -371,7 +411,25 @@ export class CloudUser extends CloudObject {
|
|
if (result?.objectId) {
|
|
if (result?.objectId) {
|
|
this.id = 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;
|
|
return this;
|
|
}
|
|
}
|
|
-}
|
|
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+// fetch("http://dev.fmode.cn:1337/parse/classes/_User?where={%22username%22:%22chengnan%22}", {
|
|
|
|
+// "headers": {
|
|
|
|
+// "accept": "*/*",
|
|
|
|
+// "accept-language": "zh-CN,zh;q=0.9",
|
|
|
|
+// "if-none-match": "W/\"fe-JIlchyuW4LZub+2xCTNV/QiJ0wc\"",
|
|
|
|
+// "x-parse-application-id": "dev",
|
|
|
|
+// "x-parse-master-key": "devmk"
|
|
|
|
+// },
|
|
|
|
+// "referrer": "http://localhost:4040/",
|
|
|
|
+// "referrerPolicy": "strict-origin-when-cross-origin",
|
|
|
|
+// "body": null,
|
|
|
|
+// "method": "GET",
|
|
|
|
+// "mode": "cors",
|
|
|
|
+// "credentials": "omit"
|
|
|
|
+// });
|