Some checks failed
Build and Deploy (service.xpcool.com) / build-and-deploy (push) Failing after 31s
将 api 层校验规则提示语、service 层 gerror.Wrap 与 response.Error 错误信息、panic 未注册提示统一改为中文,并同步中文化 cmd 路由注释 与变更记录。仅涉及注释、文档与字符串改动,无业务逻辑变更。
129 lines
4.2 KiB
Go
129 lines
4.2 KiB
Go
package admin_system_role
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|
|
|
"service.xpcool.com/internal/consts"
|
|
"service.xpcool.com/internal/dao"
|
|
"service.xpcool.com/internal/library/response"
|
|
"service.xpcool.com/internal/model/do"
|
|
"service.xpcool.com/internal/model/dto"
|
|
"service.xpcool.com/internal/model/entity"
|
|
)
|
|
|
|
// IRoleManage 管理角色及其菜单绑定。
|
|
type IRoleManage interface {
|
|
List(context.Context, dto.PageQuery) ([]*dto.RoleItem, int, error)
|
|
Create(context.Context, dto.RoleCreateInput) (uint64, error)
|
|
Update(context.Context, dto.RoleUpdateInput) error
|
|
Delete(context.Context, uint64) error
|
|
}
|
|
|
|
type roleManage struct{}
|
|
|
|
var localRoleManage IRoleManage
|
|
|
|
func NewRoleManage() IRoleManage { return &roleManage{} }
|
|
|
|
func RoleManage() IRoleManage {
|
|
if localRoleManage == nil {
|
|
panic("RoleManage 实现未注册")
|
|
}
|
|
return localRoleManage
|
|
}
|
|
func RegisterRoleManage(i IRoleManage) { localRoleManage = i }
|
|
|
|
// ---------------------- Role manage ----------------------
|
|
|
|
func (s *roleManage) List(ctx context.Context, q dto.PageQuery) ([]*dto.RoleItem, int, error) {
|
|
total, err := dao.AdminRole.Ctx(ctx).Count()
|
|
if err != nil {
|
|
return nil, 0, gerror.Wrap(err, "统计角色总数失败")
|
|
}
|
|
if total == 0 {
|
|
return nil, 0, nil
|
|
}
|
|
var list []entity.AdminRole
|
|
if err = dao.AdminRole.Ctx(ctx).Page(q.Page, q.Size).OrderAsc("id").Scan(&list); err != nil {
|
|
return nil, 0, gerror.Wrap(err, "查询角色列表失败")
|
|
}
|
|
items := make([]*dto.RoleItem, 0, len(list))
|
|
for i := range list {
|
|
r := &list[i]
|
|
menuIds, err := roleMenuIDs(ctx, r.Id)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
items = append(items, &dto.RoleItem{
|
|
Id: r.Id, Code: r.Code, Name: r.Name, Status: r.Status, MenuIds: menuIds,
|
|
CreatedAt: r.CreatedAt.Layout("2006-01-02 15:04:05"),
|
|
})
|
|
}
|
|
return items, total, nil
|
|
}
|
|
|
|
func (s *roleManage) Create(ctx context.Context, in dto.RoleCreateInput) (uint64, error) {
|
|
count, err := dao.AdminRole.Ctx(ctx).Where(do.AdminRole{Code: in.Code}).Count()
|
|
if err != nil {
|
|
return 0, gerror.Wrap(err, "校验角色编码是否存在失败")
|
|
}
|
|
if count > 0 {
|
|
return 0, response.Error(consts.CodeInvalidParam, "角色编码已存在")
|
|
}
|
|
id, err := dao.AdminRole.Ctx(ctx).Data(do.AdminRole{Code: in.Code, Name: in.Name, Status: in.Status}).InsertAndGetId()
|
|
if err != nil {
|
|
return 0, gerror.Wrap(err, "新增角色失败")
|
|
}
|
|
if err = bindRoleMenus(ctx, uint64(id), in.MenuIds); err != nil {
|
|
return 0, err
|
|
}
|
|
return uint64(id), nil
|
|
}
|
|
|
|
func (s *roleManage) Update(ctx context.Context, in dto.RoleUpdateInput) error {
|
|
if _, err := dao.AdminRole.Ctx(ctx).Where(do.AdminRole{Id: in.Id}).Data(do.AdminRole{Name: in.Name, Status: in.Status}).Update(); err != nil {
|
|
return gerror.Wrap(err, "更新角色失败")
|
|
}
|
|
return bindRoleMenus(ctx, in.Id, in.MenuIds)
|
|
}
|
|
|
|
func (s *roleManage) Delete(ctx context.Context, id uint64) error {
|
|
if _, err := dao.AdminRole.Ctx(ctx).Where(do.AdminRole{Id: id}).Delete(); err != nil {
|
|
return gerror.Wrap(err, "删除角色失败")
|
|
}
|
|
if _, err := dao.AdminRoleMenu.Ctx(ctx).Unscoped().Where(do.AdminRoleMenu{RoleId: id}).Delete(); err != nil {
|
|
return gerror.Wrap(err, "删除角色菜单关联失败")
|
|
}
|
|
if _, err := dao.AdminUserRole.Ctx(ctx).Unscoped().Where(do.AdminUserRole{RoleId: id}).Delete(); err != nil {
|
|
return gerror.Wrap(err, "删除用户角色关联失败")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func roleMenuIDs(ctx context.Context, roleID uint64) ([]uint64, error) {
|
|
var rels []entity.AdminRoleMenu
|
|
if err := dao.AdminRoleMenu.Ctx(ctx).Where(do.AdminRoleMenu{RoleId: roleID}).Scan(&rels); err != nil {
|
|
return nil, gerror.Wrap(err, "查询角色菜单关联失败")
|
|
}
|
|
ids := make([]uint64, 0, len(rels))
|
|
for _, r := range rels {
|
|
ids = append(ids, r.MenuId)
|
|
}
|
|
return ids, nil
|
|
}
|
|
|
|
// bindRoleMenus 全量重绑角色-菜单关系。
|
|
func bindRoleMenus(ctx context.Context, roleID uint64, menuIds []uint64) error {
|
|
if _, err := dao.AdminRoleMenu.Ctx(ctx).Unscoped().Where(do.AdminRoleMenu{RoleId: roleID}).Delete(); err != nil {
|
|
return gerror.Wrap(err, "清除角色菜单关联失败")
|
|
}
|
|
for _, mid := range menuIds {
|
|
if _, err := dao.AdminRoleMenu.Ctx(ctx).Data(do.AdminRoleMenu{RoleId: roleID, MenuId: mid}).Insert(); err != nil {
|
|
return gerror.Wrap(err, "绑定角色菜单失败")
|
|
}
|
|
}
|
|
return nil
|
|
}
|