feat: build core architecture & implement user auth modules 🚀 feat: 搭建核心架构并实现用户认证模块 🚀 Framework Migration: Switched from Hertz to Gin, providing a more idiomatic and lightweight web foundation. ⚡ 框架迁移:从 Hertz 切换至 Gin,构建了更符合 Go 惯例且轻量级的 Web 基础。⚡ Architectural Overhaul: Refactored the 3-layer architecture from global-variable-based calls to Explicit Dependency Injection (DI) via New... factory functions. This significantly improves testability and decoupling. 🏗️ 架构重构:将三层架构从基于“全局变量”的调用重构为通过 New... 工厂函数实现的显式依赖注入 (DI)。这大幅提升了代码的可测试性与解耦程度。🏗️ User Auth: Completed and tested Register, Login, and Token Refresh APIs with robust error handling and Bcrypt password hashing. 🔐 用户认证:完成了注册、登录与 Token 刷新接口并通过测试,包含健壮的错误处理与 Bcrypt 密码哈希加密。🔐 Config Management: Integrated Viper for centralized, environment-aware configuration management. ⚙️ 配置管理:集成了 Viper,实现了中心化且具备环境感知能力的配置管理。⚙️ DevOps & Docs: Added docker-compose.yml for seamless MySQL 8.0 & environment setup. 🐳 Updated README.md with corrections for mistakes in image quoting and formats. 📝 运维与文档: 新增 docker-compose.yml,实现 MySQL 8.0 环境的一键启动。🐳 更新 README.md,修改了一些图片引用和格式上小错误。📝
51 lines
1.5 KiB
Go
51 lines
1.5 KiB
Go
// Package model 数据模型层
|
||
// 定义所有数据结构和模型
|
||
package model
|
||
|
||
import (
|
||
"time"
|
||
)
|
||
|
||
// TableName 指定表名
|
||
// 确保与数据库表名一致
|
||
func (User) TableName() string {
|
||
return "users"
|
||
}
|
||
|
||
// User 用户模型
|
||
// 对应数据库中的users表
|
||
type User struct {
|
||
// 增加 autoIncrement 标签,对应 SQL 的 AUTO_INCREMENT
|
||
ID uint `gorm:"primaryKey;autoIncrement" json:"id"`
|
||
// 增加 unique 和 not null,确保与数据库约束一致
|
||
Username string `gorm:"type:varchar(255);not null;unique" json:"username"`
|
||
// Password 保持 json:"-" 是非常专业的做法,防止接口无意间泄露哈希值
|
||
Password string `gorm:"type:varchar(255);not null" json:"password"`
|
||
PhoneNumber string `gorm:"type:varchar(255)" json:"phone_number"`
|
||
// 设定默认值,确保 GORM 在插入时能正确处理初始配额
|
||
TokenLimit int `gorm:"default:100000" json:"token_limit"`
|
||
// 增加 default:0,防止出现 null 导致的解析问题
|
||
TokenUsage int `gorm:"default:0" json:"token_usage"`
|
||
// LastResetAt 映射 timestamp
|
||
LastResetAt time.Time `gorm:"comment:上次周用量重置时间" json:"last_reset_at"`
|
||
}
|
||
|
||
type UserRegisterRequest struct {
|
||
Username string `json:"username"`
|
||
Password string `json:"password"`
|
||
PhoneNumber string `json:"phone_number"`
|
||
}
|
||
|
||
type UserRegisterResponse struct {
|
||
ID uint `json:"id"`
|
||
}
|
||
|
||
type UserLoginRequest struct {
|
||
Username string `json:"username"`
|
||
Password string `json:"password"`
|
||
}
|
||
|
||
type UserLoginResponse struct {
|
||
Tokens
|
||
}
|