1. 需求确认

2. 结构的一些变更
This commit is contained in:
2026-01-06 11:07:04 +08:00
parent e907315afa
commit 2d566a9820
18 changed files with 2210 additions and 111 deletions

View File

@@ -1,15 +1,15 @@
package bootstrap
import (
"github.com/break/junhong_cmp_fiber/internal/handler"
"github.com/break/junhong_cmp_fiber/internal/handler/admin"
)
// initHandlers 初始化所有 Handler 实例
func initHandlers(svc *services) *Handlers {
return &Handlers{
Account: handler.NewAccountHandler(svc.Account),
Role: handler.NewRoleHandler(svc.Role),
Permission: handler.NewPermissionHandler(svc.Permission),
Account: admin.NewAccountHandler(svc.Account),
Role: admin.NewRoleHandler(svc.Role),
Permission: admin.NewPermissionHandler(svc.Permission),
// TODO: 新增 Handler 在此初始化
}
}

View File

@@ -1,14 +1,14 @@
package bootstrap
import (
"github.com/break/junhong_cmp_fiber/internal/handler"
"github.com/break/junhong_cmp_fiber/internal/handler/admin"
)
// Handlers 封装所有 HTTP 处理器
// 用于路由注册
type Handlers struct {
Account *handler.AccountHandler
Role *handler.RoleHandler
Permission *handler.PermissionHandler
Account *admin.AccountHandler
Role *admin.RoleHandler
Permission *admin.PermissionHandler
// TODO: 新增 Handler 在此添加字段
}

View File

