tab1.page.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import { Component, ElementRef, OnInit, Renderer2 } from '@angular/core';
  2. import {
  3. IonHeader,
  4. IonToolbar,
  5. IonTitle,
  6. IonContent,
  7. IonApp,
  8. IonMenu,
  9. IonButtons,
  10. IonMenuButton,
  11. IonCardHeader,
  12. IonCardTitle,
  13. IonCardSubtitle,
  14. IonCardContent,
  15. IonList,
  16. IonItem,
  17. IonThumbnail,
  18. IonLabel,
  19. IonGrid,
  20. IonRow,
  21. IonCol,
  22. IonAlert,
  23. IonCard,
  24. IonCheckbox,
  25. IonInput,
  26. IonNavLink,
  27. IonButton,
  28. IonSearchbar,
  29. IonSegment,
  30. IonSegmentButton,
  31. ModalController,
  32. IonFab,
  33. IonFabButton,
  34. IonIcon,
  35. } from '@ionic/angular/standalone';
  36. import { ExploreContainerComponent } from '../explore-container/explore-container.component';
  37. import { TodoEditComponent } from '../component/todo-edit/todo-edit.component';
  38. import { TodolistPageComponent } from '../page/todolist-page/todolist-page.component';
  39. import { Router } from '@angular/router';
  40. import { AiplanPageComponent } from '../page/aiplan-page/aiplan-page.component';
  41. import { CloudQuery, CloudNews, CloudUser } from 'src/lib/cyxncloud';
  42. import { CommonModule } from '@angular/common';
  43. import { ModalBrowsePagePage } from '../page/modal/modal-browse-page/modal-browse-page.page';
  44. import { DraggableButtonComponent } from '../component/draggable-button/draggable-button.component';
  45. @Component({
  46. selector: 'app-tab1',
  47. templateUrl: 'tab1.page.html',
  48. styleUrls: ['tab1.page.scss'],
  49. standalone: true,
  50. imports: [
  51. IonHeader,
  52. IonToolbar,
  53. IonTitle,
  54. IonContent,
  55. ExploreContainerComponent,
  56. IonApp,
  57. IonMenu,
  58. IonButtons,
  59. IonButton,
  60. IonMenuButton,
  61. IonCard,
  62. IonCardHeader,
  63. IonCardTitle,
  64. IonCardSubtitle,
  65. IonCardContent,
  66. IonList,
  67. IonItem,
  68. IonThumbnail,
  69. IonLabel,
  70. IonGrid,
  71. IonRow,
  72. IonCol,
  73. IonAlert,
  74. TodoEditComponent,
  75. IonCheckbox,
  76. IonInput,
  77. IonNavLink,
  78. AiplanPageComponent,
  79. IonSearchbar,
  80. CommonModule,
  81. IonSegment,
  82. IonSegmentButton,
  83. IonFab,
  84. IonFabButton,
  85. IonIcon,
  86. DraggableButtonComponent,
  87. ],
  88. })
  89. export class Tab1Page implements OnInit {
  90. constructor(
  91. private router: Router,
  92. private el: ElementRef,
  93. private renderer: Renderer2,
  94. private modalCtrl: ModalController
  95. ) {
  96. this.getAllNews();
  97. }
  98. ngOnInit() {
  99. this.getAllNews();
  100. const button = this.el.nativeElement.querySelector('#draggable-button');
  101. }
  102. alertButtons = ['打卡'];
  103. duringday: number = 10;
  104. goodday: number = 20;
  105. badday: number = 5;
  106. todolist: Array<string> = ['have a good day'];
  107. sexChecked: boolean = false;
  108. // 查看页面详细信息
  109. async openModal(news: any) {
  110. const modal = await this.modalCtrl.create({
  111. component: ModalBrowsePagePage,
  112. componentProps: {
  113. newsData: news, // 将news数据传递给子组件
  114. },
  115. });
  116. modal.present();
  117. }
  118. // 横向导航栏
  119. navItems = [
  120. { label: '首页', link: '/home' },
  121. { label: '健康·生活', link: '/health' },
  122. { label: '社会', link: '/society' },
  123. { label: '法制', link: '/legal' },
  124. { label: '教育', link: '/education' },
  125. { label: '科普', link: '/science' },
  126. { label: '体育', link: '/physical' },
  127. { label: '文旅', link: '/cultural' },
  128. ];
  129. ngAfterViewInit() {
  130. // 在视图初始化之后动态生成导航按钮
  131. const navContainer = this.el.nativeElement.querySelector('.horizontal-nav');
  132. for (let i = 0; i < this.navItems.length; i++) {
  133. const item = this.navItems[i];
  134. const button = this.renderer.createElement('button');
  135. const text = this.renderer.createText(item.label);
  136. this.renderer.appendChild(button, text);
  137. this.renderer.addClass(button, 'nav-item');
  138. this.renderer.listen(button, 'click', () => this.navigateTo(item.link));
  139. this.renderer.appendChild(navContainer, button);
  140. }
  141. }
  142. // 跳转页面
  143. navigateTo(path: string) {
  144. this.router.navigate([`/tabs/${path}`]);
  145. }
  146. goodbody() {
  147. let allChange = document.querySelector('#allChange');
  148. if (allChange) {
  149. allChange.setAttribute('color', 'success');
  150. }
  151. this.goodday++;
  152. this.duringday++;
  153. this.sexChecked = true;
  154. }
  155. badbody() {
  156. let allChange = document.querySelector('#allChange');
  157. if (allChange) {
  158. allChange.setAttribute('color', 'danger');
  159. }
  160. this.badday++;
  161. this.duringday = 0;
  162. this.sexChecked = true;
  163. }
  164. changeTodolist(ev: any) {
  165. this.todolist = ev;
  166. }
  167. deleteThisTodolist(todo: string) {
  168. let idx = this.todolist.findIndex((item) => item === todo);
  169. this.todolist.splice(idx, 1);
  170. }
  171. // 首页新闻
  172. newslists: any[] = [];
  173. is_loading: boolean = true;
  174. async getAllNews() {
  175. let className = 'News';
  176. let newsQuery = new CloudQuery(className);
  177. let newslist = await newsQuery.getAll();
  178. this.newslists = newslist.results;
  179. this.newslists = newslist.results.map((news: any) => {
  180. // 删除所有空格,并替换换行符\n\n为</p><p>,并在开始和结束处添加<p>和</p>
  181. news.article =
  182. '<p>' +
  183. news.article.replace(/\n\n/g, '</p><p>').replace(/\s+/g, '') +
  184. '</p>';
  185. return news;
  186. });
  187. this.is_loading = false;
  188. }
  189. // test
  190. test() {
  191. let newsData = {
  192. author: new CloudUser().toPointer(),
  193. theme: '健康',
  194. title: 'hhhh',
  195. };
  196. console.log('newsData', newsData);
  197. let news = new CloudNews();
  198. let res = news.add(newsData);
  199. }
  200. }