bindbox-game/internal/api/user/sync_douyin_orders_app.go
2026-04-20 15:53:31 +08:00

67 lines
2.0 KiB
Go

package app
import (
"context"
"net/http"
"time"
"bindbox-game/internal/code"
"bindbox-game/internal/pkg/core"
"bindbox-game/internal/pkg/validation"
)
type syncMyDouyinOrdersResponse struct {
Message string `json:"message"`
DouyinUserID string `json:"douyin_user_id"`
TotalFetched int `json:"total_fetched"`
NewOrders int `json:"new_orders"`
MatchedUsers int `json:"matched_users"`
ElapsedMS int64 `json:"elapsed_ms"`
}
// SyncMyDouyinOrders 同步当前登录用户的抖音订单
// @Summary 同步我的抖音订单
// @Description 从当前登录用户绑定的 douyin_user_id 定向拉取抖音订单并同步到本地
// @Tags APP端.用户
// @Accept json
// @Produce json
// @Security LoginVerifyToken
// @Success 200 {object} syncMyDouyinOrdersResponse
// @Failure 400 {object} code.Failure
// @Router /api/app/users/douyin/orders/sync [post]
func (h *handler) SyncMyDouyinOrders() core.HandlerFunc {
return func(ctx core.Context) {
currentUserID := int64(ctx.SessionUserInfo().Id)
if currentUserID <= 0 {
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "无效用户信息"))
return
}
currentUser, err := h.readDB.Users.WithContext(ctx.RequestContext()).
Where(h.readDB.Users.ID.Eq(currentUserID)).
First()
if err != nil {
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ServerError, validation.Error(err)))
return
}
bgCtx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
result, err := h.douyin.SyncUserOrders(bgCtx, currentUserID)
if err != nil {
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ServerError, validation.Error(err)))
return
}
ctx.Payload(syncMyDouyinOrdersResponse{
Message: "抖音订单同步成功",
DouyinUserID: currentUser.DouyinUserID,
TotalFetched: result.TotalFetched,
NewOrders: result.NewOrders,
MatchedUsers: result.MatchedUsers,
ElapsedMS: result.ElapsedMS,
})
}
}