|
@@ -4,6 +4,7 @@ export class CloudObject {
|
|
|
id: string | null = null;
|
|
|
createdAt: any;
|
|
|
updatedAt: any;
|
|
|
+ sessionToken: string | null = ""
|
|
|
data: Record<string, any> = {};
|
|
|
|
|
|
constructor(className: string) {
|
|
@@ -36,11 +37,13 @@ export class CloudObject {
|
|
|
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,
|
|
@@ -48,7 +51,7 @@ export class CloudObject {
|
|
|
mode: "cors",
|
|
|
credentials: "omit"
|
|
|
});
|
|
|
-
|
|
|
+ console.log(response);
|
|
|
const result = await response?.json();
|
|
|
if (result?.error) {
|
|
|
console.error(result?.error);
|
|
@@ -82,6 +85,7 @@ export class CloudObject {
|
|
|
// CloudQuery.ts
|
|
|
export class CloudQuery {
|
|
|
className: string;
|
|
|
+ sessionToken: string | null = "";
|
|
|
whereOptions: Record<string, any> = {};
|
|
|
|
|
|
constructor(className: string) {
|
|
@@ -154,7 +158,16 @@ export class CloudQuery {
|
|
|
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}?`;
|
|
|
|
|
@@ -166,7 +179,8 @@ export class CloudQuery {
|
|
|
const response = await fetch(url, {
|
|
|
headers: {
|
|
|
"if-none-match": "W/\"1f0-ghxH2EwTk6Blz0g89ivf2adBDKY\"",
|
|
|
- "x-parse-application-id": "hcx"
|
|
|
+ "x-parse-application-id": "hcx",
|
|
|
+ "x-parse-session-token":this.sessionToken||"",
|
|
|
},
|
|
|
body: null,
|
|
|
method: "GET",
|
|
@@ -198,23 +212,10 @@ export class CloudUser extends CloudObject {
|
|
|
constructor() {
|
|
|
super("_User"); // 假设用户类在Parse中是"_User"
|
|
|
// 读取用户缓存信息
|
|
|
- console.log("正在读取用户缓存信息");
|
|
|
- let userCacheStr = localStorage.getItem("NCloud/hcx/User")
|
|
|
- if (userCacheStr) {
|
|
|
- let userData = JSON.parse(userCacheStr)
|
|
|
- // 设置用户信息
|
|
|
- this.id = userData?.id;
|
|
|
- this.className = "_User";
|
|
|
- this.updatedAt = userData?.updatedAt;
|
|
|
- this.createdAt = userData?.createdAt;
|
|
|
- this.sessionToken = userData?.sessionToken;
|
|
|
- this.data = userData.data // 保存用户数据
|
|
|
- console.log("用户缓存信息读取完毕:");
|
|
|
- console.log(this);
|
|
|
- }
|
|
|
+ this.readCache();
|
|
|
}
|
|
|
|
|
|
- sessionToken: string | null = ""
|
|
|
+ //sessionToken: string | null = ""
|
|
|
/** 获取当前用户信息 */
|
|
|
async current() {
|
|
|
if (!this.sessionToken) {
|
|
@@ -222,20 +223,6 @@ export class CloudUser extends CloudObject {
|
|
|
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;*/
|
|
|
}
|
|
|
|
|
|
/**更新用户缓存 */
|
|
@@ -247,29 +234,14 @@ export class CloudUser extends CloudObject {
|
|
|
let query = new CloudQuery("_User");
|
|
|
query.equalTo("objectId", this.id)
|
|
|
console.log("设置条件:objectId==" + this.id)
|
|
|
- let user:any = await query.first()
|
|
|
+ let user: any = await query.first()
|
|
|
console.log("查询到条件用户信息:")
|
|
|
console.log(user)
|
|
|
//需要把sessionToken也保存起来
|
|
|
user.sessionToken = this.sessionToken
|
|
|
localStorage.setItem("NCloud/hcx/User", JSON.stringify(user))
|
|
|
console.log("已将该信息更新至用户缓存!请检查");
|
|
|
- console.log("准备读取新的用户缓存信息");
|
|
|
- let userCacheStr = localStorage.getItem("NCloud/hcx/User")
|
|
|
- console.log("读取到的用户缓存信息:");
|
|
|
- console.log(userCacheStr);
|
|
|
- if (userCacheStr) {
|
|
|
- let userData = JSON.parse(userCacheStr)
|
|
|
- // 设置用户信息
|
|
|
- this.id = userData?.id;
|
|
|
- this.className = "_User";
|
|
|
- this.updatedAt = userData?.updatedAt;
|
|
|
- this.createdAt = userData?.createdAt;
|
|
|
- this.sessionToken = userData?.sessionToken;
|
|
|
- this.data = userData.data // 保存用户数据
|
|
|
- console.log("用户缓存信息读取完毕:");
|
|
|
- console.log(this);
|
|
|
- }
|
|
|
+ this.readCache();
|
|
|
}
|
|
|
|
|
|
/** 登录 */
|
|
@@ -292,7 +264,7 @@ export class CloudUser extends CloudObject {
|
|
|
// 设置用户信息
|
|
|
this.id = result?.objectId;
|
|
|
this.sessionToken = result?.sessionToken;
|
|
|
- this.updatedAt=result?.updatedAt;
|
|
|
+ this.updatedAt = result?.updatedAt;
|
|
|
this.className = '_User';
|
|
|
this.createdAt = result?.createdAt;
|
|
|
let excludedKeys = ["objectId", "sessionToken", "createdAt", "updatedAt"];
|
|
@@ -307,13 +279,33 @@ export class CloudUser extends CloudObject {
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
- async loginByPhone(phone: string, verCode: string): Promise<CloudUser | null> {
|
|
|
- const response = await fetch(`http://1.94.237.145:1337/parse/users`, {
|
|
|
+ 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: phone, password: verCode }),
|
|
|
+ body: JSON.stringify({ username,password }), // 假设后端需要这个字段名
|
|
|
method: "POST"
|
|
|
});
|
|
|
|
|
@@ -322,6 +314,23 @@ export class CloudUser extends CloudObject {
|
|
|
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;
|
|
|
}
|
|
|
|
|
@@ -387,4 +396,35 @@ export class CloudUser extends CloudObject {
|
|
|
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)
|
|
|
+ // 设置用户信息
|
|
|
+ this.id = userData?.id;
|
|
|
+ this.className = "_User";
|
|
|
+ this.updatedAt = userData?.updatedAt;
|
|
|
+ this.createdAt = userData?.createdAt;
|
|
|
+ this.sessionToken = userData?.sessionToken;
|
|
|
+ this.data = userData.data // 保存用户数据
|
|
|
+ console.log("用户缓存信息读取完毕:");
|
|
|
+ console.log(this);
|
|
|
+ }
|
|
|
+ return this;
|
|
|
+ }
|
|
|
}
|