Browse Source

feat :tab1 evaluate

cainiao-hue 3 months ago
parent
commit
9c36c05d52

+ 16 - 14
soul-app/src/app/tab1/tab1.page.ts

@@ -8,8 +8,9 @@ import { Router } from '@angular/router';
 import { TopicDetail2Component } from '../topic-detail2/topic-detail2.component';
 import { TopicDetail3Component } from '../topic-detail3/topic-detail3.component';
 import { CloudObject, CloudQuery, CloudUser } from 'src/lib/ncloud';
-import { openUserLoginModal } from 'src/lib/user/modal-user-login/modal-user-login.component';
 import { ModalController } from '@ionic/angular/standalone';
+import { openUserEvaModal } from '../user-evaluate/user-evaluate.component';
+import { openUserLoginModal } from 'src/lib/user/modal-user-login/modal-user-login.component';
 @Component({
   selector: 'app-tab1',
   templateUrl: 'tab1.page.html',
@@ -74,16 +75,20 @@ export class Tab1Page {
     // 导航到指定的路由,并可以传递参数
     this.router.navigate([`tabs/${route}`, { id: topicId }]);
   }
-  private modalCtrl: ModalController;
-  constructor(private router: Router,modalCtrl: ModalController) {
-    this.modalCtrl = modalCtrl;
+  constructor(private router: Router,private modalCtrl: ModalController) {
     // 其他构造函数代码
   }
   matchedCounselor: { content: string; rating: number } | null = null;
-  
+  review: any = { rating: 5 };  // 示例数据
+  // 创建一个方法,用于生成填充了 null 的数组
+  createFilledArray(length: number): any[] {
+    return Array(length).fill(null);
+  }
   async evaluate() {
+    // 处理点击评价的逻辑
     // 验证用户登录
     let currentUser = new CloudUser();
+    let userPrompt = ``
     if(!currentUser?.id){
       console.log("用户未登录,请登录后重试")
       let user = await openUserLoginModal(this.modalCtrl)
@@ -95,15 +100,12 @@ export class Tab1Page {
         console.log("当前用户ID:", currentUser.id);
       }    
     }
-    // 处理点击评价的逻辑
-    console.log('用户点击了“进入评价”按钮');
-    // 您可以导航到一个新的页面来让用户填写评价,或者显示一个模态框等。
-  }
-  review: any = { rating: 5 };  // 示例数据
- 
-  // 创建一个方法,用于生成填充了 null 的数组
-  createFilledArray(length: number): any[] {
-    return Array(length).fill(null);
+     // 打开评价模态框
+     const evaluationResult = await openUserEvaModal(this.modalCtrl);
+     // 如果评价成功,重新加载评价列表
+     if (evaluationResult) {
+         await this.loadChatEvaluateList(); // 重新加载评价列表
+     }
   }
   ngOnInit() {
     this.loadChatEvaluateList()

+ 34 - 0
soul-app/src/app/user-evaluate/user-evaluate.component.html

@@ -0,0 +1,34 @@
+<!-- 用户登录状态 -->
+<ion-card>
+  <ion-card-header>
+    <ion-card-title>
+      用户名:{{currentUser?.get("username")}}
+    </ion-card-title>
+    <ion-card-subtitle >请输入您的评论信息</ion-card-subtitle>
+   </ion-card-header>
+ <ion-card-content>
+
+  <ion-item>
+    <ion-input 
+      [value]="userData['content']" 
+      (ionChange)="userDataChange('content', $event)" 
+      label="评价内容" 
+      placeholder="请您输入评价内容"></ion-input>
+  </ion-item>
+  <ion-item>
+    <ion-input 
+      type="number" 
+      [value]="userData['rating']" 
+      (ionChange)="userDataChange('rating', $event)" 
+      label="星星数量" 
+      placeholder="请您输入星星数量(1-5)" 
+      min="1" 
+      max="5"></ion-input>
+  </ion-item>
+
+   <ion-button expand="block" color="danger" (click)="save()">保存</ion-button>
+   <ion-button expand="block" color="danger" (click)="cancel()">取消</ion-button>
+ 
+
+  </ion-card-content>
+</ion-card>

+ 0 - 0
soul-app/src/app/user-evaluate/user-evaluate.component.scss


+ 22 - 0
soul-app/src/app/user-evaluate/user-evaluate.component.spec.ts

@@ -0,0 +1,22 @@
+import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
+
+import { UserEvaluateComponent } from './user-evaluate.component';
+
+describe('UserEvaluateComponent', () => {
+  let component: UserEvaluateComponent;
+  let fixture: ComponentFixture<UserEvaluateComponent>;
+
+  beforeEach(waitForAsync(() => {
+    TestBed.configureTestingModule({
+      imports: [UserEvaluateComponent],
+    }).compileComponents();
+
+    fixture = TestBed.createComponent(UserEvaluateComponent);
+    component = fixture.componentInstance;
+    fixture.detectChanges();
+  }));
+
+  it('should create', () => {
+    expect(component).toBeTruthy();
+  });
+});

+ 72 - 0
soul-app/src/app/user-evaluate/user-evaluate.component.ts

@@ -0,0 +1,72 @@
+import { Component, OnInit } from '@angular/core';
+import { ModalController } from '@ionic/angular/standalone';
+import { IonButton, IonCard, IonCardContent, IonCardHeader, IonCardSubtitle, IonCardTitle, 
+  IonContent, IonHeader, IonInput, IonItem, IonLabel, IonSegment, IonSegmentButton,
+   IonTitle, IonToolbar } from '@ionic/angular/standalone';
+import { CloudEvaluate, CloudUser } from 'src/lib/ncloud';
+
+@Component({
+  selector: 'app-user-evaluate',
+  templateUrl: './user-evaluate.component.html',
+  styleUrls: ['./user-evaluate.component.scss'],
+  standalone: true,
+  imports: [IonHeader, IonToolbar, IonTitle, IonContent, 
+    IonCard,IonCardContent,IonButton,IonCardHeader,IonCardTitle,IonCardSubtitle,
+    IonInput,IonItem,
+    IonSegment,IonSegmentButton,IonLabel
+  ],
+})
+export class UserEvaluateComponent  implements OnInit {
+
+  currentUser: CloudUser | undefined;
+  userData: any = {
+    content: '',
+    rating: 0,
+  };
+
+  constructor(private modalCtrl: ModalController) { 
+    this.currentUser = new CloudUser();
+  }
+
+  userDataChange(key: string, ev: any) {
+    let value = ev?.detail?.value;
+    if (value) {
+      this.userData[key] = value;
+    }
+  }
+
+  async save() {
+    // 确保 currentUser 是有效的 CloudUser 实例
+    if (!this.currentUser) {
+      console.error('当前用户未登录或未定义');
+      return; // 退出保存方法
+    }
+    const cloudEvaluate = new CloudEvaluate(this.userData, this.currentUser, this.modalCtrl);
+    const result = await cloudEvaluate.save(); // 调用 CloudEvaluate 的 save 方法
+    if (!result) {
+      console.error('评价保存失败');
+    }
+  }
+
+  cancel() {
+    this.modalCtrl.dismiss(null, "cancel");
+  }
+
+  ngOnInit() {}
+}
+  export async function openUserEvaModal(modalCtrl:ModalController):Promise<CloudUser|null>{
+    const modal = await modalCtrl.create({
+      component: UserEvaluateComponent,
+      breakpoints:[0.5,0.7],
+      initialBreakpoint:0.5
+    });
+    modal.present();
+  
+    const { data, role } = await modal.onWillDismiss();
+  
+    if (role === 'confirm') {
+      return data;
+    }
+    return null
+
+}

+ 51 - 2
soul-app/src/lib/ncloud.ts

@@ -28,8 +28,6 @@ export class CloudObject {
     }
 
     async save() {
-        console.log('Class Name:', this.className);
-        console.log('Object ID:', this.id);
         let method = "POST";
         let url = `http://dev.fmode.cn:1337/parse/classes/${this.className}`;
 
@@ -373,4 +371,55 @@ export class CloudUser extends CloudObject {
         localStorage.setItem("NCloud/dev/User",JSON.stringify(this.data))
         return this;
     }
+}
+
+// CloudEvaluate.ts
+export class CloudEvaluate extends CloudObject {
+    userData: Record<string, any>;
+    currentUser: CloudUser; // 添加当前用户的引用
+    modalCtrl: any; // 假设 modalCtrl 是通过构造函数传入的
+
+    constructor(userData: Record<string, any>, currentUser: CloudUser,modalCtrl: any) {
+        super("_ChatEvaluate"); 
+        this.userData = userData; // 保存用户评价数据
+        this.currentUser = currentUser; // 保存当前用户
+        this.modalCtrl = modalCtrl; // 保存模态控制器
+    }
+
+    /** 保存评价 */
+    override async save() {
+        // 确保 rating 是数字类型
+        if (this.userData['rating']) {
+            this.userData['rating'] = Number(this.userData['rating']);
+        }
+        console.log('保存评价被调用'); // 调试日志
+
+        // 调用后端 API 保存评价
+        const response = await fetch(`http://dev.fmode.cn:1337/parse/classes/ChatEvaluate`, {
+            headers: {
+                "x-parse-application-id": "dev",
+                "Content-Type": "application/json"
+            },
+            body: JSON.stringify({
+                content: this.userData['content'],
+                rating: this.userData['rating'],
+                avatar: this.currentUser.data?.['avatar'], // 假设用户头像存储在 data.avatar 中
+                user: { __type: "Pointer", className: "_User", objectId: this.currentUser.id } // 添加用户指针
+            }),
+            method: "POST"
+        });
+
+        const result = await response.json();
+        console.log('保存结果:', result); // 输出保存结果
+
+        if (result?.error) {
+            console.error(result?.error);
+            console.error('评价保存失败');
+            return null;
+        }
+
+        console.log('评价保存成功:', result);
+        this.modalCtrl.dismiss(result, "confirm"); // 关闭模态框并传递结果
+        return result; // 返回保存的结果
+    }
 }