|
@@ -1,82 +1,225 @@
|
|
|
-import { ApiService } from './../api.service';
|
|
|
import { Component, OnInit } from '@angular/core';
|
|
|
-// import { IonHeader, IonToolbar, IonTitle, IonContent,IonButton, IonButtons } from '@ionic/angular/standalone';
|
|
|
-import { ExploreContainerComponent } from '../explore-container/explore-container.component';
|
|
|
-import { FormsModule } from '@angular/forms';
|
|
|
-import { CommonModule } from '@angular/common';
|
|
|
-import { IonicModule } from '@ionic/angular';
|
|
|
+import { HttpClient } from '@angular/common/http';
|
|
|
+import { Router } from '@angular/router';
|
|
|
import { addIcons } from 'ionicons';
|
|
|
import { search } from 'ionicons/icons';
|
|
|
-// import { Router } from 'express';
|
|
|
-import { Router } from '@angular/router';
|
|
|
+import { catchError, Observable, of, tap } from 'rxjs';
|
|
|
+import { CommonModule } from '@angular/common';
|
|
|
+import { IonicModule } from '@ionic/angular';
|
|
|
+import { FormsModule } from '@angular/forms';
|
|
|
|
|
|
@Component({
|
|
|
selector: 'app-tab2',
|
|
|
templateUrl: 'tab2.page.html',
|
|
|
styleUrls: ['tab2.page.scss'],
|
|
|
standalone: true,
|
|
|
- imports: [CommonModule, FormsModule,
|
|
|
- IonicModule, ExploreContainerComponent]
|
|
|
+ imports: [CommonModule, IonicModule, FormsModule]
|
|
|
})
|
|
|
export class Tab2Page implements OnInit {
|
|
|
- selectedSegment: string = 'collection'; // 默认选择分类标签
|
|
|
- books: any[] = []; // 所有书籍
|
|
|
- filteredBooks: any[] = []; // 筛选后的书籍
|
|
|
- categories: string[] = []; // 分类列表
|
|
|
-
|
|
|
- categoryBooks: { [key: string]: any[] } = {}; // 动态存储各个类别的书籍
|
|
|
- constructor(private apiService: ApiService,private router:Router) {
|
|
|
- addIcons({search});
|
|
|
+ selectedSegment: string = 'collection'; // 默认选择分类标签
|
|
|
+ books: any[] = []; // 所有书籍
|
|
|
+ filteredBooks: any[] = []; // 筛选后的书籍
|
|
|
+ categories: any[] = []; // 分类列表
|
|
|
+
|
|
|
+ private baseUrl = 'http://localhost:3000/api'; // 后端 API 基础 URL
|
|
|
+ private loading = false; // 加载状态标志
|
|
|
+
|
|
|
+ constructor(private http: HttpClient, private router: Router) {
|
|
|
+ addIcons({ search });
|
|
|
}
|
|
|
|
|
|
- ngOnInit(){
|
|
|
- // 假设此数据来自数据库
|
|
|
- this.books = [
|
|
|
- { id: 1, title: '唐诗全集' },
|
|
|
- { id: 2, title: '诗经全集' },
|
|
|
- // 可以从数据库获取书籍数据
|
|
|
- ];
|
|
|
- this.apiService.getBooks().subscribe(data=>{
|
|
|
- this.books = data;
|
|
|
- this.filteredBooks = this.books;
|
|
|
- },error =>{
|
|
|
- console.error('Error feching books',error);
|
|
|
+ ngOnInit() {
|
|
|
+ this.loadCategories();
|
|
|
+ this.loadBooks();
|
|
|
+ }
|
|
|
+
|
|
|
+ private getBooks(categoryId?: number): Observable<any[]> {
|
|
|
+ const url = `${this.baseUrl}/books${categoryId ? `?category_id=${categoryId}` : ''}`;
|
|
|
+ return this.http.get<any[]>(url).pipe(
|
|
|
+ catchError(error => {
|
|
|
+ console.error('Error fetching books', error);
|
|
|
+ return of([]);
|
|
|
+ })
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ private getCategories(): Observable<any[]> {
|
|
|
+ return this.http.get<any[]>(`${this.baseUrl}/categories`).pipe(
|
|
|
+ catchError(error => {
|
|
|
+ console.error('Error fetching categories', error);
|
|
|
+ return of([]);
|
|
|
+ })
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ loadCategories(): void {
|
|
|
+ this.getCategories().subscribe(data => {
|
|
|
+ this.categories = data;
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- segmentChanged(event: any) {
|
|
|
- const newSegment = event.detail.value;
|
|
|
- if (newSegment === 'collection') {
|
|
|
- this.selectCategory(this.categories[0]); // 默认选择第一个类别
|
|
|
- } else {
|
|
|
- this.filteredBooks = []; // 清空书籍列表
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- selectBook(book: any) {
|
|
|
- console.log('Selected book:', book);
|
|
|
- }
|
|
|
-
|
|
|
- openSearch() {
|
|
|
- console.log('Search clicked');
|
|
|
- }
|
|
|
-
|
|
|
- selectCategory(category: string) {
|
|
|
- console.log('Selected category:', category);
|
|
|
- // 根据类别筛选书籍
|
|
|
- if (this.categoryBooks[category]) {
|
|
|
- this.filteredBooks = this.categoryBooks[category];
|
|
|
- } else {
|
|
|
- this.filteredBooks = this.books.filter(book => book.category === category);
|
|
|
-
|
|
|
- // 如果没有匹配到任何书籍,显示提示信息
|
|
|
- if (this.filteredBooks.length === 0) {
|
|
|
- this.filteredBooks = [{ title: '暂无相关书籍', count: 0 }];
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- goToBookManagement(bookId: number) {
|
|
|
- this.router.navigate(['/book-management', bookId]);
|
|
|
+ loadBooks(categoryId?: number): void {
|
|
|
+ this.loading = true;
|
|
|
+ this.getBooks(categoryId).pipe(
|
|
|
+ tap(data => {
|
|
|
+ this.books = data;
|
|
|
+ this.filteredBooks = categoryId ? data : this.books;
|
|
|
+ }),
|
|
|
+ tap(() => this.loading = false)
|
|
|
+ ).subscribe();
|
|
|
+ }
|
|
|
+
|
|
|
+ segmentChanged(event: any): void {
|
|
|
+ const newSegment = event.detail.value;
|
|
|
+ this.selectedSegment = newSegment;
|
|
|
+
|
|
|
+ if (newSegment === 'collection') {
|
|
|
+ this.filteredBooks = this.books;
|
|
|
+ } else {
|
|
|
+ this.filteredBooks = [];
|
|
|
+ // 根据需要添加其他逻辑
|
|
|
}
|
|
|
+ }
|
|
|
+
|
|
|
+ selectCategory(categoryId: number): void {
|
|
|
+ console.log('Selected category ID:', categoryId);
|
|
|
+ this.loadBooks(categoryId); // 使用新的方法加载特定分类的书籍
|
|
|
+ }
|
|
|
+
|
|
|
+ goToBookManagement(bookId: number): void {
|
|
|
+ if (bookId) {
|
|
|
+ this.router.navigate(['book-management', bookId]);
|
|
|
+ } else {
|
|
|
+ console.warn('Invalid book ID');
|
|
|
}
|
|
|
+ }
|
|
|
+
|
|
|
+ openSearch(): void {
|
|
|
+ console.log('Search clicked');
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+// import { Component, OnInit } from '@angular/core';
|
|
|
+// import { CommonModule } from '@angular/common';
|
|
|
+// import { IonicModule } from '@ionic/angular';
|
|
|
+// import { HttpClient } from '@angular/common/http';
|
|
|
+// import { Router } from '@angular/router';
|
|
|
+// import { addIcons } from 'ionicons';
|
|
|
+// import { search } from 'ionicons/icons';
|
|
|
+// import { FormsModule } from '@angular/forms';
|
|
|
+// import { catchError, of, tap ,Observable} from 'rxjs';
|
|
|
+
|
|
|
+// @Component({
|
|
|
+// selector: 'app-tab2',
|
|
|
+// templateUrl: 'tab2.page.html',
|
|
|
+// styleUrls: ['tab2.page.scss'],
|
|
|
+// standalone: true,
|
|
|
+// imports: [CommonModule, IonicModule, FormsModule]
|
|
|
+// })
|
|
|
+// export class Tab2Page implements OnInit {
|
|
|
+// selectedSegment: string = 'collection'; // 默认选择分类标签
|
|
|
+// books: any[] = []; // 所有书籍
|
|
|
+// filteredBooks: any[] = []; // 筛选后的书籍
|
|
|
+// categories: any[] = []; // 分类列表
|
|
|
+
|
|
|
+// private baseUrl = 'http://localhost:3000/api'; // 后端 API 基础 URL
|
|
|
+// private loading = false; // 加载状态标志
|
|
|
+
|
|
|
+// constructor(private http: HttpClient, private router: Router) {
|
|
|
+// addIcons({ search });
|
|
|
+// }
|
|
|
+
|
|
|
+// ngOnInit() {
|
|
|
+// this.loadCategories();
|
|
|
+// this.loadBooks();
|
|
|
+// }
|
|
|
+
|
|
|
+// loadCategories(): void {
|
|
|
+// this.http.get<any[]>(`${this.baseUrl}/categories`)
|
|
|
+// .pipe(
|
|
|
+// tap(data => this.categories = data),
|
|
|
+// catchError(error => {
|
|
|
+// console.error('Error fetching categories', error);
|
|
|
+// return of([]);
|
|
|
+// })
|
|
|
+// ).subscribe();
|
|
|
+// }
|
|
|
+
|
|
|
+// loadBooks(categoryId?: number): void {
|
|
|
+// this.loading = true;
|
|
|
+// this.http.get<any[]>(`${this.baseUrl}/books${categoryId ? `?category_id=${categoryId}` : ''}`)
|
|
|
+// .pipe(
|
|
|
+// tap(data => {
|
|
|
+// this.books = data;
|
|
|
+// this.filteredBooks = categoryId ? data : this.books;
|
|
|
+// }),
|
|
|
+// catchError(error => {
|
|
|
+// console.error('Error fetching books', error);
|
|
|
+// return of([]);
|
|
|
+// }),
|
|
|
+// tap(() => this.loading = false)
|
|
|
+// ).subscribe();
|
|
|
+// }
|
|
|
+
|
|
|
+// // ngOnInit() {
|
|
|
+// // // 获取分类数据
|
|
|
+// // this.getCategories().subscribe((data: any[]) => {
|
|
|
+// // this.categories = data; // 保存分类数据
|
|
|
+// // }, (error: any) => {
|
|
|
+// // console.error('Error fetching categories', error);
|
|
|
+// // });
|
|
|
+
|
|
|
+// // // 获取书籍数据
|
|
|
+// // this.getBooks().subscribe((data: any[]) => {
|
|
|
+// // this.books = data;
|
|
|
+// // this.filteredBooks = this.books;
|
|
|
+// // }, (error: any) => {
|
|
|
+// // console.error('Error fetching books', error);
|
|
|
+// // });
|
|
|
+// // }
|
|
|
+
|
|
|
+// getBooks(categoryId?: number) {
|
|
|
+// const url = `${this.baseUrl}/books${categoryId ? `?category_id=${categoryId}` : ''}`;
|
|
|
+// return this.http.get<any[]>(url); // 获取书籍数据
|
|
|
+// }
|
|
|
+
|
|
|
+// getCategories() {
|
|
|
+// return this.http.get<any[]>(`${this.baseUrl}/categories`); // 获取分类数据
|
|
|
+// }
|
|
|
+
|
|
|
+// segmentChanged(event: any) {
|
|
|
+// const newSegment = event.detail.value;
|
|
|
+// if (newSegment === 'collection') {
|
|
|
+// this.filteredBooks = this.books; // 默认显示所有书籍
|
|
|
+// } else if (newSegment === 'works') {
|
|
|
+// this.filteredBooks = []; // 清空书籍列表,您可以根据需要添加逻辑
|
|
|
+// } else if (newSegment === 'authors') {
|
|
|
+// this.filteredBooks = []; // 清空书籍列表,您可以根据需要添加逻辑
|
|
|
+// }
|
|
|
+// }
|
|
|
+
|
|
|
+// selectCategory(categoryId: number) {
|
|
|
+// console.log('Selected category ID:', categoryId);
|
|
|
+// // 根据类别获取书籍
|
|
|
+// this.getBooks(categoryId).subscribe({
|
|
|
+// next: data => {
|
|
|
+// this.filteredBooks = data; // 更新书籍列表
|
|
|
+// },
|
|
|
+// error: err => {
|
|
|
+// console.error('Error fetching books for category', err);
|
|
|
+// },
|
|
|
+// complete: () => {
|
|
|
+// console.log('Request completed'); // 可选的完成回调
|
|
|
+// }
|
|
|
+// });
|
|
|
+// }
|
|
|
+
|
|
|
+// goToBookManagement(bookId: number) {
|
|
|
+// this.router.navigate(['book-management', bookId]); // 直接传递 bookId
|
|
|
+// }
|
|
|
|
|
|
+// openSearch() {
|
|
|
+// console.log('Search clicked');
|
|
|
+// }
|
|
|
+// }
|