|
@@ -1,14 +1,113 @@
|
|
|
import { Component } from '@angular/core';
|
|
|
-import { IonHeader, IonToolbar, IonTitle, IonContent } from '@ionic/angular/standalone';
|
|
|
+import { IonHeader, IonToolbar, IonTitle, IonContent, IonCard, IonCardHeader, IonCardTitle, IonCardContent, IonItem, IonLabel, IonInput, IonButton, IonTextarea, IonList } from '@ionic/angular/standalone';
|
|
|
import { ExploreContainerComponent } from '../explore-container/explore-container.component';
|
|
|
+import { AuthService } from '../services/auth.service'; // 导入 AuthService
|
|
|
+import { PostService } from '../services/post.service'; // 导入 PostService
|
|
|
+import { Router } from '@angular/router'; // 导入 Router
|
|
|
+import { FormsModule } from '@angular/forms';
|
|
|
+import { CommonModule } from '@angular/common';
|
|
|
|
|
|
@Component({
|
|
|
selector: 'app-tab3',
|
|
|
templateUrl: 'tab3.page.html',
|
|
|
styleUrls: ['tab3.page.scss'],
|
|
|
standalone: true,
|
|
|
- imports: [IonHeader, IonToolbar, IonTitle, IonContent, ExploreContainerComponent],
|
|
|
+ imports: [IonHeader, IonToolbar, IonTitle, IonContent, ExploreContainerComponent
|
|
|
+,IonCard
|
|
|
+,IonCardHeader
|
|
|
+,IonCardTitle
|
|
|
+,IonCardContent
|
|
|
+,IonItem
|
|
|
+,IonLabel
|
|
|
+,IonInput
|
|
|
+,IonButton
|
|
|
+,IonTextarea
|
|
|
+,IonList
|
|
|
+,IonItem
|
|
|
+,FormsModule
|
|
|
+,CommonModule
|
|
|
+ ],
|
|
|
})
|
|
|
export class Tab3Page {
|
|
|
- constructor() {}
|
|
|
+ // 用户注册和登录属性
|
|
|
+ registerUsername: string = '';
|
|
|
+ registerPassword: string = '';
|
|
|
+ loginUsername: string = '';
|
|
|
+ loginPassword: string = '';
|
|
|
+
|
|
|
+ // 发布动态属性
|
|
|
+ postContent: string = '';
|
|
|
+ image: File | null = null;
|
|
|
+
|
|
|
+ // 动态列表
|
|
|
+ posts: any[] = [];
|
|
|
+
|
|
|
+ constructor(
|
|
|
+ private authService: AuthService, // 注入 AuthService
|
|
|
+ private postService: PostService, // 注入 PostService
|
|
|
+ private router: Router // 注入 Router
|
|
|
+ ) {}
|
|
|
+
|
|
|
+ ngOnInit() {
|
|
|
+ this.loadPosts(); // 加载动态列表
|
|
|
+ }
|
|
|
+
|
|
|
+ // 注册用户
|
|
|
+ register() {
|
|
|
+ this.authService.register(this.registerUsername, this.registerPassword).subscribe(
|
|
|
+ (response:any) => {
|
|
|
+ alert('注册成功!');
|
|
|
+ this.router.navigate(['/login']); // 注册成功后导航到登录页面
|
|
|
+ },
|
|
|
+ (error:any) => {
|
|
|
+ alert('注册失败!');
|
|
|
+ }
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ // 登录用户
|
|
|
+ login() {
|
|
|
+ this.authService.login(this.loginUsername, this.loginPassword).subscribe(
|
|
|
+ (response:any) => {
|
|
|
+ alert('登录成功!');
|
|
|
+ this.router.navigate(['/posts']); // 登录成功后导航到动态列表页面
|
|
|
+ },
|
|
|
+ (error:any) => {
|
|
|
+ alert('登录失败!');
|
|
|
+ }
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理文件选择
|
|
|
+ onFileSelected(event: any) {
|
|
|
+ this.image = event.target.files[0]; // 获取用户选择的文件
|
|
|
+ }
|
|
|
+
|
|
|
+ // 发布动态
|
|
|
+ createPost() {
|
|
|
+ const formData = new FormData(); // 创建 FormData 对象
|
|
|
+ formData.append('content', this.postContent); // 将动态内容添加到 FormData
|
|
|
+ if (this.image) {
|
|
|
+ formData.append('image', this.image); // 将图片添加到 FormData
|
|
|
+ }
|
|
|
+
|
|
|
+ this.postService.createPost(formData).subscribe(
|
|
|
+ (response:any) => {
|
|
|
+ alert('动态发布成功!'); // 发布成功提示
|
|
|
+ this.postContent = ''; // 清空输入框
|
|
|
+ this.image = null; // 清空选择的图片
|
|
|
+ this.loadPosts(); // 重新加载动态列表
|
|
|
+ },
|
|
|
+ (error:any) => {
|
|
|
+ alert('发布失败!'); // 发布失败提示
|
|
|
+ }
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ // 加载动态列表
|
|
|
+ loadPosts() {
|
|
|
+ this.postService.getPosts().subscribe((data:any) => {
|
|
|
+ this.posts = data; // 设置动态列表
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|