|
@@ -1,11 +1,11 @@
|
|
|
+import { IonicModule } from '@ionic/angular';
|
|
|
import { Component, OnInit } from '@angular/core';
|
|
|
import { HttpClient } from '@angular/common/http';
|
|
|
import { Router } from '@angular/router';
|
|
|
import { addIcons } from 'ionicons';
|
|
|
import { search } from 'ionicons/icons';
|
|
|
-import { catchError, Observable, of, tap } from 'rxjs';
|
|
|
+import { CloudObject, CloudQuery } from 'src/lib/ncloud';
|
|
|
import { CommonModule } from '@angular/common';
|
|
|
-import { IonicModule } from '@ionic/angular';
|
|
|
import { FormsModule } from '@angular/forms';
|
|
|
|
|
|
@Component({
|
|
@@ -17,209 +17,48 @@ import { FormsModule } from '@angular/forms';
|
|
|
})
|
|
|
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; // 加载状态标志
|
|
|
+ selectedGenre: string = ''; // 存储选中的体裁
|
|
|
+ PLPoemsList: Array<CloudObject> = []; // 所有诗歌列表
|
|
|
+ filteredPoemsList: Array<CloudObject> = []; // 筛选后的诗歌列表
|
|
|
|
|
|
constructor(private http: HttpClient, private router: Router) {
|
|
|
addIcons({ search });
|
|
|
}
|
|
|
|
|
|
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([]);
|
|
|
- })
|
|
|
- );
|
|
|
+ this.loadPLPoemsList();
|
|
|
}
|
|
|
|
|
|
- loadCategories(): void {
|
|
|
- this.getCategories().subscribe(data => {
|
|
|
- this.categories = data;
|
|
|
- });
|
|
|
+ // 加载所有诗歌
|
|
|
+ async loadPLPoemsList() {
|
|
|
+ let query = new CloudQuery("PL_Poems");
|
|
|
+ this.PLPoemsList = await query.find();
|
|
|
+ this.filteredPoemsList = this.PLPoemsList; // 初始化时显示所有诗歌
|
|
|
}
|
|
|
|
|
|
- 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 = [];
|
|
|
- // 根据需要添加其他逻辑
|
|
|
+ // 选择体裁
|
|
|
+ onGenreSelect(genre: string | number | undefined) {
|
|
|
+ if (typeof genre === 'string') {
|
|
|
+ this.selectedGenre = genre;
|
|
|
+ this.filterPoemsByGenre(genre);
|
|
|
+ } else if (typeof genre === 'number') {
|
|
|
+ // 如果 genre 是数字,可以根据需要进行处理,这里假设需要转换为字符串
|
|
|
+ this.selectedGenre = genre.toString();
|
|
|
+ this.filterPoemsByGenre(this.selectedGenre);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- 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]);
|
|
|
+ // 根据体裁过滤诗歌列表
|
|
|
+ filterPoemsByGenre(genre: string) {
|
|
|
+ if (genre) {
|
|
|
+ this.filteredPoemsList = this.PLPoemsList.filter(book => book.get('genre') === genre);
|
|
|
+ //query.equalTo('genre', 'genre'); // 添加查询条件来限制 type="对应的genre"
|
|
|
} else {
|
|
|
- console.warn('Invalid book ID');
|
|
|
+ this.filteredPoemsList = this.PLPoemsList; // 如果没有选择体裁,显示所有诗歌
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- openSearch(): void {
|
|
|
- console.log('Search clicked');
|
|
|
+ goToPoemDetail(bookcard: any) {
|
|
|
+ this.router.navigate(['/poem-detail'],{ queryParams: { id: bookcard.id } });
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
-// 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');
|
|
|
-// }
|
|
|
-// }
|