fix: update issues

This commit is contained in:
yanghao05
2025-04-18 10:41:07 +08:00
parent 2780d16edc
commit 2854afec53
4 changed files with 68 additions and 46 deletions

View File

@@ -2,16 +2,15 @@ package http
import ( import (
_ "embed" _ "embed"
"encoding/json"
"strings"
"text/template"
"time" "time"
"quyun/app/models" "quyun/app/models"
"quyun/app/requests" "quyun/app/requests"
"quyun/database/fields"
"quyun/database/schemas/public/model" "quyun/database/schemas/public/model"
"quyun/providers/wepay" "quyun/providers/wepay"
"github.com/go-pay/gopay/wechat/v3"
"github.com/gofiber/fiber/v3" "github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/log" "github.com/gofiber/fiber/v3/log"
"github.com/pkg/errors" "github.com/pkg/errors"
@@ -33,7 +32,11 @@ type posts struct {
// @Bind user local // @Bind user local
func (ctl *posts) List(ctx fiber.Ctx, pagination *requests.Pagination, query *ListQuery, user *model.Users) (*requests.Pager, error) { func (ctl *posts) List(ctx fiber.Ctx, pagination *requests.Pagination, query *ListQuery, user *model.Users) (*requests.Pager, error) {
cond := models.Posts.BuildConditionWithKey(query.Keyword) cond := models.Posts.BuildConditionWithKey(query.Keyword)
return models.Posts.List(ctx.Context(), pagination, cond) return models.Posts.List(ctx.Context(), pagination, cond, func(item model.Posts) model.Posts {
item.Assets = fields.ToJson([]fields.MediaAsset{})
item.Content = ""
return item
})
} }
// Show // Show
@@ -49,32 +52,27 @@ func (ctl *posts) Show(ctx fiber.Ctx, id int64) (*model.Posts, error) {
// @Bind query query // @Bind query query
func (ctl *posts) Mine(ctx fiber.Ctx, pagination *requests.Pagination, query *ListQuery) (*requests.Pager, error) { func (ctl *posts) Mine(ctx fiber.Ctx, pagination *requests.Pagination, query *ListQuery) (*requests.Pager, error) {
log.Infof("Fetching posts for user with pagination: %+v and keyword: %v", pagination, query.Keyword) log.Infof("Fetching posts for user with pagination: %+v and keyword: %v", pagination, query.Keyword)
return models.Users.PostList(ctx.Context(), 1, pagination, query.Keyword) return models.Users.PostList(ctx.Context(), 1, pagination, query.Keyword, func(item model.Posts) model.Posts {
item.Assets = fields.ToJson([]fields.MediaAsset{})
item.Content = ""
return item
})
} }
//go:embed buy.html.tpl
var buyTpl string
// Buy // Buy
// @Router /api/posts/buy/:id [get] // @Router /api/posts/buy/:id [get]
// @Bind id path // @Bind id path
func (ctl *posts) Buy(ctx fiber.Ctx, id int64) error { // @Bind user local
var userId int64 = 1 func (ctl *posts) Buy(ctx fiber.Ctx, id int64, user *model.Users) (*wechat.JSAPIPayParams, error) {
user, err := models.Users.GetByID(ctx.Context(), userId)
if err != nil {
return errors.Wrapf(err, " failed to get user: %d", userId)
}
post, err := models.Posts.GetByID(ctx.Context(), id) post, err := models.Posts.GetByID(ctx.Context(), id)
if err != nil { if err != nil {
return errors.Wrapf(err, " failed to get post: %d", id) return nil, errors.Wrapf(err, " failed to get post: %d", id)
} }
// create order // create order
order, err := models.Orders.Create(ctx.Context(), userId, post.ID) order, err := models.Orders.Create(ctx.Context(), user.ID, post.ID)
if err != nil { if err != nil {
return errors.Wrap(err, "订单创建失败") return nil, errors.Wrap(err, "订单创建失败")
} }
prePayResp, err := ctl.wepay.V3TransactionJsapi(ctx.Context(), func(bm *wepay.BodyMap) { prePayResp, err := ctl.wepay.V3TransactionJsapi(ctx.Context(), func(bm *wepay.BodyMap) {
@@ -86,30 +84,8 @@ func (ctl *posts) Buy(ctx fiber.Ctx, id int64) error {
}) })
if err != nil { if err != nil {
log.Errorf("wepay.V3TransactionJsapi err: %v", err) log.Errorf("wepay.V3TransactionJsapi err: %v", err)
return errors.Wrap(err, "微信支付失败") return nil, errors.Wrap(err, "微信支付失败")
} }
signature, err := prePayResp.PaySignOfJSAPI() return prePayResp.PaySignOfJSAPI()
if err != nil {
return errors.Wrap(err, "failed to get pay sign")
}
// render buyTpl with html/template
tpl, err := template.New("buy").Parse(buyTpl)
if err != nil {
return errors.Wrap(err, "failed to parse template")
}
signatureBytes, err := json.Marshal(signature)
if err != nil {
return errors.Wrap(err, "failed to marshal signature")
}
var buf strings.Builder
if err := tpl.Execute(&buf, map[string]interface{}{"signature": string(signatureBytes)}); err != nil {
return errors.Wrap(err, "failed to execute template")
}
ctx.Set("Content-Type", "text/html; charset=utf-8")
return ctx.SendString(buf.String())
} }

View File

@@ -11,6 +11,7 @@ import (
. "github.com/go-jet/jet/v2/postgres" . "github.com/go-jet/jet/v2/postgres"
"github.com/go-jet/jet/v2/qrm" "github.com/go-jet/jet/v2/qrm"
"github.com/samber/lo"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@@ -120,7 +121,7 @@ func (m *postsModel) countByCondition(ctx context.Context, expr BoolExpression)
return cnt.Cnt, nil return cnt.Cnt, nil
} }
func (m *postsModel) List(ctx context.Context, pagination *requests.Pagination, cond BoolExpression) (*requests.Pager, error) { func (m *postsModel) List(ctx context.Context, pagination *requests.Pagination, cond BoolExpression, callbacks ...func(model.Posts) model.Posts) (*requests.Pager, error) {
pagination.Format() pagination.Format()
tbl := table.Posts tbl := table.Posts
@@ -145,6 +146,14 @@ func (m *postsModel) List(ctx context.Context, pagination *requests.Pagination,
return nil, err return nil, err
} }
if len(callbacks) > 0 {
for _, f := range callbacks {
posts = lo.Map(posts, func(item model.Posts, _ int) model.Posts {
return f(item)
})
}
}
return &requests.Pager{ return &requests.Pager{
Items: posts, Items: posts,
Total: count, Total: count,

View File

@@ -9,6 +9,7 @@ import (
"quyun/database/schemas/public/table" "quyun/database/schemas/public/table"
. "github.com/go-jet/jet/v2/postgres" . "github.com/go-jet/jet/v2/postgres"
"github.com/samber/lo"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@@ -183,7 +184,7 @@ func (m *usersModel) DeleteByID(ctx context.Context, id int64) error {
} }
// PostList returns a paginated list of posts for a user // PostList returns a paginated list of posts for a user
func (m *usersModel) PostList(ctx context.Context, userId int64, pagination *requests.Pagination, keyword *string) (*requests.Pager, error) { func (m *usersModel) PostList(ctx context.Context, userId int64, pagination *requests.Pagination, keyword *string, callbacks ...func(model.Posts) model.Posts) (*requests.Pager, error) {
pagination.Format() pagination.Format()
tblUserPosts := table.UserPosts tblUserPosts := table.UserPosts
@@ -221,6 +222,14 @@ func (m *usersModel) PostList(ctx context.Context, userId int64, pagination *req
return nil, err return nil, err
} }
if len(callbacks) > 0 {
for _, f := range callbacks {
posts = lo.Map(posts, func(item model.Posts, _ int) model.Posts {
return f(item)
})
}
}
return &requests.Pager{ return &requests.Pager{
Items: posts, Items: posts,
Total: cnt.Cnt, Total: cnt.Cnt,

View File

@@ -1 +1,29 @@
package main package main_test
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
"github.com/speps/go-hashids/v2"
)
func Test_hassID(t *testing.T) {
Convey("test_hassID", t, func() {
Convey("step 1", func() {
data := hashids.NewData()
data.MinLength = 6
data.Salt = "xixi0202"
data.Alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
hashId, err := hashids.NewWithData(data)
So(err, ShouldBeNil)
So(hashId, ShouldNotBeNil)
item := []int{1, 2}
str, err := hashId.Encode(item)
So(err, ShouldBeNil)
So(str, ShouldNotBeEmpty)
t.Logf("ids:%+v %s", item, str)
})
})
}