feat(auth): 重构认证模块,添加登录和检查Token功能,更新路由和数据结构

This commit is contained in:
2025-12-30 18:12:22 +08:00
parent 6d7f4ad1c6
commit 179b6aa0e2
8 changed files with 136 additions and 22 deletions

View File

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

View File

@@ -0,0 +1,32 @@
package dto
import "quyun/v2/pkg/consts"
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,56 @@
// Code generated by atomctl. DO NOT EDIT.
// Package auth provides HTTP route definitions and registration
// for the quyun/v2 application.
package auth
import (
dto "quyun/v2/app/http/super/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: Get /super/v1/auth/token -> auth.CheckToken")
router.Get("/super/v1/auth/token"[len(r.Path()):], DataFunc0(
r.auth.CheckToken,
))
r.log.Debugf("Registering route: Post /super/v1/auth/login -> auth.Login")
router.Post("/super/v1/auth/login"[len(r.Path()):], DataFunc1(
r.auth.Login,
Body[dto.LoginForm]("form"),
))
r.log.Info("Successfully registered all routes")
}

View File

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

View File

@@ -10,13 +10,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() (*contents, error) {
obj := &contents{}
@@ -32,7 +25,6 @@ func Provide(opts ...opt.Option) error {
return err
}
if err := container.Container.Provide(func(
auth *auth,
contents *contents,
middlewares *middlewares.Middlewares,
orders *orders,
@@ -40,7 +32,6 @@ func Provide(opts ...opt.Option) error {
users *users,
) (contracts.HttpRoute, error) {
obj := &Routes{
auth: auth,
contents: contents,
middlewares: middlewares,
orders: orders,

View File

@@ -23,7 +23,6 @@ type Routes struct {
log *log.Entry `inject:"false"`
middlewares *middlewares.Middlewares
// Controller instances
auth *auth
contents *contents
orders *orders
tenants *tenants
@@ -45,16 +44,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: Get /super/v1/auth/token -> auth.CheckToken")
router.Get("/super/v1/auth/token"[len(r.Path()):], DataFunc0(
r.auth.CheckToken,
))
r.log.Debugf("Registering route: Post /super/v1/auth/login -> auth.Login")
router.Post("/super/v1/auth/login"[len(r.Path()):], DataFunc1(
r.auth.Login,
Body[dto.LoginForm]("form"),
))
// Register routes for controller: contents
r.log.Debugf("Registering route: Get /super/v1/contents -> contents.List")
router.Get("/super/v1/contents"[len(r.Path()):], DataFunc1(

View File

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