@@ -1,4 +1,4 @@
package handler
package admin
import (
"strconv"

View File

@@ -1,4 +1,4 @@
package handler
package admin
import (
"strconv"

View File

@@ -1,4 +1,4 @@
package handler
package admin
import (
"strconv"

View File

@@ -1,4 +1,4 @@
package handler
package admin
import (
"fmt"

View File

@@ -2,48 +2,56 @@ package model
// CreateAccountRequest 创建账号请求
type CreateAccountRequest struct {
Username string `json:"username" validate:"required,min=3,max=50"`
Phone string `json:"phone" validate:"required,len=11"`
Password string `json:"password" validate:"required,min=8,max=32"`
UserType int `json:"user_type" validate:"required,min=1,max=4"`
ShopID *uint `json:"shop_id"`
ParentID *uint `json:"parent_id"`
Username string `json:"username" validate:"required,min=3,max=50" required:"true" minLength:"3" maxLength:"50" description:"用户名"`
Phone string `json:"phone" validate:"required,len=11" required:"true" minLength:"11" maxLength:"11" description:"手机号"`
Password string `json:"password" validate:"required,min=8,max=32" required:"true" minLength:"8" maxLength:"32" description:"密码"`
UserType int `json:"user_type" validate:"required,min=1,max=4" required:"true" minimum:"1" maximum:"4" description:"用户类型 (1:Root, 2:Admin, 3:Agent, 4:Merchant)"`
ShopID *uint `json:"shop_id" description:"关联店铺ID"`
ParentID *uint `json:"parent_id" description:"父账号ID"`
}
// UpdateAccountRequest 更新账号请求
type UpdateAccountRequest struct {
Username *string `json:"username" validate:"omitempty,min=3,max=50"`
Phone *string `json:"phone" validate:"omitempty,len=11"`
Password *string `json:"password" validate:"omitempty,min=8,max=32"`
Status *int `json:"status" validate:"omitempty,min=0,max=1"`
Username *string `json:"username" validate:"omitempty,min=3,max=50" minLength:"3" maxLength:"50" description:"用户名"`
Phone *string `json:"phone" validate:"omitempty,len=11" minLength:"11" maxLength:"11" description:"手机号"`
Password *string `json:"password" validate:"omitempty,min=8,max=32" minLength:"8" maxLength:"32" description:"密码"`
Status *int `json:"status" validate:"omitempty,min=0,max=1" minimum:"0" maximum:"1" description:"状态 (0:禁用, 1:启用)"`
}
// AccountListRequest 账号列表查询请求
type AccountListRequest struct {
Page int `json:"page" query:"page" validate:"omitempty,min=1"`
PageSize int `json:"page_size" query:"page_size" validate:"omitempty,min=1,max=100"`
Username string `json:"username" query:"username" validate:"omitempty,max=50"`
Phone string `json:"phone" query:"phone" validate:"omitempty,max=20"`
UserType *int `json:"user_type" query:"user_type" validate:"omitempty,min=1,max=4"`
Status *int `json:"status" query:"status" validate:"omitempty,min=0,max=1"`
Page int `json:"page" query:"page" validate:"omitempty,min=1" minimum:"1" description:"页码"`
PageSize int `json:"page_size" query:"page_size" validate:"omitempty,min=1,max=100" minimum:"1" maximum:"100" description:"每页数量"`
Username string `json:"username" query:"username" validate:"omitempty,max=50" maxLength:"50" description:"用户名模糊查询"`
Phone string `json:"phone" query:"phone" validate:"omitempty,max=20" maxLength:"20" description:"手机号模糊查询"`
UserType *int `json:"user_type" query:"user_type" validate:"omitempty,min=1,max=4" minimum:"1" maximum:"4" description:"用户类型"`
Status *int `json:"status" query:"status" validate:"omitempty,min=0,max=1" minimum:"0" maximum:"1" description:"状态"`
}
// AccountResponse 账号响应
type AccountResponse struct {
ID uint `json:"id"`
Username string `json:"username"`
Phone string `json:"phone"`
UserType int `json:"user_type"`
ShopID *uint `json:"shop_id,omitempty"`
ParentID *uint `json:"parent_id,omitempty"`
Status int `json:"status"`
Creator uint `json:"creator"`
Updater uint `json:"updater"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
ID uint `json:"id" description:"账号ID"`
Username string `json:"username" description:"用户名"`
Phone string `json:"phone" description:"手机号"`
UserType int `json:"user_type" description:"用户类型"`
ShopID *uint `json:"shop_id,omitempty" description:"关联店铺ID"`
ParentID *uint `json:"parent_id,omitempty" description:"父账号ID"`
Status int `json:"status" description:"状态"`
Creator uint `json:"creator" description:"创建人ID"`
Updater uint `json:"updater" description:"更新人ID"`
CreatedAt string `json:"created_at" description:"创建时间"`
UpdatedAt string `json:"updated_at" description:"更新时间"`
}
// AssignRolesRequest 分配角色请求
type AssignRolesRequest struct {
RoleIDs []uint `json:"role_ids" validate:"required,min=1"`
RoleIDs []uint `json:"role_ids" validate:"required,min=1" required:"true" minItems:"1" description:"角色ID列表"`
}
// AccountPageResult 账号分页响应
type AccountPageResult struct {
Items []AccountResponse `json:"items" description:"账号列表"`
Total int64 `json:"total" description:"总记录数"`
Page int `json:"page" description:"当前页码"`
Size int `json:"size" description:"每页数量"`
}

24
internal/model/common.go Normal file
View File

@@ -0,0 +1,24 @@
package model
// IDReq 通用 ID 路径参数请求
type IDReq struct {
ID uint `path:"id" description:"ID" required:"true"`
}
// UpdateAccountParams 更新账号聚合参数 (用于文档生成)
type UpdateAccountParams struct {
IDReq
UpdateAccountRequest
}
// AssignRolesParams 分配角色聚合参数 (用于文档生成)
type AssignRolesParams struct {
IDReq
AssignRolesRequest
}
// RemoveRoleParams 移除角色聚合参数
type RemoveRoleParams struct {
AccountID uint `path:"account_id" description:"账号ID" required:"true"`
RoleID uint `path:"role_id" description:"角色ID" required:"true"`
}

View File

@@ -2,58 +2,72 @@ package model
// CreatePermissionRequest 创建权限请求
type CreatePermissionRequest struct {
PermName string `json:"perm_name" validate:"required,min=1,max=50"`
PermCode string `json:"perm_code" validate:"required,min=1,max=100"`
PermType int `json:"perm_type" validate:"required,min=1,max=2"`
URL string `json:"url" validate:"omitempty,max=255"`
ParentID *uint `json:"parent_id"`
Sort int `json:"sort" validate:"omitempty,min=0"`
PermName string `json:"perm_name" validate:"required,min=1,max=50" required:"true" minLength:"1" maxLength:"50" description:"权限名称"`
PermCode string `json:"perm_code" validate:"required,min=1,max=100" required:"true" minLength:"1" maxLength:"100" description:"权限编码"`
PermType int `json:"perm_type" validate:"required,min=1,max=2" required:"true" minimum:"1" maximum:"2" description:"权限类型 (1:菜单, 2:按钮)"`
URL string `json:"url" validate:"omitempty,max=255" maxLength:"255" description:"请求路径"`
ParentID *uint `json:"parent_id" description:"父权限ID"`
Sort int `json:"sort" validate:"omitempty,min=0" minimum:"0" description:"排序值"`
}
// UpdatePermissionRequest 更新权限请求
type UpdatePermissionRequest struct {
PermName *string `json:"perm_name" validate:"omitempty,min=1,max=50"`
PermCode *string `json:"perm_code" validate:"omitempty,min=1,max=100"`
URL *string `json:"url" validate:"omitempty,max=255"`
ParentID *uint `json:"parent_id"`
Sort *int `json:"sort" validate:"omitempty,min=0"`
Status *int `json:"status" validate:"omitempty,min=0,max=1"`
PermName *string `json:"perm_name" validate:"omitempty,min=1,max=50" minLength:"1" maxLength:"50" description:"权限名称"`
PermCode *string `json:"perm_code" validate:"omitempty,min=1,max=100" minLength:"1" maxLength:"100" description:"权限编码"`
URL *string `json:"url" validate:"omitempty,max=255" maxLength:"255" description:"请求路径"`
ParentID *uint `json:"parent_id" description:"父权限ID"`
Sort *int `json:"sort" validate:"omitempty,min=0" minimum:"0" description:"排序值"`
Status *int `json:"status" validate:"omitempty,min=0,max=1" minimum:"0" maximum:"1" description:"状态 (0:禁用, 1:启用)"`
}
// UpdatePermissionParams 更新权限参数聚合
type UpdatePermissionParams struct {
IDReq
UpdatePermissionRequest
}
// PermissionListRequest 权限列表查询请求
type PermissionListRequest struct {
Page int `json:"page" query:"page" validate:"omitempty,min=1"`
PageSize int `json:"page_size" query:"page_size" validate:"omitempty,min=1,max=100"`
PermName string `json:"perm_name" query:"perm_name" validate:"omitempty,max=50"`
PermCode string `json:"perm_code" query:"perm_code" validate:"omitempty,max=100"`
PermType *int `json:"perm_type" query:"perm_type" validate:"omitempty,min=1,max=2"`
ParentID *uint `json:"parent_id" query:"parent_id"`
Status *int `json:"status" query:"status" validate:"omitempty,min=0,max=1"`
Page int `json:"page" query:"page" validate:"omitempty,min=1" minimum:"1" description:"页码"`
PageSize int `json:"page_size" query:"page_size" validate:"omitempty,min=1,max=100" minimum:"1" maximum:"100" description:"每页数量"`
PermName string `json:"perm_name" query:"perm_name" validate:"omitempty,max=50" maxLength:"50" description:"权限名称模糊查询"`
PermCode string `json:"perm_code" query:"perm_code" validate:"omitempty,max=100" maxLength:"100" description:"权限编码模糊查询"`
PermType *int `json:"perm_type" query:"perm_type" validate:"omitempty,min=1,max=2" minimum:"1" maximum:"2" description:"权限类型"`
ParentID *uint `json:"parent_id" query:"parent_id" description:"父权限ID"`
Status *int `json:"status" query:"status" validate:"omitempty,min=0,max=1" minimum:"0" maximum:"1" description:"状态"`
}
// PermissionResponse 权限响应
type PermissionResponse struct {
ID uint `json:"id"`
PermName string `json:"perm_name"`
PermCode string `json:"perm_code"`
PermType int `json:"perm_type"`
URL string `json:"url,omitempty"`
ParentID *uint `json:"parent_id,omitempty"`
Sort int `json:"sort"`
Status int `json:"status"`
Creator uint `json:"creator"`
Updater uint `json:"updater"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
ID uint `json:"id" description:"权限ID"`
PermName string `json:"perm_name" description:"权限名称"`
PermCode string `json:"perm_code" description:"权限编码"`
PermType int `json:"perm_type" description:"权限类型"`
URL string `json:"url,omitempty" description:"请求路径"`
ParentID *uint `json:"parent_id,omitempty" description:"父权限ID"`
Sort int `json:"sort" description:"排序值"`
Status int `json:"status" description:"状态"`
Creator uint `json:"creator" description:"创建人ID"`
Updater uint `json:"updater" description:"更新人ID"`
CreatedAt string `json:"created_at" description:"创建时间"`
UpdatedAt string `json:"updated_at" description:"更新时间"`
}
// PermissionPageResult 权限分页响应
type PermissionPageResult struct {
Items []PermissionResponse `json:"items" description:"权限列表"`
Total int64 `json:"total" description:"总记录数"`
Page int `json:"page" description:"当前页码"`
Size int `json:"size" description:"每页数量"`
}
// PermissionTreeNode 权限树节点(用于层级展示)
type PermissionTreeNode struct {
ID uint `json:"id"`
PermName string `json:"perm_name"`
PermCode string `json:"perm_code"`
PermType int `json:"perm_type"`
URL string `json:"url,omitempty"`
Sort int `json:"sort"`
Children []*PermissionTreeNode `json:"children,omitempty"`
}
ID uint `json:"id" description:"权限ID"`
PermName string `json:"perm_name" description:"权限名称"`
PermCode string `json:"perm_code" description:"权限编码"`
PermType int `json:"perm_type" description:"权限类型"`
URL string `json:"url,omitempty" description:"请求路径"`
Sort int `json:"sort" description:"排序值"`
Children []*PermissionTreeNode `json:"children,omitempty" description:"子权限列表"`
}

View File

@@ -2,41 +2,67 @@ package model
// CreateRoleRequest 创建角色请求
type CreateRoleRequest struct {
RoleName string `json:"role_name" validate:"required,min=1,max=50"`
RoleDesc string `json:"role_desc" validate:"omitempty,max=255"`
RoleType int `json:"role_type" validate:"required,min=1,max=3"`
RoleName string `json:"role_name" validate:"required,min=1,max=50" required:"true" minLength:"1" maxLength:"50" description:"角色名称"`
RoleDesc string `json:"role_desc" validate:"omitempty,max=255" maxLength:"255" description:"角色描述"`
RoleType int `json:"role_type" validate:"required,min=1,max=3" required:"true" minimum:"1" maximum:"3" description:"角色类型 (1:超级管理员, 2:普通管理员, 3:操作员)"`
}
// UpdateRoleRequest 更新角色请求
type UpdateRoleRequest struct {
RoleName *string `json:"role_name" validate:"omitempty,min=1,max=50"`
RoleDesc *string `json:"role_desc" validate:"omitempty,max=255"`
Status *int `json:"status" validate:"omitempty,min=0,max=1"`
RoleName *string `json:"role_name" validate:"omitempty,min=1,max=50" minLength:"1" maxLength:"50" description:"角色名称"`
RoleDesc *string `json:"role_desc" validate:"omitempty,max=255" maxLength:"255" description:"角色描述"`
Status *int `json:"status" validate:"omitempty,min=0,max=1" minimum:"0" maximum:"1" description:"状态 (0:禁用, 1:启用)"`
}
// UpdateRoleParams 更新角色参数聚合
type UpdateRoleParams struct {
IDReq
UpdateRoleRequest
}
// RoleListRequest 角色列表查询请求
type RoleListRequest struct {
Page int `json:"page" query:"page" validate:"omitempty,min=1"`
PageSize int `json:"page_size" query:"page_size" validate:"omitempty,min=1,max=100"`
RoleName string `json:"role_name" query:"role_name" validate:"omitempty,max=50"`
RoleType *int `json:"role_type" query:"role_type" validate:"omitempty,min=1,max=3"`
Status *int `json:"status" query:"status" validate:"omitempty,min=0,max=1"`
Page int `json:"page" query:"page" validate:"omitempty,min=1" minimum:"1" description:"页码"`
PageSize int `json:"page_size" query:"page_size" validate:"omitempty,min=1,max=100" minimum:"1" maximum:"100" description:"每页数量"`
RoleName string `json:"role_name" query:"role_name" validate:"omitempty,max=50" maxLength:"50" description:"角色名称模糊查询"`
RoleType *int `json:"role_type" query:"role_type" validate:"omitempty,min=1,max=3" minimum:"1" maximum:"3" description:"角色类型"`
Status *int `json:"status" query:"status" validate:"omitempty,min=0,max=1" minimum:"0" maximum:"1" description:"状态"`
}
// RoleResponse 角色响应
type RoleResponse struct {
ID uint `json:"id"`
RoleName string `json:"role_name"`
RoleDesc string `json:"role_desc"`
RoleType int `json:"role_type"`
Status int `json:"status"`
Creator uint `json:"creator"`
Updater uint `json:"updater"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
ID uint `json:"id" description:"角色ID"`
RoleName string `json:"role_name" description:"角色名称"`
RoleDesc string `json:"role_desc" description:"角色描述"`
RoleType int `json:"role_type" description:"角色类型"`
Status int `json:"status" description:"状态"`
Creator uint `json:"creator" description:"创建人ID"`
Updater uint `json:"updater" description:"更新人ID"`
CreatedAt string `json:"created_at" description:"创建时间"`
UpdatedAt string `json:"updated_at" description:"更新时间"`
}
// RolePageResult 角色分页响应
type RolePageResult struct {
Items []RoleResponse `json:"items" description:"角色列表"`
Total int64 `json:"total" description:"总记录数"`
Page int `json:"page" description:"当前页码"`
Size int `json:"size" description:"每页数量"`
}
// AssignPermissionsRequest 分配权限请求
type AssignPermissionsRequest struct {
PermIDs []uint `json:"perm_ids" validate:"required,min=1"`
PermIDs []uint `json:"perm_ids" validate:"required,min=1" required:"true" minItems:"1" description:"权限ID列表"`
}
// AssignPermissionsParams 分配权限参数聚合
type AssignPermissionsParams struct {
IDReq
AssignPermissionsRequest
}
// RemovePermissionParams 移除权限参数聚合
type RemovePermissionParams struct {
RoleID uint `path:"role_id" required:"true" description:"角色ID"`
PermID uint `path:"perm_id" required:"true" description:"权限ID"`
}

View File

@@ -3,22 +3,71 @@ package routes
import (
"github.com/gofiber/fiber/v2"
"github.com/break/junhong_cmp_fiber/internal/handler"
"github.com/break/junhong_cmp_fiber/internal/handler/admin"
"github.com/break/junhong_cmp_fiber/internal/model"
"github.com/break/junhong_cmp_fiber/pkg/openapi"
)
// registerAccountRoutes 注册账号相关路由
func registerAccountRoutes(api fiber.Router, h *handler.AccountHandler) {
func registerAccountRoutes(api fiber.Router, h *admin.AccountHandler, doc *openapi.Generator, basePath string) {
accounts := api.Group("/accounts")
groupPath := basePath + "/accounts"
// 账号 CRUD
accounts.Post("", h.Create) // POST /api/v1/accounts
accounts.Get("", h.List) // GET /api/v1/accounts
accounts.Get("/:id", h.Get) // GET /api/v1/accounts/:id
accounts.Put("/:id", h.Update) // PUT /api/v1/accounts/:id
accounts.Delete("/:id", h.Delete) // DELETE /api/v1/accounts/:id
Register(accounts, doc, groupPath, "POST", "", h.Create, RouteSpec{
Summary: "创建账号",
Tags: []string{"Account"},
Input: new(model.CreateAccountRequest),
Output: new(model.AccountResponse),
})
Register(accounts, doc, groupPath, "GET", "", h.List, RouteSpec{
Summary: "账号列表",
Tags: []string{"Account"},
Input: new(model.AccountListRequest),
Output: new(model.AccountPageResult),
})
Register(accounts, doc, groupPath, "GET", "/:id", h.Get, RouteSpec{
Summary: "获取账号详情",
Tags: []string{"Account"},
Input: new(model.IDReq),
Output: new(model.AccountResponse),
})
Register(accounts, doc, groupPath, "PUT", "/:id", h.Update, RouteSpec{
Summary: "更新账号",
Tags: []string{"Account"},
Input: new(model.UpdateAccountParams),
Output: new(model.AccountResponse),
})
Register(accounts, doc, groupPath, "DELETE", "/:id", h.Delete, RouteSpec{
Summary: "删除账号",
Tags: []string{"Account"},
Input: new(model.IDReq),
Output: nil,
})
// 账号-角色关联
accounts.Post("/:id/roles", h.AssignRoles) // POST /api/v1/accounts/:id/roles
accounts.Get("/:id/roles", h.GetRoles) // GET /api/v1/accounts/:id/roles
accounts.Delete("/:account_id/roles/:role_id", h.RemoveRole) // DELETE /api/v1/accounts/:account_id/roles/:role_id
Register(accounts, doc, groupPath, "POST", "/:id/roles", h.AssignRoles, RouteSpec{
Summary: "分配角色",
Tags: []string{"Account"},
Input: new(model.AssignRolesParams),
Output: nil, // TODO: Define AccountRole response DTO
})
Register(accounts, doc, groupPath, "GET", "/:id/roles", h.GetRoles, RouteSpec{
Summary: "获取账号角色",
Tags: []string{"Account"},
Input: new(model.IDReq),
Output: new([]model.Role),
})
Register(accounts, doc, groupPath, "DELETE", "/:account_id/roles/:role_id", h.RemoveRole, RouteSpec{
Summary: "移除角色",
Tags: []string{"Account"},
Input: new(model.RemoveRoleParams),
Output: nil,
})
}