Files
affiliate_dash/backend/internal/handler/platform_lewan.go
T
yml2213 5f642eb7a6 feat: 优化发货交付页面与凭据生成,支持一键复制结果图片及官方商品图片本地缓存
- 交付界面重构:隐藏内部订单号、流水号与发货渠道,展示聚焦于买家游戏UID、角色名与交付时间
- 发货结果凭据生成:采用 Canvas 生成高清发货存证图,优化存证说明文案与自适应排版
- 一键复制图片:支持直接将结果凭证图片写入系统剪贴板,方便客服在聊天窗口快捷粘贴
- 官方图片本地化缓存:全量下载乐玩商品官方主图并建立本地持久化路由,规避外部源站跨域及破损问题
- 修复乐玩渠道角色UID提取与履约状态幂等回填逻辑
2026-09-14 17:19:12 +08:00

156 lines
4.6 KiB
Go

package handler
import (
"net/http"
"strconv"
"affiliate_dash/internal/middleware"
"affiliate_dash/internal/pkg/lewan"
"affiliate_dash/internal/pkg/response"
"affiliate_dash/internal/service"
"github.com/gin-gonic/gin"
)
type PlatformLewanHandler struct {
merchantSvc *service.MerchantService
lewanClient *lewan.Client
}
func NewPlatformLewanHandler(merchantSvc *service.MerchantService, lewanClient *lewan.Client) *PlatformLewanHandler {
return &PlatformLewanHandler{
merchantSvc: merchantSvc,
lewanClient: lewanClient,
}
}
// ListLewanProducts GET /api/platform/lewan/products
// 从乐玩开放平台实时拉取商品列表供管理员浏览与关联
func (h *PlatformLewanHandler) ListLewanProducts(c *gin.Context) {
if h.lewanClient == nil {
response.BadRequest(c, "乐玩开放平台尚未配置")
return
}
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
size, _ := strconv.Atoi(c.DefaultQuery("size", "50"))
keyword := c.Query("keyword")
result, err := h.lewanClient.ListProducts(c.Request.Context(), page, size, keyword)
if err != nil {
response.BadRequest(c, err.Error())
return
}
response.OK(c, result)
}
type updateRoutingReq struct {
RoutingMode string `json:"routing_mode" binding:"required"`
Priority []string `json:"priority"`
DLCGoodID string `json:"dlc_good_id"`
LewanProductID string `json:"lewan_product_id"`
LewanName string `json:"lewan_name"`
DeliveryType string `json:"delivery_type"`
}
// UpdateProductRouting PUT /api/platform/products/:id/routing
// 管理员更新指定平台商品的履约渠道与优先级
func (h *PlatformLewanHandler) UpdateProductRouting(c *gin.Context) {
idParam := c.Param("id")
catalogID, err := strconv.ParseUint(idParam, 10, 64)
if err != nil || catalogID == 0 {
response.BadRequest(c, "无效的商品ID")
return
}
var req updateRoutingReq
if err := c.ShouldBindJSON(&req); err != nil {
response.BadRequest(c, "参数错误:routing_mode 必填")
return
}
actorUserID := middleware.GetUserID(c)
routingCfg := service.ProductRoutingConfig{
RoutingMode: req.RoutingMode,
Priority: req.Priority,
DLCGoodID: req.DLCGoodID,
LewanProductID: req.LewanProductID,
LewanName: req.LewanName,
DeliveryType: req.DeliveryType,
}
if err := h.merchantSvc.UpdateProductRouting(uint(catalogID), routingCfg, actorUserID); err != nil {
response.BadRequest(c, err.Error())
return
}
response.OK(c, gin.H{"updated": true})
}
type syncLewanProductReq struct {
LewanProductID string `json:"lewan_product_id" binding:"required"`
Name string `json:"name" binding:"required"`
Category string `json:"category"`
SKU string `json:"sku"`
Price int64 `json:"price"`
DeliveryType string `json:"delivery_type"`
}
// SyncLewanProduct POST /api/platform/lewan/sync-catalog
// 从乐玩拉取到的商品同步/添加到平台自营商品目录
func (h *PlatformLewanHandler) SyncLewanProduct(c *gin.Context) {
var req syncLewanProductReq
if err := c.ShouldBindJSON(&req); err != nil {
response.BadRequest(c, "参数错误:lewan_product_id 与 name 必填")
return
}
actorUserID := middleware.GetUserID(c)
item, err := h.merchantSvc.SyncLewanProductToCatalog(service.SyncLewanProductInput{
LewanProductID: req.LewanProductID,
Name: req.Name,
Category: req.Category,
SKU: req.SKU,
Price: req.Price,
DeliveryType: req.DeliveryType,
}, actorUserID)
if err != nil {
response.BadRequest(c, err.Error())
return
}
response.WithStatus(c, http.StatusCreated, item)
}
type batchSyncLewanReq struct {
Products []syncLewanProductReq `json:"products" binding:"required,min=1"`
}
// BatchSyncLewanProducts POST /api/platform/lewan/batch-sync-catalog
// 批量同步/添加乐玩商品到自营商品目录
func (h *PlatformLewanHandler) BatchSyncLewanProducts(c *gin.Context) {
var req batchSyncLewanReq
if err := c.ShouldBindJSON(&req); err != nil {
response.BadRequest(c, "参数错误:products 列表不能为空")
return
}
actorUserID := middleware.GetUserID(c)
inputs := make([]service.SyncLewanProductInput, len(req.Products))
for i, p := range req.Products {
inputs[i] = service.SyncLewanProductInput{
LewanProductID: p.LewanProductID,
Name: p.Name,
Category: p.Category,
SKU: p.SKU,
Price: p.Price,
DeliveryType: p.DeliveryType,
}
}
count, err := h.merchantSvc.BatchSyncLewanProductsToCatalog(inputs, actorUserID)
if err != nil {
response.BadRequest(c, err.Error())
return
}
response.OK(c, gin.H{"synced_count": count})
}