feat: rbac完善,file接入完成

This commit is contained in:
Blizzard
2026-05-01 12:56:08 +08:00
parent bbd3f834b9
commit a93477ea8e
81 changed files with 5470 additions and 371 deletions
@@ -0,0 +1,29 @@
package role
import (
"net/http"
"sundynix-micro-go/app/system/api/internal/logic/role"
"sundynix-micro-go/app/system/api/internal/svc"
"sundynix-micro-go/app/system/api/internal/types"
"sundynix-micro-go/common/response"
"github.com/zeromicro/go-zero/rest/httpx"
)
func AssignRoleMenusHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.AssignRoleMenusReq
if err := httpx.Parse(r, &req); err != nil {
response.Fail(w, err.Error())
return
}
l := role.NewAssignRoleMenusLogic(r.Context(), svcCtx)
err := l.AssignRoleMenus(&req)
if err != nil {
response.Fail(w, err.Error())
} else {
response.Ok(w)
}
}
}
@@ -0,0 +1,33 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.10.1
package role
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"sundynix-micro-go/app/system/api/internal/logic/role"
"sundynix-micro-go/app/system/api/internal/svc"
"sundynix-micro-go/app/system/api/internal/types"
"sundynix-micro-go/common/response"
)
// 角色详情(含已授权菜单)
func GetRoleDetailHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.IdQueryReq
if err := httpx.Parse(r, &req); err != nil {
response.Fail(w, err.Error())
return
}
l := role.NewGetRoleDetailLogic(r.Context(), svcCtx)
resp, err := l.GetRoleDetail(&req)
if err != nil {
response.Fail(w, err.Error())
} else {
response.OkWithData(w, resp)
}
}
}
+24
View File
@@ -138,6 +138,12 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
server.AddRoutes(
[]rest.Route{
{
// 角色授权菜单
Method: http.MethodPost,
Path: "/role/assignMenus",
Handler: role.AssignRoleMenusHandler(serverCtx),
},
{
// 创建角色
Method: http.MethodPost,
@@ -150,6 +156,12 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Path: "/role/delete",
Handler: role.DeleteRoleHandler(serverCtx),
},
{
// 角色详情(含已授权菜单)
Method: http.MethodGet,
Path: "/role/detail",
Handler: role.GetRoleDetailHandler(serverCtx),
},
{
// 角色列表
Method: http.MethodPost,
@@ -169,6 +181,12 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
server.AddRoutes(
[]rest.Route{
{
// 用户授权角色
Method: http.MethodPost,
Path: "/user/assignRoles",
Handler: user.AssignUserRolesHandler(serverCtx),
},
{
// 创建用户
Method: http.MethodPost,
@@ -181,6 +199,12 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Path: "/user/delete",
Handler: user.DeleteUserHandler(serverCtx),
},
{
// 用户详情(含角色信息)
Method: http.MethodGet,
Path: "/user/detail",
Handler: user.GetUserDetailHandler(serverCtx),
},
{
// 用户列表
Method: http.MethodPost,
@@ -0,0 +1,29 @@
package user
import (
"net/http"
"sundynix-micro-go/app/system/api/internal/logic/user"
"sundynix-micro-go/app/system/api/internal/svc"
"sundynix-micro-go/app/system/api/internal/types"
"sundynix-micro-go/common/response"
"github.com/zeromicro/go-zero/rest/httpx"
)
func AssignUserRolesHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.AssignUserRolesReq
if err := httpx.Parse(r, &req); err != nil {
response.Fail(w, err.Error())
return
}
l := user.NewAssignUserRolesLogic(r.Context(), svcCtx)
err := l.AssignUserRoles(&req)
if err != nil {
response.Fail(w, err.Error())
} else {
response.Ok(w)
}
}
}
@@ -0,0 +1,33 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.10.1
package user
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"sundynix-micro-go/app/system/api/internal/logic/user"
"sundynix-micro-go/app/system/api/internal/svc"
"sundynix-micro-go/app/system/api/internal/types"
"sundynix-micro-go/common/response"
)
// 用户详情(含角色信息)
func GetUserDetailHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.IdQueryReq
if err := httpx.Parse(r, &req); err != nil {
response.Fail(w, err.Error())
return
}
l := user.NewGetUserDetailLogic(r.Context(), svcCtx)
resp, err := l.GetUserDetail(&req)
if err != nil {
response.Fail(w, err.Error())
} else {
response.OkWithData(w, resp)
}
}
}
@@ -0,0 +1,29 @@
package role
import (
"context"
"sundynix-micro-go/app/system/api/internal/svc"
"sundynix-micro-go/app/system/api/internal/types"
"sundynix-micro-go/app/system/rpc/system"
"github.com/zeromicro/go-zero/core/logx"
)
type AssignRoleMenusLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewAssignRoleMenusLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AssignRoleMenusLogic {
return &AssignRoleMenusLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
}
func (l *AssignRoleMenusLogic) AssignRoleMenus(req *types.AssignRoleMenusReq) error {
_, err := l.svcCtx.SystemRpc.AssignRoleMenus(l.ctx, &system.AssignRoleMenusReq{
RoleId: req.RoleId,
MenuIds: req.MenuIds,
})
return err
}
@@ -0,0 +1,46 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.10.1
package role
import (
"context"
"sundynix-micro-go/app/system/api/internal/svc"
"sundynix-micro-go/app/system/api/internal/types"
"sundynix-micro-go/app/system/rpc/system"
"github.com/zeromicro/go-zero/core/logx"
)
type GetRoleDetailLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 角色详情(含已授权菜单)
func NewGetRoleDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetRoleDetailLogic {
return &GetRoleDetailLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *GetRoleDetailLogic) GetRoleDetail(req *types.IdQueryReq) (resp *types.RoleDetailResp, err error) {
rpcResp, err := l.svcCtx.SystemRpc.GetRoleDetail(l.ctx, &system.GetRoleDetailReq{
Id: req.Id,
})
if err != nil {
return nil, err
}
return &types.RoleDetailResp{
Id: rpcResp.Id,
Name: rpcResp.Name,
Code: rpcResp.Code,
Sort: int(rpcResp.Sort),
MenuIds: rpcResp.MenuIds,
}, nil
}
@@ -20,7 +20,7 @@ func NewUpdateRoleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Update
func (l *UpdateRoleLogic) UpdateRole(req *types.RoleUpdateReq) error {
_, err := l.svcCtx.SystemRpc.UpdateRole(l.ctx, &system.RoleUpdateReq{
Id: req.Id, Name: req.Name, Code: req.Code, Sort: int32(req.Sort), MenuIds: req.MenuIds,
Id: req.Id, Name: req.Name, Code: req.Code, Sort: int32(req.Sort),
})
return err
}
@@ -0,0 +1,29 @@
package user
import (
"context"
"sundynix-micro-go/app/system/api/internal/svc"
"sundynix-micro-go/app/system/api/internal/types"
"sundynix-micro-go/app/system/rpc/system"
"github.com/zeromicro/go-zero/core/logx"
)
type AssignUserRolesLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewAssignUserRolesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AssignUserRolesLogic {
return &AssignUserRolesLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
}
func (l *AssignUserRolesLogic) AssignUserRoles(req *types.AssignUserRolesReq) error {
_, err := l.svcCtx.SystemRpc.AssignUserRoles(l.ctx, &system.AssignUserRolesReq{
UserId: req.UserId,
RoleIds: req.RoleIds,
})
return err
}
@@ -21,11 +21,21 @@ func NewCreateUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Create
}
func (l *CreateUserLogic) CreateUser(req *types.UserCreateReq) error {
_, err := l.svcCtx.SystemRpc.CreateUser(l.ctx, &system.CreateUserReq{
resp, err := l.svcCtx.SystemRpc.CreateUser(l.ctx, &system.CreateUserReq{
Name: req.Name,
Account: req.Account,
Password: req.Password,
Phone: req.Phone,
})
if err != nil {
return err
}
// 创建完成后立即绑定角色
if len(req.RoleIds) > 0 && resp != nil {
_, err = l.svcCtx.SystemRpc.AssignUserRoles(l.ctx, &system.AssignUserRolesReq{
UserId: resp.User.Id,
RoleIds: req.RoleIds,
})
}
return err
}
@@ -0,0 +1,47 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.10.1
package user
import (
"context"
"sundynix-micro-go/app/system/api/internal/svc"
"sundynix-micro-go/app/system/api/internal/types"
"sundynix-micro-go/app/system/rpc/system"
"github.com/zeromicro/go-zero/core/logx"
)
type GetUserDetailLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 用户详情(含角色信息)
func NewGetUserDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserDetailLogic {
return &GetUserDetailLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *GetUserDetailLogic) GetUserDetail(req *types.IdQueryReq) (resp *types.UserDetailResp, err error) {
rpcResp, err := l.svcCtx.SystemRpc.GetUserDetail(l.ctx, &system.GetUserDetailReq{
Id: req.Id,
})
if err != nil {
return nil, err
}
return &types.UserDetailResp{
Id: rpcResp.Id,
Name: rpcResp.Name,
Account: rpcResp.Account,
NickName: rpcResp.NickName,
Phone: rpcResp.Phone,
RoleIds: rpcResp.RoleIds,
}, nil
}
@@ -32,6 +32,15 @@ func (l *GetUserListLogic) GetUserList(req *types.UserListReq) (resp interface{}
}
var list []map[string]interface{}
for _, u := range rpcResp.List {
var roles []map[string]interface{}
for _, r := range u.Roles {
roles = append(roles, map[string]interface{}{
"id": r.Id,
"name": r.Name,
"code": r.Code,
})
}
list = append(list, map[string]interface{}{
"id": u.Id,
"name": u.Name,
@@ -40,6 +49,7 @@ func (l *GetUserListLogic) GetUserList(req *types.UserListReq) (resp interface{}
"phone": u.Phone,
"gender": u.Gender,
"createdAt": u.CreatedAt,
"roles": roles,
})
}
return map[string]interface{}{"list": list, "total": rpcResp.Total}, nil
+40 -11
View File
@@ -3,6 +3,16 @@
package types
type AssignRoleMenusReq struct {
RoleId string `json:"roleId"`
MenuIds []string `json:"menuIds"`
}
type AssignUserRolesReq struct {
UserId string `json:"userId"`
RoleIds []string `json:"roleIds"`
}
type ClientListReq struct {
Current int `json:"current,optional"`
PageSize int `json:"pageSize,optional"`
@@ -49,6 +59,10 @@ type DictUpdateReq struct {
Desc string `json:"desc,optional"`
}
type IdQueryReq struct {
Id string `form:"id"`
}
type IdReq struct {
Id string `json:"id"`
}
@@ -103,6 +117,14 @@ type ResetPasswordReq struct {
Password string `json:"password"`
}
type RoleDetailResp struct {
Id string `json:"id"`
Name string `json:"name"`
Code string `json:"code"`
Sort int `json:"sort"`
MenuIds []string `json:"menuIds"`
}
type RoleListReq struct {
Current int `json:"current,optional"`
PageSize int `json:"pageSize,optional"`
@@ -117,11 +139,10 @@ type RoleReq struct {
}
type RoleUpdateReq struct {
Id string `json:"id"`
Name string `json:"name,optional"`
Code string `json:"code,optional"`
Sort int `json:"sort,optional"`
MenuIds []string `json:"menuIds,optional"`
Id string `json:"id"`
Name string `json:"name,optional"`
Code string `json:"code,optional"`
Sort int `json:"sort,optional"`
}
type UserCreateReq struct {
@@ -133,6 +154,15 @@ type UserCreateReq struct {
RoleIds []string `json:"roleIds,optional"`
}
type UserDetailResp struct {
Id string `json:"id"`
Name string `json:"name"`
Account string `json:"account"`
NickName string `json:"nickName"`
Phone string `json:"phone"`
RoleIds []string `json:"roleIds"`
}
type UserListReq struct {
Current int `json:"current,optional"`
PageSize int `json:"pageSize,optional"`
@@ -141,10 +171,9 @@ type UserListReq struct {
}
type UserUpdateReq struct {
Id string `json:"id"`
Name string `json:"name,optional"`
Account string `json:"account,optional"`
Phone string `json:"phone,optional"`
NickName string `json:"nickName,optional"`
RoleIds []string `json:"roleIds,optional"`
Id string `json:"id"`
Name string `json:"name,optional"`
Account string `json:"account,optional"`
Phone string `json:"phone,optional"`
NickName string `json:"nickName,optional"`
}
+51 -11
View File
@@ -12,6 +12,9 @@ type (
IdReq {
Id string `json:"id"`
}
IdQueryReq {
Id string `form:"id"`
}
IdsReq {
Ids []string `json:"ids"`
}
@@ -49,17 +52,27 @@ type (
MenuIds []string `json:"menuIds,optional"`
}
RoleUpdateReq {
Id string `json:"id"`
Name string `json:"name,optional"`
Code string `json:"code,optional"`
Sort int `json:"sort,optional"`
MenuIds []string `json:"menuIds,optional"`
Id string `json:"id"`
Name string `json:"name,optional"`
Code string `json:"code,optional"`
Sort int `json:"sort,optional"`
}
RoleListReq {
Current int `json:"current,optional"`
PageSize int `json:"pageSize,optional"`
Name string `json:"name,optional"`
}
RoleDetailResp {
Id string `json:"id"`
Name string `json:"name"`
Code string `json:"code"`
Sort int `json:"sort"`
MenuIds []string `json:"menuIds"`
}
AssignRoleMenusReq {
RoleId string `json:"roleId"`
MenuIds []string `json:"menuIds"`
}
// ---------- 菜单 ----------
MenuReq {
ParentId string `json:"parentId,optional"`
@@ -125,12 +138,11 @@ type (
RoleIds []string `json:"roleIds,optional"`
}
UserUpdateReq {
Id string `json:"id"`
Name string `json:"name,optional"`
Account string `json:"account,optional"`
Phone string `json:"phone,optional"`
NickName string `json:"nickName,optional"`
RoleIds []string `json:"roleIds,optional"`
Id string `json:"id"`
Name string `json:"name,optional"`
Account string `json:"account,optional"`
Phone string `json:"phone,optional"`
NickName string `json:"nickName,optional"`
}
UserListReq {
Current int `json:"current,optional"`
@@ -138,10 +150,22 @@ type (
Name string `json:"name,optional"`
Account string `json:"account,optional"`
}
UserDetailResp {
Id string `json:"id"`
Name string `json:"name"`
Account string `json:"account"`
NickName string `json:"nickName"`
Phone string `json:"phone"`
RoleIds []string `json:"roleIds"`
}
ResetPasswordReq {
Id string `json:"id"`
Password string `json:"password"`
}
AssignUserRolesReq {
UserId string `json:"userId"`
RoleIds []string `json:"roleIds"`
}
)
// ========== 需要鉴权的接口 ==========
@@ -189,6 +213,14 @@ service system-api {
@doc "角色列表"
@handler GetRoleList
post /role/list (RoleListReq)
@doc "角色详情(含已授权菜单)"
@handler GetRoleDetail
get /role/detail (IdQueryReq) returns (RoleDetailResp)
@doc "角色授权菜单"
@handler AssignRoleMenus
post /role/assignMenus (AssignRoleMenusReq)
}
@server (
@@ -266,6 +298,10 @@ service system-api {
@handler GetUserList
post /user/list (UserListReq)
@doc "用户详情(含角色信息)"
@handler GetUserDetail
get /user/detail (IdQueryReq) returns (UserDetailResp)
@doc "创建用户"
@handler CreateUser
post /user/create (UserCreateReq)
@@ -281,5 +317,9 @@ service system-api {
@doc "重置密码"
@handler ResetPassword
post /user/resetPassword (ResetPasswordReq)
@doc "用户授权角色"
@handler AssignUserRoles
post /user/assignRoles (AssignUserRolesReq)
}