feat(auth): 重构认证模块,添加OTP发送和登录功能

This commit is contained in:
2025-12-30 18:10:24 +08:00
parent 3952d80d30
commit 6d7f4ad1c6
8 changed files with 149 additions and 24 deletions

View File

@@ -3,6 +3,7 @@ package http
import (
super_v1 "quyun/v2/app/http/super/v1"
v1 "quyun/v2/app/http/v1"
v1_auth "quyun/v2/app/http/v1/auth"
"go.ipao.vip/atom/container"
)
@@ -10,6 +11,7 @@ import (
func Providers() container.Providers {
return container.Providers{
{Provider: v1.Provide},
{Provider: v1_auth.Provide},
{Provider: super_v1.Provide},
}
}

View File

@@ -1,4 +1,4 @@
package v1
package auth
import (
"quyun/v2/app/http/v1/dto"

View File

@@ -0,0 +1,36 @@
package dto
import "quyun/v2/pkg/consts"
type SendOTPForm struct {
Phone string `json:"phone"`
}
type LoginForm struct {
Phone string `json:"phone"`
OTP string `json:"otp"`
}
type LoginResponse struct {
Token string `json:"token"`
User *User `json:"user"`
}
type User struct {
ID string `json:"id"`
Phone string `json:"phone"`
Nickname string `json:"nickname"`
Avatar string `json:"avatar"`
Gender consts.Gender `json:"gender"`
Bio string `json:"bio"`
Birthday string `json:"birthday"` // YYYY-MM-DD
Location *Location `json:"location"`
Balance float64 `json:"balance"`
Points int64 `json:"points"`
IsRealNameVerified bool `json:"is_real_name_verified"`
}
type Location struct {
Province string `json:"province"`
City string `json:"city"`
}

View File

@@ -0,0 +1,37 @@
package auth
import (
"quyun/v2/app/middlewares"
"go.ipao.vip/atom"
"go.ipao.vip/atom/container"
"go.ipao.vip/atom/contracts"
"go.ipao.vip/atom/opt"
)
func Provide(opts ...opt.Option) error {
if err := container.Container.Provide(func() (*Auth, error) {
obj := &Auth{}
return obj, nil
}); err != nil {
return err
}
if err := container.Container.Provide(func(
auth *Auth,
middlewares *middlewares.Middlewares,
) (contracts.HttpRoute, error) {
obj := &Routes{
auth: auth,
middlewares: middlewares,
}
if err := obj.Prepare(); err != nil {
return nil, err
}
return obj, nil
}, atom.GroupRoutes); err != nil {
return err
}
return nil
}

View File

@@ -0,0 +1,57 @@
// Code generated by atomctl. DO NOT EDIT.
// Package auth provides HTTP route definitions and registration
// for the quyun/v2 application.
package auth
import (
"quyun/v2/app/http/v1/dto"
"quyun/v2/app/middlewares"
"github.com/gofiber/fiber/v3"
log "github.com/sirupsen/logrus"
_ "go.ipao.vip/atom"
_ "go.ipao.vip/atom/contracts"
. "go.ipao.vip/atom/fen"
)
// Routes implements the HttpRoute contract and provides route registration
// for all controllers in the auth module.
//
// @provider contracts.HttpRoute atom.GroupRoutes
type Routes struct {
log *log.Entry `inject:"false"`
middlewares *middlewares.Middlewares
// Controller instances
auth *Auth
}
// Prepare initializes the routes provider with logging configuration.
func (r *Routes) Prepare() error {
r.log = log.WithField("module", "routes.auth")
r.log.Info("Initializing routes module")
return nil
}
// Name returns the unique identifier for this routes provider.
func (r *Routes) Name() string {
return "auth"
}
// Register registers all HTTP routes with the provided fiber router.
// Each route is registered with its corresponding controller action and parameter bindings.
func (r *Routes) Register(router fiber.Router) {
// Register routes for controller: Auth
r.log.Debugf("Registering route: Post /v1/auth/login -> auth.Login")
router.Post("/v1/auth/login"[len(r.Path()):], DataFunc1(
r.auth.Login,
Body[dto.LoginForm]("form"),
))
r.log.Debugf("Registering route: Post /v1/auth/otp -> auth.SendOTP")
router.Post("/v1/auth/otp"[len(r.Path()):], Func1(
r.auth.SendOTP,
Body[dto.SendOTPForm]("form"),
))
r.log.Info("Successfully registered all routes")
}

View File

@@ -0,0 +1,9 @@
package auth
func (r *Routes) Path() string {
return "/auth"
}
func (r *Routes) Middlewares() []any {
return []any{}
}

View File

@@ -2,6 +2,7 @@ package v1
import (
"quyun/v2/app/middlewares"
"quyun/v2/providers/storage"
"go.ipao.vip/atom"
"go.ipao.vip/atom/container"
@@ -10,13 +11,6 @@ import (
)
func Provide(opts ...opt.Option) error {
if err := container.Container.Provide(func() (*Auth, error) {
obj := &Auth{}
return obj, nil
}); err != nil {
return err
}
if err := container.Container.Provide(func() (*Common, error) {
obj := &Common{}
@@ -39,7 +33,6 @@ func Provide(opts ...opt.Option) error {
return err
}
if err := container.Container.Provide(func(
auth *Auth,
common *Common,
content *Content,
creator *Creator,
@@ -50,7 +43,6 @@ func Provide(opts ...opt.Option) error {
user *User,
) (contracts.HttpRoute, error) {
obj := &Routes{
auth: auth,
common: common,
content: content,
creator: creator,
@@ -68,8 +60,12 @@ func Provide(opts ...opt.Option) error {
}, atom.GroupRoutes); err != nil {
return err
}
if err := container.Container.Provide(func() (*Storage, error) {
obj := &Storage{}
if err := container.Container.Provide(func(
storage *storage.Storage,
) (*Storage, error) {
obj := &Storage{
storage: storage,
}
return obj, nil
}); err != nil {

View File

@@ -24,7 +24,6 @@ type Routes struct {
log *log.Entry `inject:"false"`
middlewares *middlewares.Middlewares
// Controller instances
auth *Auth
common *Common
content *Content
creator *Creator
@@ -49,17 +48,6 @@ func (r *Routes) Name() string {
// Register registers all HTTP routes with the provided fiber router.
// Each route is registered with its corresponding controller action and parameter bindings.
func (r *Routes) Register(router fiber.Router) {
// Register routes for controller: Auth
r.log.Debugf("Registering route: Post /v1/auth/login -> auth.Login")
router.Post("/v1/auth/login"[len(r.Path()):], DataFunc1(
r.auth.Login,
Body[dto.LoginForm]("form"),
))
r.log.Debugf("Registering route: Post /v1/auth/otp -> auth.SendOTP")
router.Post("/v1/auth/otp"[len(r.Path()):], Func1(
r.auth.SendOTP,
Body[dto.SendOTPForm]("form"),
))
// Register routes for controller: Common
r.log.Debugf("Registering route: Post /v1/upload -> common.Upload")
router.Post("/v1/upload"[len(r.Path()):], DataFunc2(