feat(admin): 平台商户成员管理改用抽屉,消除商户视角切换与全局状态污染
- 交互重构:在「平台商户」与「平台账号」点击成员管理时,不再跳转路由或改写 localStorage.merchant_id,改为滑出 MerchantMembersDrawer 抽屉 - 抽屉能力:支持查看指定商户成员列表、新增成员(新建账号或关联已有账号)、修改角色与启用状态、移除成员 - 顶栏清理:彻底移除顶栏右上角的「当前商户:xxx」标签与「退出商户视角」按钮,超管登录自动清理任何残留上下文 - 后端补齐:新增平台专属 /api/platform/merchants/:id/members 与 roles 接口,平台操作不再依赖 X-Merchant-ID - 验证:tsc -b && vite build 成功;全量 go test ./... 包含路由与鉴权回归测试 100% 通过
This commit is contained in:
@@ -180,3 +180,71 @@ func (h *MerchantHandler) AdminUpdateMerchantAccount(c *gin.Context) {
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -243,7 +243,11 @@ func Setup(h *Handlers) *gin.Engine {
|
||||
platform.GET("/platform/orders", h.Merchant.ListPlatformOrders)
|
||||
platform.POST("/platform/merchants", h.Merchant.CreateMerchant)
|
||||
platform.PATCH("/platform/merchants/:id", h.Merchant.UpdateMerchantSettings)
|
||||
platform.GET("/platform/merchants/:id/members", h.Merchant.ListPlatformMerchantMembers)
|
||||
platform.POST("/platform/merchants/:id/members", h.Merchant.AddPlatformMerchantMember)
|
||||
platform.PATCH("/platform/merchants/:id/members/:mid", h.Merchant.UpdatePlatformMerchantMember)
|
||||
platform.DELETE("/platform/merchants/:id/members/:mid", h.Merchant.DeletePlatformMerchantMember)
|
||||
platform.GET("/platform/merchants/:id/roles", h.Merchant.ListPlatformMerchantRoles)
|
||||
platform.GET("/platform/product-catalog", h.Merchant.ListProductCatalog)
|
||||
platform.GET("/platform/merchants/:id/products", h.Merchant.ListMerchantProductsByAdmin)
|
||||
platform.PATCH("/platform/merchants/:id/products/:pid", h.Merchant.AdminUpdateMerchantProduct)
|
||||
|
||||
@@ -213,3 +213,30 @@ func TestRequestLoggerSuppressesPollingOnInfo(t *testing.T) {
|
||||
t.Errorf("expected GET order to appear as [DEBUG] in DEBUG mode, got: %s", buf.String())
|
||||
}
|
||||
}
|
||||
|
||||
// TestPlatformMerchantMembersRoutes 验证平台管理员访问商户成员与角色接口完全独立于 X-Merchant-ID。
|
||||
func TestPlatformMerchantMembersRoutes(t *testing.T) {
|
||||
engine, jm := newTestRouter(t)
|
||||
token, err := jm.Generate(1, "admin", model.RoleAdmin)
|
||||
if err != nil {
|
||||
t.Fatalf("generate token: %v", err)
|
||||
}
|
||||
|
||||
// 1. 获取商户 1 的成员列表
|
||||
req := httptest.NewRequest("GET", "/api/platform/merchants/1/members", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
w := httptest.NewRecorder()
|
||||
engine.ServeHTTP(w, req)
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("平台管理员查询商户成员应返回 200,实际 %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
|
||||
// 2. 获取商户 1 的角色列表
|
||||
req = httptest.NewRequest("GET", "/api/platform/merchants/1/roles", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
w = httptest.NewRecorder()
|
||||
engine.ServeHTTP(w, req)
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("平台管理员查询商户角色应返回 200,实际 %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user