|
@@ -1,664 +0,0 @@
|
|
|
-// CloudObject.ts
|
|
|
-export class CloudObject {
|
|
|
- className: string;
|
|
|
- id: string | null = null;
|
|
|
- createdAt: any;
|
|
|
- updatedAt: any;
|
|
|
- sessionToken: string | null = ""
|
|
|
- data: Record<string, any> = {};
|
|
|
-
|
|
|
- constructor(className: string) {
|
|
|
- this.className = className;
|
|
|
- }
|
|
|
-
|
|
|
- toPointer() {
|
|
|
- return { "__type": "Pointer", "className": this.className, "objectId": this.id };
|
|
|
- }
|
|
|
-
|
|
|
- set(json: Record<string, any>) {
|
|
|
- Object.keys(json).forEach(key => {
|
|
|
- if (["objectId", "id", "createdAt", "updatedAt", "ACL"].indexOf(key) > -1) {
|
|
|
- return;
|
|
|
- }
|
|
|
- this.data[key] = json[key];
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- get(key: string) {
|
|
|
- return this.data[key] || null;
|
|
|
- }
|
|
|
-
|
|
|
- async save() {
|
|
|
- let method = "POST";
|
|
|
- let url = `http://1.94.237.145:1337/parse/classes/${this.className}`;
|
|
|
-
|
|
|
- // 更新
|
|
|
- if (this.id) {
|
|
|
- url += `/${this.id}`;
|
|
|
- method = "PUT";
|
|
|
- }
|
|
|
- console.log("准备发送" + method + "请求:" + url + ",body为:");
|
|
|
- const body = JSON.stringify(this.data);
|
|
|
- console.log(body);
|
|
|
- const response = await fetch(url, {
|
|
|
- headers: {
|
|
|
- "content-type": "application/json;charset=UTF-8",
|
|
|
- "x-parse-session-token": this.sessionToken || "",
|
|
|
- "x-parse-application-id": "hcx"
|
|
|
- },
|
|
|
- body: body,
|
|
|
- method: method,
|
|
|
- mode: "cors",
|
|
|
- credentials: "omit"
|
|
|
- });
|
|
|
- console.log(response);
|
|
|
- const result = await response?.json();
|
|
|
- if (result?.error) {
|
|
|
- console.error(result?.error);
|
|
|
- }
|
|
|
- if (result?.objectId) {
|
|
|
- this.id = result?.objectId;
|
|
|
- }
|
|
|
- return this;
|
|
|
- }
|
|
|
-
|
|
|
- async destroy() {
|
|
|
- if (!this.id) return;
|
|
|
- const response = await fetch(`http://1.94.237.145:1337/parse/classes/${this.className}/${this.id}`, {
|
|
|
- headers: {
|
|
|
- "x-parse-application-id": "hcx"
|
|
|
- },
|
|
|
- body: null,
|
|
|
- method: "DELETE",
|
|
|
- mode: "cors",
|
|
|
- credentials: "omit"
|
|
|
- });
|
|
|
-
|
|
|
- const result = await response?.json();
|
|
|
- if (result) {
|
|
|
- this.id = null;
|
|
|
- }
|
|
|
- return true;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-// CloudQuery.ts
|
|
|
-// export class CloudQuery {
|
|
|
-// className: string;
|
|
|
-// sessionToken: string | null = "";
|
|
|
-// whereOptions: Record<string, any> = {};
|
|
|
-
|
|
|
-// constructor(className: string) {
|
|
|
-// this.className = className;
|
|
|
-// }
|
|
|
-
|
|
|
-// include(...fileds: string[]) {
|
|
|
-// this.whereOptions["include"] = fileds;
|
|
|
-// }
|
|
|
-// greaterThan(key: string, value: any) {
|
|
|
-// if (!this.whereOptions["where"][key]) this.whereOptions["where"][key] = {};
|
|
|
-// this.whereOptions["where"][key]["$gt"] = value;
|
|
|
-// }
|
|
|
-
|
|
|
-// greaterThanAndEqualTo(key: string, value: any) {
|
|
|
-// if (!this.whereOptions["where"][key]) this.whereOptions["where"][key] = {};
|
|
|
-// this.whereOptions["where"][key]["$gte"] = value;
|
|
|
-// }
|
|
|
-
|
|
|
-// lessThan(key: string, value: any) {
|
|
|
-// if (!this.whereOptions["where"][key]) this.whereOptions["where"][key] = {};
|
|
|
-// this.whereOptions["where"][key]["$lt"] = value;
|
|
|
-// }
|
|
|
-
|
|
|
-// lessThanAndEqualTo(key: string, value: any) {
|
|
|
-// if (!this.whereOptions["where"][key]) this.whereOptions["where"][key] = {};
|
|
|
-// this.whereOptions["where"][key]["$lte"] = value;
|
|
|
-// }
|
|
|
-
|
|
|
-// equalTo(key: string, value: any) {
|
|
|
-// if (!this.whereOptions["where"]) this.whereOptions["where"] = {};
|
|
|
-// this.whereOptions["where"][key] = value;
|
|
|
-// }
|
|
|
-
|
|
|
-// async get(id: string) {
|
|
|
-// const url = `http://1.94.237.145:1337/parse/classes/${this.className}/${id}?`;
|
|
|
-
|
|
|
-// const response = await fetch(url, {
|
|
|
-// headers: {
|
|
|
-// "if-none-match": "W/\"1f0-ghxH2EwTk6Blz0g89ivf2adBDKY\"",
|
|
|
-// "x-parse-application-id": "hcx"
|
|
|
-// },
|
|
|
-// body: null,
|
|
|
-// method: "GET",
|
|
|
-// mode: "cors",
|
|
|
-// credentials: "omit"
|
|
|
-// });
|
|
|
-
|
|
|
-// const json = await response?.json();
|
|
|
-// if (json) {
|
|
|
-// let existsObject = this.dataToObj(json);
|
|
|
-// return existsObject;
|
|
|
-// }
|
|
|
-// return null;
|
|
|
-// }
|
|
|
-// /**
|
|
|
-// * @select查询
|
|
|
-// */
|
|
|
-// async main() {
|
|
|
-// let response = await fetch(`http://1.94.237.145:1337/api/psql/select`, {
|
|
|
-// headers: {
|
|
|
-// "Content-Type": "application/json",
|
|
|
-// },
|
|
|
-// body: JSON.stringify({ "sql": "SELECT * FROM \"_User\" limit 5", "params": [] }),
|
|
|
-// method: "POST",
|
|
|
-// mode: "cors",
|
|
|
-// credentials: "omit"
|
|
|
-// });
|
|
|
-
|
|
|
-// let result = await response?.json();
|
|
|
-// console.log(result)
|
|
|
-// }
|
|
|
-
|
|
|
-// async find(): Promise<Array<CloudObject>> {
|
|
|
-// let url = `http://1.94.237.145:1337/parse/classes/${this.className}?`;
|
|
|
-
|
|
|
-// let queryStr = ``
|
|
|
-// // if (Object.keys(this.whereOptions).length) {
|
|
|
-// // const whereStr = JSON.stringify(this.whereOptions);
|
|
|
-// // url += `where=${whereStr}`;
|
|
|
-// // }
|
|
|
-// Object.keys(this.whereOptions).forEach(key => {
|
|
|
-// let paramStr = JSON.stringify(this.whereOptions[key]);
|
|
|
-// if (key == "include") {
|
|
|
-// paramStr = this.whereOptions[key]?.join(",")
|
|
|
-// }
|
|
|
-// if (queryStr) {
|
|
|
-// url += `${key}=${paramStr}`;
|
|
|
-// } else {
|
|
|
-// url += `&${key}=${paramStr}`;
|
|
|
-// }
|
|
|
-// })
|
|
|
-
|
|
|
-// const response = await fetch(url, {
|
|
|
-// headers: {
|
|
|
-// "if-none-match": "W/\"1f0-ghxH2EwTk6Blz0g89ivf2adBDKY\"",
|
|
|
-// "x-parse-application-id": "hcx"
|
|
|
-// },
|
|
|
-// body: null,
|
|
|
-// method: "GET",
|
|
|
-// mode: "cors",
|
|
|
-// credentials: "omit"
|
|
|
-// });
|
|
|
-
|
|
|
-// const json = await response?.json();
|
|
|
-// let list = json?.results || []
|
|
|
-// let objList = list.map((item: any) => this.dataToObj(item))
|
|
|
-// return objList || [];
|
|
|
-// }
|
|
|
-// /**
|
|
|
-// * @返回第一个查找到的结果
|
|
|
-// * @返回结果参考:CloudObject {
|
|
|
-// * id: 'e3wWcCfNwm',
|
|
|
-// * data: {…},
|
|
|
-// * className: '_User',
|
|
|
-// * sessionToken:"r:c0f4fe9cb8eb83d7f0295ff57e948419"
|
|
|
-// * createdAt: '2024-12-17T02:41:34.768Z',
|
|
|
-// * updatedAt: '2024-12-23T00:33:15.961Z'}
|
|
|
-// */
|
|
|
-// async first() {
|
|
|
-// let url = `http://1.94.237.145:1337/parse/classes/${this.className}?`;
|
|
|
-
|
|
|
-// if (Object.keys(this.whereOptions["where"]).length) {
|
|
|
-// const whereStr = JSON.stringify(this.whereOptions["where"]);
|
|
|
-// console.log("first查询条件" + whereStr)
|
|
|
-// url += `where=${whereStr}`;
|
|
|
-// console.log("first方法请求URL:" + url)
|
|
|
-// }
|
|
|
-
|
|
|
-// const response = await fetch(url, {
|
|
|
-// headers: {
|
|
|
-// "if-none-match": "W/\"1f0-ghxH2EwTk6Blz0g89ivf2adBDKY\"",
|
|
|
-// "x-parse-application-id": "hcx",
|
|
|
-// "x-parse-session-token": this.sessionToken || "",
|
|
|
-// },
|
|
|
-// body: null,
|
|
|
-// method: "GET",
|
|
|
-// mode: "cors",
|
|
|
-// credentials: "omit"
|
|
|
-// });
|
|
|
-
|
|
|
-// const json = await response?.json();
|
|
|
-// const exists = json?.results?.[0] || null;
|
|
|
-// if (exists) {
|
|
|
-// let existsObject = this.dataToObj(exists)
|
|
|
-// return existsObject;
|
|
|
-// }
|
|
|
-// return null
|
|
|
-// }
|
|
|
-
|
|
|
-// dataToObj(exists: any): CloudObject {
|
|
|
-// let existsObject = new CloudObject(this.className);
|
|
|
-// existsObject.set(exists);
|
|
|
-// existsObject.id = exists.objectId;
|
|
|
-// existsObject.createdAt = exists.createdAt;
|
|
|
-// existsObject.updatedAt = exists.updatedAt;
|
|
|
-// return existsObject;
|
|
|
-// }
|
|
|
-// }
|
|
|
-
|
|
|
-export class CloudQuery {
|
|
|
- className: string;
|
|
|
- sessionToken: string | null = null;
|
|
|
- whereOptions: Record<string, any> = {};
|
|
|
- orderOptions: string[] = []; // 用于存储排序字段和方向的数组
|
|
|
- limit: number | null = null; // 限制返回对象的数量
|
|
|
-
|
|
|
- constructor(className: string) {
|
|
|
- this.className = className;
|
|
|
- }
|
|
|
-
|
|
|
- include(...fields: string[]) {
|
|
|
- this.whereOptions["include"] = fields;
|
|
|
- }
|
|
|
-
|
|
|
- greaterThan(key: string, value: any) {
|
|
|
- if (!this.whereOptions["where"]) this.whereOptions["where"] = {};
|
|
|
- this.whereOptions["where"][key] = { "$gt": value };
|
|
|
- }
|
|
|
-
|
|
|
- greaterThanAndEqualTo(key: string, value: any) {
|
|
|
- if (!this.whereOptions["where"]) this.whereOptions["where"] = {};
|
|
|
- this.whereOptions["where"][key] = { "$gte": value };
|
|
|
- }
|
|
|
-
|
|
|
- lessThan(key: string, value: any) {
|
|
|
- if (!this.whereOptions["where"]) this.whereOptions["where"] = {};
|
|
|
- this.whereOptions["where"][key] = { "$lt": value };
|
|
|
- }
|
|
|
-
|
|
|
- lessThanAndEqualTo(key: string, value: any) {
|
|
|
- if (!this.whereOptions["where"]) this.whereOptions["where"] = {};
|
|
|
- this.whereOptions["where"][key] = { "$lte": value };
|
|
|
- }
|
|
|
-
|
|
|
- equalTo(key: string, value: any) {
|
|
|
- if (!this.whereOptions["where"]) this.whereOptions["where"] = {};
|
|
|
- this.whereOptions["where"][key] = value;
|
|
|
- }
|
|
|
-
|
|
|
- orderByAscending(key: string) {
|
|
|
- this.orderOptions.push(key); // 升序排序
|
|
|
- }
|
|
|
-
|
|
|
- orderByDescending(key: string) {
|
|
|
- this.orderOptions.push(`-${key}`); // 降序排序
|
|
|
- }
|
|
|
-
|
|
|
- setLimit(limit: number) {
|
|
|
- this.limit = limit;
|
|
|
- }
|
|
|
-
|
|
|
- async get(id: string): Promise<CloudObject | null> {
|
|
|
- const url = `http://1.94.237.145:1337/parse/classes/${this.className}/${id}`;
|
|
|
-
|
|
|
- const response = await fetch(url, {
|
|
|
- headers: {
|
|
|
- "X-Parse-Application-Id": "hcx",
|
|
|
- "X-Parse-Session-Token": this.sessionToken || ""
|
|
|
- },
|
|
|
- method: "GET",
|
|
|
- mode: "cors",
|
|
|
- credentials: "omit"
|
|
|
- });
|
|
|
-
|
|
|
- const json = await response.json();
|
|
|
- if (json) {
|
|
|
- return this.dataToObj(json);
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
- /**
|
|
|
- * @select查询
|
|
|
- */
|
|
|
- async main() {
|
|
|
- let response = await fetch(`http://1.94.237.145:1337/api/psql/select`, {
|
|
|
- headers: {
|
|
|
- "Content-Type": "application/json",
|
|
|
- },
|
|
|
- body: JSON.stringify({ "sql": "SELECT * FROM \"_User\" limit 5", "params": [] }),
|
|
|
- method: "POST",
|
|
|
- mode: "cors",
|
|
|
- credentials: "omit"
|
|
|
- });
|
|
|
-
|
|
|
- let result = await response?.json();
|
|
|
- console.log(result)
|
|
|
- }
|
|
|
-
|
|
|
- async find(): Promise<Array<CloudObject>> {
|
|
|
- const url = new URL(`http://1.94.237.145:1337/parse/classes/${this.className}`);
|
|
|
- const params = new URLSearchParams();
|
|
|
-
|
|
|
- // 添加 where 参数
|
|
|
- if (this.whereOptions["where"]) {
|
|
|
- params.append('where', JSON.stringify(this.whereOptions["where"]));
|
|
|
- }
|
|
|
-
|
|
|
- // 添加 include 参数
|
|
|
- if (this.whereOptions["include"]) {
|
|
|
- params.append('include', this.whereOptions["include"].join(','));
|
|
|
- }
|
|
|
-
|
|
|
- // 添加 order 参数
|
|
|
- if (this.orderOptions.length > 0) {
|
|
|
- params.append('order', this.orderOptions.join(','));
|
|
|
- }
|
|
|
-
|
|
|
- // 添加 limit 参数
|
|
|
- if (this.limit !== null) {
|
|
|
- params.append('limit', this.limit.toString());
|
|
|
- }
|
|
|
-
|
|
|
- // 设置 URL 的搜索参数
|
|
|
- url.search = params.toString();
|
|
|
-
|
|
|
- const response = await fetch(url.href, {
|
|
|
- headers: {
|
|
|
- "X-Parse-Application-Id": "hcx",
|
|
|
- "X-Parse-Session-Token": this.sessionToken || ""
|
|
|
- },
|
|
|
- method: "GET",
|
|
|
- mode: "cors",
|
|
|
- credentials: "omit"
|
|
|
- });
|
|
|
-
|
|
|
- const json = await response.json();
|
|
|
- const results = json.results || [];
|
|
|
- return results.map((item: any) => this.dataToObj(item));
|
|
|
- }
|
|
|
- /**
|
|
|
- * @返回第一个查找到的结果
|
|
|
- * @返回结果参考:CloudObject {
|
|
|
- * id: 'e3wWcCfNwm',
|
|
|
- * data: {…},
|
|
|
- * className: '_User',
|
|
|
- * sessionToken:"r:c0f4fe9cb8eb83d7f0295ff57e948419"
|
|
|
- * createdAt: '2024-12-17T02:41:34.768Z',
|
|
|
- * updatedAt: '2024-12-23T00:33:15.961Z'}
|
|
|
- */
|
|
|
- async first() {
|
|
|
- let url = `http://1.94.237.145:1337/parse/classes/${this.className}?`;
|
|
|
-
|
|
|
- if (Object.keys(this.whereOptions["where"]).length) {
|
|
|
- const whereStr = JSON.stringify(this.whereOptions["where"]);
|
|
|
- console.log("first查询条件" + whereStr)
|
|
|
- url += `where=${whereStr}`;
|
|
|
- console.log("first方法请求URL:" + url)
|
|
|
- }
|
|
|
-
|
|
|
- const response = await fetch(url, {
|
|
|
- headers: {
|
|
|
- "if-none-match": "W/\"1f0-ghxH2EwTk6Blz0g89ivf2adBDKY\"",
|
|
|
- "x-parse-application-id": "hcx",
|
|
|
- "x-parse-session-token": this.sessionToken || "",
|
|
|
- },
|
|
|
- body: null,
|
|
|
- method: "GET",
|
|
|
- mode: "cors",
|
|
|
- credentials: "omit"
|
|
|
- });
|
|
|
-
|
|
|
- const json = await response?.json();
|
|
|
- const exists = json?.results?.[0] || null;
|
|
|
- if (exists) {
|
|
|
- let existsObject = this.dataToObj(exists)
|
|
|
- return existsObject;
|
|
|
- }
|
|
|
- return null
|
|
|
- }
|
|
|
-
|
|
|
- dataToObj(data: any): CloudObject {
|
|
|
- let obj = new CloudObject(this.className);
|
|
|
- obj.set(data);
|
|
|
- obj.id = data.objectId;
|
|
|
- obj.createdAt = data.createdAt;
|
|
|
- obj.updatedAt = data.updatedAt;
|
|
|
- return obj;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-// CloudUser.ts
|
|
|
-export class CloudUser extends CloudObject {
|
|
|
- constructor() {
|
|
|
- super("_User"); // 假设用户类在Parse中是"_User"
|
|
|
- // 读取用户缓存信息
|
|
|
- this.readCache();
|
|
|
- }
|
|
|
-
|
|
|
- //sessionToken: string | null = ""
|
|
|
- /** 获取当前用户信息 */
|
|
|
- current() {
|
|
|
- if (!this.sessionToken) {
|
|
|
- console.error("用户未登录");
|
|
|
- return null;
|
|
|
- }
|
|
|
- return this;
|
|
|
- }
|
|
|
-
|
|
|
- /**更新用户缓存 */
|
|
|
- async updateCache() {
|
|
|
- if (!this.sessionToken) {
|
|
|
- console.error("正在执行更新用户缓存:用户未登录");
|
|
|
- return;
|
|
|
- }
|
|
|
- let query = new CloudQuery("_User");
|
|
|
- query.equalTo("objectId", this.id)
|
|
|
- console.log("设置条件:objectId==" + this.id)
|
|
|
- let user: any = await query.find()
|
|
|
- console.log("查询到条件用户信息:")
|
|
|
- console.log(user)
|
|
|
- //需要把sessionToken也保存起来
|
|
|
- user.sessionToken = this.sessionToken
|
|
|
- localStorage.setItem("NCloud/hcx/User", JSON.stringify(user))
|
|
|
- console.log("已将该信息更新至用户缓存!请检查");
|
|
|
- this.readCache();
|
|
|
- return user;
|
|
|
- }
|
|
|
-
|
|
|
- /** 登录 */
|
|
|
- async loginByUsername(username: string, password: string): Promise<CloudUser | null> {
|
|
|
- const response = await fetch(`http://1.94.237.145:1337/parse/login`, {
|
|
|
- headers: {
|
|
|
- "x-parse-application-id": "hcx",
|
|
|
- "Content-Type": "application/json"
|
|
|
- },
|
|
|
- body: JSON.stringify({ username, password }),
|
|
|
- method: "POST"
|
|
|
- });
|
|
|
-
|
|
|
- const result = await response?.json();
|
|
|
- if (result?.error) {
|
|
|
- console.error(result?.error);
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- // 设置用户信息
|
|
|
- this.id = result?.objectId;
|
|
|
- this.sessionToken = result?.sessionToken;
|
|
|
- this.updatedAt = result?.updatedAt;
|
|
|
- this.className = '_User';
|
|
|
- this.createdAt = result?.createdAt;
|
|
|
- let excludedKeys = ["objectId", "sessionToken", "createdAt", "updatedAt"];
|
|
|
- for (let key of excludedKeys) {
|
|
|
- delete result[key];
|
|
|
- }
|
|
|
- this.data = result; // 保存用户数据
|
|
|
- // 缓存用户信息
|
|
|
- console.log("登录成功后即将缓存的信息如下:");
|
|
|
- console.log(this)
|
|
|
- localStorage.setItem("NCloud/hcx/User", JSON.stringify(this))
|
|
|
- return this;
|
|
|
- }
|
|
|
-
|
|
|
- async loginByPhone(phone: string, verCode: string, trueVerCode: string): Promise<CloudUser | null> {
|
|
|
- if (trueVerCode !== verCode) {
|
|
|
- console.error('验证码错误');
|
|
|
- return null;
|
|
|
- }
|
|
|
- let query = new CloudQuery("_User");
|
|
|
- query.equalTo("phone", phone);
|
|
|
- let user: any = await query.first();
|
|
|
- //查询手机号对应的用户信息
|
|
|
- let username: string = '';
|
|
|
- let password: string = 'secretpassword';
|
|
|
- if (!user?.id) {
|
|
|
- console.error('该手机号未注册');
|
|
|
- //用户名即为手机号
|
|
|
- username = phone.slice(0, 3) + '****' + phone.slice(-4);
|
|
|
- console.log('该手机号未注册,即将自动创建该用户:' + username + ',密码为:' + password);
|
|
|
- }
|
|
|
- //如果用户存在,则直接登录
|
|
|
- console.log('该手机号已注册,即将直接登录该用户')
|
|
|
- username = user.get('username');
|
|
|
- console.log('用户名:' + username + ',密码为:' + password);
|
|
|
- const response = await fetch(`http://1.94.237.145:1337/parse/login`, {
|
|
|
- headers: {
|
|
|
- "x-parse-application-id": "hcx",
|
|
|
- "Content-Type": "application/json"
|
|
|
- },
|
|
|
- body: JSON.stringify({ username, password }), // 假设后端需要这个字段名
|
|
|
- method: "POST"
|
|
|
- });
|
|
|
-
|
|
|
- const result = await response?.json();
|
|
|
- if (result?.error) {
|
|
|
- console.error(result?.error);
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- // 设置用户信息
|
|
|
- this.id = result?.objectId;
|
|
|
- this.sessionToken = result?.sessionToken;
|
|
|
- this.updatedAt = result?.updatedAt;
|
|
|
- this.className = '_User';
|
|
|
- this.createdAt = result?.createdAt;
|
|
|
- let excludedKeys = ["objectId", "sessionToken", "createdAt", "updatedAt"];
|
|
|
- for (let key of excludedKeys) {
|
|
|
- delete result[key];
|
|
|
- }
|
|
|
- this.data = result; // 保存用户数据
|
|
|
-
|
|
|
- // 缓存用户信息
|
|
|
- console.log("登录成功后即将缓存的信息如下:");
|
|
|
- console.log(this);
|
|
|
- localStorage.setItem("NCloud/hcx/User", JSON.stringify(this));
|
|
|
- return this;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- /** 登出 */
|
|
|
- async logout() {
|
|
|
- if (!this.sessionToken) {
|
|
|
- console.error("用户未登录");
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- const response = await fetch(`http://1.94.237.145:1337/parse/logout`, {
|
|
|
- headers: {
|
|
|
- "x-parse-application-id": "hcx",
|
|
|
- "x-parse-session-token": this.sessionToken
|
|
|
- },
|
|
|
- method: "POST"
|
|
|
- });
|
|
|
-
|
|
|
- let result = await response?.json();
|
|
|
-
|
|
|
- if (result?.error) {
|
|
|
- console.error(result?.error);
|
|
|
- if (result?.error == "Invalid session token") {
|
|
|
- this.clearUserCache()
|
|
|
- return true;
|
|
|
- }
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- this.clearUserCache()
|
|
|
- return true;
|
|
|
- }
|
|
|
- clearUserCache() {
|
|
|
- // 清除用户信息
|
|
|
- localStorage.removeItem("NCloud/hcx/User")
|
|
|
- this.id = null;
|
|
|
- this.sessionToken = null;
|
|
|
- this.data = {};
|
|
|
- }
|
|
|
-
|
|
|
- /** 注册 */
|
|
|
- async signUp(username: string, password: string, additionalData: Record<string, any> = {}) {
|
|
|
- const userData = {
|
|
|
- username,
|
|
|
- password,
|
|
|
- ...additionalData // 合并额外的用户数据
|
|
|
- };
|
|
|
-
|
|
|
- const response = await fetch(`http://1.94.237.145:1337/parse/users`, {
|
|
|
- headers: {
|
|
|
- "x-parse-application-id": "hcx",
|
|
|
- "Content-Type": "application/json"
|
|
|
- },
|
|
|
- body: JSON.stringify(userData),
|
|
|
- method: "POST"
|
|
|
- });
|
|
|
-
|
|
|
- const result = await response?.json();
|
|
|
- if (result?.error) {
|
|
|
- console.error(result?.error);
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- // 设置用户信息
|
|
|
- // 缓存用户信息
|
|
|
- console.log(result)
|
|
|
- localStorage.setItem("NCloud/hcx/User", JSON.stringify(result))
|
|
|
- this.id = result?.objectId;
|
|
|
- this.sessionToken = result?.sessionToken;
|
|
|
- this.data = result; // 保存用户数据
|
|
|
- return this;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * @读取缓存
|
|
|
- * @读取示例:json{
|
|
|
- * id: "5b29481e037c6a007f7d0320",
|
|
|
- * className: "_User",
|
|
|
- * createdAt: "2018-06-27T09:54:38.815Z",
|
|
|
- * updatedAt: "2018-06-27T09:54:38.815Z",
|
|
|
- * sessionToken: "<KEY>",
|
|
|
- * data: {...}
|
|
|
- * }
|
|
|
- */
|
|
|
- readCache() {
|
|
|
- console.log("正在执行readCache方法:读取用户缓存信息");
|
|
|
- let userCacheStr = localStorage.getItem("NCloud/hcx/User")
|
|
|
- console.log("读取到的用户缓存信息:");
|
|
|
- console.log(userCacheStr);
|
|
|
- if (userCacheStr) {
|
|
|
- let userData = JSON.parse(userCacheStr)
|
|
|
- console.log(userData)
|
|
|
- // 设置用户信息
|
|
|
- console.log(userData?.id)
|
|
|
- this.id = userData?.id;
|
|
|
- this.className = "_User";
|
|
|
- console.log(userData?.updatedAt)
|
|
|
- this.updatedAt = userData?.updatedAt;
|
|
|
- console.log(userData?.createdAt)
|
|
|
- this.createdAt = userData?.createdAt;
|
|
|
- console.log(userData?.sessionToken)
|
|
|
- this.sessionToken = userData?.sessionToken;
|
|
|
- this.data = userData.data // 保存用户数据
|
|
|
- console.log("用户缓存信息读取完毕:");
|
|
|
- console.log(this);
|
|
|
- }
|
|
|
- return this;
|
|
|
- }
|
|
|
-}
|