Browse Source

feat: consultation with user equalTo

未来全栈 4 months ago
parent
commit
5c835bdb24

+ 2 - 2
wisdom-app/src/app/page/page-my-health/page-my-health.component.html

@@ -16,8 +16,8 @@
          <ion-card-title>就诊时间:{{message?.updatedAt}}</ion-card-title>
        </ion-card-header>
        <ion-card-content>
-        <p>就诊医生{{message.data["doctor"]}}</p>
-        <p>就诊部门{{message.data["depart"]}}</p>
+        <p>就诊医生{{message.data["doctor"]?.name}}</p>
+        <p>就诊部门{{message.data["depart"]?.name}}</p>
         <p>门诊名称{{message.data["title"]}}</p>
         <p>就诊内容{{message.data["content"]}}</p>
         <!-- <fm-markdown-preview class="content-style" [content]="responseMsg"></fm-markdown-preview> -->

+ 3 - 1
wisdom-app/src/app/page/page-my-health/page-my-health.component.ts

@@ -44,9 +44,11 @@ export class PageMyHealthComponent  implements OnInit {
   allMessage : Array<CloudObject> = [];
 
   async loadData(){
+    let user = new CloudUser();
     console.log("objectId",this.objectId);
     let query = new CloudQuery('Consultation');
-    console.log(query.include("user"))
+    query.include("doctor","depart");
+    query.equalTo("user",user?.id);
     // 根据用户id查询, 其中用户id字段是指针,指向User表中的objectId
     // query.equalTo("user",{ "__type": "Pointer", "className": "_User", "objectId": this.objectId });
     this.allMessage = await query.find();

+ 55 - 32
wisdom-app/src/lib/ncloud.ts

@@ -16,7 +16,7 @@ export class CloudObject {
 
     set(json: Record<string, any>) {
         Object.keys(json).forEach(key => {
-            if (["objectId", "id", "createdAt", "updatedAt", "ACL"].indexOf(key) > -1) {
+            if (["objectId", "id", "createdAt", "updatedAt"].indexOf(key) > -1) {
                 return;
             }
             this.data[key] = json[key];
@@ -29,7 +29,7 @@ export class CloudObject {
 
     async save() {
         let method = "POST";
-        let url = `http://dev.fmode.cn:1337/parse/classes/${this.className}`;
+        let url = `https://dev.fmode.cn/parse/classes/${this.className}`;
 
         // 更新
         if (this.id) {
@@ -61,7 +61,7 @@ export class CloudObject {
 
     async destroy() {
         if (!this.id) return;
-        const response = await fetch(`http://dev.fmode.cn:1337/parse/classes/${this.className}/${this.id}`, {
+        const response = await fetch(`https://dev.fmode.cn/parse/classes/${this.className}/${this.id}`, {
             headers: {
                 "x-parse-application-id": "dev"
             },
@@ -87,7 +87,7 @@ export class CloudQuery {
     constructor(className: string) {
         this.className = className;
     }
-    // 作用是将查询参数转换为对象
+
     include(...fileds:string[]) {
         this.queryParams["include"] = fileds;
     }
@@ -112,11 +112,12 @@ export class CloudQuery {
     }
 
     equalTo(key: string, value: any) {
+        if (!this.queryParams["where"]) this.queryParams["where"] = {};
         this.queryParams["where"][key] = value;
     }
 
     async get(id: string) {
-        const url = `http://dev.fmode.cn:1337/parse/classes/${this.className}/${id}?`;
+        const url = `https://dev.fmode.cn/parse/classes/${this.className}/${id}?`;
 
         const response = await fetch(url, {
             headers: {
@@ -130,35 +131,31 @@ export class CloudQuery {
         });
 
         const json = await response?.json();
-        // return json || {};
-        const exists = json?.results?.[0] || null;
-        if (exists) {
-            let existsObject = this.dataToObj(exists)
+        if (json) {
+            let existsObject = this.dataToObj(json)
             return existsObject;
         }
         return null
-
     }
 
-    async find() {
-        let url = `http://dev.fmode.cn:1337/parse/classes/${this.className}?`;
+    async find():Promise<Array<CloudObject>> {
+        let url = `https://dev.fmode.cn/parse/classes/${this.className}?`;
 
         let queryStr = ``
         Object.keys(this.queryParams).forEach(key=>{
-            let paramStr = JSON.stringify(this.queryParams[key]); // 作用是将对象转换为JSON字符串
+            let paramStr = JSON.stringify(this.queryParams[key]);
             if(key=="include"){
                 paramStr = this.queryParams[key]?.join(",")
             }
-            if(key=="where"){
-                paramStr = JSON.stringify(this.queryParams[key]);
-
-            }
             if(queryStr) {
                 url += `${key}=${paramStr}`;
             }else{
                 url += `&${key}=${paramStr}`;
             }
         })
+        // if (Object.keys(this.queryParams["where"]).length) {
+            
+        // }
 
         const response = await fetch(url, {
             headers: {
@@ -179,7 +176,7 @@ export class CloudQuery {
 
 
     async first() {
-        let url = `http://dev.fmode.cn:1337/parse/classes/${this.className}?`;
+        let url = `https://dev.fmode.cn/parse/classes/${this.className}?`;
 
         if (Object.keys(this.queryParams["where"]).length) {
             const whereStr = JSON.stringify(this.queryParams["where"]);
@@ -198,15 +195,12 @@ export class CloudQuery {
         });
 
         const json = await response?.json();
-        // const exists = json?.results?.[0] || null;
-        // if (exists) {
-        //     let existsObject = this.dataToObj(exists)
-        //     return existsObject;
-        // }
-        // return null
-        let list = json?.results || []
-        let objList = list.map((item:any)=>this.dataToObj(item))
-        return objList || [];
+        const exists = json?.results?.[0] || null;
+        if (exists) {
+            let existsObject = this.dataToObj(exists)
+            return existsObject;
+        }
+        return null
     }
 
     dataToObj(exists:any):CloudObject{
@@ -242,7 +236,7 @@ export class CloudUser extends CloudObject {
             return null;
         }
         return this;
-        // const response = await fetch(`http://dev.fmode.cn:1337/parse/users/me`, {
+        // const response = await fetch(`https://dev.fmode.cn/parse/users/me`, {
         //     headers: {
         //         "x-parse-application-id": "dev",
         //         "x-parse-session-token": this.sessionToken // 使用sessionToken进行身份验证
@@ -260,7 +254,7 @@ export class CloudUser extends CloudObject {
 
     /** 登录 */
     async login(username: string, password: string):Promise<CloudUser|null> {
-        const response = await fetch(`http://dev.fmode.cn:1337/parse/login`, {
+        const response = await fetch(`https://dev.fmode.cn/parse/login`, {
             headers: {
                 "x-parse-application-id": "dev",
                 "Content-Type": "application/json"
@@ -292,7 +286,7 @@ export class CloudUser extends CloudObject {
             return;
         }
 
-        const response = await fetch(`http://dev.fmode.cn:1337/parse/logout`, {
+        const response = await fetch(`https://dev.fmode.cn/parse/logout`, {
             headers: {
                 "x-parse-application-id": "dev",
                 "x-parse-session-token": this.sessionToken
@@ -322,7 +316,7 @@ export class CloudUser extends CloudObject {
             ...additionalData // 合并额外的用户数据
         };
 
-        const response = await fetch(`http://dev.fmode.cn:1337/parse/users`, {
+        const response = await fetch(`https://dev.fmode.cn/parse/users`, {
             headers: {
                 "x-parse-application-id": "dev",
                 "Content-Type": "application/json"
@@ -349,7 +343,7 @@ export class CloudUser extends CloudObject {
 
     override async save() {
         let method = "POST";
-        let url = `http://dev.fmode.cn:1337/parse/users`;
+        let url = `https://dev.fmode.cn/parse/users`;
     
         // 更新用户信息
         if (this.id) {
@@ -386,4 +380,33 @@ export class CloudUser extends CloudObject {
         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
+    }){
+
+        let reqOpts:any =  {
+            headers: {
+                "x-parse-application-id": "dev",
+                "Content-Type": "application/json"
+            },
+            method: options?.method || "POST",
+            mode: "cors",
+            credentials: "omit"
+        }
+        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 json = await response.json();
+        return json
+    }
 }