fix: issues

This commit is contained in:
yanghao05
2025-04-25 21:19:03 +08:00
parent b35abb2090
commit 5bc3ae468d
15 changed files with 129 additions and 64 deletions

View File

@@ -29,11 +29,13 @@ type posts struct {
}
// List posts
// @Router /api/posts [get]
// @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 *model.Users) (*requests.Pager, error) {
log.Infof("ok", pagination, query.Keyword, user.ID)
cond := models.Posts.BuildConditionWithKey(query.Keyword)
pager, err := models.Posts.List(ctx.Context(), pagination, cond, func(item model.Posts) model.Posts {
item.Assets = fields.ToJson([]fields.MediaAsset{})
@@ -41,6 +43,7 @@ func (ctl *posts) List(ctx fiber.Ctx, pagination *requests.Pagination, query *Li
return item
})
if err != nil {
log.WithError(err).Errorf("post list err: %v", err)
return nil, err
}
@@ -55,6 +58,7 @@ func (ctl *posts) List(ctx fiber.Ctx, pagination *requests.Pagination, query *Li
mediaUrls := lo.FilterMap(medias, func(item model.Medias, _ int) (string, bool) {
url, err := ctl.oss.GetSignedUrl(ctx.Context(), item.Path)
if err != nil {
log.WithError(err).Errorf("head image GetSignedUrl err: %v", err)
return "", false
}
@@ -93,7 +97,7 @@ type PostItem struct {
}
// Show
// @Router /api/posts/:id [get]
// @Router /posts/:id [get]
// @Bind id path
// @Bind user local
func (ctl *posts) Show(ctx fiber.Ctx, id int64, user *model.Users) (*PostItem, error) {
@@ -141,7 +145,7 @@ type PlayUrl struct {
}
// Play
// @Router /api/posts/:id/play [get]
// @Router /posts/:id/play [get]
// @Bind id path
// @Bind user local
func (ctl *posts) Play(ctx fiber.Ctx, id int64, user *model.Users) (*PlayUrl, error) {
@@ -174,7 +178,7 @@ func (ctl *posts) Play(ctx fiber.Ctx, id int64, user *model.Users) (*PlayUrl, er
}
// Mine posts
// @Router /api/posts/mine [get]
// @Router /posts/mine [get]
// @Bind pagination query
// @Bind query query
func (ctl *posts) Mine(ctx fiber.Ctx, pagination *requests.Pagination, query *ListQuery) (*requests.Pager, error) {
@@ -187,7 +191,7 @@ func (ctl *posts) Mine(ctx fiber.Ctx, pagination *requests.Pagination, query *Li
}
// Buy
// @Router /api/posts/:id/buy [get]
// @Router /posts/:id/buy [get]
// @Bind id path
// @Bind user local
func (ctl *posts) Buy(ctx fiber.Ctx, id int64, user *model.Users) (*wechat.JSAPIPayParams, error) {

View File

@@ -57,13 +57,11 @@ func Provide(opts ...opt.Option) error {
auth *auth,
pays *pays,
posts *posts,
weChat *weChat,
) (contracts.HttpRoute, error) {
obj := &Routes{
auth: auth,
pays: pays,
posts: posts,
weChat: weChat,
auth: auth,
pays: pays,
posts: posts,
}
if err := obj.Prepare(); err != nil {
return nil, err
@@ -73,12 +71,5 @@ func Provide(opts ...opt.Option) error {
}, atom.GroupRoutes); err != nil {
return err
}
if err := container.Container.Provide(func() (*weChat, error) {
obj := &weChat{}
return obj, nil
}); err != nil {
return err
}
return nil
}

View File

@@ -14,11 +14,10 @@ import (
// @provider contracts.HttpRoute atom.GroupRoutes
type Routes struct {
log *log.Entry `inject:"false"`
auth *auth
pays *pays
posts *posts
weChat *weChat
log *log.Entry `inject:"false"`
auth *auth
pays *pays
posts *posts
}
func (r *Routes) Prepare() error {
@@ -51,41 +50,35 @@ func (r *Routes) Register(router fiber.Router) {
))
// 注册路由组: posts
router.Get("/api/posts", DataFunc3(
router.Get("/posts", DataFunc3(
r.posts.List,
Query[requests.Pagination]("pagination"),
Query[ListQuery]("query"),
Local[*model.Users]("user"),
))
router.Get("/api/posts/:id", DataFunc2(
router.Get("/posts/:id", DataFunc2(
r.posts.Show,
PathParam[int64]("id"),
Local[*model.Users]("user"),
))
router.Get("/api/posts/:id/play", DataFunc2(
router.Get("/posts/:id/play", DataFunc2(
r.posts.Play,
PathParam[int64]("id"),
Local[*model.Users]("user"),
))
router.Get("/api/posts/mine", DataFunc2(
router.Get("/posts/mine", DataFunc2(
r.posts.Mine,
Query[requests.Pagination]("pagination"),
Query[ListQuery]("query"),
))
router.Get("/api/posts/:id/buy", DataFunc2(
router.Get("/posts/:id/buy", DataFunc2(
r.posts.Buy,
PathParam[int64]("id"),
Local[*model.Users]("user"),
))
// 注册路由组: weChat
router.Get("/MP_verify_:code.txt", Func1(
r.weChat.Verify,
PathParam[string]("code"),
))
}

View File

@@ -1,13 +0,0 @@
package http
import "github.com/gofiber/fiber/v3"
// @provider
type weChat struct{}
// Verify
// @Router /MP_verify_:code.txt [get]
// @Bind code path
func (*weChat) Verify(ctx fiber.Ctx, code string) error {
return ctx.SendString(code)
}

View File

@@ -7,23 +7,21 @@ import (
"quyun/app/models"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/log"
log "github.com/sirupsen/logrus"
)
func (f *Middlewares) Auth(ctx fiber.Ctx) error {
if strings.HasPrefix(ctx.Path(), "/MP_verify_") {
return ctx.Next()
}
if strings.HasPrefix(ctx.Path(), "/auth/") {
if strings.HasPrefix(ctx.Path(), "/v1/auth/") {
return ctx.Next()
}
uu, err := models.Users.GetByID(ctx.Context(), 1)
if err != nil {
log.WithError(err).Error("failed to get user")
return ctx.SendString("NOT OK")
}
ctx.Locals("user", uu)
log.Infof("set ctx user: %d", uu.ID)
return ctx.Next()
fullUrl := string(ctx.Request().URI().FullURI())

View File

@@ -1,10 +1,16 @@
package middlewares
import (
"strings"
"github.com/gofiber/fiber/v3"
)
func (f *Middlewares) AuthAdmin(ctx fiber.Ctx) error {
if !strings.HasPrefix(ctx.Path(), "/v1/admin") {
return ctx.Next()
}
if ctx.Path() == "/v1/admin/auth" {
return ctx.Next()
}

View File

@@ -6,9 +6,12 @@ import (
)
func (f *Middlewares) DebugMode(ctx fiber.Ctx) error {
log.Info("------------------")
log.Infof("c.Path: %s", ctx.Path())
log.Infof("Request Method: %s", ctx.Method())
log.Infof("FullURL: %s", ctx.Request().URI().FullURI())
log.Infof("StartPATH: %s", ctx.Params("*"))
log.Info("------------------")
return ctx.Next()
}

View File

@@ -0,0 +1,19 @@
package middlewares
import (
"strings"
"github.com/gofiber/fiber/v3"
)
func (f *Middlewares) WechatMpVerify(ctx fiber.Ctx) error {
if !strings.HasPrefix(ctx.Path(), "/MP_verify_") {
return ctx.Next()
}
path := strings.Replace(ctx.Path(), "MP_verify_", "", 1)
path = strings.Replace(path, ".txt", "", 1)
path = strings.Trim(path, "/")
return ctx.SendString(path)
}

View File

@@ -2,8 +2,8 @@ package http
import (
"context"
"path/filepath"
"quyun/app/errorx"
appHttp "quyun/app/http"
"quyun/app/jobs"
"quyun/app/middlewares"
@@ -25,7 +25,9 @@ import (
"go.ipao.vip/atom/container"
"go.ipao.vip/atom/contracts"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/middleware/favicon"
"github.com/rogeecn/fabfile"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"go.uber.org/dig"
@@ -84,22 +86,54 @@ func Serve(cmd *cobra.Command, args []string) error {
svc.Http.Engine.Get("/swagger/*", swagger.HandlerDefault)
}
svc.Http.Engine.Use(errorx.Middleware)
// svc.Http.Engine.Use(errorx.Middleware)
svc.Http.Engine.Use(svc.Middlewares.DebugMode)
svc.Http.Engine.Use(favicon.New(favicon.Config{
Data: []byte{},
}))
group := svc.Http.Engine.Group("")
group := svc.Http.Engine.
Group("v1").
Use(
svc.Middlewares.WechatMpVerify,
svc.Middlewares.Auth,
svc.Middlewares.AuthAdmin,
)
for _, route := range svc.Routes {
if route.Name() == "admin" {
route.Register(group.Use(svc.Middlewares.AuthAdmin))
route.Register(group)
continue
}
route.Register(group.Use(svc.Middlewares.Auth))
route.Register(group)
}
// statics
svc.Http.Engine.Get("/admin*", func(ctx fiber.Ctx) error {
f := ctx.Params("*")
if f == "/" {
f = "index.html"
}
file, err := fabfile.Find(filepath.Join("frontend/admin/dist", f))
if err != nil {
return ctx.SendStatus(fiber.StatusNotFound)
}
return ctx.SendFile(file)
})
svc.Http.Engine.Get("*", func(ctx fiber.Ctx) error {
f := ctx.Params("*")
if f == "/" || f == "" {
f = "index.html"
}
file, err := fabfile.Find(filepath.Join("frontend/wechat/dist", f))
if err != nil {
return ctx.SendStatus(fiber.StatusNotFound)
}
return ctx.SendFile(file)
})
// job
if err := svc.Job.Start(ctx); err != nil {
log.WithError(err).Error("job start failed")
return err

6
backend/dist/dist.go vendored Normal file
View File

@@ -0,0 +1,6 @@
package dist
import "embed"
//go:embed admin/*
var Admin embed.FS

View File

@@ -2,7 +2,7 @@ import axios from 'axios';
// Create a base axios instance
export const apiClient = axios.create({
baseURL: import.meta.env.VITE_API_BASE_URL || '',
baseURL: import.meta.env.VITE_API_BASE_URL || '/v1',
timeout: 10000,
headers: {
'Content-Type': 'application/json',

View File

@@ -4,6 +4,18 @@ import { resolve } from 'path';
import { defineConfig } from 'vite';
export default defineConfig({
build: {
rollupOptions: {
output: {
chunkFileNames: 'static/assets/js/[name]-[hash].js',
entryFileNames: 'static/assets/js/[name]-[hash].js',
assetFileNames: 'static/assets/[ext]/[name]-[hash].[ext]',
}
}
},
base: "./",
minify: true,
sourceMap: true,
plugins: [
vue(),
tailwindcss(),

View File

@@ -2,7 +2,7 @@ import axios from 'axios';
// Create axios instance with default config
const client = axios.create({
baseURL: '/',
baseURL: '/v1',
timeout: 10000,
headers: {
'Content-Type': 'application/json',

View File

@@ -2,7 +2,7 @@ import client from './client';
export const postApi = {
list({ page = 1, limit = 10, keyword = '' } = {}) {
return client.get('/api/posts', {
return client.get('/posts', {
params: {
page,
limit,
@@ -12,13 +12,13 @@ export const postApi = {
},
play(id) {
return client.get(`/api/posts/${id}/play`);
return client.get(`/posts/${id}/play`);
},
show(id) {
return client.get(`/api/posts/${id}`);
return client.get(`/posts/${id}`);
},
mine({ page = 1, limit = 10 } = {}) {
return client.get('/api/posts/mine', {
return client.get('/posts/mine', {
params: {
page,
limit
@@ -26,6 +26,6 @@ export const postApi = {
});
},
buy(id) {
return client.post(`/api/posts/buy/${id}`);
return client.post(`/posts/buy/${id}`);
}
}

View File

@@ -4,6 +4,18 @@ import { resolve } from 'path';
import { defineConfig } from 'vite';
export default defineConfig({
build: {
rollupOptions: {
output: {
chunkFileNames: 'static/assets/js/[name]-[hash].js',
entryFileNames: 'static/assets/js/[name]-[hash].js',
assetFileNames: 'static/assets/[ext]/[name]-[hash].[ext]',
}
}
},
base: "./",
minify: true,
sourceMap: true,
plugins: [
vue(),
tailwindcss(),