All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 6m17s
- 合并 customer_account 和 shop_account 路由到统一的 account 接口 - 新增统一认证接口 (auth handler) - 实现越权防护中间件和权限检查工具函数 - 新增操作审计日志模型和服务 - 更新数据库迁移 (版本 39: account_operation_log 表) - 补充集成测试覆盖权限检查和审计日志场景
224 lines
6.0 KiB
Go
224 lines
6.0 KiB
Go
// Package admin 提供管理后台的 HTTP 处理器
|
|
// 包含账号管理、角色管理、权限管理、任务管理等功能的 Handler 实现
|
|
package admin
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
|
"github.com/break/junhong_cmp_fiber/pkg/response"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"github.com/break/junhong_cmp_fiber/internal/model/dto"
|
|
accountService "github.com/break/junhong_cmp_fiber/internal/service/account"
|
|
)
|
|
|
|
// AccountHandler 账号 Handler
|
|
type AccountHandler struct {
|
|
service *accountService.Service
|
|
}
|
|
|
|
// NewAccountHandler 创建账号 Handler
|
|
func NewAccountHandler(service *accountService.Service) *AccountHandler {
|
|
return &AccountHandler{service: service}
|
|
}
|
|
|
|
// Create 创建账号
|
|
// POST /api/admin/accounts
|
|
func (h *AccountHandler) Create(c *fiber.Ctx) error {
|
|
var req dto.CreateAccountRequest
|
|
if err := c.BodyParser(&req); err != nil {
|
|
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
|
}
|
|
|
|
account, err := h.service.Create(c.UserContext(), &req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return response.Success(c, account)
|
|
}
|
|
|
|
// Get 获取账号详情
|
|
// GET /api/admin/accounts/:id
|
|
func (h *AccountHandler) Get(c *fiber.Ctx) error {
|
|
id, err := strconv.ParseUint(c.Params("id"), 10, 64)
|
|
if err != nil {
|
|
return errors.New(errors.CodeInvalidParam, "无效的账号 ID")
|
|
}
|
|
|
|
account, err := h.service.Get(c.UserContext(), uint(id))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return response.Success(c, account)
|
|
}
|
|
|
|
// Update 更新账号
|
|
// PUT /api/admin/accounts/:id
|
|
func (h *AccountHandler) Update(c *fiber.Ctx) error {
|
|
id, err := strconv.ParseUint(c.Params("id"), 10, 64)
|
|
if err != nil {
|
|
return errors.New(errors.CodeInvalidParam, "无效的账号 ID")
|
|
}
|
|
|
|
var req dto.UpdateAccountRequest
|
|
if err := c.BodyParser(&req); err != nil {
|
|
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
|
}
|
|
|
|
account, err := h.service.Update(c.UserContext(), uint(id), &req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return response.Success(c, account)
|
|
}
|
|
|
|
// Delete 删除账号
|
|
// DELETE /api/admin/accounts/:id
|
|
func (h *AccountHandler) Delete(c *fiber.Ctx) error {
|
|
id, err := strconv.ParseUint(c.Params("id"), 10, 64)
|
|
if err != nil {
|
|
return errors.New(errors.CodeInvalidParam, "无效的账号 ID")
|
|
}
|
|
|
|
if err := h.service.Delete(c.UserContext(), uint(id)); err != nil {
|
|
return err
|
|
}
|
|
|
|
return response.Success(c, nil)
|
|
}
|
|
|
|
// List 查询账号列表
|
|
// GET /api/admin/accounts
|
|
func (h *AccountHandler) List(c *fiber.Ctx) error {
|
|
var req dto.AccountListRequest
|
|
if err := c.QueryParser(&req); err != nil {
|
|
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
|
}
|
|
|
|
accounts, total, err := h.service.List(c.UserContext(), &req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return response.SuccessWithPagination(c, accounts, total, req.Page, req.PageSize)
|
|
}
|
|
|
|
// AssignRoles 为账号分配角色
|
|
// POST /api/admin/accounts/:id/roles
|
|
func (h *AccountHandler) AssignRoles(c *fiber.Ctx) error {
|
|
id, err := strconv.ParseUint(c.Params("id"), 10, 64)
|
|
if err != nil {
|
|
return errors.New(errors.CodeInvalidParam, "无效的账号 ID")
|
|
}
|
|
|
|
var req dto.AssignRolesRequest
|
|
if err := c.BodyParser(&req); err != nil {
|
|
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
|
}
|
|
|
|
ars, err := h.service.AssignRoles(c.UserContext(), uint(id), req.RoleIDs)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return response.Success(c, ars)
|
|
}
|
|
|
|
// GetRoles 获取账号的所有角色
|
|
// GET /api/admin/accounts/:id/roles
|
|
func (h *AccountHandler) GetRoles(c *fiber.Ctx) error {
|
|
id, err := strconv.ParseUint(c.Params("id"), 10, 64)
|
|
if err != nil {
|
|
return errors.New(errors.CodeInvalidParam, "无效的账号 ID")
|
|
}
|
|
|
|
roles, err := h.service.GetRoles(c.UserContext(), uint(id))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return response.Success(c, roles)
|
|
}
|
|
|
|
// RemoveRole 移除账号的角色
|
|
// DELETE /api/admin/accounts/:account_id/roles/:role_id
|
|
func (h *AccountHandler) RemoveRole(c *fiber.Ctx) error {
|
|
id, err := strconv.ParseUint(c.Params("id"), 10, 64)
|
|
if err != nil {
|
|
return errors.New(errors.CodeInvalidParam, "无效的账号 ID")
|
|
}
|
|
|
|
roleID, err := strconv.ParseUint(c.Params("role_id"), 10, 64)
|
|
if err != nil {
|
|
return errors.New(errors.CodeInvalidParam, "无效的角色 ID")
|
|
}
|
|
|
|
if err := h.service.RemoveRole(c.UserContext(), uint(id), uint(roleID)); err != nil {
|
|
return err
|
|
}
|
|
|
|
return response.Success(c, nil)
|
|
}
|
|
|
|
// UpdatePassword 修改账号密码
|
|
// PUT /api/admin/accounts/:id/password
|
|
func (h *AccountHandler) UpdatePassword(c *fiber.Ctx) error {
|
|
id, err := strconv.ParseUint(c.Params("id"), 10, 64)
|
|
if err != nil {
|
|
return errors.New(errors.CodeInvalidParam, "无效的账号 ID")
|
|
}
|
|
|
|
var req dto.UpdatePasswordRequest
|
|
if err := c.BodyParser(&req); err != nil {
|
|
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
|
}
|
|
|
|
if err := h.service.UpdatePassword(c.UserContext(), uint(id), req.NewPassword); err != nil {
|
|
return err
|
|
}
|
|
|
|
return response.Success(c, nil)
|
|
}
|
|
|
|
// UpdateStatus 修改账号状态
|
|
// PUT /api/admin/accounts/:id/status
|
|
func (h *AccountHandler) UpdateStatus(c *fiber.Ctx) error {
|
|
id, err := strconv.ParseUint(c.Params("id"), 10, 64)
|
|
if err != nil {
|
|
return errors.New(errors.CodeInvalidParam, "无效的账号 ID")
|
|
}
|
|
|
|
var req dto.UpdateStatusRequest
|
|
if err := c.BodyParser(&req); err != nil {
|
|
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
|
}
|
|
|
|
if err := h.service.UpdateStatus(c.UserContext(), uint(id), req.Status); err != nil {
|
|
return err
|
|
}
|
|
|
|
return response.Success(c, nil)
|
|
}
|
|
|
|
// ListPlatformAccounts 查询平台账号列表(兼容旧路由)
|
|
// 自动筛选 user_type IN (1, 2) 的账号
|
|
// GET /api/admin/accounts - 查询平台账号列表
|
|
func (h *AccountHandler) ListPlatformAccounts(c *fiber.Ctx) error {
|
|
var req dto.PlatformAccountListRequest
|
|
if err := c.QueryParser(&req); err != nil {
|
|
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
|
|
}
|
|
|
|
accounts, total, err := h.service.ListPlatformAccounts(c.UserContext(), &req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return response.SuccessWithPagination(c, accounts, total, req.Page, req.PageSize)
|
|
}
|