- 交互重构:在「平台商户」与「平台账号」点击成员管理时,不再跳转路由或改写 localStorage.merchant_id,改为滑出 MerchantMembersDrawer 抽屉 - 抽屉能力:支持查看指定商户成员列表、新增成员(新建账号或关联已有账号)、修改角色与启用状态、移除成员 - 顶栏清理:彻底移除顶栏右上角的「当前商户:xxx」标签与「退出商户视角」按钮,超管登录自动清理任何残留上下文 - 后端补齐:新增平台专属 /api/platform/merchants/:id/members 与 roles 接口,平台操作不再依赖 X-Merchant-ID - 验证:tsc -b && vite build 成功;全量 go test ./... 包含路由与鉴权回归测试 100% 通过
251 lines
7.6 KiB
Go
251 lines
7.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)
|
|
}
|
|
|
|
// ListPlatformMerchantMembers GET /api/platform/merchants/:id/members
|
|
func (h *MerchantHandler) ListPlatformMerchantMembers(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if id == 0 {
|
|
response.BadRequest(c, "商户 ID 无效")
|
|
return
|
|
}
|
|
members, err := h.merchantSvc.ListMembers(uint(id))
|
|
if err != nil {
|
|
response.ServerError(c, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, members)
|
|
}
|
|
|
|
// UpdatePlatformMerchantMember PATCH /api/platform/merchants/:id/members/:mid
|
|
func (h *MerchantHandler) UpdatePlatformMerchantMember(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
mid, _ := strconv.ParseUint(c.Param("mid"), 10, 64)
|
|
if id == 0 || mid == 0 {
|
|
response.BadRequest(c, "参数无效")
|
|
return
|
|
}
|
|
var req updateMemberReq
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.BadRequest(c, "参数错误:角色必填")
|
|
return
|
|
}
|
|
member, err := h.merchantSvc.UpdateMember(uint(id), uint(mid), service.UpdateMemberInput{
|
|
Role: req.Role, Status: req.Status, IsDefault: req.IsDefault,
|
|
}, middleware.GetUserID(c))
|
|
if err != nil {
|
|
response.BadRequest(c, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, member)
|
|
}
|
|
|
|
// DeletePlatformMerchantMember DELETE /api/platform/merchants/:id/members/:mid
|
|
func (h *MerchantHandler) DeletePlatformMerchantMember(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
mid, _ := strconv.ParseUint(c.Param("mid"), 10, 64)
|
|
if id == 0 || mid == 0 {
|
|
response.BadRequest(c, "参数无效")
|
|
return
|
|
}
|
|
if err := h.merchantSvc.RemoveMember(uint(id), uint(mid), middleware.GetUserID(c)); err != nil {
|
|
response.BadRequest(c, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, nil)
|
|
}
|
|
|
|
// ListPlatformMerchantRoles GET /api/platform/merchants/:id/roles
|
|
func (h *MerchantHandler) ListPlatformMerchantRoles(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if id == 0 {
|
|
response.BadRequest(c, "商户 ID 无效")
|
|
return
|
|
}
|
|
roles, err := h.merchantSvc.ListRoles(uint(id))
|
|
if err != nil {
|
|
response.ServerError(c, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, roles)
|
|
}
|