|
@@ -2,8 +2,8 @@
|
|
|
export class CloudObject {
|
|
|
className: string;
|
|
|
id: string | null = null;
|
|
|
- createdAt:any;
|
|
|
- updatedAt:any;
|
|
|
+ createdAt: any;
|
|
|
+ updatedAt: any;
|
|
|
data: Record<string, any> = {};
|
|
|
|
|
|
constructor(className: string) {
|
|
@@ -88,7 +88,7 @@ export class CloudQuery {
|
|
|
this.className = className;
|
|
|
}
|
|
|
|
|
|
- include(...fileds:string[]) {
|
|
|
+ include(...fileds: string[]) {
|
|
|
this.queryParams["include"] = fileds;
|
|
|
}
|
|
|
greaterThan(key: string, value: any) {
|
|
@@ -138,23 +138,23 @@ export class CloudQuery {
|
|
|
return null
|
|
|
}
|
|
|
|
|
|
- async find():Promise<Array<CloudObject>> {
|
|
|
+ async find(): Promise<CloudObject[]> {
|
|
|
let url = `http://1.94.237.145:1337/parse/classes/${this.className}?`;
|
|
|
|
|
|
let queryStr = ``
|
|
|
- Object.keys(this.queryParams).forEach(key=>{
|
|
|
+ Object.keys(this.queryParams).forEach(key => {
|
|
|
let paramStr = JSON.stringify(this.queryParams[key]);
|
|
|
- if(key=="include"){
|
|
|
+ if (key == "include") {
|
|
|
paramStr = this.queryParams[key]?.join(",")
|
|
|
}
|
|
|
- if(queryStr) {
|
|
|
+ if (queryStr) {
|
|
|
url += `${key}=${paramStr}`;
|
|
|
- }else{
|
|
|
+ } else {
|
|
|
url += `&${key}=${paramStr}`;
|
|
|
}
|
|
|
})
|
|
|
// if (Object.keys(this.queryParams["where"]).length) {
|
|
|
-
|
|
|
+
|
|
|
// }
|
|
|
|
|
|
const response = await fetch(url, {
|
|
@@ -170,7 +170,7 @@ export class CloudQuery {
|
|
|
|
|
|
const json = await response?.json();
|
|
|
let list = json?.results || []
|
|
|
- let objList = list.map((item:any)=>this.dataToObj(item))
|
|
|
+ let objList = list.map((item: any) => this.dataToObj(item))
|
|
|
return objList || [];
|
|
|
}
|
|
|
|
|
@@ -203,7 +203,7 @@ export class CloudQuery {
|
|
|
return null
|
|
|
}
|
|
|
|
|
|
- dataToObj(exists:any):CloudObject{
|
|
|
+ dataToObj(exists: any): CloudObject {
|
|
|
let existsObject = new CloudObject(this.className);
|
|
|
existsObject.set(exists);
|
|
|
existsObject.id = exists.objectId;
|
|
@@ -215,45 +215,48 @@ export class CloudQuery {
|
|
|
|
|
|
// CloudUser.ts
|
|
|
export class CloudUser extends CloudObject {
|
|
|
- constructor() {
|
|
|
- super("_User"); // 假设用户类在Parse中是"_User"
|
|
|
- // 读取用户缓存信息
|
|
|
+ /**
|
|
|
+ * @读取用户缓存信息
|
|
|
+ */
|
|
|
+ readCache() {
|
|
|
+ console.warn("-------------------------")
|
|
|
+ console.warn("正在执行readCache方法:");
|
|
|
let userCacheStr = localStorage.getItem("NCloud/dev/User")
|
|
|
- if(userCacheStr){
|
|
|
- let userData = JSON.parse(userCacheStr)
|
|
|
- // 设置用户信息
|
|
|
+ console.log("用户缓存信息:", userCacheStr);
|
|
|
+ if (userCacheStr) {
|
|
|
+ let userData = JSON.parse(userCacheStr);
|
|
|
+ console.log("缓存信息转换JSON:")
|
|
|
+ console.log(userData);
|
|
|
this.id = userData?.objectId;
|
|
|
this.sessionToken = userData?.sessionToken;
|
|
|
this.data = userData; // 保存用户数据
|
|
|
+ console.log("缓存信息已保存至用户数据中,用户数据如下:")
|
|
|
+ console.log(this);
|
|
|
+ console.warn("-------------------------")
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- sessionToken:string|null = ""
|
|
|
- /** 获取当前用户信息 */
|
|
|
+ constructor() {
|
|
|
+ super("_User"); // 假设用户类在Parse中是"_User"
|
|
|
+ this.readCache(); // 读取用户缓存信息
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @获取用户当前信息
|
|
|
+ */
|
|
|
+ sessionToken: string | null = ""
|
|
|
async current() {
|
|
|
if (!this.sessionToken) {
|
|
|
console.error("用户未登录");
|
|
|
return null;
|
|
|
}
|
|
|
return this;
|
|
|
- // const response = await fetch(`http://1.94.237.145:1337/parse/users/me`, {
|
|
|
- // headers: {
|
|
|
- // "x-parse-application-id": "hcx",
|
|
|
- // "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 loginByUsername(username: string, password: string):Promise<CloudUser|null> {
|
|
|
+ async loginByUsername(username: string, password: string): Promise<CloudUser | null> {
|
|
|
+ console.warn("-------------------------");
|
|
|
+ console.warn("正在执行ncloud中的登录方法:开始验证");
|
|
|
const response = await fetch(`http://1.94.237.145:1337/parse/login`, {
|
|
|
headers: {
|
|
|
"x-parse-application-id": "hcx",
|
|
@@ -262,20 +265,21 @@ export class CloudUser extends CloudObject {
|
|
|
body: JSON.stringify({ username, password }),
|
|
|
method: "POST"
|
|
|
});
|
|
|
-
|
|
|
const result = await response?.json();
|
|
|
+ console.log("请求返回结果:\n", result);
|
|
|
if (result?.error) {
|
|
|
console.error(result?.error);
|
|
|
return null;
|
|
|
}
|
|
|
-
|
|
|
- // 设置用户信息
|
|
|
+ console.log("正在读取并设置用户信息...");
|
|
|
this.id = result?.objectId;
|
|
|
this.sessionToken = result?.sessionToken;
|
|
|
this.data = result; // 保存用户数据
|
|
|
- // 缓存用户信息
|
|
|
- console.log(result)
|
|
|
- localStorage.setItem("NCloud/dev/User",JSON.stringify(result))
|
|
|
+ console.log("设置完毕,您的信息如下:")
|
|
|
+ console.log(this);
|
|
|
+ localStorage.setItem("NCloud/dev/User", JSON.stringify(result))// 缓存用户信息
|
|
|
+ console.warn("验证流程结束!")
|
|
|
+ console.warn("-------------------------");
|
|
|
return this;
|
|
|
}
|
|
|
|
|
@@ -298,7 +302,7 @@ export class CloudUser extends CloudObject {
|
|
|
|
|
|
if (result?.error) {
|
|
|
console.error(result?.error);
|
|
|
- if(result?.error=="Invalid session token"){
|
|
|
+ if (result?.error == "Invalid session token") {
|
|
|
this.clearUserCache()
|
|
|
return true;
|
|
|
}
|
|
@@ -308,7 +312,7 @@ export class CloudUser extends CloudObject {
|
|
|
this.clearUserCache()
|
|
|
return true;
|
|
|
}
|
|
|
- clearUserCache(){
|
|
|
+ clearUserCache() {
|
|
|
// 清除用户信息
|
|
|
localStorage.removeItem("NCloud/dev/User")
|
|
|
this.id = null;
|
|
@@ -342,30 +346,33 @@ export class CloudUser extends CloudObject {
|
|
|
// 设置用户信息
|
|
|
// 缓存用户信息
|
|
|
console.log(result)
|
|
|
- localStorage.setItem("NCloud/dev/User",JSON.stringify(result))
|
|
|
+ localStorage.setItem("NCloud/dev/User", JSON.stringify(result))
|
|
|
this.id = result?.objectId;
|
|
|
this.sessionToken = result?.sessionToken;
|
|
|
this.data = result; // 保存用户数据
|
|
|
return this;
|
|
|
}
|
|
|
-
|
|
|
+ /**
|
|
|
+ * 用户重写save方法,用于更新用户信息
|
|
|
+ */
|
|
|
override async save() {
|
|
|
+ console.warn("-----------------\n" + "准备执行save方法更新用户信息...")
|
|
|
let method = "POST";
|
|
|
let url = `http://1.94.237.145:1337/parse/users`;
|
|
|
-
|
|
|
+
|
|
|
// 更新用户信息
|
|
|
if (this.id) {
|
|
|
url += `/${this.id}`;
|
|
|
method = "PUT";
|
|
|
}
|
|
|
-
|
|
|
- let data:any = JSON.parse(JSON.stringify(this.data))
|
|
|
+ console.log("正在发送请求:" + url)
|
|
|
+ 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 = {
|
|
|
+ let headersOptions: any = {
|
|
|
"content-type": "application/json;charset=UTF-8",
|
|
|
"x-parse-application-id": "hcx",
|
|
|
"x-parse-session-token": this.sessionToken, // 添加sessionToken以进行身份验证
|
|
@@ -377,26 +384,29 @@ export class CloudUser extends CloudObject {
|
|
|
mode: "cors",
|
|
|
credentials: "omit"
|
|
|
});
|
|
|
-
|
|
|
+
|
|
|
const result = await response?.json();
|
|
|
+ console.log("请求返回结果:\n", result);
|
|
|
if (result?.error) {
|
|
|
console.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))
|
|
|
+ console.log("重新设置用户缓存中...")
|
|
|
+ console.warn("更新流程结束!"+"\n-----------------\n")
|
|
|
return this;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-export class CloudApi{
|
|
|
- async fetch(path:string,body:any,options?:{
|
|
|
- method:string
|
|
|
- body:any
|
|
|
- }){
|
|
|
+export class CloudApi {
|
|
|
+ async fetch(path: string, body: any, options?: {
|
|
|
+ method: string
|
|
|
+ body: any
|
|
|
+ }) {
|
|
|
|
|
|
- let reqOpts:any = {
|
|
|
+ let reqOpts: any = {
|
|
|
headers: {
|
|
|
"x-parse-application-id": "hcx",
|
|
|
"Content-Type": "application/json"
|
|
@@ -405,15 +415,15 @@ export class CloudApi{
|
|
|
mode: "cors",
|
|
|
credentials: "omit"
|
|
|
}
|
|
|
- if(body||options?.body){
|
|
|
+ if (body || options?.body) {
|
|
|
reqOpts.body = JSON.stringify(body || options?.body);
|
|
|
reqOpts.json = true;
|
|
|
}
|
|
|
let host = `http://1.94.237.145:1337`
|
|
|
// host = `http://127.0.0.1:1337`
|
|
|
- let url = `${host}/api/`+path
|
|
|
- console.log(url,reqOpts)
|
|
|
- const response = await fetch(url,reqOpts);
|
|
|
+ let url = `${host}/api/` + path
|
|
|
+ console.log(url, reqOpts)
|
|
|
+ const response = await fetch(url, reqOpts);
|
|
|
let json = await response.json();
|
|
|
return json
|
|
|
}
|