fix: issues

This commit is contained in:
Rogee
2025-01-14 19:48:23 +08:00
parent b429f30916
commit 48a0a6c195
14 changed files with 1037 additions and 195 deletions

View File

@@ -61,7 +61,7 @@ func (c *OrderController) List(ctx fiber.Ctx, claim *jwt.Claims, pagination *req
// Create order
// @Router /api/v1/orders [post]
// @Bind claim local
// @Bind hash path
// @Bind hash
// @Bind tenantSlug cookie key(tenant)
func (c *OrderController) Create(ctx fiber.Ctx, claim *jwt.Claims, tenantSlug, hash string) (*UserOrder, error) {
user, err := c.userSvc.GetUserByID(ctx.Context(), claim.UserID)

View File

@@ -1,6 +1,8 @@
package orders
import (
"fmt"
"backend/app/errorx"
"backend/app/http/posts"
"backend/app/http/tenants"
@@ -10,7 +12,6 @@ import (
"backend/providers/jwt"
"backend/providers/pay"
"github.com/go-pay/gopay/wechat/v3"
"github.com/gofiber/fiber/v3"
"github.com/samber/lo"
log "github.com/sirupsen/logrus"
@@ -32,10 +33,11 @@ func (c *PayController) Prepare() error {
}
// JSPay
// @Router /api/v1/orders/pay/:orderID/js [get]
// @Router /api/v1/orders/pay/:orderID/:channel [get]
// @Bind claim local
// @Bind orderID path
func (ctl *PayController) JSPay(ctx fiber.Ctx, claim *jwt.Claims, orderID string) (*wechat.JSAPIPayParams, error) {
// @Bind channel path
func (ctl *PayController) Pay(ctx fiber.Ctx, claim *jwt.Claims, channel, orderID string) (any, error) {
order, err := ctl.svc.GetUserOrderByOrderID(ctx.Context(), orderID, claim.UserID)
if err != nil {
return nil, err
@@ -58,6 +60,15 @@ func (ctl *PayController) JSPay(ctx fiber.Ctx, claim *jwt.Claims, orderID string
return nil, errorx.BadRequest.WithMsg("未绑定微信")
}
switch channel {
case "wechat-js":
return ctl.payWechatJS(ctx, order, &oauth)
}
return nil, errorx.BadRequest.WithMsg("支付渠道错误")
}
func (ctl *PayController) payWechatJS(ctx fiber.Ctx, order *model.Orders, oauth *model.UserOauths) (any, error) {
params, err := ctl.pay.WeChat_JSApiPayRequest(
ctx.Context(),
oauth.OpenID,
@@ -65,7 +76,7 @@ func (ctl *PayController) JSPay(ctx fiber.Ctx, claim *jwt.Claims, orderID string
order.Title,
order.Amount,
1,
"/v1/orders/pay/wechat/notify",
ctl.getNotifyURL(ctx, "wechat"),
)
if err != nil {
return nil, err
@@ -73,3 +84,13 @@ func (ctl *PayController) JSPay(ctx fiber.Ctx, claim *jwt.Claims, orderID string
return params, nil
}
func (ctl *PayController) getNotifyURL(ctx fiber.Ctx, channel string) string {
return fmt.Sprintf("%s://%s/v1/orders/pay/notify/%s", ctx.Request().URI().Scheme(), ctx.Host(), channel)
}
// @Router /v1/orders/pay/notify/:channel [post]
// @Bind channel path
func (c *PayController) Notify(ctx fiber.Ctx, channel string) (any, error) {
return nil, nil
}

View File

@@ -46,9 +46,10 @@ func (r *Routes) Register(router fiber.Router) {
))
// 注册路由组: PayController
router.Get("/api/v1/orders/pay/:orderID/js", DataFunc2(
r.payController.JSPay,
router.Get("/api/v1/orders/pay/:orderID/:channel", DataFunc3(
r.payController.Pay,
Local[*jwt.Claims]("claim"),
PathParam[string]("channel"),
PathParam[string]("orderID"),
))

View File

@@ -1,8 +1,13 @@
package posts
import (
"time"
"backend/app/errorx"
"backend/app/http/tenants"
"backend/app/http/users"
"backend/app/requests"
"backend/database/fields"
"backend/database/models/qvyun_v2/public/model"
"backend/providers/jwt"
@@ -10,12 +15,15 @@ import (
"github.com/jinzhu/copier"
"github.com/samber/lo"
log "github.com/sirupsen/logrus"
"github.com/speps/go-hashids/v2"
)
// @provider
type Controller struct {
tenantSvc *tenants.Service
svc *Service
hashIds *hashids.HashID
userSvc *users.Service
tenantSvc *tenants.Service
log *log.Entry `inject:"false"`
}
@@ -109,7 +117,16 @@ func (c *Controller) Show(ctx fiber.Ctx, claim *jwt.Claims, tenantSlug, hash str
return nil, err
}
post, err := c.svc.GetPostByHash(ctx.Context(), tenant.ID, hash)
postIds, err := c.hashIds.DecodeInt64WithError(hash)
if err != nil {
return nil, errorx.RecordNotExists
}
if tenant.ID != postIds[0] {
return nil, errorx.RecordNotExists
}
post, err := c.svc.GetPostByID(ctx.Context(), postIds[1])
if err != nil {
return nil, err
}
@@ -120,3 +137,40 @@ func (c *Controller) Show(ctx fiber.Ctx, claim *jwt.Claims, tenantSlug, hash str
return userPost, nil
}
// @Router /api/v1/posts [post]
// @Bind claim local
// @Bind tenantSlug cookie key(tenant)
// @Bind body body
func (ctl *Controller) Create(ctx fiber.Ctx, claim *jwt.Claims, tenantSlug string, body *PostBody) error {
user, err := ctl.userSvc.GetUserByID(ctx.Context(), claim.UserID)
if err != nil {
return err
}
tenant, err := ctl.tenantSvc.GetTenantBySlug(ctx.Context(), tenantSlug)
if err != nil {
return err
}
post := &model.Posts{
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
TenantID: tenant.ID,
UserID: user.ID,
Title: body.Title,
Description: body.Description,
Content: body.Content,
PosterAssetID: 0,
Stage: fields.PostStagePending,
Status: fields.PostStatusPending,
Price: body.Price,
Discount: body.Discount,
}
if err := ctl.svc.Create(ctx.Context(), tenant, user, post); err != nil {
return err
}
return nil
}

View File

@@ -30,3 +30,11 @@ type UserPostFilter struct {
CreatedAt *time.Time `query:"created_at"`
Keyword *string `json:"title"`
}
type PostBody struct {
Title string
Description string
Content string
Price int64
Discount int16
}

View File

@@ -13,14 +13,16 @@ import (
. "github.com/go-jet/jet/v2/postgres"
"github.com/samber/lo"
log "github.com/sirupsen/logrus"
"github.com/speps/go-hashids/v2"
"go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
)
// @provider:except
type Service struct {
db *sql.DB
log *log.Entry `inject:"false"`
db *sql.DB
hashIds *hashids.HashID
log *log.Entry `inject:"false"`
}
func (svc *Service) Prepare() error {
@@ -163,22 +165,14 @@ func (svc *Service) GetPosts(ctx context.Context, pagination *requests.Paginatio
return posts, count.Cnt, nil
}
// GetPostByHash
func (svc *Service) GetPostByHash(ctx context.Context, tenantID int64, hash string) (*model.Posts, error) {
_, span := otel.Start(ctx, "users.service.GetPostByHash")
// GetPostByID
func (svc *Service) GetPostByID(ctx context.Context, id int64) (*model.Posts, error) {
_, span := otel.Start(ctx, "users.service.GetPostByID")
defer span.End()
span.SetAttributes(
attribute.String("hash", hash),
)
span.SetAttributes(attribute.Int64("post.id", id))
tbl := table.Posts
stmt := tbl.
SELECT(tbl.AllColumns).
WHERE(
tbl.Hash.EQ(String(hash)).AND(
tbl.TenantID.EQ(Int64(tenantID)),
),
)
stmt := tbl.SELECT(tbl.AllColumns).WHERE(tbl.ID.EQ(Int64(id)))
span.SetAttributes(semconv.DBStatementKey.String(stmt.DebugSql()))
var post model.Posts
@@ -220,3 +214,40 @@ func (svc *Service) GetUserBoughtIDs(ctx context.Context, tenantID, userID int64
return item.PostID
}), nil
}
// Create
func (svc *Service) Create(ctx context.Context, tenant *model.Tenants, user *model.Users, post *model.Posts) error {
_, span := otel.Start(ctx, "users.service.Create")
defer span.End()
tbl := table.Posts
stmt := tbl.INSERT(tbl.MutableColumns).MODEL(post)
span.SetAttributes(semconv.DBStatementKey.String(stmt.DebugSql()))
if _, err := stmt.ExecContext(ctx, svc.db); err != nil {
return err
}
return nil
}
// GetPostByHash
func (svc *Service) GetPostByHash(ctx context.Context, tenantID int64, hash string) (*model.Posts, error) {
_, span := otel.Start(ctx, "users.service.GetPostByHash")
defer span.End()
span.SetAttributes(
attribute.Int64("tenant.id", tenantID),
attribute.String("hash", hash),
)
postIDs, err := svc.hashIds.DecodeInt64WithError(hash)
if err != nil {
return nil, err
}
if tenantID != postIDs[0] {
return nil, nil
}
return svc.GetPostByID(ctx, postIDs[1])
}