|
@@ -1,9 +1,15 @@
|
|
|
// CloudObject.ts
|
|
|
+
|
|
|
+let serverURL = `https://dev.fmode.cn/parse`;
|
|
|
+if (location.protocol == "http:") {
|
|
|
+ serverURL = `http://dev.fmode.cn:1337/parse`;
|
|
|
+}
|
|
|
+
|
|
|
export class CloudObject {
|
|
|
className: string;
|
|
|
- id: string | null = null;
|
|
|
- createdAt:any;
|
|
|
- updatedAt:any;
|
|
|
+ id: string | undefined = undefined;
|
|
|
+ createdAt: any;
|
|
|
+ updatedAt: any;
|
|
|
data: Record<string, any> = {};
|
|
|
|
|
|
constructor(className: string) {
|
|
@@ -29,7 +35,7 @@ export class CloudObject {
|
|
|
|
|
|
async save() {
|
|
|
let method = "POST";
|
|
|
- let url = `https://dev.fmode.cn/parse/classes/${this.className}`;
|
|
|
+ let url = serverURL + `/classes/${this.className}`;
|
|
|
|
|
|
// 更新
|
|
|
if (this.id) {
|
|
@@ -61,7 +67,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(serverURL + `/classes/${this.className}/${this.id}`, {
|
|
|
headers: {
|
|
|
"x-parse-application-id": "dev"
|
|
|
},
|
|
@@ -73,7 +79,7 @@ export class CloudObject {
|
|
|
|
|
|
const result = await response?.json();
|
|
|
if (result) {
|
|
|
- this.id = null;
|
|
|
+ this.id = undefined;
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
@@ -82,13 +88,13 @@ export class CloudObject {
|
|
|
// CloudQuery.ts
|
|
|
export class CloudQuery {
|
|
|
className: string;
|
|
|
- queryParams: Record<string, any> = {};
|
|
|
+ queryParams: Record<string, any> = { where: {} };
|
|
|
|
|
|
constructor(className: string) {
|
|
|
this.className = className;
|
|
|
}
|
|
|
|
|
|
- include(...fileds:string[]) {
|
|
|
+ include(...fileds: string[]) {
|
|
|
this.queryParams["include"] = fileds;
|
|
|
}
|
|
|
greaterThan(key: string, value: any) {
|
|
@@ -117,7 +123,7 @@ export class CloudQuery {
|
|
|
}
|
|
|
|
|
|
async get(id: string) {
|
|
|
- const url = `https://dev.fmode.cn/parse/classes/${this.className}/${id}?`;
|
|
|
+ const url = serverURL + `/classes/${this.className}/${id}?`;
|
|
|
|
|
|
const response = await fetch(url, {
|
|
|
headers: {
|
|
@@ -138,23 +144,23 @@ export class CloudQuery {
|
|
|
return null
|
|
|
}
|
|
|
|
|
|
- async find():Promise<Array<CloudObject>> {
|
|
|
- let url = `https://dev.fmode.cn/parse/classes/${this.className}?`;
|
|
|
+ async find(): Promise<Array<CloudObject>> {
|
|
|
+ let url = serverURL + `/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,17 +176,17 @@ 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 || [];
|
|
|
}
|
|
|
|
|
|
|
|
|
async first() {
|
|
|
- let url = `https://dev.fmode.cn/parse/classes/${this.className}?`;
|
|
|
+ let url = serverURL + `/classes/${this.className}?`;
|
|
|
|
|
|
if (Object.keys(this.queryParams["where"]).length) {
|
|
|
const whereStr = JSON.stringify(this.queryParams["where"]);
|
|
|
- url += `where=${whereStr}`;
|
|
|
+ url += `where=${whereStr}&limit=1`;
|
|
|
}
|
|
|
|
|
|
const response = await fetch(url, {
|
|
@@ -203,8 +209,13 @@ export class CloudQuery {
|
|
|
return null
|
|
|
}
|
|
|
|
|
|
- dataToObj(exists:any):CloudObject{
|
|
|
+ dataToObj(exists: any): CloudObject {
|
|
|
let existsObject = new CloudObject(this.className);
|
|
|
+ Object.keys(exists).forEach(key => {
|
|
|
+ if (exists[key]?.__type == "Object") {
|
|
|
+ exists[key] = this.dataToObj(exists[key])
|
|
|
+ }
|
|
|
+ })
|
|
|
existsObject.set(exists);
|
|
|
existsObject.id = exists.objectId;
|
|
|
existsObject.createdAt = exists.createdAt;
|
|
@@ -219,7 +230,7 @@ export class CloudUser extends CloudObject {
|
|
|
super("_User"); // 假设用户类在Parse中是"_User"
|
|
|
// 读取用户缓存信息
|
|
|
let userCacheStr = localStorage.getItem("NCloud/dev/User")
|
|
|
- if(userCacheStr){
|
|
|
+ if (userCacheStr) {
|
|
|
let userData = JSON.parse(userCacheStr)
|
|
|
// 设置用户信息
|
|
|
this.id = userData?.objectId;
|
|
@@ -228,7 +239,7 @@ export class CloudUser extends CloudObject {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- sessionToken:string|null = ""
|
|
|
+ sessionToken: string | null = ""
|
|
|
/** 获取当前用户信息 */
|
|
|
async current() {
|
|
|
if (!this.sessionToken) {
|
|
@@ -236,7 +247,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(serverURL + `/users/me`, {
|
|
|
// headers: {
|
|
|
// "x-parse-application-id": "dev",
|
|
|
// "x-parse-session-token": this.sessionToken // 使用sessionToken进行身份验证
|
|
@@ -253,8 +264,8 @@ export class CloudUser extends CloudObject {
|
|
|
}
|
|
|
|
|
|
/** 登录 */
|
|
|
- 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> {
|
|
|
+ const response = await fetch(serverURL + `/login`, {
|
|
|
headers: {
|
|
|
"x-parse-application-id": "dev",
|
|
|
"Content-Type": "application/json"
|
|
@@ -268,14 +279,14 @@ export class CloudUser extends CloudObject {
|
|
|
console.error(result?.error);
|
|
|
return null;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 设置用户信息
|
|
|
this.id = result?.objectId;
|
|
|
this.sessionToken = result?.sessionToken;
|
|
|
this.data = result; // 保存用户数据
|
|
|
// 缓存用户信息
|
|
|
console.log(result)
|
|
|
- localStorage.setItem("NCloud/dev/User",JSON.stringify(result))
|
|
|
+ localStorage.setItem("NCloud/dev/User", JSON.stringify(result))
|
|
|
return this;
|
|
|
}
|
|
|
|
|
@@ -286,7 +297,7 @@ export class CloudUser extends CloudObject {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- const response = await fetch(`https://dev.fmode.cn/parse/logout`, {
|
|
|
+ const response = await fetch(serverURL + `/logout`, {
|
|
|
headers: {
|
|
|
"x-parse-application-id": "dev",
|
|
|
"x-parse-session-token": this.sessionToken
|
|
@@ -298,7 +309,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,10 +319,10 @@ export class CloudUser extends CloudObject {
|
|
|
this.clearUserCache()
|
|
|
return true;
|
|
|
}
|
|
|
- clearUserCache(){
|
|
|
+ clearUserCache() {
|
|
|
// 清除用户信息
|
|
|
localStorage.removeItem("NCloud/dev/User")
|
|
|
- this.id = null;
|
|
|
+ this.id = undefined;
|
|
|
this.sessionToken = null;
|
|
|
this.data = {};
|
|
|
}
|
|
@@ -324,7 +335,7 @@ export class CloudUser extends CloudObject {
|
|
|
...additionalData // 合并额外的用户数据
|
|
|
};
|
|
|
|
|
|
- const response = await fetch(`https://dev.fmode.cn/parse/users`, {
|
|
|
+ const response = await fetch(serverURL + `/users`, {
|
|
|
headers: {
|
|
|
"x-parse-application-id": "dev",
|
|
|
"Content-Type": "application/json"
|
|
@@ -342,7 +353,7 @@ 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; // 保存用户数据
|
|
@@ -351,21 +362,21 @@ export class CloudUser extends CloudObject {
|
|
|
|
|
|
override async save() {
|
|
|
let method = "POST";
|
|
|
- let url = `https://dev.fmode.cn/parse/users`;
|
|
|
-
|
|
|
+ let url = serverURL + `/users`;
|
|
|
+
|
|
|
// 更新用户信息
|
|
|
if (this.id) {
|
|
|
url += `/${this.id}`;
|
|
|
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.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": "dev",
|
|
|
"x-parse-session-token": this.sessionToken, // 添加sessionToken以进行身份验证
|
|
@@ -377,7 +388,7 @@ export class CloudUser extends CloudObject {
|
|
|
mode: "cors",
|
|
|
credentials: "omit"
|
|
|
});
|
|
|
-
|
|
|
+
|
|
|
const result = await response?.json();
|
|
|
if (result?.error) {
|
|
|
console.error(result?.error);
|
|
@@ -385,18 +396,18 @@ export class CloudUser extends CloudObject {
|
|
|
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;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-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": "dev",
|
|
|
"Content-Type": "application/json"
|
|
@@ -405,15 +416,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 = `https://dev.fmode.cn`
|
|
|
// 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
|
|
|
}
|