Version:0.0.1.dev.260202
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,修改了一些图片引用和格式上小错误。📝
This commit is contained in:
5
backend/api/container.go
Normal file
5
backend/api/container.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package api
|
||||
|
||||
type ApiHandlers struct {
|
||||
UserHandler *UserHandler
|
||||
}
|
||||
97
backend/api/user.go
Normal file
97
backend/api/user.go
Normal file
@@ -0,0 +1,97 @@
|
||||
// Package api 定义API接口层
|
||||
// 包含所有对外暴露的HTTP接口定义
|
||||
package api
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/smartflow/backend/model"
|
||||
"github.com/smartflow/backend/respond"
|
||||
"github.com/smartflow/backend/service"
|
||||
)
|
||||
|
||||
type UserHandler struct {
|
||||
// 伸出手:准备接住 Service
|
||||
svc *service.UserService
|
||||
}
|
||||
|
||||
// NewUserHandler:组装 Handler 的“工厂”
|
||||
func NewUserHandler(svc *service.UserService) *UserHandler {
|
||||
return &UserHandler{
|
||||
svc: svc, // 把传进来的 Service 揣进口袋里
|
||||
}
|
||||
}
|
||||
|
||||
// UserRegister 用户注册API
|
||||
// 处理用户注册请求
|
||||
func (api *UserHandler) UserRegister(c *gin.Context) {
|
||||
var user model.UserRegisterRequest
|
||||
err := c.ShouldBindJSON(&user)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, respond.WrongParamType)
|
||||
return
|
||||
}
|
||||
|
||||
retUser, err := api.svc.UserRegister(user)
|
||||
if err != nil {
|
||||
switch {
|
||||
case errors.Is(err, respond.InvalidName), errors.Is(err, respond.MissingParam),
|
||||
errors.Is(err, respond.ParamTooLong): //如果是无效ID或者缺少参数的错误
|
||||
c.JSON(http.StatusBadRequest, err)
|
||||
return
|
||||
default:
|
||||
c.JSON(http.StatusInternalServerError, respond.InternalError(err))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, respond.OKWithData(respond.Ok, retUser))
|
||||
}
|
||||
|
||||
func (api *UserHandler) UserLogin(c *gin.Context) {
|
||||
var req model.UserLoginRequest
|
||||
err := c.ShouldBindJSON(&req)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, respond.WrongParamType)
|
||||
return
|
||||
}
|
||||
tokens, err := api.svc.UserLogin(&req)
|
||||
if err != nil {
|
||||
switch {
|
||||
case errors.Is(err, respond.WrongName), errors.Is(err, respond.WrongPwd): //如果是无效ID或者缺少参数的错误
|
||||
c.JSON(http.StatusBadRequest, err)
|
||||
return
|
||||
default:
|
||||
c.JSON(http.StatusInternalServerError, respond.InternalError(err))
|
||||
return
|
||||
}
|
||||
}
|
||||
c.JSON(http.StatusOK, respond.OKWithData(respond.Ok, tokens))
|
||||
}
|
||||
|
||||
func (api *UserHandler) RefreshTokenHandler(c *gin.Context) {
|
||||
var requestBody struct {
|
||||
RefreshToken string `json:"old_refresh_token"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&requestBody); err != nil {
|
||||
c.JSON(http.StatusBadRequest, respond.WrongParamType)
|
||||
return
|
||||
}
|
||||
if requestBody.RefreshToken == "" {
|
||||
c.JSON(http.StatusBadRequest, respond.MissingParam)
|
||||
}
|
||||
tokens, err := api.svc.RefreshTokenHandler(requestBody.RefreshToken)
|
||||
if err != nil {
|
||||
switch {
|
||||
case errors.Is(err, respond.InvalidRefreshToken), errors.Is(err, respond.InvalidClaims),
|
||||
errors.Is(err, respond.InvalidTokenSingingMethod): //如果是无效刷新令牌或者无效claims或者无效签名方法
|
||||
c.JSON(http.StatusBadRequest, err)
|
||||
return
|
||||
default:
|
||||
c.JSON(http.StatusInternalServerError, respond.InternalError(err))
|
||||
}
|
||||
}
|
||||
c.JSON(http.StatusOK, respond.OKWithData(respond.Ok, tokens))
|
||||
}
|
||||
68
backend/auth/jwt_generater.go
Normal file
68
backend/auth/jwt_generater.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/golang-jwt/jwt/v4"
|
||||
"github.com/smartflow/backend/respond"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var RefreshKey = []byte(viper.GetString("jwt.accessSecret")) // 用于签名和验证刷新Token的密钥
|
||||
var AccessKey = []byte(viper.GetString("jwt.refreshSecret")) // 用于签名和验证访问Token的密钥
|
||||
|
||||
// GenerateTokens 生成访问令牌和刷新令牌
|
||||
func GenerateTokens(userID int) (string, string, error) {
|
||||
// 创建访问令牌
|
||||
accessToken := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
||||
"user_id": userID, // 获取用户ID
|
||||
"exp": time.Now().Add(15 * time.Minute).Unix(), // 设置访问令牌过期时间为 15 分钟
|
||||
"token_type": "access_token", // 令牌类型为访问令牌
|
||||
})
|
||||
|
||||
// 使用密钥签名访问令牌
|
||||
accessTokenString, err := accessToken.SignedString(AccessKey)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
// 创建刷新令牌
|
||||
refreshToken := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
||||
"user_id": userID, // 获取用户ID
|
||||
"exp": time.Now().Add(7 * 24 * time.Hour).Unix(), // 设置刷新令牌过期时间为 7 天
|
||||
"token_type": "refresh_token", // 令牌类型为刷新令牌
|
||||
})
|
||||
|
||||
// 使用密钥签名刷新令牌
|
||||
refreshTokenString, err := refreshToken.SignedString(RefreshKey)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
return accessTokenString, refreshTokenString, nil
|
||||
}
|
||||
|
||||
func ValidateRefreshToken(tokenString string) (*jwt.Token, error) {
|
||||
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
|
||||
// 检查签名方法是否为 HMAC
|
||||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||
return nil, respond.InvalidTokenSingingMethod
|
||||
}
|
||||
// 返回用于验证的密钥
|
||||
return RefreshKey, nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 进一步检查载荷中 token_type 是否正确
|
||||
claims, ok := token.Claims.(jwt.MapClaims)
|
||||
if !ok {
|
||||
return nil, respond.InvalidClaims
|
||||
}
|
||||
// 检查 token_type 是否是 refresh_token
|
||||
if claimType, ok := claims["token_type"].(string); !ok || claimType != "refresh_token" {
|
||||
return nil, respond.WrongTokenType
|
||||
}
|
||||
return token, nil
|
||||
}
|
||||
49
backend/cmd/start.go
Normal file
49
backend/cmd/start.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/smartflow/backend/api"
|
||||
"github.com/smartflow/backend/dao"
|
||||
"github.com/smartflow/backend/inits"
|
||||
"github.com/smartflow/backend/routers"
|
||||
"github.com/smartflow/backend/service"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// loadConfig 加载配置
|
||||
// 从配置文件中读取配置信息
|
||||
func loadConfig() error {
|
||||
// 设置配置文件路径
|
||||
viper.SetConfigName("config")
|
||||
viper.SetConfigType("yaml")
|
||||
viper.AddConfigPath(".")
|
||||
// 读取配置文件
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
return fmt.Errorf("failed to read config file: %w", err)
|
||||
}
|
||||
log.Println("Config loaded successfully")
|
||||
return nil
|
||||
}
|
||||
|
||||
// Start 启动函数
|
||||
func Start() {
|
||||
// 加载配置
|
||||
if err := loadConfig(); err != nil {
|
||||
log.Fatalf("Failed to load config: %v", err)
|
||||
}
|
||||
// 初始化数据库
|
||||
db, err := inits.ConnectDB()
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to connect to database: %v", err)
|
||||
}
|
||||
userRepo := dao.NewUserDAO(db)
|
||||
userService := service.NewUserService(userRepo)
|
||||
userApi := api.NewUserHandler(userService)
|
||||
handlers := &api.ApiHandlers{
|
||||
UserHandler: userApi,
|
||||
}
|
||||
r := routers.RegisterRouters(handlers)
|
||||
routers.StartEngine(r)
|
||||
}
|
||||
33
backend/config.yaml
Normal file
33
backend/config.yaml
Normal file
@@ -0,0 +1,33 @@
|
||||
# 应用配置文件
|
||||
# 包含服务器、数据库等基础配置
|
||||
|
||||
server:
|
||||
port: 8080
|
||||
mode: debug
|
||||
timeout: 30s
|
||||
|
||||
database:
|
||||
host: localhost
|
||||
port: 3306
|
||||
user: smartflow_user
|
||||
password: "smartflow_password_456"
|
||||
dbname: "smartflow"
|
||||
charset: utf8mb4
|
||||
parseTime: true
|
||||
loc: Local
|
||||
|
||||
jwt:
|
||||
accessSecret: "smartflow_jwt_access_secret_123"
|
||||
refreshSecret: "smartflow_jwt_refresh_secret_123"
|
||||
accessTokenExpire: 15min
|
||||
refreshTokenExpire: 7d
|
||||
|
||||
log:
|
||||
level: info
|
||||
path: logs/
|
||||
|
||||
redis:
|
||||
host: localhost
|
||||
port: 6379
|
||||
password: "redis_password_789"
|
||||
db: 0
|
||||
74
backend/dao/user.go
Normal file
74
backend/dao/user.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/smartflow/backend/model"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// UserDAO 用户数据访问对象
|
||||
// 负责用户相关的数据库操作
|
||||
type UserDAO struct {
|
||||
// 这是一个口袋,用来装数据库连接实例
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewUserDAO 创建UserDAO实例
|
||||
// NewUserDAO 接收一个 *gorm.DB,并把它塞进结构体的口袋里
|
||||
func NewUserDAO(db *gorm.DB) *UserDAO {
|
||||
return &UserDAO{
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
// Create 创建新用户
|
||||
// 插入新用户信息到数据库
|
||||
func (dao *UserDAO) Create(username, phoneNumber, password string) (*model.User, error) {
|
||||
// 创建User实例
|
||||
user := &model.User{
|
||||
Username: username,
|
||||
PhoneNumber: phoneNumber,
|
||||
Password: password, // 注意:实际项目中应该对密码进行加密处理
|
||||
TokenLimit: 100000, // 默认值
|
||||
TokenUsage: 0, // 初始使用量为0
|
||||
LastResetAt: time.Now(), // 设置为当前时间
|
||||
}
|
||||
|
||||
// 插入数据
|
||||
if err := dao.db.Create(user).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (dao *UserDAO) IfUsernameExists(name string) (bool, error) {
|
||||
err := dao.db.Where("username = ?", name).First(&model.User{}).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return false, nil
|
||||
}
|
||||
return true, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (dao *UserDAO) GetUserHashedPasswordByName(name string) (string, error) {
|
||||
var user model.User
|
||||
err := dao.db.Where("username = ?", name).First(&user).Error
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return user.Password, nil
|
||||
}
|
||||
|
||||
func (dao *UserDAO) GetUserIDByName(name string) (int, error) {
|
||||
var user model.User
|
||||
err := dao.db.Where("username = ?", name).First(&user).Error
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
return int(user.ID), nil
|
||||
}
|
||||
58
backend/go.mod
Normal file
58
backend/go.mod
Normal file
@@ -0,0 +1,58 @@
|
||||
module github.com/smartflow/backend
|
||||
|
||||
go 1.23.4
|
||||
|
||||
require (
|
||||
github.com/gin-gonic/gin v1.11.0
|
||||
github.com/golang-jwt/jwt/v4 v4.5.2
|
||||
github.com/spf13/viper v1.21.0
|
||||
golang.org/x/crypto v0.40.0
|
||||
gorm.io/driver/mysql v1.6.0
|
||||
gorm.io/gorm v1.31.1
|
||||
)
|
||||
|
||||
require (
|
||||
filippo.io/edwards25519 v1.1.0 // indirect
|
||||
github.com/bytedance/sonic v1.14.0 // indirect
|
||||
github.com/bytedance/sonic/loader v0.3.0 // indirect
|
||||
github.com/cloudwego/base64x v0.1.6 // indirect
|
||||
github.com/fsnotify/fsnotify v1.9.0 // indirect
|
||||
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
|
||||
github.com/gin-contrib/sse v1.1.0 // indirect
|
||||
github.com/go-playground/locales v0.14.1 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||
github.com/go-playground/validator/v10 v10.27.0 // indirect
|
||||
github.com/go-sql-driver/mysql v1.8.1 // indirect
|
||||
github.com/go-viper/mapstructure/v2 v2.4.0 // indirect
|
||||
github.com/goccy/go-json v0.10.2 // indirect
|
||||
github.com/goccy/go-yaml v1.18.0 // indirect
|
||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||
github.com/jinzhu/now v1.1.5 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.3.0 // indirect
|
||||
github.com/leodido/go-urn v1.4.0 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
|
||||
github.com/quic-go/qpack v0.5.1 // indirect
|
||||
github.com/quic-go/quic-go v0.54.0 // indirect
|
||||
github.com/sagikazarmark/locafero v0.11.0 // indirect
|
||||
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 // indirect
|
||||
github.com/spf13/afero v1.15.0 // indirect
|
||||
github.com/spf13/cast v1.10.0 // indirect
|
||||
github.com/spf13/pflag v1.0.10 // indirect
|
||||
github.com/subosito/gotenv v1.6.0 // indirect
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||
github.com/ugorji/go/codec v1.3.0 // indirect
|
||||
go.uber.org/mock v0.5.0 // indirect
|
||||
go.yaml.in/yaml/v3 v3.0.4 // indirect
|
||||
golang.org/x/arch v0.20.0 // indirect
|
||||
golang.org/x/mod v0.26.0 // indirect
|
||||
golang.org/x/net v0.42.0 // indirect
|
||||
golang.org/x/sync v0.16.0 // indirect
|
||||
golang.org/x/sys v0.35.0 // indirect
|
||||
golang.org/x/text v0.28.0 // indirect
|
||||
golang.org/x/tools v0.35.0 // indirect
|
||||
google.golang.org/protobuf v1.36.9 // indirect
|
||||
)
|
||||
132
backend/go.sum
Normal file
132
backend/go.sum
Normal file
@@ -0,0 +1,132 @@
|
||||
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
|
||||
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
|
||||
github.com/bytedance/sonic v1.14.0 h1:/OfKt8HFw0kh2rj8N0F6C/qPGRESq0BbaNZgcNXXzQQ=
|
||||
github.com/bytedance/sonic v1.14.0/go.mod h1:WoEbx8WTcFJfzCe0hbmyTGrfjt8PzNEBdxlNUO24NhA=
|
||||
github.com/bytedance/sonic/loader v0.3.0 h1:dskwH8edlzNMctoruo8FPTJDF3vLtDT0sXZwvZJyqeA=
|
||||
github.com/bytedance/sonic/loader v0.3.0/go.mod h1:N8A3vUdtUebEY2/VQC0MyhYeKUFosQU6FxH2JmUe6VI=
|
||||
github.com/cloudwego/base64x v0.1.6 h1:t11wG9AECkCDk5fMSoxmufanudBtJ+/HemLstXDLI2M=
|
||||
github.com/cloudwego/base64x v0.1.6/go.mod h1:OFcloc187FXDaYHvrNIjxSe8ncn0OOM8gEHfghB2IPU=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
|
||||
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
|
||||
github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k=
|
||||
github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
|
||||
github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3GqacKw1NM=
|
||||
github.com/gabriel-vasile/mimetype v1.4.8/go.mod h1:ByKUIKGjh1ODkGM1asKUbQZOLGrPjydw3hYPU2YU9t8=
|
||||
github.com/gin-contrib/sse v1.1.0 h1:n0w2GMuUpWDVp7qSpvze6fAu9iRxJY4Hmj6AmBOU05w=
|
||||
github.com/gin-contrib/sse v1.1.0/go.mod h1:hxRZ5gVpWMT7Z0B0gSNYqqsSCNIJMjzvm6fqCz9vjwM=
|
||||
github.com/gin-gonic/gin v1.11.0 h1:OW/6PLjyusp2PPXtyxKHU0RbX6I/l28FTdDlae5ueWk=
|
||||
github.com/gin-gonic/gin v1.11.0/go.mod h1:+iq/FyxlGzII0KHiBGjuNn4UNENUlKbGlNmc+W50Dls=
|
||||
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
|
||||
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
|
||||
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
|
||||
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
|
||||
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
|
||||
github.com/go-playground/validator/v10 v10.27.0 h1:w8+XrWVMhGkxOaaowyKH35gFydVHOvC0/uWoy2Fzwn4=
|
||||
github.com/go-playground/validator/v10 v10.27.0/go.mod h1:I5QpIEbmr8On7W0TktmJAumgzX4CA1XNl4ZmDuVHKKo=
|
||||
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
|
||||
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
|
||||
github.com/go-viper/mapstructure/v2 v2.4.0 h1:EBsztssimR/CONLSZZ04E8qAkxNYq4Qp9LvH92wZUgs=
|
||||
github.com/go-viper/mapstructure/v2 v2.4.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
|
||||
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
|
||||
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
|
||||
github.com/goccy/go-yaml v1.18.0 h1:8W7wMFS12Pcas7KU+VVkaiCng+kG8QiFeFwzFb+rwuw=
|
||||
github.com/goccy/go-yaml v1.18.0/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
|
||||
github.com/golang-jwt/jwt/v4 v4.5.2 h1:YtQM7lnr8iZ+j5q71MGKkNw9Mn7AjHM68uc9g5fXeUI=
|
||||
github.com/golang-jwt/jwt/v4 v4.5.2/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
|
||||
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
|
||||
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
||||
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
|
||||
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
|
||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||
github.com/klauspost/cpuid/v2 v2.3.0 h1:S4CRMLnYUhGeDFDqkGriYKdfoFlDnMtqTiI/sFzhA9Y=
|
||||
github.com/klauspost/cpuid/v2 v2.3.0/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
|
||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
|
||||
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
|
||||
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/quic-go/qpack v0.5.1 h1:giqksBPnT/HDtZ6VhtFKgoLOWmlyo9Ei6u9PqzIMbhI=
|
||||
github.com/quic-go/qpack v0.5.1/go.mod h1:+PC4XFrEskIVkcLzpEkbLqq1uCoxPhQuvK5rH1ZgaEg=
|
||||
github.com/quic-go/quic-go v0.54.0 h1:6s1YB9QotYI6Ospeiguknbp2Znb/jZYjZLRXn9kMQBg=
|
||||
github.com/quic-go/quic-go v0.54.0/go.mod h1:e68ZEaCdyviluZmy44P6Iey98v/Wfz6HCjQEm+l8zTY=
|
||||
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
|
||||
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
|
||||
github.com/sagikazarmark/locafero v0.11.0 h1:1iurJgmM9G3PA/I+wWYIOw/5SyBtxapeHDcg+AAIFXc=
|
||||
github.com/sagikazarmark/locafero v0.11.0/go.mod h1:nVIGvgyzw595SUSUE6tvCp3YYTeHs15MvlmU87WwIik=
|
||||
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 h1:+jumHNA0Wrelhe64i8F6HNlS8pkoyMv5sreGx2Ry5Rw=
|
||||
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8/go.mod h1:3n1Cwaq1E1/1lhQhtRK2ts/ZwZEhjcQeJQ1RuC6Q/8U=
|
||||
github.com/spf13/afero v1.15.0 h1:b/YBCLWAJdFWJTN9cLhiXXcD7mzKn9Dm86dNnfyQw1I=
|
||||
github.com/spf13/afero v1.15.0/go.mod h1:NC2ByUVxtQs4b3sIUphxK0NioZnmxgyCrfzeuq8lxMg=
|
||||
github.com/spf13/cast v1.10.0 h1:h2x0u2shc1QuLHfxi+cTJvs30+ZAHOGRic8uyGTDWxY=
|
||||
github.com/spf13/cast v1.10.0/go.mod h1:jNfB8QC9IA6ZuY2ZjDp0KtFO2LZZlg4S/7bzP6qqeHo=
|
||||
github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
|
||||
github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
github.com/spf13/viper v1.21.0 h1:x5S+0EU27Lbphp4UKm1C+1oQO+rKx36vfCoaVebLFSU=
|
||||
github.com/spf13/viper v1.21.0/go.mod h1:P0lhsswPGWD/1lZJ9ny3fYnVqxiegrlNrEmgLjbTCAY=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
|
||||
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
|
||||
github.com/ugorji/go/codec v1.3.0 h1:Qd2W2sQawAfG8XSvzwhBeoGq71zXOC/Q1E9y/wUcsUA=
|
||||
github.com/ugorji/go/codec v1.3.0/go.mod h1:pRBVtBSKl77K30Bv8R2P+cLSGaTtex6fsA2Wjqmfxj4=
|
||||
go.uber.org/mock v0.5.0 h1:KAMbZvZPyBPWgD14IrIQ38QCyjwpvVVV6K/bHl1IwQU=
|
||||
go.uber.org/mock v0.5.0/go.mod h1:ge71pBPLYDk7QIi1LupWxdAykm7KIEFchiOqd6z7qMM=
|
||||
go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc=
|
||||
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
|
||||
golang.org/x/arch v0.20.0 h1:dx1zTU0MAE98U+TQ8BLl7XsJbgze2WnNKF/8tGp/Q6c=
|
||||
golang.org/x/arch v0.20.0/go.mod h1:bdwinDaKcfZUGpH09BB7ZmOfhalA8lQdzl62l8gGWsk=
|
||||
golang.org/x/crypto v0.40.0 h1:r4x+VvoG5Fm+eJcxMaY8CQM7Lb0l1lsmjGBQ6s8BfKM=
|
||||
golang.org/x/crypto v0.40.0/go.mod h1:Qr1vMER5WyS2dfPHAlsOj01wgLbsyWtFn/aY+5+ZdxY=
|
||||
golang.org/x/mod v0.26.0 h1:EGMPT//Ezu+ylkCijjPc+f4Aih7sZvaAr+O3EHBxvZg=
|
||||
golang.org/x/mod v0.26.0/go.mod h1:/j6NAhSk8iQ723BGAUyoAcn7SlD7s15Dp9Nd/SfeaFQ=
|
||||
golang.org/x/net v0.42.0 h1:jzkYrhi3YQWD6MLBJcsklgQsoAcw89EcZbJw8Z614hs=
|
||||
golang.org/x/net v0.42.0/go.mod h1:FF1RA5d3u7nAYA4z2TkclSCKh68eSXtiFwcWQpPXdt8=
|
||||
golang.org/x/sync v0.16.0 h1:ycBJEhp9p4vXvUZNszeOq0kGTPghopOL8q0fq3vstxw=
|
||||
golang.org/x/sync v0.16.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.35.0 h1:vz1N37gP5bs89s7He8XuIYXpyY0+QlsKmzipCbUtyxI=
|
||||
golang.org/x/sys v0.35.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
||||
golang.org/x/text v0.28.0 h1:rhazDwis8INMIwQ4tpjLDzUhx6RlXqZNPEM0huQojng=
|
||||
golang.org/x/text v0.28.0/go.mod h1:U8nCwOR8jO/marOQ0QbDiOngZVEBB7MAiitBuMjXiNU=
|
||||
golang.org/x/tools v0.35.0 h1:mBffYraMEf7aa0sB+NuKnuCy8qI/9Bughn8dC2Gu5r0=
|
||||
golang.org/x/tools v0.35.0/go.mod h1:NKdj5HkL/73byiZSJjqJgKn3ep7KjFkBOkR/Hps3VPw=
|
||||
google.golang.org/protobuf v1.36.9 h1:w2gp2mA27hUeUzj9Ex9FBjsBm40zfaDtEWow293U7Iw=
|
||||
google.golang.org/protobuf v1.36.9/go.mod h1:fuxRtAxBytpl4zzqUh6/eyUujkJdNiuEkXntxiD/uRU=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gorm.io/driver/mysql v1.6.0 h1:eNbLmNTpPpTOVZi8MMxCi2aaIm0ZpInbORNXDwyLGvg=
|
||||
gorm.io/driver/mysql v1.6.0/go.mod h1:D/oCC2GWK3M/dqoLxnOlaNKmXz8WNTfcS9y5ovaSqKo=
|
||||
gorm.io/gorm v1.31.1 h1:7CA8FTFz/gRfgqgpeKIBcervUn3xSyPUmr6B2WXJ7kg=
|
||||
gorm.io/gorm v1.31.1/go.mod h1:XyQVbO2k6YkOis7C2437jSit3SsDK72s7n7rsSHd+Gs=
|
||||
36
backend/inits/db.go
Normal file
36
backend/inits/db.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package inits
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// ConnectDB 连接数据库
|
||||
// 从config.yaml中读取数据库配置
|
||||
// 返回错误信息
|
||||
func ConnectDB() (*gorm.DB, error) {
|
||||
// 从配置中读取数据库信息
|
||||
host := viper.GetString("database.host")
|
||||
port := viper.GetString("database.port")
|
||||
user := viper.GetString("database.user")
|
||||
password := viper.GetString("database.password")
|
||||
dbname := viper.GetString("database.dbname")
|
||||
|
||||
// 构建DSN连接字符串
|
||||
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
|
||||
user, password, host, port, dbname)
|
||||
|
||||
// 连接数据库
|
||||
var err error
|
||||
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
log.Println("Database connected successfully")
|
||||
return db, nil
|
||||
}
|
||||
7
backend/main.go
Normal file
7
backend/main.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package main
|
||||
|
||||
import "github.com/smartflow/backend/cmd"
|
||||
|
||||
func main() {
|
||||
cmd.Start()
|
||||
}
|
||||
3
backend/middleware/middleware.go
Normal file
3
backend/middleware/middleware.go
Normal file
@@ -0,0 +1,3 @@
|
||||
// Package middleware 中间件层
|
||||
// 包含所有HTTP请求中间件
|
||||
package middleware
|
||||
6
backend/model/auth.go
Normal file
6
backend/model/auth.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package model
|
||||
|
||||
type Tokens struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
RefreshToken string `json:"refresh_token"`
|
||||
}
|
||||
50
backend/model/user.go
Normal file
50
backend/model/user.go
Normal file
@@ -0,0 +1,50 @@
|
||||
// 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
|
||||
}
|
||||
121
backend/respond/respond.go
Normal file
121
backend/respond/respond.go
Normal file
@@ -0,0 +1,121 @@
|
||||
// Package respond 响应处理
|
||||
// 统一API响应格式和处理逻辑
|
||||
package respond
|
||||
|
||||
type Response struct { //响应结构体
|
||||
Status string `json:"status"`
|
||||
Info string `json:"info"`
|
||||
}
|
||||
|
||||
type FinalResponse struct { //最终响应结构体
|
||||
Status string `json:"status"`
|
||||
Info string `json:"info"`
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
||||
// 实现error接口
|
||||
func (r Response) Error() string { // 实现 error 接口
|
||||
return r.Info
|
||||
}
|
||||
|
||||
func OKWithData(response Response, data interface{}) FinalResponse { //传入一个响应结构体和数据,返回一个最终响应结构体
|
||||
var finalResponse FinalResponse
|
||||
finalResponse.Status = response.Status
|
||||
finalResponse.Info = response.Info
|
||||
finalResponse.Data = data
|
||||
return finalResponse
|
||||
}
|
||||
|
||||
func InternalError(err error) Response { //服务器错误
|
||||
return Response{
|
||||
Status: "500",
|
||||
Info: err.Error(),
|
||||
}
|
||||
}
|
||||
|
||||
var ( //请求相关的响应
|
||||
Ok = Response{ //正常
|
||||
Status: "10000",
|
||||
Info: "success",
|
||||
}
|
||||
|
||||
WrongName = Response{ //用户名错误
|
||||
Status: "40001",
|
||||
Info: "wrong username",
|
||||
}
|
||||
|
||||
WrongPwd = Response{ //密码错误
|
||||
Status: "40002",
|
||||
Info: "wrong password",
|
||||
}
|
||||
|
||||
InvalidName = Response{ //用户名无效
|
||||
Status: "40003",
|
||||
Info: "the username already exists",
|
||||
}
|
||||
|
||||
MissingParam = Response{ //缺少参数
|
||||
Status: "40004",
|
||||
Info: "missing param",
|
||||
}
|
||||
|
||||
WrongParamType = Response{ //参数错误
|
||||
Status: "40005",
|
||||
Info: "wrong param type",
|
||||
}
|
||||
|
||||
ParamTooLong = Response{ //参数过长
|
||||
Status: "40006",
|
||||
Info: "param too long",
|
||||
}
|
||||
|
||||
WrongUsernameOrPwd = Response{ //用户名或密码错误
|
||||
Status: "40007",
|
||||
Info: "wrong username or password",
|
||||
}
|
||||
|
||||
WrongGender = Response{ //性别错误
|
||||
Status: "40008",
|
||||
Info: "wrong gender",
|
||||
}
|
||||
|
||||
MissingToken = Response{ //缺少token
|
||||
Status: "40009",
|
||||
Info: "missing token",
|
||||
}
|
||||
|
||||
InvalidTokenSingingMethod = Response{ //jwt token签名方法无效
|
||||
Status: "40010",
|
||||
Info: "invalid signing method",
|
||||
}
|
||||
|
||||
InvalidToken = Response{ //无效token
|
||||
Status: "40011",
|
||||
Info: "invalid token",
|
||||
}
|
||||
|
||||
InvalidClaims = Response{ //无效声明
|
||||
Status: "40012",
|
||||
Info: "invalid claims",
|
||||
}
|
||||
|
||||
WrongUserID = Response{ //用户ID错误
|
||||
Status: "40013",
|
||||
Info: "wrong userid",
|
||||
}
|
||||
|
||||
ErrUnauthorized = Response{ //未授权,没有权限
|
||||
Status: "40014",
|
||||
Info: "unauthorized",
|
||||
}
|
||||
|
||||
InvalidRefreshToken = Response{ //刷新令牌无效
|
||||
Status: "40015",
|
||||
Info: "invalid refresh token",
|
||||
}
|
||||
|
||||
WrongTokenType = Response{ //无效令牌类型
|
||||
Status: "40016",
|
||||
Info: "wrong token type",
|
||||
}
|
||||
)
|
||||
52
backend/routers/routers.go
Normal file
52
backend/routers/routers.go
Normal file
@@ -0,0 +1,52 @@
|
||||
// Package routers 路由配置
|
||||
// 定义所有HTTP路由和路由组
|
||||
package routers
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/smartflow/backend/api"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// StartEngine 注册路由
|
||||
func StartEngine(r *gin.Engine) {
|
||||
// 从配置中获取端口
|
||||
port := viper.GetString("server.port")
|
||||
if port == "" {
|
||||
port = "8080" // 默认端口
|
||||
}
|
||||
|
||||
// 启动服务器
|
||||
log.Printf("Server starting on port %s...", port)
|
||||
if err := r.Run(":" + port); err != nil {
|
||||
log.Fatalf("Failed to start server: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func RegisterRouters(handlers *api.ApiHandlers) *gin.Engine {
|
||||
// 初始化Gin引擎
|
||||
r := gin.Default()
|
||||
// 在这里注册所有的路由和路由组
|
||||
apiGroup := r.Group("/api/v1")
|
||||
{
|
||||
// 健康检查路由
|
||||
apiGroup.GET("/health", func(c *gin.Context) {
|
||||
c.JSON(200, gin.H{
|
||||
"status": "ok",
|
||||
"version": "1.0.0",
|
||||
})
|
||||
})
|
||||
|
||||
userGroup := apiGroup.Group("/user")
|
||||
{
|
||||
userGroup.POST("/register", handlers.UserHandler.UserRegister)
|
||||
userGroup.POST("/login", handlers.UserHandler.UserLogin)
|
||||
userGroup.POST("/refresh-token", handlers.UserHandler.RefreshTokenHandler)
|
||||
}
|
||||
}
|
||||
// 初始化Gin引擎
|
||||
log.Println("Routes setup completed")
|
||||
return r
|
||||
}
|
||||
109
backend/service/user.go
Normal file
109
backend/service/user.go
Normal file
@@ -0,0 +1,109 @@
|
||||
// Package service 业务逻辑层
|
||||
// 包含所有核心业务逻辑
|
||||
package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/golang-jwt/jwt/v4"
|
||||
"github.com/smartflow/backend/auth"
|
||||
"github.com/smartflow/backend/dao"
|
||||
"github.com/smartflow/backend/model"
|
||||
"github.com/smartflow/backend/respond"
|
||||
"github.com/smartflow/backend/utils"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type UserService struct {
|
||||
// 伸出手:准备接住 DAO
|
||||
repo *dao.UserDAO
|
||||
}
|
||||
|
||||
// NewUserService:组装 Service 的“工厂”
|
||||
func NewUserService(repo *dao.UserDAO) *UserService {
|
||||
return &UserService{
|
||||
repo: repo, // 把传进来的 DAO 揣进口袋里
|
||||
}
|
||||
}
|
||||
|
||||
func (sv *UserService) UserRegister(user model.UserRegisterRequest) (*model.UserRegisterResponse, error) {
|
||||
//检查是否有空字段
|
||||
if user.Username == "" || user.Password == "" ||
|
||||
user.PhoneNumber == "" {
|
||||
return nil, respond.MissingParam
|
||||
}
|
||||
// 检查字段长度是否超过90%
|
||||
if len(user.Username) > 45 || len(user.Password) > 229 || len(user.PhoneNumber) > 18 {
|
||||
return nil, respond.ParamTooLong
|
||||
}
|
||||
//检查用户名是否已存在
|
||||
result, err := sv.repo.IfUsernameExists(user.Username)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if result {
|
||||
return nil, respond.InvalidName
|
||||
}
|
||||
hashedPwd, err := utils.HashPassword(user.Password) //调用utils层的方法
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
user.Password = hashedPwd //将user的密码字段改为加密后的密码
|
||||
newUser, err := sv.repo.Create(user.Username, user.PhoneNumber, user.Password)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
//返回注册成功的用户ID
|
||||
return &model.UserRegisterResponse{ID: newUser.ID}, nil
|
||||
}
|
||||
|
||||
func (sv *UserService) UserLogin(req *model.UserLoginRequest) (*model.Tokens, error) {
|
||||
var tokens model.Tokens
|
||||
hashedPwd, err := sv.repo.GetUserHashedPasswordByName(req.Username) //调用dao层的方法
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, respond.WrongName
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
result, err := utils.CompareHashPwdAndPwd(hashedPwd, req.Password) //比较密码是否匹配
|
||||
if err != nil { //其他错误
|
||||
return &tokens, err
|
||||
} else if !result { //密码不匹配
|
||||
return nil, respond.WrongPwd
|
||||
}
|
||||
id, err := sv.repo.GetUserIDByName(req.Username)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, respond.WrongName
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
tokens.AccessToken, tokens.RefreshToken, err = auth.GenerateTokens(id) //生成jwt key
|
||||
if err != nil { //其他错误
|
||||
return nil, err
|
||||
}
|
||||
return &tokens, nil
|
||||
}
|
||||
|
||||
func (sv *UserService) RefreshTokenHandler(refreshToken string) (*model.Tokens, error) {
|
||||
// 验证刷新令牌
|
||||
token, err := auth.ValidateRefreshToken(refreshToken)
|
||||
if err != nil || !token.Valid { // 刷新令牌无效
|
||||
return nil, respond.InvalidRefreshToken
|
||||
}
|
||||
|
||||
// 生成新的访问令牌和刷新令牌
|
||||
if claims, ok := token.Claims.(jwt.MapClaims); ok {
|
||||
userID := int(claims["user_id"].(float64))
|
||||
newAccessToken, newRefreshToken, err := auth.GenerateTokens(userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 返回新的访问令牌和刷新令牌
|
||||
return &model.Tokens{AccessToken: newAccessToken, RefreshToken: newRefreshToken}, nil
|
||||
} else {
|
||||
return nil, respond.InvalidClaims
|
||||
}
|
||||
}
|
||||
30
backend/utils/pwd_encryption.go
Normal file
30
backend/utils/pwd_encryption.go
Normal file
@@ -0,0 +1,30 @@
|
||||
// Package utils 工具函数库
|
||||
// 包含各种通用工具函数
|
||||
package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
// HashPassword 用于对密码进行哈希加密
|
||||
func HashPassword(pwd string) (string, error) {
|
||||
hashedPwd, err := bcrypt.GenerateFromPassword([]byte(pwd), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(hashedPwd), nil
|
||||
}
|
||||
|
||||
// CompareHashPwdAndPwd 用于比较哈希密码和密码是否匹配
|
||||
func CompareHashPwdAndPwd(hashedPwd, pwd string) (bool, error) {
|
||||
err := bcrypt.CompareHashAndPassword([]byte(hashedPwd), []byte(pwd))
|
||||
if errors.Is(err, bcrypt.ErrMismatchedHashAndPassword) { //密码不匹配
|
||||
return false, nil
|
||||
} else if err != nil { //其他错误
|
||||
return false, err
|
||||
} else { //密码匹配
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user