tab2.page.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import { Component, OnInit } from '@angular/core';
  2. import { Router } from '@angular/router';
  3. import { AlertController, ModalController } from '@ionic/angular';
  4. interface KnowledgeItem {
  5. id: string;
  6. title: string;
  7. summary: string;
  8. image: string;
  9. type: 'article' | 'video';
  10. category: string;
  11. tag?: 'emergency' | 'prevention' | 'expert';
  12. duration?: string;
  13. isEmergency?: boolean;
  14. }
  15. interface Expert {
  16. id: string;
  17. name: string;
  18. avatar: string;
  19. specialty: string;
  20. isOnline: boolean;
  21. rating: number;
  22. }
  23. @Component({
  24. selector: 'app-tab2',
  25. templateUrl: './tab2.page.html',
  26. styleUrls: ['./tab2.page.scss'],
  27. standalone: false,
  28. })
  29. export class Tab2Page implements OnInit {
  30. searchQuery = '';
  31. isLargeFont = false;
  32. currentCategory = 'all';
  33. categories = [
  34. { id: 'all', name: '全部', icon: 'apps' },
  35. { id: 'wheat', name: '小麦', icon: 'leaf' },
  36. { id: 'rice', name: '水稻', icon: 'water' },
  37. { id: 'corn', name: '玉米', icon: 'ear' },
  38. { id: 'fruit', name: '果树', icon: 'nutrition' },
  39. ];
  40. onlineExperts: Expert[] = [
  41. {
  42. id: '1',
  43. name: '张农艺师',
  44. avatar: 'assets/images/expert1.jpg',
  45. specialty: '水稻种植专家',
  46. isOnline: true,
  47. rating: 4.8
  48. },
  49. {
  50. id: '2',
  51. name: '李研究员',
  52. avatar: 'assets/images/expert2.jpg',
  53. specialty: '果树病虫害防治',
  54. isOnline: true,
  55. rating: 4.9
  56. },
  57. {
  58. id: '3',
  59. name: '王技术员',
  60. avatar: 'assets/images/expert3.jpg',
  61. specialty: '土壤改良',
  62. isOnline: true,
  63. rating: 4.7
  64. }
  65. ];
  66. knowledgeList: KnowledgeItem[] = [
  67. {
  68. id: '1',
  69. title: '小麦赤霉病防治',
  70. summary: '识别赤霉病早期症状及防治方案',
  71. image: 'assets/images/wheat-disease.jpg',
  72. type: 'article',
  73. category: 'wheat',
  74. tag: 'emergency',
  75. isEmergency: true
  76. },
  77. {
  78. id: '2',
  79. title: '水稻科学灌溉技巧',
  80. summary: '不同生长阶段的水分管理要点',
  81. image: 'assets/images/rice-irrigation.jpg',
  82. type: 'article',
  83. category: 'rice',
  84. tag: 'expert',
  85. duration: '5分钟阅读'
  86. },
  87. {
  88. id: '3',
  89. title: '果树修剪实操指南',
  90. summary: '视频演示冬季修剪的正确方法',
  91. image: 'assets/images/fruit-pruning.jpg',
  92. type: 'video',
  93. category: 'fruit',
  94. duration: '12分钟'
  95. },
  96. {
  97. id: '4',
  98. title: '玉米螟虫预防措施',
  99. summary: '生物防治与化学防治结合方案',
  100. image: 'assets/images/corn-pest.jpg',
  101. type: 'article',
  102. category: 'corn',
  103. tag: 'prevention'
  104. },
  105. {
  106. id: '5',
  107. title: '土壤改良方法',
  108. summary: '酸性土壤改良的7种有效方式',
  109. image: 'assets/images/soil-improve.jpg',
  110. type: 'article',
  111. category: 'all',
  112. duration: '8分钟阅读'
  113. }
  114. ];
  115. filteredKnowledge: KnowledgeItem[] = [];
  116. constructor(
  117. private router: Router,
  118. private alertCtrl: AlertController
  119. ) {}
  120. ngOnInit() {
  121. this.filteredKnowledge = [...this.knowledgeList];
  122. }
  123. toggleFontSize() {
  124. this.isLargeFont = !this.isLargeFont;
  125. }
  126. refreshData() {
  127. // 模拟刷新数据
  128. this.filteredKnowledge = [...this.knowledgeList];
  129. this.searchQuery = '';
  130. this.currentCategory = 'all';
  131. }
  132. resetFilters() {
  133. this.refreshData();
  134. }
  135. onSearch() {
  136. if (!this.searchQuery) {
  137. this.filteredKnowledge = [...this.knowledgeList];
  138. return;
  139. }
  140. const query = this.searchQuery.toLowerCase();
  141. this.filteredKnowledge = this.knowledgeList.filter(item =>
  142. item.title.toLowerCase().includes(query) ||
  143. item.summary.toLowerCase().includes(query)
  144. );
  145. }
  146. startVoiceSearch() {
  147. console.log('启动语音搜索');
  148. // 实际应集成语音识别API
  149. }
  150. filterByCategory() {
  151. if (this.currentCategory === 'all') {
  152. this.filteredKnowledge = [...this.knowledgeList];
  153. } else {
  154. this.filteredKnowledge = this.knowledgeList.filter(
  155. item => item.category === this.currentCategory
  156. );
  157. }
  158. }
  159. navigateToDetail(item: KnowledgeItem) {
  160. this.router.navigate(['/knowledge-detail', item.id]);
  161. }
  162. getTagColor(tag: string): string {
  163. switch (tag) {
  164. case 'emergency': return 'danger';
  165. case 'prevention': return 'warning';
  166. case 'expert': return 'success';
  167. default: return 'primary';
  168. }
  169. }
  170. getTagLabel(tag: string): string {
  171. switch (tag) {
  172. case 'emergency': return '紧急';
  173. case 'prevention': return '预防';
  174. case 'expert': return '专家推荐';
  175. default: return '';
  176. }
  177. }
  178. }