tab1.page.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. import { Component, AfterViewInit } from '@angular/core';
  2. import { Router } from '@angular/router';
  3. import { ModalController, NavController } from '@ionic/angular';
  4. import { HttpClient } from '@angular/common/http';
  5. import { Observable } from 'rxjs';
  6. import { map, catchError } from 'rxjs/operators';
  7. @Component({
  8. selector: 'app-tab1',
  9. templateUrl: 'tab1.page.html',
  10. styleUrls: ['tab1.page.scss']
  11. })
  12. export class Tab1Page implements AfterViewInit {
  13. doctors = [
  14. {
  15. avatar: '../../assets/images/doctor.png',
  16. name: '张医生',
  17. specialty: '心血管科'
  18. },
  19. {
  20. avatar: '../../assets/images/doctor.png',
  21. name: '李医生',
  22. specialty: '神经科'
  23. },
  24. {
  25. avatar: '../../assets/images/doctor.png',
  26. name: '王医生',
  27. specialty: '儿科'
  28. },
  29. {
  30. avatar: '../../assets/images/doctor.png',
  31. name: '赵医生',
  32. specialty: '外科'
  33. },
  34. {
  35. avatar: '../../assets/images/doctor.png',
  36. name: '陈医生',
  37. specialty: '内科'
  38. }
  39. ];
  40. // 功能按钮数据
  41. functionItems1 = [
  42. { label: '我的病历', icon: 'document-text', route: '/medical-records' },
  43. { label: '在线问诊', icon: 'chatbubbles', route: '/online-consultation' },
  44. { label: '健康档案', icon: 'person', route: '/health-profile' },
  45. { label: '预约挂号', icon: 'calendar', route: '/appointment' },
  46. { label: '健康资讯', icon: 'newspaper', route: '/health-news' }
  47. ];
  48. functionItems2 = [
  49. { label: '药品购买', icon: 'medkit', route: '/medicine-purchase' },
  50. { label: '体检套餐', icon: 'clipboard', route: '/checkup-packages' },
  51. { label: '专家讲座', icon: 'podium', route: '/expert-lectures' },
  52. { label: '远程医疗', icon: 'videocam', route: '/telemedicine' },
  53. { label: '健康社区', icon: 'people', route: '/health-community' }
  54. ];
  55. currentIndex = 0;
  56. startX: number = 0; // 初始化 startX
  57. endX: number = 0; // 初始化 endX
  58. isDragging: boolean = false; // 标记是否正在拖动
  59. currentTranslate: number = 0; // 当前的平移值
  60. cardWidth = 216; // 每个卡片的宽度加上间距 (200 + 16)
  61. isCyclic: boolean = true; // 是否启用循环滑动
  62. ngAfterViewInit() {
  63. this.updateCarousel(); // 确保初始状态正确
  64. this.calculateCardWidth(); // 动态计算卡片宽度
  65. }
  66. // 计算卡片宽度
  67. calculateCardWidth() {
  68. const container = document.getElementById('carousel');
  69. if (container && container.children.length > 0) {
  70. const firstCard = container.children[0] as HTMLElement;
  71. this.cardWidth = firstCard.offsetWidth + 16; // 加上间距
  72. }
  73. }
  74. // 更新轮播位置
  75. updateCarousel() {
  76. this.currentTranslate = -this.currentIndex * this.cardWidth;
  77. this.updateCarouselPosition();
  78. }
  79. // 更新轮播位置
  80. updateCarouselPosition() {
  81. const container = document.getElementById('carousel');
  82. if (container) {
  83. container.style.transform = `translateX(${this.currentTranslate}px)`;
  84. }
  85. }
  86. // 触摸开始
  87. onTouchStart(event: TouchEvent) {
  88. this.startX = event.touches[0].clientX;
  89. this.isDragging = true;
  90. }
  91. // 触摸移动
  92. onTouchMove(event: TouchEvent) {
  93. if (this.isDragging) {
  94. const touchX = event.touches[0].clientX;
  95. const deltaX = touchX - this.startX;
  96. this.currentTranslate = -this.currentIndex * this.cardWidth + deltaX; // 动态更新平移值
  97. // 限制 currentTranslate 的值
  98. const maxTranslate = -(this.doctors.length - 1) * this.cardWidth;
  99. this.currentTranslate = Math.max(Math.min(this.currentTranslate, 0), maxTranslate);
  100. this.updateCarouselPosition();
  101. }
  102. }
  103. // 触摸结束
  104. onTouchEnd(event: TouchEvent) {
  105. this.endX = event.changedTouches[0].clientX;
  106. this.isDragging = false;
  107. const swipeThreshold = 50; // 滑动阈值
  108. const deltaX = this.startX - this.endX;
  109. if (deltaX > swipeThreshold) {
  110. // 向左滑动
  111. this.nextSlide();
  112. } else if (deltaX < -swipeThreshold) {
  113. // 向右滑动
  114. this.prevSlide();
  115. } else {
  116. // 如果滑动距离不够,则恢复到原来的位置
  117. this.snapToNearestCard();
  118. }
  119. }
  120. // 上一张
  121. prevSlide() {
  122. if (this.isCyclic) {
  123. if (this.currentIndex === 0) {
  124. this.currentIndex = this.doctors.length - 1;
  125. } else {
  126. this.currentIndex--;
  127. }
  128. } else {
  129. if (this.currentIndex > 0) {
  130. this.currentIndex--;
  131. }
  132. }
  133. this.updateCarousel();
  134. }
  135. // 下一张
  136. nextSlide() {
  137. if (this.isCyclic) {
  138. if (this.currentIndex === this.doctors.length - 1) {
  139. this.currentIndex = 0;
  140. } else {
  141. this.currentIndex++;
  142. }
  143. } else {
  144. if (this.currentIndex < this.doctors.length - 1) {
  145. this.currentIndex++;
  146. }
  147. }
  148. this.updateCarousel();
  149. }
  150. // 快照到最近的卡片
  151. snapToNearestCard() {
  152. const cardIndex = Math.round(-this.currentTranslate / this.cardWidth);
  153. this.currentIndex = Math.max(0, Math.min(cardIndex, this.doctors.length - 1));
  154. this.updateCarousel();
  155. }
  156. // 在线咨询按钮点击事件
  157. onConsultNow() {
  158. console.log(`立即咨询`);
  159. this.router.navigate(['/consultation']);
  160. }
  161. // 在线咨询按钮点击事件
  162. onlineConsultNow(doctor: any) {
  163. console.log(`在线咨询: ${doctor.name}`);
  164. // 跳转到 ConsultationPage 并传递医生信息
  165. this.router.navigate(['/consultation'], { state: { doctor: doctor } });
  166. }
  167. // 导航到指定路由
  168. navigateTo(route: string) {
  169. this.router.navigate([route]);
  170. }
  171. // 发布求医信息
  172. publishHealthInfo() {
  173. // 这里可以添加发布求医信息的逻辑
  174. console.log('发布求医信息');
  175. }
  176. isLoginModalOpen = false; // 声明 isLoginModalOpen 属性
  177. user = {
  178. username: '',
  179. password: ''
  180. };
  181. constructor(
  182. private router: Router,
  183. private modalController: ModalController,
  184. private navCtrl: NavController,
  185. private http: HttpClient // 注入 HttpClient
  186. ) {}
  187. openLoginModal() {
  188. this.isLoginModalOpen = true;
  189. console.log('打开登录/注册模态框');
  190. }
  191. closeLoginModal() {
  192. this.isLoginModalOpen = false;
  193. console.log('关闭登录/注册模态框');
  194. }
  195. onLoginModalDismissed(event: any) {
  196. this.isLoginModalOpen = false;
  197. console.log('登录/注册模态框已关闭');
  198. }
  199. onLoginFormSubmit(formValue: any) {
  200. // 处理登录逻辑
  201. console.log('登录表单提交:', formValue);
  202. // 发送登录请求
  203. this.loginUser(formValue.username, formValue.password)
  204. .subscribe(
  205. (response) => {
  206. console.log('登录成功:', response);
  207. // 这里可以处理登录成功的逻辑,例如跳转到主页
  208. this.closeLoginModal();
  209. },
  210. (error) => {
  211. console.error('登录失败:', error);
  212. // 这里可以处理登录失败的逻辑,例如显示错误消息
  213. this.closeLoginModal();
  214. }
  215. );
  216. }
  217. registerUser() {
  218. // 处理注册逻辑
  219. console.log('注册用户:', this.user);
  220. // 发送注册请求
  221. this.registerNewUser(this.user.username, this.user.password)
  222. .subscribe(
  223. (response) => {
  224. console.log('注册成功:', response);
  225. // 这里可以处理注册成功的逻辑,例如跳转到登录页面
  226. this.closeLoginModal();
  227. },
  228. (error) => {
  229. console.error('注册失败:', error);
  230. // 这里可以处理注册失败的逻辑,例如显示错误消息
  231. this.closeLoginModal();
  232. }
  233. );
  234. }
  235. private loginUser(username: string, password: string): Observable<any> {
  236. const loginUrl = 'YOUR_API_ENDPOINT/login'; // 替换为你的登录 API 端点
  237. return this.http.post(loginUrl, { username, password })
  238. .pipe(
  239. map(response => response),
  240. catchError(error => {
  241. throw error;
  242. })
  243. );
  244. }
  245. private registerNewUser(username: string, password: string): Observable<any> {
  246. const registerUrl = 'YOUR_API_ENDPOINT/register'; // 替换为你的注册 API 端点
  247. return this.http.post(registerUrl, { username, password })
  248. .pipe(
  249. map(response => response),
  250. catchError(error => {
  251. throw error;
  252. })
  253. );
  254. }
  255. // 新增方法:处理“点击了解更多”按钮点击事件
  256. onLearnMore() {
  257. // 跳转到详情页面
  258. this.navCtrl.navigateForward('/details');
  259. }
  260. }