page-psysurvey.component_20241223232115.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { CommonModule } from '@angular/common';
  2. import { Component, OnInit } from '@angular/core';
  3. import { Router } from '@angular/router';
  4. import { IonHeader,IonButton, IonContent, IonIcon, IonItem, IonLabel, IonList,
  5. IonListHeader,IonCardHeader,IonCardTitle,IonCardContent, IonTitle,IonCard, IonToolbar,IonInput,IonSearchbar } from '@ionic/angular/standalone';
  6. import { HttpClient } from '@angular/common/http';
  7. import { UserService } from '../user.service'; // 确保路径正确
  8. @Component({
  9. selector: 'app-page-psysurvey',
  10. templateUrl: './page-psysurvey.component.html',
  11. styleUrls: ['./page-psysurvey.component.scss'],
  12. standalone: true,
  13. imports: [IonHeader,IonToolbar,IonTitle,IonContent,
  14. IonList,IonListHeader,IonItem,IonCardTitle,
  15. IonLabel,IonIcon,IonButton,IonCardContent,
  16. IonInput,IonSearchbar,IonCard,IonCardHeader,
  17. CommonModule,HttpClient
  18. ]
  19. })
  20. export class PagePsysurveyComponent implements OnInit {
  21. surveys: any[] = []; // 存储问卷通知
  22. filteredSurveys: any[] = []; // 存储过滤后的问卷
  23. userApartment: string = '' ; // 当前用户的学院
  24. constructor(private router: Router,private http: HttpClient,private userService: UserService) { }
  25. goTab1(){
  26. this.router.navigate(['tabs/tab1']);
  27. }
  28. goPublishSurvey(){
  29. this.router.navigate(['tabs/page-publishsurvey'])
  30. }
  31. ngOnInit() {
  32. // 动态获取当前用户的学院信息
  33. this.userService.getCurrentUser().subscribe(user => {
  34. this.userApartment = user.apartment; // 从用户数据中获取学院
  35. this.getSurveys(); // 在获取到用户信息后获取问卷
  36. }, error => {
  37. console.error('获取用户信息时出错:', error);
  38. // 可以设置一个默认值或处理错误
  39. });
  40. }
  41. getSurveys() {
  42. this.http.get('http://127.0.0.1:4040/apps/DevServer/browser/survey').subscribe(
  43. (response: any) => {
  44. this.surveys = response; // 假设响应是问卷数组
  45. this.filterSurveys(); // 过滤问卷
  46. },
  47. error => {
  48. console.error('获取问卷时出错:', error);
  49. }
  50. );
  51. }
  52. filterSurveys() {
  53. this.filteredSurveys = this.surveys.filter(survey => {
  54. return survey.audience === this.userApartment || survey.audience === 'all';
  55. });
  56. }
  57. }