package main import ( "fmt" "log" "time" "golang.org/x/crypto/bcrypt" "gorm.io/driver/sqlite" "gorm.io/gorm" ) // UserAccount 用户账号模型 type UserAccount struct { ID uint `gorm:"primaryKey" json:"id"` Username string `gorm:"type:varchar(64);uniqueIndex" json:"username"` PasswordHash string `gorm:"type:varchar(255)" json:"-"` // 不在JSON中暴露 Name string `gorm:"type:varchar(64)" json:"name"` Phone string `gorm:"type:varchar(20);uniqueIndex" json:"phone"` UserType string `gorm:"type:varchar(16)" json:"user_type"` // student, parent, teacher, admin Status int `gorm:"default:1" json:"status"` // 1:正常, 0:禁用 LastLoginAt *time.Time `json:"last_login_at"` LastLoginIP string `json:"last_login_ip"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` } func main() { fmt.Println("AI近视防控系统 - 登录功能验证测试") // 连接到SQLite数据库 db, err := gorm.Open(sqlite.Open("test_myopia.db"), &gorm.Config{}) if err != nil { log.Fatal("连接数据库失败: ", err) } // 测试登录功能 testLogin(db, "admin", "Admin123!@#") testLogin(db, "teacher", "Teacher123!@#") testLogin(db, "student", "Student123!@#") testLogin(db, "parent", "Parent123!@#") fmt.Println("\n✅ 登录功能验证完成!") fmt.Println("💡 提示: 所有测试账号密码均已正确设置,可正常使用登录功能") } func testLogin(db *gorm.DB, username, password string) { fmt.Printf("\n--- 测试用户: %s ---\n", username) var user UserAccount result := db.Where("username = ?", username).First(&user) if result.Error != nil { fmt.Printf("❌ 用户不存在: %s\n", username) return } if user.Status == 0 { fmt.Printf("❌ 用户已被禁用: %s\n", username) return } // 验证密码 err := bcrypt.CompareHashAndPassword([]byte(user.PasswordHash), []byte(password)) if err != nil { fmt.Printf("❌ 密码验证失败: %s\n", username) return } fmt.Printf("✅ 用户 %s 登录验证成功!\n", username) fmt.Printf(" - 用户名: %s\n", user.Username) fmt.Printf(" - 姓名: %s\n", user.Name) fmt.Printf(" - 角色: %s\n", user.UserType) fmt.Printf(" - 手机: %s\n", user.Phone) fmt.Printf(" - 状态: %d\n", user.Status) }