Files
yml2213 ae71bc99b8 refactor(handler): 按业务域拆分 merchant.go(847 → 最大 182 行)
纯文件级移动,无行为变更:
- merchant.go            结构体、构造函数与跨域共用辅助(mapKeys/pageParams)
- merchant_product.go    商品目录与分配
- merchant_order.go      订单、人工/测试下单与发货链接
- merchant_wallet.go     钱包、积分流水与管理员调账
- merchant_api_client.go API 客户端与回调订阅
- merchant_member.go     成员与角色
- merchant_platform.go   平台侧租户管理

校验:与 HEAD 的声明行 diff 为空,MerchantHandler 方法数 41 → 41。
2026-10-06 19:43:13 +08:00

122 lines
3.4 KiB
Go

package handler
import (
"strconv"
"time"
"affiliate_dash/internal/middleware"
"affiliate_dash/internal/pkg/response"
"affiliate_dash/internal/service"
"github.com/gin-gonic/gin"
)
func (h *MerchantHandler) ListAPIClients(c *gin.Context) {
clients, err := h.merchantSvc.ListAPIClients(middleware.GetMerchantID(c))
if err != nil {
response.ServerError(c, err.Error())
return
}
response.OK(c, clients)
}
type apiClientReq struct {
Name string `json:"name" binding:"required"`
Scopes string `json:"scopes" binding:"required"`
SignatureVersion string `json:"signature_version"`
ExpiresAt string `json:"expires_at"`
}
func (h *MerchantHandler) CreateAPIClient(c *gin.Context) {
var req apiClientReq
if err := c.ShouldBindJSON(&req); err != nil {
response.BadRequest(c, "参数错误:name 与 scopes 必填")
return
}
var expiresAt *time.Time
if req.ExpiresAt != "" {
value, err := time.Parse(time.RFC3339, req.ExpiresAt)
if err != nil {
response.BadRequest(c, "expires_at 必须是 RFC3339 时间")
return
}
expiresAt = &value
}
credential, err := h.merchantSvc.CreateAPIClient(middleware.GetMerchantID(c), service.CreateAPIClientInput{
Name: req.Name,
Scopes: req.Scopes,
SignatureVersion: req.SignatureVersion,
ExpiresAt: expiresAt,
}, middleware.GetUserID(c))
if err != nil {
response.BadRequest(c, err.Error())
return
}
response.OK(c, credential)
}
type statusReq struct {
Status string `json:"status" binding:"required"`
}
func (h *MerchantHandler) UpdateAPIClientStatus(c *gin.Context) {
var req statusReq
if err := c.ShouldBindJSON(&req); err != nil {
response.BadRequest(c, "参数错误")
return
}
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
if err := h.merchantSvc.UpdateAPIClientStatus(middleware.GetMerchantID(c), uint(id), req.Status, middleware.GetUserID(c)); err != nil {
response.BadRequest(c, err.Error())
return
}
response.OK(c, nil)
}
// DeleteAPIClient DELETE /api/merchant/api-clients/:id
func (h *MerchantHandler) DeleteAPIClient(c *gin.Context) {
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
if err := h.merchantSvc.DeleteAPIClient(middleware.GetMerchantID(c), uint(id), middleware.GetUserID(c)); err != nil {
response.BadRequest(c, err.Error())
return
}
response.OK(c, nil)
}
func (h *MerchantHandler) ListCallbacks(c *gin.Context) {
subscription, err := h.callbackSvc.GetSubscription(middleware.GetMerchantID(c))
if err != nil {
response.ServerError(c, err.Error())
return
}
response.OK(c, subscription)
}
type callbackReq struct {
Name string `json:"name"`
URL string `json:"url" binding:"required"`
Events string `json:"events" binding:"required"`
Status string `json:"status"`
RotateSecret bool `json:"rotate_secret"`
}
func (h *MerchantHandler) CreateCallback(c *gin.Context) {
var req callbackReq
if err := c.ShouldBindJSON(&req); err != nil {
response.BadRequest(c, "参数错误:url、events 必填")
return
}
credential, err := h.callbackSvc.CreateSubscription(middleware.GetMerchantID(c), service.CreateCallbackInput{
Name: req.Name,
URL: req.URL,
Events: req.Events,
Status: req.Status,
RotateSecret: req.RotateSecret,
}, middleware.GetUserID(c))
if err != nil {
response.BadRequest(c, err.Error())
return
}
response.OK(c, credential)
}