|
@@ -1,13 +1,13 @@
|
|
|
import { Component } from '@angular/core';
|
|
|
-import { IonicModule } from '@ionic/angular';
|
|
|
+import { IonicModule,AlertController } from '@ionic/angular';
|
|
|
import { CommonModule } from '@angular/common';
|
|
|
import { FormsModule } from '@angular/forms';
|
|
|
import { addIcons } from 'ionicons';
|
|
|
-import {
|
|
|
- chevronBack,
|
|
|
- settings,
|
|
|
- cloudUpload,
|
|
|
- chevronForward,
|
|
|
+import {
|
|
|
+ chevronBack,
|
|
|
+ settings,
|
|
|
+ cloudUpload,
|
|
|
+ chevronForward,
|
|
|
statsChart,
|
|
|
diamond,
|
|
|
calendar,
|
|
@@ -21,8 +21,10 @@ import {
|
|
|
add,
|
|
|
barbell,
|
|
|
restaurant,
|
|
|
- shareSocial
|
|
|
+ shareSocial,
|
|
|
+ person
|
|
|
} from 'ionicons/icons';
|
|
|
+import { CloudUser } from 'src/lib/ncloud';
|
|
|
|
|
|
@Component({
|
|
|
selector: 'app-tab4',
|
|
@@ -33,31 +35,128 @@ import {
|
|
|
})
|
|
|
export class Tab4Page {
|
|
|
vipPanelOpen = false;
|
|
|
+ currentUser: CloudUser | null = null;
|
|
|
+ isLoggedIn = false;
|
|
|
+ username = '请登录';
|
|
|
|
|
|
- constructor() {
|
|
|
- // 注册所有需要的图标
|
|
|
+ constructor(private alertCtrl: AlertController) {
|
|
|
addIcons({
|
|
|
- chevronBack,
|
|
|
- settings,
|
|
|
- cloudUpload,
|
|
|
- chevronForward,
|
|
|
- statsChart,
|
|
|
- diamond,
|
|
|
- calendar,
|
|
|
- alarm,
|
|
|
- star,
|
|
|
- videocam,
|
|
|
- trophy,
|
|
|
- nutrition,
|
|
|
- wallet,
|
|
|
- gift,
|
|
|
- add,
|
|
|
- barbell,
|
|
|
- restaurant,
|
|
|
- shareSocial
|
|
|
+ settings, cloudUpload, chevronForward, statsChart,
|
|
|
+ diamond, calendar, alarm, star, videocam, trophy,
|
|
|
+ nutrition, wallet, gift, person
|
|
|
});
|
|
|
+ this.checkLoginStatus();
|
|
|
}
|
|
|
|
|
|
+ async checkLoginStatus() {
|
|
|
+ this.currentUser = new CloudUser();
|
|
|
+ const user = await this.currentUser.current();
|
|
|
+ if (user && user.id) {
|
|
|
+ this.isLoggedIn = true;
|
|
|
+ this.username = user.get('username') || '用户';
|
|
|
+ } else {
|
|
|
+ this.isLoggedIn = false;
|
|
|
+ this.username = '请登录';
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async showLoginAlert() {
|
|
|
+ if (this.isLoggedIn) return;
|
|
|
+
|
|
|
+ const alert = await this.alertCtrl.create({
|
|
|
+ header: '登录',
|
|
|
+ inputs: [
|
|
|
+ {
|
|
|
+ name: 'username',
|
|
|
+ type: 'text',
|
|
|
+ placeholder: '用户名'
|
|
|
+ },
|
|
|
+ {
|
|
|
+ name: 'password',
|
|
|
+ type: 'password',
|
|
|
+ placeholder: '密码'
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ buttons: [
|
|
|
+ {
|
|
|
+ text: '取消',
|
|
|
+ role: 'cancel'
|
|
|
+ },
|
|
|
+ {
|
|
|
+ text: '登录',
|
|
|
+ handler: async (data:any) => {
|
|
|
+ if (!data.username || !data.password) {
|
|
|
+ return false; // 阻止关闭
|
|
|
+ }
|
|
|
+ await this.handleLogin(data.username, data.password);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ },
|
|
|
+ {
|
|
|
+ text: '注册',
|
|
|
+ handler: async (data:any) => {
|
|
|
+ if (!data.username || !data.password) {
|
|
|
+ return false; // 阻止关闭
|
|
|
+ }
|
|
|
+ await this.handleSignUp(data.username, data.password);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ]
|
|
|
+ });
|
|
|
+
|
|
|
+ await alert.present();
|
|
|
+ }
|
|
|
+
|
|
|
+ async handleLogin(username: string, password: string) {
|
|
|
+ const user = new CloudUser();
|
|
|
+ try {
|
|
|
+ await user.login(username, password);
|
|
|
+ if (user.id) {
|
|
|
+ this.isLoggedIn = true;
|
|
|
+ this.username = username;
|
|
|
+ } else {
|
|
|
+ this.showErrorAlert('登录失败');
|
|
|
+ }
|
|
|
+ } catch (error) {
|
|
|
+ this.showErrorAlert('登录错误: ' + (error instanceof Error ? error.message : '未知错误'));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async handleSignUp(username: string, password: string) {
|
|
|
+ const user = new CloudUser();
|
|
|
+ try {
|
|
|
+ await user.signUp(username, password);
|
|
|
+ if (user.id) {
|
|
|
+ this.isLoggedIn = true;
|
|
|
+ this.username = username;
|
|
|
+ } else {
|
|
|
+ this.showErrorAlert('注册失败');
|
|
|
+ }
|
|
|
+ } catch (error) {
|
|
|
+ this.showErrorAlert('注册错误: ' + (error instanceof Error ? error.message : '未知错误'));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async showErrorAlert(message: string) {
|
|
|
+ const alert = await this.alertCtrl.create({
|
|
|
+ header: '错误',
|
|
|
+ message: message,
|
|
|
+ buttons: ['确定']
|
|
|
+ });
|
|
|
+ await alert.present();
|
|
|
+ }
|
|
|
+
|
|
|
+ async logout() {
|
|
|
+ if (!this.isLoggedIn) return;
|
|
|
+
|
|
|
+ const user = new CloudUser();
|
|
|
+ await user.logout();
|
|
|
+ this.isLoggedIn = false;
|
|
|
+ this.username = '请登录';
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
// 顶部栏操作
|
|
|
goBack() {
|
|
|
console.log('返回上一页');
|
|
@@ -114,4 +213,4 @@ export class Tab4Page {
|
|
|
shareMoment() {
|
|
|
console.log('分享动态');
|
|
|
}
|
|
|
-}
|
|
|
+}
|