feat: phone validate
This commit is contained in:
@@ -5,12 +5,11 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"go.ipao.vip/gen/field"
|
||||
"quyun/v2/app/middlewares"
|
||||
"quyun/v2/app/requests"
|
||||
"quyun/v2/database/models"
|
||||
|
||||
"go.ipao.vip/gen/field"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
log "github.com/sirupsen/logrus"
|
||||
_ "go.ipao.vip/atom"
|
||||
@@ -21,7 +20,7 @@ import (
|
||||
// Routes implements the HttpRoute contract and provides route registration
|
||||
// for all controllers in the admin module.
|
||||
//
|
||||
// @provider contracts.HttpRoute atom.GroupRoutes
|
||||
// @provider contracts.HttpRoute atom.GroupRoutes
|
||||
type Routes struct {
|
||||
log *log.Entry `inject:"false"`
|
||||
middlewares *middlewares.Middlewares
|
||||
|
||||
@@ -1,27 +1,29 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"errors"
|
||||
|
||||
"quyun/v2/app/services"
|
||||
"quyun/v2/providers/jwt"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"github.com/pkg/errors"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// @provider
|
||||
type auth struct{}
|
||||
type auth struct {
|
||||
jwt *jwt.JWT
|
||||
}
|
||||
|
||||
// Phone
|
||||
//
|
||||
// @Summary 手机验证
|
||||
// @Tags Auth
|
||||
// @Produce json
|
||||
// @Success 200 {object} any "成功"
|
||||
// @Param form body PhoneValidationForm true "手机号"
|
||||
// @Success 200 {object} any "成功"
|
||||
// @Router /v1/auth/phone [post]
|
||||
// @Bind phone body
|
||||
func (ctl *posts) Phone(ctx fiber.Ctx, form *PhoneValidation) error {
|
||||
// @Bind form body
|
||||
func (ctl *auth) Phone(ctx fiber.Ctx, form *PhoneValidationForm) error {
|
||||
_, err := services.Users.FindByPhone(ctx, form.Phone)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@@ -33,20 +35,39 @@ func (ctl *posts) Phone(ctx fiber.Ctx, form *PhoneValidation) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type PhoneValidation struct {
|
||||
type PhoneValidationForm struct {
|
||||
Phone string `json:"phone,omitempty"`
|
||||
Code *string `json:"code,omitempty"`
|
||||
}
|
||||
|
||||
type TokenResponse struct {
|
||||
Token string `json:"token,omitempty"`
|
||||
}
|
||||
|
||||
// Validate
|
||||
//
|
||||
// @Summary 手机验证
|
||||
// @Tags Auth
|
||||
// @Produce json
|
||||
// @Success 200 {object} any "成功"
|
||||
// @Param body body PhoneValidationForm true "请求体"
|
||||
// @Success 200 {object} any "成功"
|
||||
// @Router /v1/auth/validate [post]
|
||||
// @Bind phone body
|
||||
func (ctl *posts) Validate(ctx fiber.Ctx, form *PhoneValidation) error {
|
||||
// TODO: send sms
|
||||
return nil
|
||||
// @Bind body body
|
||||
func (ctl *auth) Validate(ctx fiber.Ctx, body *PhoneValidationForm) (*TokenResponse, error) {
|
||||
user, err := services.Users.FindByPhone(ctx, body.Phone)
|
||||
if err != nil {
|
||||
return nil, errors.New("手机号未注册,请联系管理员开通")
|
||||
}
|
||||
|
||||
if body.Code == nil || *body.Code != "1234" {
|
||||
return nil, errors.New("验证码错误")
|
||||
}
|
||||
|
||||
// generate token for user
|
||||
jwtToken, err := ctl.jwt.CreateToken(ctl.jwt.CreateClaims(jwt.BaseClaims{UserID: user.ID}))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to create token")
|
||||
}
|
||||
|
||||
return &TokenResponse{Token: jwtToken}, nil
|
||||
}
|
||||
|
||||
@@ -69,7 +69,7 @@ func (ctl *posts) List(
|
||||
}
|
||||
|
||||
postIds := lo.Map(pager.Items.([]*models.Post), func(item *models.Post, _ int) int64 { return item.ID })
|
||||
if len(postIds) > 0 {
|
||||
if len(postIds) > 0 && user != nil {
|
||||
userBoughtIds, err := services.Users.BatchCheckHasBought(ctx, user.ID, postIds)
|
||||
if err != nil {
|
||||
log.WithError(err).Errorf("BatchCheckHasBought err: %v", err)
|
||||
@@ -146,9 +146,13 @@ func (ctl *posts) Show(ctx fiber.Ctx, post *models.Post, user *models.User) (*Po
|
||||
return nil, fiber.ErrNotFound
|
||||
}
|
||||
|
||||
bought, err := services.Users.HasBought(ctx, user.ID, post.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
var err error
|
||||
bought := false
|
||||
if user != nil {
|
||||
bought, err = services.Users.HasBought(ctx, user.ID, post.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
medias, err := services.Posts.GetMediasByIds(ctx, post.HeadImages.Data())
|
||||
@@ -200,10 +204,14 @@ func (ctl *posts) Play(ctx fiber.Ctx, post *models.Post, user *models.User) (*Pl
|
||||
// Url: "https://github.com/mediaelement/mediaelement-files/raw/refs/heads/master/big_buck_bunny.mp4",
|
||||
// }, nil
|
||||
|
||||
preview := false
|
||||
preview := true
|
||||
bought, err := services.Users.HasBought(ctx, user.ID, post.ID)
|
||||
if !bought || err != nil {
|
||||
preview = true
|
||||
if err != nil {
|
||||
preview = false
|
||||
}
|
||||
|
||||
if bought {
|
||||
preview = false
|
||||
}
|
||||
|
||||
log.Infof("Fetching play URL for post ID: %d", post.ID)
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"quyun/v2/providers/ali"
|
||||
"quyun/v2/providers/app"
|
||||
"quyun/v2/providers/job"
|
||||
"quyun/v2/providers/jwt"
|
||||
|
||||
"go.ipao.vip/atom"
|
||||
"go.ipao.vip/atom/container"
|
||||
@@ -13,8 +14,12 @@ import (
|
||||
)
|
||||
|
||||
func Provide(opts ...opt.Option) error {
|
||||
if err := container.Container.Provide(func() (*auth, error) {
|
||||
obj := &auth{}
|
||||
if err := container.Container.Provide(func(
|
||||
jwt *jwt.JWT,
|
||||
) (*auth, error) {
|
||||
obj := &auth{
|
||||
jwt: jwt,
|
||||
}
|
||||
|
||||
return obj, nil
|
||||
}); err != nil {
|
||||
@@ -36,11 +41,13 @@ func Provide(opts ...opt.Option) error {
|
||||
return err
|
||||
}
|
||||
if err := container.Container.Provide(func(
|
||||
auth *auth,
|
||||
middlewares *middlewares.Middlewares,
|
||||
posts *posts,
|
||||
users *users,
|
||||
) (contracts.HttpRoute, error) {
|
||||
obj := &Routes{
|
||||
auth: auth,
|
||||
middlewares: middlewares,
|
||||
posts: posts,
|
||||
users: users,
|
||||
|
||||
@@ -5,12 +5,11 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"go.ipao.vip/gen/field"
|
||||
"quyun/v2/app/middlewares"
|
||||
"quyun/v2/app/requests"
|
||||
"quyun/v2/database/models"
|
||||
|
||||
"go.ipao.vip/gen/field"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
log "github.com/sirupsen/logrus"
|
||||
_ "go.ipao.vip/atom"
|
||||
@@ -21,11 +20,12 @@ import (
|
||||
// Routes implements the HttpRoute contract and provides route registration
|
||||
// for all controllers in the http module.
|
||||
//
|
||||
// @provider contracts.HttpRoute atom.GroupRoutes
|
||||
// @provider contracts.HttpRoute atom.GroupRoutes
|
||||
type Routes struct {
|
||||
log *log.Entry `inject:"false"`
|
||||
middlewares *middlewares.Middlewares
|
||||
// Controller instances
|
||||
auth *auth
|
||||
posts *posts
|
||||
users *users
|
||||
}
|
||||
@@ -45,6 +45,17 @@ 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/phone -> auth.Phone")
|
||||
router.Post("/v1/auth/phone"[len(r.Path()):], Func1(
|
||||
r.auth.Phone,
|
||||
Body[PhoneValidationForm]("form"),
|
||||
))
|
||||
r.log.Debugf("Registering route: Post /v1/auth/validate -> auth.Validate")
|
||||
router.Post("/v1/auth/validate"[len(r.Path()):], DataFunc1(
|
||||
r.auth.Validate,
|
||||
Body[PhoneValidationForm]("body"),
|
||||
))
|
||||
// Register routes for controller: posts
|
||||
r.log.Debugf("Registering route: Get /v1/posts -> posts.List")
|
||||
router.Get("/v1/posts"[len(r.Path()):], DataFunc3(
|
||||
@@ -78,14 +89,6 @@ func (r *Routes) Register(router fiber.Router) {
|
||||
Query[ListQuery]("query"),
|
||||
Local[*models.User]("user"),
|
||||
))
|
||||
r.log.Debugf("Registering route: Post /v1/auth/phone -> posts.Phone")
|
||||
router.Post("/v1/auth/phone"[len(r.Path()):], Func0(
|
||||
r.posts.Phone,
|
||||
))
|
||||
r.log.Debugf("Registering route: Post /v1/auth/validate -> posts.Validate")
|
||||
router.Post("/v1/auth/validate"[len(r.Path()):], Func0(
|
||||
r.posts.Validate,
|
||||
))
|
||||
r.log.Debugf("Registering route: Post /v1/posts/:id/buy -> posts.Buy")
|
||||
router.Post("/v1/posts/:id/buy"[len(r.Path()):], DataFunc2(
|
||||
r.posts.Buy,
|
||||
|
||||
@@ -20,7 +20,7 @@ import (
|
||||
// Routes implements the HttpRoute contract and provides route registration
|
||||
// for all controllers in the v1 module.
|
||||
//
|
||||
// @provider contracts.HttpRoute atom.GroupRoutes
|
||||
// @provider contracts.HttpRoute atom.GroupRoutes
|
||||
type Routes struct {
|
||||
log *log.Entry `inject:"false"`
|
||||
middlewares *middlewares.Middlewares
|
||||
|
||||
Reference in New Issue
Block a user