Files
affiliate_dash/backend/internal/handler/merchant_platform.go
T
yml2213 663d7e1e9f fix(admin): 商户筛选下拉改用不分页的精简接口
自查新做的下游订单页时发现的问题:商户筛选下拉调用
/api/platform/merchants?size=200,而分页查询会把 >100 的 size 静默降级为 20
(service.normalizePage),因此商户超过 20 家时下拉里会少一批商户,界面不报任何错。

- 新增 service.ListMerchantOptions:不分页返回全部启用商户的精简信息
  (id/code/name),独立于分页的 ListMerchants
- 新增 GET /api/platform/merchant-options
- 前端改用该接口;types 增加 MerchantOption,api 增加 merchantOptions()

测试:
- TestListMerchantOptionsReturnsAllActiveMerchants:造 25 家启用 + 1 家禁用,
  断言返回数量与库中启用数一致、必须超过分页上限、且不含已禁用商户
- TestListMerchantsClampsOversizedPageSize:钉住「size=200 实际只返回 20 条,
  而 total 反映真实总数」这一事实,即上面接口存在的理由
2026-10-06 20:44:48 +08:00

183 lines
5.6 KiB
Go

package handler
import (
"strconv"
"affiliate_dash/internal/middleware"
"affiliate_dash/internal/pkg/response"
"affiliate_dash/internal/service"
"github.com/gin-gonic/gin"
)
func (h *MerchantHandler) ListPlatformMerchants(c *gin.Context) {
page, size := pageParams(c)
list, total, err := h.merchantSvc.ListMerchants(page, size)
if err != nil {
response.ServerError(c, err.Error())
return
}
response.Page(c, list, total, page, size)
}
// ListMerchantOptions GET /api/platform/merchant-options
//
// 返回全部启用商户的精简列表,供筛选下拉使用。分页接口的 size 上限是 100,
// 下拉需要「全部」,因此单开一个不分页的轻量接口。
func (h *MerchantHandler) ListMerchantOptions(c *gin.Context) {
options, err := h.merchantSvc.ListMerchantOptions()
if err != nil {
response.ServerError(c, err.Error())
return
}
response.OK(c, options)
}
type createMerchantReq struct {
Code string `json:"code" binding:"required"`
Name string `json:"name" binding:"required"`
ContactName string `json:"contact_name"`
ContactInfo string `json:"contact_info"`
OwnerUserID uint `json:"owner_user_id"`
OwnerUsername string `json:"owner_username"`
OwnerPassword string `json:"owner_password"`
OwnerNickname string `json:"owner_nickname"`
Features string `json:"features"`
FeeType string `json:"fee_type"`
FeeRateBP int64 `json:"fee_rate_bp"`
FeeFixedAmount int64 `json:"fee_fixed_amount"`
}
func (h *MerchantHandler) CreateMerchant(c *gin.Context) {
var req createMerchantReq
if err := c.ShouldBindJSON(&req); err != nil {
response.BadRequest(c, "参数错误:code、name 必填")
return
}
merchant, err := h.merchantSvc.CreateMerchant(service.CreateMerchantInput{
Code: req.Code,
Name: req.Name,
ContactName: req.ContactName,
ContactInfo: req.ContactInfo,
OwnerUserID: req.OwnerUserID,
OwnerUsername: req.OwnerUsername,
OwnerPassword: req.OwnerPassword,
OwnerNickname: req.OwnerNickname,
Features: req.Features,
FeeType: req.FeeType,
FeeRateBP: req.FeeRateBP,
FeeFixedAmount: req.FeeFixedAmount,
}, middleware.GetUserID(c))
if err != nil {
response.BadRequest(c, err.Error())
return
}
response.OK(c, merchant)
}
type updateMerchantSettingsReq struct {
Name *string `json:"name"`
Status *string `json:"status"`
ContactName *string `json:"contact_name"`
ContactInfo *string `json:"contact_info"`
Features *string `json:"features"`
FeeType *string `json:"fee_type"`
FeeRateBP *int64 `json:"fee_rate_bp"`
FeeFixedAmount *int64 `json:"fee_fixed_amount"`
}
func (h *MerchantHandler) UpdateMerchantSettings(c *gin.Context) {
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
var req updateMerchantSettingsReq
if err := c.ShouldBindJSON(&req); err != nil {
response.BadRequest(c, "参数错误")
return
}
if err := h.merchantSvc.UpdateMerchantSettings(uint(id), service.UpdateMerchantSettingsInput{
Name: req.Name,
Status: req.Status,
ContactName: req.ContactName,
ContactInfo: req.ContactInfo,
Features: req.Features,
FeeType: req.FeeType,
FeeRateBP: req.FeeRateBP,
FeeFixedAmount: req.FeeFixedAmount,
}, middleware.GetUserID(c)); err != nil {
response.BadRequest(c, err.Error())
return
}
response.OK(c, nil)
}
func (h *MerchantHandler) AddPlatformMerchantMember(c *gin.Context) {
var req addMemberReq
if err := c.ShouldBindJSON(&req); err != nil {
response.BadRequest(c, "参数错误:user_id 与 role 必填")
return
}
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
member, err := h.merchantSvc.AddMember(uint(id), service.AddMemberInput{
UserID: req.UserID,
Username: req.Username,
Password: req.Password,
Nickname: req.Nickname,
Role: req.Role,
IsDefault: req.IsDefault,
}, middleware.GetUserID(c))
if err != nil {
response.BadRequest(c, err.Error())
return
}
response.OK(c, member)
}
// AdminGetMerchantAccount 平台管理员查看指定商户的负责人登录账号。
func (h *MerchantHandler) AdminGetMerchantAccount(c *gin.Context) {
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
if id == 0 {
response.BadRequest(c, "商户 ID 无效")
return
}
accounts, err := h.merchantSvc.ListOwnerAccounts(uint(id))
if err != nil {
response.ServerError(c, err.Error())
return
}
response.OK(c, accounts)
}
type adminUpdateMerchantAccountReq struct {
UserID uint `json:"user_id" binding:"required"`
Username string `json:"username" binding:"required,min=3,max=64"`
Nickname string `json:"nickname" binding:"max=64"`
Password string `json:"password" binding:"omitempty,min=6,max=128"`
Status *int `json:"status"`
}
// AdminUpdateMerchantAccount 平台管理员维护指定商户的负责人登录账号:
// 修改用户名/昵称、重置密码、启用停用;密码留空表示不重置。
func (h *MerchantHandler) AdminUpdateMerchantAccount(c *gin.Context) {
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
var req adminUpdateMerchantAccountReq
if err := c.ShouldBindJSON(&req); err != nil {
response.BadRequest(c, "参数错误:用户名必填(3-64 位),密码至少 6 位")
return
}
if id == 0 {
response.BadRequest(c, "商户 ID 无效")
return
}
account, err := h.merchantSvc.UpdateMerchantOwnerAccount(uint(id), service.UpdateMerchantOwnerAccountInput{
UserID: req.UserID,
Username: req.Username,
Nickname: req.Nickname,
Password: req.Password,
Status: req.Status,
}, middleware.GetUserID(c))
if err != nil {
response.BadRequest(c, err.Error())
return
}
response.OK(c, account)
}