page-psysurvey.component_20241223235522.ts 2.3 KB

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