This commit is contained in:
40
backend_v1/app/http/admin/auth.go
Normal file
40
backend_v1/app/http/admin/auth.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"quyun/v2/providers/jwt"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
)
|
||||
|
||||
// @provider
|
||||
type auth struct {
|
||||
jwt *jwt.JWT
|
||||
}
|
||||
|
||||
type AuthBody struct {
|
||||
Username string `json:"username" validate:"required"`
|
||||
Password string `json:"password" validate:"required"`
|
||||
}
|
||||
|
||||
type TokenResponse struct {
|
||||
Token string `json:"token"`
|
||||
}
|
||||
|
||||
// Login
|
||||
//
|
||||
// @Router /admin/auth [post]
|
||||
// @Bind body body
|
||||
func (ctl *auth) Login(ctx fiber.Ctx, body *AuthBody) (*TokenResponse, error) {
|
||||
if body.Username == "pl.yang" && body.Password == "Xixi@0202" {
|
||||
claim := ctl.jwt.CreateClaims(jwt.BaseClaims{
|
||||
UserID: -20140202,
|
||||
})
|
||||
|
||||
token, err := ctl.jwt.CreateToken(claim)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &TokenResponse{Token: token}, nil
|
||||
}
|
||||
return nil, fiber.ErrUnauthorized
|
||||
}
|
||||
63
backend_v1/app/http/admin/medias.go
Normal file
63
backend_v1/app/http/admin/medias.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"quyun/v2/app/requests"
|
||||
"quyun/v2/app/services"
|
||||
"quyun/v2/database"
|
||||
"quyun/v2/database/models"
|
||||
"quyun/v2/providers/ali"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
)
|
||||
|
||||
// @provider
|
||||
type medias struct {
|
||||
oss *ali.OSSClient
|
||||
}
|
||||
|
||||
// List medias
|
||||
//
|
||||
// @Router /admin/medias [get]
|
||||
// @Bind pagination query
|
||||
// @Bind query query
|
||||
func (ctl *medias) List(ctx fiber.Ctx, pagination *requests.Pagination, query *ListQuery) (*requests.Pager, error) {
|
||||
return services.Medias.List(ctx, pagination, models.MediaQuery.Name.Like(database.WrapLike(*query.Keyword)))
|
||||
}
|
||||
|
||||
// Show media
|
||||
//
|
||||
// @Router /admin/medias/:id [get]
|
||||
// @Bind id path
|
||||
func (ctl *medias) Show(ctx fiber.Ctx, id int64) error {
|
||||
media, err := services.Medias.FindByID(ctx, id)
|
||||
if err != nil {
|
||||
return ctx.SendString("Media not found")
|
||||
}
|
||||
|
||||
url, err := ctl.oss.GetSignedUrl(ctx, media.Path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return ctx.Redirect().To(url)
|
||||
}
|
||||
|
||||
// Delete
|
||||
//
|
||||
// @Router /admin/medias/:id [delete]
|
||||
// @Bind id path
|
||||
func (ctl *medias) Delete(ctx fiber.Ctx, id int64) error {
|
||||
media, err := services.Medias.FindByID(ctx, id)
|
||||
if err != nil {
|
||||
return ctx.SendString("Media not found")
|
||||
}
|
||||
|
||||
if err := ctl.oss.Delete(ctx, media.Path); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := media.Delete(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
return ctx.SendStatus(fiber.StatusNoContent)
|
||||
}
|
||||
109
backend_v1/app/http/admin/orders.go
Normal file
109
backend_v1/app/http/admin/orders.go
Normal file
@@ -0,0 +1,109 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"quyun/v2/app/requests"
|
||||
"quyun/v2/app/services"
|
||||
"quyun/v2/database/models"
|
||||
"quyun/v2/pkg/fields"
|
||||
"quyun/v2/providers/wepay"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"github.com/pkg/errors"
|
||||
"go.ipao.vip/gen"
|
||||
)
|
||||
|
||||
type OrderListQuery struct {
|
||||
OrderNumber *string `query:"order_number"`
|
||||
UserID *int64 `query:"user_id"`
|
||||
}
|
||||
|
||||
// @provider
|
||||
type orders struct {
|
||||
wepay *wepay.Client
|
||||
}
|
||||
|
||||
// List users
|
||||
//
|
||||
// @Router /admin/orders [get]
|
||||
// @Bind pagination query
|
||||
// @Bind query query
|
||||
func (ctl *orders) List(
|
||||
ctx fiber.Ctx,
|
||||
pagination *requests.Pagination,
|
||||
query *OrderListQuery,
|
||||
) (*requests.Pager, error) {
|
||||
conds := []gen.Condition{}
|
||||
if query.OrderNumber != nil {
|
||||
conds = append(conds, models.OrderQuery.OrderNo.Eq(*query.OrderNumber))
|
||||
}
|
||||
|
||||
if query.UserID != nil {
|
||||
conds = append(conds, models.OrderQuery.UserID.Eq(*query.UserID))
|
||||
}
|
||||
|
||||
return services.Orders.List(ctx, pagination, conds...)
|
||||
}
|
||||
|
||||
// Refund
|
||||
// @Router /admin/orders/:id/refund [post]
|
||||
// @Bind id path
|
||||
func (ctl *orders) Refund(ctx fiber.Ctx, id int64) error {
|
||||
order, err := services.Orders.FindByID(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
user, err := services.Users.FindByID(ctx, order.UserID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
post, err := services.Posts.FindByID(ctx, order.PostID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if order.PaymentMethod == "balance" {
|
||||
if err := services.Users.AddBalance(ctx, user.ID, order.Meta.Data().CostBalance); err != nil {
|
||||
return errors.Wrap(err, "add balance failed")
|
||||
}
|
||||
|
||||
if err := services.Users.RevokeUserPosts(ctx, user.ID, order.PostID); err != nil {
|
||||
return errors.Wrap(err, "revoke posts failed")
|
||||
}
|
||||
|
||||
order.Status = fields.OrderStatusRefundSuccess
|
||||
if _, err := order.Update(ctx); err != nil {
|
||||
return errors.Wrap(err, "update order failed")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
refundTotal := order.Price*int64(order.Discount)/100 - order.Meta.Data().CostBalance
|
||||
resp, err := ctl.wepay.Refund(ctx, func(bm *wepay.BodyMap) {
|
||||
bm.
|
||||
OutRefundNo(order.OrderNo).
|
||||
OutTradeNo(order.OrderNo).
|
||||
TransactionID(order.TransactionID).
|
||||
CNYRefundAmount(refundTotal, refundTotal).
|
||||
RefundReason(fmt.Sprintf("%s 退款", post.Title))
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
meta := order.Meta.Data()
|
||||
meta.RefundResp = resp
|
||||
order.Meta = meta.JsonType()
|
||||
order.RefundTransactionID = resp.RefundId
|
||||
order.Status = fields.OrderStatusRefundProcessing
|
||||
|
||||
if _, err := order.Update(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
215
backend_v1/app/http/admin/posts.go
Normal file
215
backend_v1/app/http/admin/posts.go
Normal file
@@ -0,0 +1,215 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"quyun/v2/app/requests"
|
||||
"quyun/v2/app/services"
|
||||
"quyun/v2/database/models"
|
||||
"quyun/v2/pkg/fields"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"github.com/samber/lo"
|
||||
"go.ipao.vip/gen"
|
||||
"go.ipao.vip/gen/types"
|
||||
)
|
||||
|
||||
type ListQuery struct {
|
||||
Keyword *string `query:"keyword"`
|
||||
}
|
||||
|
||||
// @provider
|
||||
type posts struct{}
|
||||
|
||||
// List posts
|
||||
//
|
||||
// @Router /admin/posts [get]
|
||||
// @Bind pagination query
|
||||
// @Bind query query
|
||||
func (ctl *posts) List(ctx fiber.Ctx, pagination *requests.Pagination, query *ListQuery) (*requests.Pager, error) {
|
||||
conds := []gen.Condition{
|
||||
models.PostQuery.Title.Like(*query.Keyword),
|
||||
}
|
||||
pager, err := services.Posts.List(ctx, pagination, conds...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
postIds := lo.Map(pager.Items.([]models.Post), func(item models.Post, _ int) int64 {
|
||||
return item.ID
|
||||
})
|
||||
if len(postIds) > 0 {
|
||||
postCntMap, err := services.Posts.BoughtStatistics(ctx, postIds)
|
||||
if err != nil {
|
||||
return pager, err
|
||||
}
|
||||
|
||||
items := lo.Map(pager.Items.([]models.Post), func(item models.Post, _ int) PostItem {
|
||||
cnt := int64(0)
|
||||
if v, ok := postCntMap[item.ID]; ok {
|
||||
cnt = v
|
||||
}
|
||||
|
||||
return PostItem{Post: &item, BoughtCount: cnt}
|
||||
})
|
||||
|
||||
pager.Items = items
|
||||
}
|
||||
return pager, err
|
||||
}
|
||||
|
||||
type PostForm struct {
|
||||
Title string `json:"title"`
|
||||
HeadImages []int64 `json:"head_images"`
|
||||
Price int64 `json:"price"`
|
||||
Discount int16 `json:"discount"`
|
||||
Introduction string `json:"introduction"`
|
||||
Medias []int64 `json:"medias"`
|
||||
Status fields.PostStatus `json:"status"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
// Create
|
||||
//
|
||||
// @Router /admin/posts [post]
|
||||
// @Bind form body
|
||||
func (ctl *posts) Create(ctx fiber.Ctx, form *PostForm) error {
|
||||
post := models.Post{
|
||||
Title: form.Title,
|
||||
HeadImages: types.NewJSONType(form.HeadImages),
|
||||
Price: form.Price,
|
||||
Discount: form.Discount,
|
||||
Description: form.Introduction,
|
||||
Status: form.Status,
|
||||
Content: form.Content,
|
||||
Tags: types.NewJSONType([]string{}),
|
||||
Assets: types.NewJSONType([]fields.MediaAsset{}),
|
||||
}
|
||||
|
||||
if form.Medias != nil {
|
||||
medias, err := services.Medias.GetByIds(ctx, form.Medias)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
assets := lo.Map(medias, func(media *models.Media, _ int) fields.MediaAsset {
|
||||
return fields.MediaAsset{
|
||||
Type: media.MimeType,
|
||||
Media: media.ID,
|
||||
Metas: lo.ToPtr(media.Metas.Data()),
|
||||
}
|
||||
})
|
||||
post.Assets = types.NewJSONType(assets)
|
||||
}
|
||||
|
||||
if err := post.Create(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Update posts
|
||||
//
|
||||
// @Router /admin/posts/:id [put]
|
||||
// @Bind id path
|
||||
// @Bind form body
|
||||
func (ctl *posts) Update(ctx fiber.Ctx, id int64, form *PostForm) error {
|
||||
post, err := services.Posts.FindByID(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
post.Title = form.Title
|
||||
post.HeadImages = types.NewJSONType(form.HeadImages)
|
||||
post.Price = form.Price
|
||||
post.Discount = form.Discount
|
||||
post.Description = form.Introduction
|
||||
post.Status = form.Status
|
||||
post.Content = form.Content
|
||||
post.Tags = types.NewJSONType([]string{})
|
||||
|
||||
if form.Medias != nil {
|
||||
medias, err := services.Medias.GetByIds(ctx, form.Medias)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
assets := lo.Map(medias, func(media *models.Media, _ int) fields.MediaAsset {
|
||||
return fields.MediaAsset{
|
||||
Type: media.MimeType,
|
||||
Media: media.ID,
|
||||
Metas: lo.ToPtr(media.Metas.Data()),
|
||||
}
|
||||
})
|
||||
post.Assets = types.NewJSONType(assets)
|
||||
}
|
||||
|
||||
if _, err := post.Update(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Delete posts
|
||||
//
|
||||
// @Router /admin/posts/:id [delete]
|
||||
// @Bind id path
|
||||
func (ctl *posts) Delete(ctx fiber.Ctx, id int64) error {
|
||||
post, err := services.Posts.FindByID(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if post == nil {
|
||||
return fiber.ErrNotFound
|
||||
}
|
||||
|
||||
if _, err := post.ForceDelete(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type PostItem struct {
|
||||
*models.Post
|
||||
Medias []*models.Media `json:"medias"`
|
||||
BoughtCount int64 `json:"bought_count"`
|
||||
}
|
||||
|
||||
// Show posts by id
|
||||
//
|
||||
// @Router /admin/posts/:id [get]
|
||||
// @Bind id path
|
||||
func (ctl *posts) Show(ctx fiber.Ctx, id int64) (*PostItem, error) {
|
||||
post, err := services.Posts.FindByID(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
medias, err := services.Medias.GetByIds(ctx, lo.Map(post.Assets.Data(), func(asset fields.MediaAsset, _ int) int64 {
|
||||
return asset.Media
|
||||
}))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &PostItem{
|
||||
Post: post,
|
||||
Medias: medias,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// SendTo
|
||||
//
|
||||
// @Router /admin/posts/:id/send-to/:userId [post]
|
||||
// @Bind id path
|
||||
// @Bind userId path
|
||||
func (ctl *posts) SendTo(ctx fiber.Ctx, id, userId int64) error {
|
||||
post, err := services.Posts.FindByID(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
user, err := services.Users.FindByID(ctx, userId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := services.Posts.SendTo(ctx, post.ID, user.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
116
backend_v1/app/http/admin/provider.gen.go
Executable file
116
backend_v1/app/http/admin/provider.gen.go
Executable file
@@ -0,0 +1,116 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"quyun/v2/app/middlewares"
|
||||
"quyun/v2/providers/ali"
|
||||
"quyun/v2/providers/app"
|
||||
"quyun/v2/providers/job"
|
||||
"quyun/v2/providers/jwt"
|
||||
"quyun/v2/providers/wepay"
|
||||
|
||||
"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(
|
||||
jwt *jwt.JWT,
|
||||
) (*auth, error) {
|
||||
obj := &auth{
|
||||
jwt: jwt,
|
||||
}
|
||||
|
||||
return obj, nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := container.Container.Provide(func(
|
||||
oss *ali.OSSClient,
|
||||
) (*medias, error) {
|
||||
obj := &medias{
|
||||
oss: oss,
|
||||
}
|
||||
|
||||
return obj, nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := container.Container.Provide(func(
|
||||
wepay *wepay.Client,
|
||||
) (*orders, error) {
|
||||
obj := &orders{
|
||||
wepay: wepay,
|
||||
}
|
||||
|
||||
return obj, nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := container.Container.Provide(func() (*posts, error) {
|
||||
obj := &posts{}
|
||||
|
||||
return obj, nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := container.Container.Provide(func(
|
||||
auth *auth,
|
||||
medias *medias,
|
||||
middlewares *middlewares.Middlewares,
|
||||
orders *orders,
|
||||
posts *posts,
|
||||
statistics *statistics,
|
||||
uploads *uploads,
|
||||
users *users,
|
||||
) (contracts.HttpRoute, error) {
|
||||
obj := &Routes{
|
||||
auth: auth,
|
||||
medias: medias,
|
||||
middlewares: middlewares,
|
||||
orders: orders,
|
||||
posts: posts,
|
||||
statistics: statistics,
|
||||
uploads: uploads,
|
||||
users: users,
|
||||
}
|
||||
if err := obj.Prepare(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return obj, nil
|
||||
}, atom.GroupRoutes); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := container.Container.Provide(func() (*statistics, error) {
|
||||
obj := &statistics{}
|
||||
|
||||
return obj, nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := container.Container.Provide(func(
|
||||
app *app.Config,
|
||||
job *job.Job,
|
||||
oss *ali.OSSClient,
|
||||
) (*uploads, error) {
|
||||
obj := &uploads{
|
||||
app: app,
|
||||
job: job,
|
||||
oss: oss,
|
||||
}
|
||||
|
||||
return obj, nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := container.Container.Provide(func() (*users, error) {
|
||||
obj := &users{}
|
||||
|
||||
return obj, nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
163
backend_v1/app/http/admin/routes.gen.go
Normal file
163
backend_v1/app/http/admin/routes.gen.go
Normal file
@@ -0,0 +1,163 @@
|
||||
// Code generated by atomctl. DO NOT EDIT.
|
||||
|
||||
// Package admin provides HTTP route definitions and registration
|
||||
// for the quyun/v2 application.
|
||||
package admin
|
||||
|
||||
import (
|
||||
"quyun/v2/app/middlewares"
|
||||
"quyun/v2/app/requests"
|
||||
|
||||
"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 admin module.
|
||||
//
|
||||
// @provider contracts.HttpRoute atom.GroupRoutes
|
||||
type Routes struct {
|
||||
log *log.Entry `inject:"false"`
|
||||
middlewares *middlewares.Middlewares
|
||||
// Controller instances
|
||||
auth *auth
|
||||
medias *medias
|
||||
orders *orders
|
||||
posts *posts
|
||||
statistics *statistics
|
||||
uploads *uploads
|
||||
users *users
|
||||
}
|
||||
|
||||
// Prepare initializes the routes provider with logging configuration.
|
||||
func (r *Routes) Prepare() error {
|
||||
r.log = log.WithField("module", "routes.admin")
|
||||
r.log.Info("Initializing routes module")
|
||||
return nil
|
||||
}
|
||||
|
||||
// Name returns the unique identifier for this routes provider.
|
||||
func (r *Routes) Name() string {
|
||||
return "admin"
|
||||
}
|
||||
|
||||
// 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 /admin/auth -> auth.Login")
|
||||
router.Post("/admin/auth"[len(r.Path()):], DataFunc1(
|
||||
r.auth.Login,
|
||||
Body[AuthBody]("body"),
|
||||
))
|
||||
// Register routes for controller: medias
|
||||
r.log.Debugf("Registering route: Delete /admin/medias/:id -> medias.Delete")
|
||||
router.Delete("/admin/medias/:id"[len(r.Path()):], Func1(
|
||||
r.medias.Delete,
|
||||
PathParam[int64]("id"),
|
||||
))
|
||||
r.log.Debugf("Registering route: Get /admin/medias -> medias.List")
|
||||
router.Get("/admin/medias"[len(r.Path()):], DataFunc2(
|
||||
r.medias.List,
|
||||
Query[requests.Pagination]("pagination"),
|
||||
Query[ListQuery]("query"),
|
||||
))
|
||||
r.log.Debugf("Registering route: Get /admin/medias/:id -> medias.Show")
|
||||
router.Get("/admin/medias/:id"[len(r.Path()):], Func1(
|
||||
r.medias.Show,
|
||||
PathParam[int64]("id"),
|
||||
))
|
||||
// Register routes for controller: orders
|
||||
r.log.Debugf("Registering route: Get /admin/orders -> orders.List")
|
||||
router.Get("/admin/orders"[len(r.Path()):], DataFunc2(
|
||||
r.orders.List,
|
||||
Query[requests.Pagination]("pagination"),
|
||||
Query[OrderListQuery]("query"),
|
||||
))
|
||||
r.log.Debugf("Registering route: Post /admin/orders/:id/refund -> orders.Refund")
|
||||
router.Post("/admin/orders/:id/refund"[len(r.Path()):], Func1(
|
||||
r.orders.Refund,
|
||||
PathParam[int64]("id"),
|
||||
))
|
||||
// Register routes for controller: posts
|
||||
r.log.Debugf("Registering route: Delete /admin/posts/:id -> posts.Delete")
|
||||
router.Delete("/admin/posts/:id"[len(r.Path()):], Func1(
|
||||
r.posts.Delete,
|
||||
PathParam[int64]("id"),
|
||||
))
|
||||
r.log.Debugf("Registering route: Get /admin/posts -> posts.List")
|
||||
router.Get("/admin/posts"[len(r.Path()):], DataFunc2(
|
||||
r.posts.List,
|
||||
Query[requests.Pagination]("pagination"),
|
||||
Query[ListQuery]("query"),
|
||||
))
|
||||
r.log.Debugf("Registering route: Get /admin/posts/:id -> posts.Show")
|
||||
router.Get("/admin/posts/:id"[len(r.Path()):], DataFunc1(
|
||||
r.posts.Show,
|
||||
PathParam[int64]("id"),
|
||||
))
|
||||
r.log.Debugf("Registering route: Post /admin/posts -> posts.Create")
|
||||
router.Post("/admin/posts"[len(r.Path()):], Func1(
|
||||
r.posts.Create,
|
||||
Body[PostForm]("form"),
|
||||
))
|
||||
r.log.Debugf("Registering route: Post /admin/posts/:id/send-to/:userId -> posts.SendTo")
|
||||
router.Post("/admin/posts/:id/send-to/:userId"[len(r.Path()):], Func2(
|
||||
r.posts.SendTo,
|
||||
PathParam[int64]("id"),
|
||||
PathParam[int64]("userId"),
|
||||
))
|
||||
r.log.Debugf("Registering route: Put /admin/posts/:id -> posts.Update")
|
||||
router.Put("/admin/posts/:id"[len(r.Path()):], Func2(
|
||||
r.posts.Update,
|
||||
PathParam[int64]("id"),
|
||||
Body[PostForm]("form"),
|
||||
))
|
||||
// Register routes for controller: statistics
|
||||
r.log.Debugf("Registering route: Get /admin/statistics -> statistics.statistics")
|
||||
router.Get("/admin/statistics"[len(r.Path()):], DataFunc0(
|
||||
r.statistics.statistics,
|
||||
))
|
||||
// Register routes for controller: uploads
|
||||
r.log.Debugf("Registering route: Get /admin/uploads/pre-uploaded-check/:md5.:ext -> uploads.PreUploadCheck")
|
||||
router.Get("/admin/uploads/pre-uploaded-check/:md5.:ext"[len(r.Path()):], DataFunc3(
|
||||
r.uploads.PreUploadCheck,
|
||||
PathParam[string]("md5"),
|
||||
PathParam[string]("ext"),
|
||||
QueryParam[string]("mime"),
|
||||
))
|
||||
r.log.Debugf("Registering route: Post /admin/uploads/post-uploaded-action -> uploads.PostUploadedAction")
|
||||
router.Post("/admin/uploads/post-uploaded-action"[len(r.Path()):], Func1(
|
||||
r.uploads.PostUploadedAction,
|
||||
Body[PostUploadedForm]("body"),
|
||||
))
|
||||
// Register routes for controller: users
|
||||
r.log.Debugf("Registering route: Get /admin/users -> users.List")
|
||||
router.Get("/admin/users"[len(r.Path()):], DataFunc2(
|
||||
r.users.List,
|
||||
Query[requests.Pagination]("pagination"),
|
||||
Query[UserListQuery]("query"),
|
||||
))
|
||||
r.log.Debugf("Registering route: Get /admin/users/:id -> users.Show")
|
||||
router.Get("/admin/users/:id"[len(r.Path()):], DataFunc1(
|
||||
r.users.Show,
|
||||
PathParam[int64]("id"),
|
||||
))
|
||||
r.log.Debugf("Registering route: Get /admin/users/:id/articles -> users.Articles")
|
||||
router.Get("/admin/users/:id/articles"[len(r.Path()):], DataFunc2(
|
||||
r.users.Articles,
|
||||
PathParam[int64]("id"),
|
||||
Query[requests.Pagination]("pagination"),
|
||||
))
|
||||
r.log.Debugf("Registering route: Post /admin/users/:id/balance -> users.Balance")
|
||||
router.Post("/admin/users/:id/balance"[len(r.Path()):], Func2(
|
||||
r.users.Balance,
|
||||
PathParam[int64]("id"),
|
||||
Body[UserBalance]("balance"),
|
||||
))
|
||||
|
||||
r.log.Info("Successfully registered all routes")
|
||||
}
|
||||
9
backend_v1/app/http/admin/routes.manual.go
Normal file
9
backend_v1/app/http/admin/routes.manual.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package admin
|
||||
|
||||
func (r *Routes) Path() string {
|
||||
return "/admin"
|
||||
}
|
||||
|
||||
func (r *Routes) Middlewares() []any {
|
||||
return []any{}
|
||||
}
|
||||
68
backend_v1/app/http/admin/statistics.go
Normal file
68
backend_v1/app/http/admin/statistics.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"quyun/v2/app/services"
|
||||
"quyun/v2/database/models"
|
||||
"quyun/v2/pkg/fields"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
)
|
||||
|
||||
// @provider
|
||||
type statistics struct{}
|
||||
|
||||
type StatisticsResponse struct {
|
||||
PostDraft int64 `json:"post_draft"`
|
||||
PostPublished int64 `json:"post_published"`
|
||||
Media int64 `json:"media"`
|
||||
Order int64 `json:"order"`
|
||||
User int64 `json:"user"`
|
||||
Amount int64 `json:"amount"`
|
||||
}
|
||||
|
||||
// dashboard statistics
|
||||
//
|
||||
// @Router /admin/statistics [get]
|
||||
func (s *statistics) statistics(ctx fiber.Ctx) (*StatisticsResponse, error) {
|
||||
statistics := &StatisticsResponse{}
|
||||
|
||||
var err error
|
||||
|
||||
statistics.PostDraft, err = services.Posts.Count(
|
||||
ctx,
|
||||
models.PostQuery.Status.Eq(fields.PostStatusDraft),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
statistics.PostPublished, err = services.Posts.Count(
|
||||
ctx,
|
||||
models.PostQuery.Status.Eq(fields.PostStatusPublished),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
statistics.Media, err = services.Medias.Count(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// model.ExprCond(table.Orders.Status.EQ(Int(int64(fields.OrderStatusCompleted)))))
|
||||
statistics.Order, err = services.Orders.Count(ctx, models.OrderQuery.Status.Eq(fields.OrderStatusCompleted))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
statistics.User, err = services.Users.Count(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
statistics.Amount, err = services.Orders.SumAmount(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return statistics, nil
|
||||
}
|
||||
90
backend_v1/app/http/admin/uploads.go
Normal file
90
backend_v1/app/http/admin/uploads.go
Normal file
@@ -0,0 +1,90 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
|
||||
"quyun/v2/app/jobs"
|
||||
"quyun/v2/app/services"
|
||||
"quyun/v2/database/models"
|
||||
"quyun/v2/providers/ali"
|
||||
"quyun/v2/providers/app"
|
||||
"quyun/v2/providers/job"
|
||||
|
||||
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
|
||||
"github.com/gofiber/fiber/v3"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const UPLOAD_PATH = "quyun"
|
||||
|
||||
// @provider
|
||||
type uploads struct {
|
||||
app *app.Config
|
||||
oss *ali.OSSClient
|
||||
job *job.Job
|
||||
}
|
||||
|
||||
type PreCheckResp struct {
|
||||
Exists bool `json:"exists"`
|
||||
PreSign *oss.PresignResult `json:"pre_sign"`
|
||||
}
|
||||
|
||||
// PreUploadCheck
|
||||
//
|
||||
// @Router /admin/uploads/pre-uploaded-check/:md5.:ext [get]
|
||||
// @Bind md5 path
|
||||
// @Bind ext path
|
||||
// @Bind mime query
|
||||
func (up *uploads) PreUploadCheck(ctx fiber.Ctx, md5, ext, mime string) (*PreCheckResp, error) {
|
||||
_, err := services.Medias.GetByHash(ctx, md5)
|
||||
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
preSign, err := up.oss.PreSignUpload(ctx, fmt.Sprintf("%s.%s", md5, ext), mime)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &PreCheckResp{Exists: false, PreSign: preSign}, nil
|
||||
}
|
||||
|
||||
return &PreCheckResp{Exists: true}, nil
|
||||
}
|
||||
|
||||
type PostUploadedForm struct {
|
||||
OriginalName string `json:"originalName"`
|
||||
Md5 string `json:"md5"`
|
||||
MimeType string `json:"mimeType"`
|
||||
Size int64 `json:"size"`
|
||||
}
|
||||
|
||||
// PostUploadedAction
|
||||
//
|
||||
// @Router /admin/uploads/post-uploaded-action [post]
|
||||
// @Bind body body
|
||||
func (up *uploads) PostUploadedAction(ctx fiber.Ctx, body *PostUploadedForm) error {
|
||||
m, err := services.Medias.GetByHash(ctx, body.Md5)
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return err
|
||||
}
|
||||
|
||||
m = &models.Media{
|
||||
Name: body.OriginalName,
|
||||
MimeType: body.MimeType,
|
||||
Size: body.Size,
|
||||
Hash: body.Md5,
|
||||
Path: filepath.Join(UPLOAD_PATH, body.Md5+filepath.Ext(body.OriginalName)),
|
||||
}
|
||||
if err := m.Create(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if m.MimeType == "video/mp4" {
|
||||
if err := up.job.Add(&jobs.DownloadFromAliOSS{MediaHash: m.Hash}); err != nil {
|
||||
log.WithError(err).WithField("media", m).Errorf("add job failed")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
64
backend_v1/app/http/admin/users.go
Normal file
64
backend_v1/app/http/admin/users.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"quyun/v2/app/requests"
|
||||
"quyun/v2/app/services"
|
||||
"quyun/v2/database"
|
||||
"quyun/v2/database/models"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"go.ipao.vip/gen"
|
||||
)
|
||||
|
||||
type UserListQuery struct {
|
||||
Keyword *string `query:"keyword"`
|
||||
}
|
||||
|
||||
// @provider
|
||||
type users struct{}
|
||||
|
||||
// List users
|
||||
//
|
||||
// @Router /admin/users [get]
|
||||
// @Bind pagination query
|
||||
// @Bind query query
|
||||
func (ctl *users) List(ctx fiber.Ctx, pagination *requests.Pagination, query *UserListQuery) (*requests.Pager, error) {
|
||||
conds := []gen.Condition{
|
||||
models.UserQuery.Username.Like(database.WrapLike(*query.Keyword)),
|
||||
}
|
||||
return services.Users.List(ctx, pagination, conds...)
|
||||
}
|
||||
|
||||
// Show user
|
||||
//
|
||||
// @Router /admin/users/:id [get]
|
||||
// @Bind id path
|
||||
func (ctl *users) Show(ctx fiber.Ctx, id int64) (*models.User, error) {
|
||||
return services.Users.FindByID(ctx, id)
|
||||
}
|
||||
|
||||
// Articles show user bought articles
|
||||
//
|
||||
// @Router /admin/users/:id/articles [get]
|
||||
// @Bind id path
|
||||
// @Bind pagination query
|
||||
func (ctl *users) Articles(ctx fiber.Ctx, id int64, pagination *requests.Pagination) (*requests.Pager, error) {
|
||||
return services.Posts.Bought(ctx, id, pagination)
|
||||
}
|
||||
|
||||
type UserBalance struct {
|
||||
Balance int64 `json:"balance"`
|
||||
}
|
||||
|
||||
// Balance
|
||||
//
|
||||
// @Router /admin/users/:id/balance [post]
|
||||
// @Bind id path
|
||||
// @Bind balance body
|
||||
func (ctl *users) Balance(ctx fiber.Ctx, id int64, balance *UserBalance) error {
|
||||
user, err := services.Users.FindByID(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return services.Users.AddBalance(ctx, user.ID, balance.Balance)
|
||||
}
|
||||
337
backend_v1/app/http/posts.go
Normal file
337
backend_v1/app/http/posts.go
Normal file
@@ -0,0 +1,337 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"quyun/v2/app/jobs"
|
||||
"quyun/v2/app/requests"
|
||||
"quyun/v2/app/services"
|
||||
"quyun/v2/database"
|
||||
"quyun/v2/database/models"
|
||||
"quyun/v2/pkg/fields"
|
||||
"quyun/v2/providers/ali"
|
||||
"quyun/v2/providers/app"
|
||||
"quyun/v2/providers/job"
|
||||
"quyun/v2/providers/wepay"
|
||||
|
||||
"github.com/go-pay/gopay/wechat/v3"
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/samber/lo"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"go.ipao.vip/gen"
|
||||
)
|
||||
|
||||
type ListQuery struct {
|
||||
Keyword *string `query:"keyword"`
|
||||
}
|
||||
|
||||
// @provider
|
||||
type posts struct {
|
||||
wepay *wepay.Client
|
||||
oss *ali.OSSClient
|
||||
job *job.Job
|
||||
app *app.Config
|
||||
}
|
||||
|
||||
// List posts
|
||||
//
|
||||
// @Router /posts [get]
|
||||
// @Bind pagination query
|
||||
// @Bind query query
|
||||
// @Bind user local
|
||||
func (ctl *posts) List(
|
||||
ctx fiber.Ctx,
|
||||
pagination *requests.Pagination,
|
||||
query *ListQuery,
|
||||
user *models.User,
|
||||
) (*requests.Pager, error) {
|
||||
tbl, _ := models.PostQuery.QueryContext(ctx)
|
||||
conds := []gen.Condition{
|
||||
tbl.Status.Eq(fields.PostStatusPublished),
|
||||
tbl.Title.Like(database.WrapLike(*query.Keyword)),
|
||||
}
|
||||
|
||||
pager, err := services.Posts.List(ctx, pagination, conds...)
|
||||
if err != nil {
|
||||
log.WithError(err).Errorf("post list err: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
postIds := lo.Map(pager.Items.([]models.Post), func(item models.Post, _ int) int64 { return item.ID })
|
||||
if len(postIds) > 0 {
|
||||
userBoughtIds, err := services.Users.BatchCheckHasBought(ctx, user.ID, postIds)
|
||||
if err != nil {
|
||||
log.WithError(err).Errorf("BatchCheckHasBought err: %v", err)
|
||||
}
|
||||
|
||||
items := lo.FilterMap(pager.Items.([]models.Post), func(item models.Post, _ int) (PostItem, bool) {
|
||||
medias, err := services.Posts.GetMediasByIds(ctx, item.HeadImages.Data())
|
||||
if err != nil {
|
||||
log.Errorf("GetMediaByIds err: %v", err)
|
||||
return PostItem{}, false
|
||||
}
|
||||
mediaUrls := lo.FilterMap(medias, func(item *models.Media, _ int) (string, bool) {
|
||||
url, err := ctl.oss.GetSignedUrl(ctx, item.Path)
|
||||
if err != nil {
|
||||
log.WithError(err).Errorf("head image GetSignedUrl err: %v", err)
|
||||
return "", false
|
||||
}
|
||||
|
||||
return url, true
|
||||
})
|
||||
|
||||
_, bought := userBoughtIds[item.ID]
|
||||
|
||||
return PostItem{
|
||||
ID: item.ID,
|
||||
Title: item.Title,
|
||||
Description: item.Description,
|
||||
Price: item.Price,
|
||||
Discount: item.Discount,
|
||||
Views: item.Views,
|
||||
Likes: item.Likes,
|
||||
Tags: item.Tags.Data(),
|
||||
HeadImages: mediaUrls,
|
||||
Bought: bought,
|
||||
RechargeWechat: ctl.app.RechargeWechat,
|
||||
}, true
|
||||
})
|
||||
|
||||
pager.Items = items
|
||||
}
|
||||
|
||||
return pager, nil
|
||||
}
|
||||
|
||||
type PostItem struct {
|
||||
ID int64 `json:"id"`
|
||||
Bought bool `json:"bought"`
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
Content string `json:"content"`
|
||||
Price int64 `json:"price"`
|
||||
Discount int16 `json:"discount"`
|
||||
Views int64 `json:"views"`
|
||||
Likes int64 `json:"likes"`
|
||||
Tags []string `json:"tags"`
|
||||
HeadImages []string `json:"head_images"`
|
||||
RechargeWechat string `json:"recharge_wechat,omitempty"`
|
||||
}
|
||||
|
||||
// Show
|
||||
//
|
||||
// @Router /posts/:id/show [get]
|
||||
// @Bind id path
|
||||
// @Bind user local
|
||||
func (ctl *posts) Show(ctx fiber.Ctx, id int64, user *models.User) (*PostItem, error) {
|
||||
log.Infof("Fetching post with ID: %d", id)
|
||||
|
||||
post, err := services.Posts.FindByID(
|
||||
ctx,
|
||||
id,
|
||||
models.PostQuery.Status.Eq(fields.PostStatusPublished),
|
||||
)
|
||||
if err != nil {
|
||||
log.WithError(err).Errorf("GetByID err: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
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())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
mediaUrls := lo.FilterMap(medias, func(item *models.Media, _ int) (string, bool) {
|
||||
url, err := ctl.oss.GetSignedUrl(ctx, item.Path)
|
||||
if err != nil {
|
||||
return "", false
|
||||
}
|
||||
|
||||
return url, true
|
||||
})
|
||||
|
||||
return &PostItem{
|
||||
ID: post.ID,
|
||||
Title: post.Title,
|
||||
Description: post.Description,
|
||||
Content: post.Content,
|
||||
Price: post.Price,
|
||||
Discount: post.Discount,
|
||||
Views: post.Views,
|
||||
Likes: post.Likes,
|
||||
Tags: post.Tags.Data(),
|
||||
HeadImages: mediaUrls,
|
||||
Bought: bought,
|
||||
RechargeWechat: ctl.app.RechargeWechat,
|
||||
}, nil
|
||||
}
|
||||
|
||||
type PlayUrl struct {
|
||||
Url string `json:"url"`
|
||||
}
|
||||
|
||||
// Play
|
||||
//
|
||||
// @Router /posts/:id/play [get]
|
||||
// @Bind id path
|
||||
// @Bind user local
|
||||
func (ctl *posts) Play(ctx fiber.Ctx, id int64, user *models.User) (*PlayUrl, error) {
|
||||
log := log.WithField("PlayPostID", strconv.FormatInt(id, 10))
|
||||
// return &PlayUrl{
|
||||
// Url: "https://github.com/mediaelement/mediaelement-files/raw/refs/heads/master/big_buck_bunny.mp4",
|
||||
// }, nil
|
||||
|
||||
preview := false
|
||||
bought, err := services.Users.HasBought(ctx, user.ID, id)
|
||||
if !bought || err != nil {
|
||||
preview = true
|
||||
}
|
||||
|
||||
log.Infof("Fetching play URL for post ID: %d", id)
|
||||
post, err := services.Posts.FindByID(ctx, id)
|
||||
if err != nil {
|
||||
log.WithError(err).Errorf("GetByID err: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
go services.Posts.IncrViewCount(ctx, post.ID)
|
||||
|
||||
for _, asset := range post.Assets.Data() {
|
||||
if asset.Type == "video/mp4" && asset.Metas != nil && asset.Metas.Short == preview {
|
||||
media, err := services.Medias.FindByID(ctx, asset.Media)
|
||||
if err != nil {
|
||||
log.WithError(err).Errorf("medias GetByID err: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
duration := 2*asset.Metas.Duration + 30
|
||||
if asset.Metas.Duration == 0 {
|
||||
duration = 60 * 5
|
||||
}
|
||||
url, err := ctl.oss.GetSignedUrl(
|
||||
ctx,
|
||||
media.Path,
|
||||
ali.WithExpire(time.Second*time.Duration(duration)),
|
||||
)
|
||||
if err != nil {
|
||||
log.WithError(err).Errorf("media GetSignedUrl err: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
return &PlayUrl{Url: url}, nil
|
||||
}
|
||||
}
|
||||
return nil, errors.New("视频不存在")
|
||||
}
|
||||
|
||||
// Mine posts
|
||||
//
|
||||
// @Router /posts/mine [get]
|
||||
// @Bind pagination query
|
||||
// @Bind query query
|
||||
// @Bind user local
|
||||
func (ctl *posts) Mine(
|
||||
ctx fiber.Ctx,
|
||||
pagination *requests.Pagination,
|
||||
query *ListQuery,
|
||||
user *models.User,
|
||||
) (*requests.Pager, error) {
|
||||
log.Infof("Fetching posts for user with pagination: %+v and keyword: %v", pagination, query.Keyword)
|
||||
|
||||
conds := []gen.Condition{
|
||||
models.PostQuery.Status.Eq(fields.PostStatusPublished),
|
||||
models.PostQuery.Title.Like(database.WrapLike(*query.Keyword)),
|
||||
}
|
||||
|
||||
pager, err := services.Users.PostList(ctx, user.ID, pagination, conds...)
|
||||
if err != nil {
|
||||
log.WithError(err).Errorf("post list err: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
postIds := lo.Map(pager.Items.([]*models.Post), func(item *models.Post, _ int) int64 { return item.ID })
|
||||
if len(postIds) > 0 {
|
||||
items := lo.FilterMap(pager.Items.([]*models.Post), func(item *models.Post, _ int) (PostItem, bool) {
|
||||
medias, err := services.Medias.GetByIds(ctx, item.HeadImages.Data())
|
||||
if err != nil {
|
||||
log.Errorf("GetMediaByIds err: %v", err)
|
||||
return PostItem{}, false
|
||||
}
|
||||
mediaUrls := lo.FilterMap(medias, func(item *models.Media, _ int) (string, bool) {
|
||||
url, err := ctl.oss.GetSignedUrl(ctx, item.Path)
|
||||
if err != nil {
|
||||
log.WithError(err).Errorf("head image GetSignedUrl err: %v", err)
|
||||
return "", false
|
||||
}
|
||||
|
||||
return url, true
|
||||
})
|
||||
|
||||
return PostItem{
|
||||
ID: item.ID,
|
||||
Title: item.Title,
|
||||
Description: item.Description,
|
||||
Price: item.Price,
|
||||
Discount: item.Discount,
|
||||
Views: item.Views,
|
||||
Likes: item.Likes,
|
||||
Tags: item.Tags.Data(),
|
||||
HeadImages: mediaUrls,
|
||||
RechargeWechat: ctl.app.RechargeWechat,
|
||||
}, true
|
||||
})
|
||||
|
||||
pager.Items = items
|
||||
}
|
||||
return pager, nil
|
||||
}
|
||||
|
||||
// Buy
|
||||
//
|
||||
// @Router /posts/:id/buy [post]
|
||||
// @Bind id path
|
||||
// @Bind user local
|
||||
func (ctl *posts) Buy(ctx fiber.Ctx, id int64, user *models.User) (*wechat.JSAPIPayParams, error) {
|
||||
bought, err := services.Users.HasBought(ctx, user.ID, id)
|
||||
if err != nil {
|
||||
return nil, errors.New("查询购买失败")
|
||||
}
|
||||
|
||||
if bought {
|
||||
return nil, errors.New("已经购买过了")
|
||||
}
|
||||
|
||||
post, err := services.Posts.FindByID(ctx, id)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, " failed to get post: %d", id)
|
||||
}
|
||||
// payPrice := post.PayPrice()
|
||||
|
||||
order, err := services.Orders.CreateFromUserPostID(ctx, user.ID, post.ID)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "订单创建失败")
|
||||
}
|
||||
|
||||
if user.Balance >= post.PayPrice() {
|
||||
if err := services.Orders.SetMeta(ctx, order.ID, func(om fields.OrderMeta) fields.OrderMeta {
|
||||
om.CostBalance = post.PayPrice()
|
||||
return om
|
||||
}); err != nil {
|
||||
return nil, errors.Wrap(err, "订单创建失败")
|
||||
}
|
||||
|
||||
if err := ctl.job.Add(&jobs.BalancePayNotify{OrderNo: order.OrderNo}); err != nil {
|
||||
log.Errorf("add job error:%v", err)
|
||||
return nil, errors.Wrap(err, "Failed to add job")
|
||||
}
|
||||
|
||||
return &wechat.JSAPIPayParams{
|
||||
AppId: "balance",
|
||||
}, nil
|
||||
}
|
||||
return nil, errors.Errorf("账户余额不足, 当前余额:%0.2f, 请联系管理员购买或充值", float64(user.Balance)/100)
|
||||
}
|
||||
60
backend_v1/app/http/provider.gen.go
Executable file
60
backend_v1/app/http/provider.gen.go
Executable file
@@ -0,0 +1,60 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"quyun/v2/app/middlewares"
|
||||
"quyun/v2/providers/ali"
|
||||
"quyun/v2/providers/app"
|
||||
"quyun/v2/providers/job"
|
||||
"quyun/v2/providers/wepay"
|
||||
|
||||
"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(
|
||||
app *app.Config,
|
||||
job *job.Job,
|
||||
oss *ali.OSSClient,
|
||||
wepay *wepay.Client,
|
||||
) (*posts, error) {
|
||||
obj := &posts{
|
||||
app: app,
|
||||
job: job,
|
||||
oss: oss,
|
||||
wepay: wepay,
|
||||
}
|
||||
|
||||
return obj, nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := container.Container.Provide(func(
|
||||
middlewares *middlewares.Middlewares,
|
||||
posts *posts,
|
||||
users *users,
|
||||
) (contracts.HttpRoute, error) {
|
||||
obj := &Routes{
|
||||
middlewares: middlewares,
|
||||
posts: posts,
|
||||
users: users,
|
||||
}
|
||||
if err := obj.Prepare(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return obj, nil
|
||||
}, atom.GroupRoutes); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := container.Container.Provide(func() (*users, error) {
|
||||
obj := &users{}
|
||||
|
||||
return obj, nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
14
backend_v1/app/http/provider.manual.go
Executable file
14
backend_v1/app/http/provider.manual.go
Executable file
@@ -0,0 +1,14 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"quyun/v2/app/http/admin"
|
||||
|
||||
"go.ipao.vip/atom/container"
|
||||
)
|
||||
|
||||
func Providers() []container.ProviderContainer {
|
||||
return []container.ProviderContainer{
|
||||
{Provider: Provide},
|
||||
{Provider: admin.Provide},
|
||||
}
|
||||
}
|
||||
93
backend_v1/app/http/routes.gen.go
Normal file
93
backend_v1/app/http/routes.gen.go
Normal file
@@ -0,0 +1,93 @@
|
||||
// Code generated by atomctl. DO NOT EDIT.
|
||||
|
||||
// Package http provides HTTP route definitions and registration
|
||||
// for the quyun/v2 application.
|
||||
package http
|
||||
|
||||
import (
|
||||
"quyun/v2/app/middlewares"
|
||||
"quyun/v2/app/requests"
|
||||
"quyun/v2/database/models"
|
||||
|
||||
"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 http module.
|
||||
//
|
||||
// @provider contracts.HttpRoute atom.GroupRoutes
|
||||
type Routes struct {
|
||||
log *log.Entry `inject:"false"`
|
||||
middlewares *middlewares.Middlewares
|
||||
// Controller instances
|
||||
posts *posts
|
||||
users *users
|
||||
}
|
||||
|
||||
// Prepare initializes the routes provider with logging configuration.
|
||||
func (r *Routes) Prepare() error {
|
||||
r.log = log.WithField("module", "routes.http")
|
||||
r.log.Info("Initializing routes module")
|
||||
return nil
|
||||
}
|
||||
|
||||
// Name returns the unique identifier for this routes provider.
|
||||
func (r *Routes) Name() string {
|
||||
return "http"
|
||||
}
|
||||
|
||||
// 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: posts
|
||||
r.log.Debugf("Registering route: Get /posts -> posts.List")
|
||||
router.Get("/posts"[len(r.Path()):], DataFunc3(
|
||||
r.posts.List,
|
||||
Query[requests.Pagination]("pagination"),
|
||||
Query[ListQuery]("query"),
|
||||
Local[*models.User]("user"),
|
||||
))
|
||||
r.log.Debugf("Registering route: Get /posts/:id/play -> posts.Play")
|
||||
router.Get("/posts/:id/play"[len(r.Path()):], DataFunc2(
|
||||
r.posts.Play,
|
||||
PathParam[int64]("id"),
|
||||
Local[*models.User]("user"),
|
||||
))
|
||||
r.log.Debugf("Registering route: Get /posts/:id/show -> posts.Show")
|
||||
router.Get("/posts/:id/show"[len(r.Path()):], DataFunc2(
|
||||
r.posts.Show,
|
||||
PathParam[int64]("id"),
|
||||
Local[*models.User]("user"),
|
||||
))
|
||||
r.log.Debugf("Registering route: Get /posts/mine -> posts.Mine")
|
||||
router.Get("/posts/mine"[len(r.Path()):], DataFunc3(
|
||||
r.posts.Mine,
|
||||
Query[requests.Pagination]("pagination"),
|
||||
Query[ListQuery]("query"),
|
||||
Local[*models.User]("user"),
|
||||
))
|
||||
r.log.Debugf("Registering route: Post /posts/:id/buy -> posts.Buy")
|
||||
router.Post("/posts/:id/buy"[len(r.Path()):], DataFunc2(
|
||||
r.posts.Buy,
|
||||
PathParam[int64]("id"),
|
||||
Local[*models.User]("user"),
|
||||
))
|
||||
// Register routes for controller: users
|
||||
r.log.Debugf("Registering route: Get /users/profile -> users.Profile")
|
||||
router.Get("/users/profile"[len(r.Path()):], DataFunc1(
|
||||
r.users.Profile,
|
||||
Local[*models.User]("user"),
|
||||
))
|
||||
r.log.Debugf("Registering route: Put /users/username -> users.Update")
|
||||
router.Put("/users/username"[len(r.Path()):], Func2(
|
||||
r.users.Update,
|
||||
Local[*models.User]("user"),
|
||||
Body[ProfileForm]("form"),
|
||||
))
|
||||
|
||||
r.log.Info("Successfully registered all routes")
|
||||
}
|
||||
9
backend_v1/app/http/routes.manual.go
Normal file
9
backend_v1/app/http/routes.manual.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package http
|
||||
|
||||
func (r *Routes) Path() string {
|
||||
return "/http"
|
||||
}
|
||||
|
||||
func (r *Routes) Middlewares() []any {
|
||||
return []any{}
|
||||
}
|
||||
59
backend_v1/app/http/users.go
Normal file
59
backend_v1/app/http/users.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"quyun/v2/app/services"
|
||||
"quyun/v2/database/models"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
)
|
||||
|
||||
// @provider
|
||||
type users struct{}
|
||||
|
||||
type UserInfo struct {
|
||||
ID int64 `json:"id,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at,omitempty"`
|
||||
Username string `json:"username,omitempty"`
|
||||
Avatar string `json:"avatar,omitempty"`
|
||||
Balance int64 `json:"balance"`
|
||||
}
|
||||
|
||||
// @Router /users/profile [get]
|
||||
// @Bind user local
|
||||
func (ctl *users) Profile(ctx fiber.Ctx, user *models.User) (*UserInfo, error) {
|
||||
return &UserInfo{
|
||||
ID: user.ID,
|
||||
CreatedAt: user.CreatedAt,
|
||||
Username: user.Username,
|
||||
Avatar: user.Avatar,
|
||||
Balance: user.Balance,
|
||||
}, nil
|
||||
}
|
||||
|
||||
type ProfileForm struct {
|
||||
Username string `json:"username" validate:"required"`
|
||||
}
|
||||
|
||||
// Update
|
||||
//
|
||||
// @Router /users/username [put]
|
||||
// @Bind user local
|
||||
// @Bind form body
|
||||
func (ctl *users) Update(ctx fiber.Ctx, user *models.User, form *ProfileForm) error {
|
||||
username := strings.TrimSpace(form.Username)
|
||||
if len([]rune(username)) > 12 {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Username exceeds maximum length of 12 characters")
|
||||
}
|
||||
|
||||
if username == "" {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Username cannot be empty")
|
||||
}
|
||||
|
||||
if err := services.Users.SetUsername(ctx, user.ID, username); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -3,9 +3,7 @@ package v1
|
||||
import (
|
||||
"mime/multipart"
|
||||
|
||||
"quyun/v2/app/errorx"
|
||||
"quyun/v2/app/requests"
|
||||
"quyun/v2/app/services"
|
||||
"quyun/v2/providers/jwt"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
@@ -63,14 +61,14 @@ func (d *demo) Foo(
|
||||
file *multipart.FileHeader,
|
||||
req *FooUploadReq,
|
||||
) error {
|
||||
_, err := services.Test.Test(ctx)
|
||||
if err != nil {
|
||||
// 示例:在控制器层自定义错误消息/附加数据
|
||||
appErr := errorx.Wrap(err).
|
||||
WithMsg("获取测试失败").
|
||||
WithData(fiber.Map{"route": "/v1/test"}).
|
||||
WithParams("handler", "Test.Hello")
|
||||
return appErr
|
||||
}
|
||||
// _, err := services.Test.Test(ctx)
|
||||
// if err != nil {
|
||||
// // 示例:在控制器层自定义错误消息/附加数据
|
||||
// appErr := errorx.Wrap(err).
|
||||
// WithMsg("获取测试失败").
|
||||
// WithData(fiber.Map{"route": "/v1/test"}).
|
||||
// WithParams("handler", "Test.Hello")
|
||||
// return appErr
|
||||
// }
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user