Files
quyun-v2/backend/app/http/web/auth.go
Rogee 87f569cc6a feat: add tenant admin invite management, ledger overview, order details, and order management features
- Implemented Invite management with creation, searching, and disabling functionalities.
- Added Ledger overview for financial transactions with filtering options.
- Developed Order Detail view for individual order insights and refund capabilities.
- Created Orders management page with search, reset, and pagination features.
- Enhanced user experience with toast notifications for actions and error handling.
2025-12-24 19:41:44 +08:00

70 lines
1.7 KiB
Go

package web
import (
"quyun/v2/app/errorx"
"quyun/v2/app/http/web/dto"
"quyun/v2/app/services"
"quyun/v2/pkg/consts"
"quyun/v2/providers/jwt"
"github.com/gofiber/fiber/v3"
)
// @provider
type auth struct {
jwt *jwt.JWT
}
// Login 用户登录(平台侧,非超级管理员)。
//
// @Summary 用户登录
// @Tags Web
// @Accept json
// @Produce json
// @Param form body dto.LoginForm true "form"
// @Success 200 {object} dto.LoginResponse "成功"
// @Router /v1/auth/login [post]
// @Bind form body
func (ctl *auth) login(ctx fiber.Ctx, form *dto.LoginForm) (*dto.LoginResponse, error) {
m, err := services.User.FindByUsername(ctx, form.Username)
if err != nil {
return nil, errorx.Wrap(err).WithMsg("用户名或密码错误")
}
if ok := m.ComparePassword(ctx, form.Password); !ok {
return nil, errorx.Wrap(errorx.ErrInvalidCredentials).WithMsg("用户名或密码错误")
}
token, err := ctl.jwt.CreateToken(ctl.jwt.CreateClaims(jwt.BaseClaims{
UserID: m.ID,
}))
if err != nil {
return nil, errorx.Wrap(err).WithMsg("登录凭证生成失败")
}
return &dto.LoginResponse{Token: token}, nil
}
// Token 刷新登录凭证。
//
// @Summary 刷新 Token
// @Tags Web
// @Accept json
// @Produce json
// @Success 200 {object} dto.LoginResponse "成功"
// @Router /v1/auth/token [get]
func (ctl *auth) token(ctx fiber.Ctx) (*dto.LoginResponse, error) {
claims, ok := ctx.Locals(consts.CtxKeyClaims).(*jwt.Claims)
if !ok || claims == nil || claims.UserID <= 0 {
return nil, errorx.ErrTokenInvalid
}
token, err := ctl.jwt.CreateToken(ctl.jwt.CreateClaims(jwt.BaseClaims{
UserID: claims.UserID,
}))
if err != nil {
return nil, errorx.Wrap(err).WithMsg("登录凭证生成失败")
}
return &dto.LoginResponse{Token: token}, nil
}