import { router } from '@kit.ArkUI'; import logManager, { LogCategory, LogEventType } from '../common/logtext'; import settingsService from '../common/SettingsService'; import { DatabaseService } from '../common/DatabaseService'; @Entry @Component @Preview struct Login { @State message: string = '智能教室管理系统'; @State username: string = ''; // 用户名 @State password: string = ''; // 密码 @State isLoading: boolean = false; // 加载状态 @State errorMessage: string = ''; // 错误信息 // 获取数据库服务实例 private dbService: DatabaseService = DatabaseService.getInstance(); // 登录验证方法 private async handleLogin(): Promise { this.isLoading = true; this.errorMessage = ''; // 用户名不能为空 if (!this.username || this.username.trim() === '') { this.errorMessage = '请输入账号'; this.isLoading = false; return; } // 密码不能为空 if (!this.password || this.password.trim() === '') { this.errorMessage = '请输入密码'; this.isLoading = false; return; } try { // 使用异步验证方法 const isValid = await this.dbService.validateUser(this.username, this.password); if (isValid) { // 设置当前用户 settingsService.setCurrentAccount(this.username); // 记录登录日志 logManager.info(LogCategory.USER, LogEventType.USER_LOGIN, `User logged in: ${this.username}`); // 跳转到过渡页面 router.replaceUrl({ url: 'pages/TransitionPage' }); } else { this.errorMessage = '账号或密码错误'; logManager.info(LogCategory.USER, LogEventType.USER_LOGIN_FAIL, `Login failed: ${this.username}`); } } catch (error) { this.errorMessage = '登录失败,请稍后重试'; logManager.info(LogCategory.USER, LogEventType.USER_LOGIN_FAIL, `Login error: ${error}`); } finally { this.isLoading = false; } } build() { Column({space: 20}) { // 增加行间距 // 顶部标题和图标 Column() { Image('http://139.155.155.67:2342/images/tdcat.webp') .height(150) .width(150) .margin({ top: 40, bottom: 20 }) Text(this.message) .fontColor('#333333') .fontSize(28) .fontWeight(FontWeight.Bold) .margin({ bottom: 30 }) } .width('100%') .alignItems(HorizontalAlign.Center) // 登录表单 Column({ space: 30 }) { // 账号输入框 Column({ space: 8 }) { Text("账号") .fontSize(16) .fontColor('#666666') .alignSelf(ItemAlign.Start) TextInput({ placeholder: '请输入账号' }) .type(InputType.Normal) .placeholderColor('#999999') .placeholderFont({ size: 16 }) .caretColor('#2196F3') .width('100%') .height(50) .fontSize(16) .fontColor('#333333') .backgroundColor('#F5F5F5') .borderRadius(8) .padding({ left: 16, right: 16 }) .onChange((value: string) => { this.username = value; this.errorMessage = ''; // 清除错误信息 }) } .width('100%') // 密码输入框 Column({ space: 8 }) { Text("密码") .fontSize(16) .fontColor('#666666') .alignSelf(ItemAlign.Start) TextInput({ placeholder: '请输入密码' }) .type(InputType.Password) .placeholderColor('#999999') .placeholderFont({ size: 16 }) .caretColor('#2196F3') .width('100%') .height(50) .fontSize(16) .fontColor('#333333') .backgroundColor('#F5F5F5') .borderRadius(8) .padding({ left: 16, right: 16 }) .onChange((value: string) => { this.password = value; this.errorMessage = ''; // 清除错误信息 }) } .width('100%') // 错误信息提示 if (this.errorMessage !== '') { Text(this.errorMessage) .fontSize(14) .fontColor('#FF0000') .width('100%') } // 登录按钮 Button("登录") .width('100%') .height(50) .fontSize(18) .fontWeight(FontWeight.Medium) .backgroundColor('#2196F3') .borderRadius(8) .fontColor(Color.White) .stateEffect(true) .enabled(!this.isLoading) .opacity(this.isLoading ? 0.6 : 1) .onClick(() => { //this.Apply() this.handleLogin(); }) // 测试按钮 - 直接跳转到首页 Button("测试直接进入") .width('100%') .height(40) .fontSize(16) .fontWeight(FontWeight.Medium) .backgroundColor('#4CAF50') .borderRadius(8) .fontColor(Color.White) .margin({ top: 10 }) .onClick(() => { // 设置账户为测试用户 settingsService.setCurrentAccount('test'); // 记录测试登录 logManager.info(LogCategory.USER, LogEventType.USER_LOGIN, 'Test user logged in'); // 直接跳转到首页 router.replaceUrl({ url: 'pages/HomePage' }); }) } .width('90%') .padding(20) .backgroundColor(Color.White) .borderRadius(16) .shadow({ radius: 8, color: 'rgba(0,0,0,0.1)', offsetY: 4 }) // 底部提示 Row() { Button({ type: ButtonType.Capsule }) { Text("忘记密码?") .fontSize(14) .fontColor('#2196F3') } .backgroundColor(Color.Transparent) .onClick(() => { AlertDialog.show({ message: "忘记密码功能尚未开发,请联系管理员重置密码。", confirm: { value: "确定", action: () => {} } }); }) Blank(100) Button({ type: ButtonType.Capsule }) { Text("注册") .fontSize(14) .fontColor('#2196F3') } .backgroundColor(Color.Transparent) .onClick(() => { AlertDialog.show({ message: "注册功能尚未开发,请联系管理员创建账号。", confirm: { value: "确定", action: () => {} } }); }) } .width('90%') .margin({ top: 20 }) } .height('100%') .width('100%') .alignItems(HorizontalAlign.Center) .backgroundColor('#F5F7FA') // 页面背景色 } // 页面生命周期方法 onPageShow(): void { this.Log_LoginEvent('onPageShow'); } onPageHide(): void { this.Log_LoginEvent('onPageHide'); } onBackPress(): void { this.Log_LoginEvent('onBackPress'); } aboutToAppear(): void { this.Log_LoginEvent('aboutToAppear'); } aboutToDisappear(): void { this.Log_LoginEvent('aboutToDisappear'); } // 通用日志记录方法 public Log_LoginEvent(eventName: string): void { // 根据事件名称选择合适的日志事件类型 switch (eventName) { case 'aboutToAppear': logManager.info(LogCategory.USER, LogEventType.PAGE_APPEAR, 'LoginPage'); break; case 'aboutToDisappear': logManager.info(LogCategory.USER, LogEventType.PAGE_DISAPPEAR, 'LoginPage'); break; case 'onPageShow': logManager.info(LogCategory.USER, LogEventType.PAGE_SHOW, 'LoginPage'); break; case 'onPageHide': logManager.info(LogCategory.USER, LogEventType.PAGE_HIDE, 'LoginPage'); break; case 'onBackPress': logManager.info(LogCategory.USER, LogEventType.PAGE_BACK, 'LoginPage'); break; default: logManager.info(LogCategory.USER, LogEventType.SYSTEM_INFO, `LoginPage: ${eventName}`); } } }