任务三
This commit is contained in:
@@ -0,0 +1,398 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"kra/internal/biz/system"
|
||||
"kra/internal/server/middleware"
|
||||
|
||||
"github.com/go-kratos/kratos/v2/errors"
|
||||
"github.com/go-kratos/kratos/v2/transport/http"
|
||||
)
|
||||
|
||||
// ApiService API服务
|
||||
type ApiService struct {
|
||||
uc *system.ApiUsecase
|
||||
}
|
||||
|
||||
// NewApiService 创建API服务
|
||||
func NewApiService(uc *system.ApiUsecase) *ApiService {
|
||||
return &ApiService{uc: uc}
|
||||
}
|
||||
|
||||
// ApiInfo API信息
|
||||
type ApiInfo struct {
|
||||
ID uint `json:"ID"`
|
||||
Path string `json:"path"`
|
||||
Description string `json:"description"`
|
||||
ApiGroup string `json:"apiGroup"`
|
||||
Method string `json:"method"`
|
||||
}
|
||||
|
||||
// CreateApiRequest 创建API请求
|
||||
type CreateApiRequest struct {
|
||||
Path string `json:"path"`
|
||||
Description string `json:"description"`
|
||||
ApiGroup string `json:"apiGroup"`
|
||||
Method string `json:"method"`
|
||||
}
|
||||
|
||||
// CreateApi 创建API
|
||||
func (s *ApiService) CreateApi(ctx context.Context, req *CreateApiRequest) error {
|
||||
api := &system.Api{
|
||||
Path: req.Path,
|
||||
Description: req.Description,
|
||||
ApiGroup: req.ApiGroup,
|
||||
Method: req.Method,
|
||||
}
|
||||
return s.uc.CreateApi(ctx, api)
|
||||
}
|
||||
|
||||
// UpdateApiRequest 更新API请求
|
||||
type UpdateApiRequest struct {
|
||||
ID uint `json:"ID"`
|
||||
Path string `json:"path"`
|
||||
Description string `json:"description"`
|
||||
ApiGroup string `json:"apiGroup"`
|
||||
Method string `json:"method"`
|
||||
}
|
||||
|
||||
// UpdateApi 更新API
|
||||
func (s *ApiService) UpdateApi(ctx context.Context, req *UpdateApiRequest) error {
|
||||
api := &system.Api{
|
||||
ID: req.ID,
|
||||
Path: req.Path,
|
||||
Description: req.Description,
|
||||
ApiGroup: req.ApiGroup,
|
||||
Method: req.Method,
|
||||
}
|
||||
return s.uc.UpdateApi(ctx, api)
|
||||
}
|
||||
|
||||
// DeleteApi 删除API
|
||||
func (s *ApiService) DeleteApi(ctx context.Context, id uint) error {
|
||||
return s.uc.DeleteApi(ctx, id)
|
||||
}
|
||||
|
||||
// DeleteApisByIdsRequest 批量删除API请求
|
||||
type DeleteApisByIdsRequest struct {
|
||||
Ids []uint `json:"ids"`
|
||||
}
|
||||
|
||||
// DeleteApisByIds 批量删除API
|
||||
func (s *ApiService) DeleteApisByIds(ctx context.Context, req *DeleteApisByIdsRequest) error {
|
||||
return s.uc.DeleteApisByIds(ctx, req.Ids)
|
||||
}
|
||||
|
||||
// GetApiById 根据ID获取API
|
||||
func (s *ApiService) GetApiById(ctx context.Context, id uint) (*ApiInfo, error) {
|
||||
api, err := s.uc.GetApiById(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return toApiInfo(api), nil
|
||||
}
|
||||
|
||||
// GetApiListRequest 获取API列表请求
|
||||
type GetApiListRequest struct {
|
||||
Page int `json:"page"`
|
||||
PageSize int `json:"pageSize"`
|
||||
Path string `json:"path"`
|
||||
Description string `json:"description"`
|
||||
Method string `json:"method"`
|
||||
ApiGroup string `json:"apiGroup"`
|
||||
OrderKey string `json:"orderKey"`
|
||||
Desc bool `json:"desc"`
|
||||
}
|
||||
|
||||
// GetApiListResponse 获取API列表响应
|
||||
type GetApiListResponse struct {
|
||||
List []*ApiInfo `json:"list"`
|
||||
Total int64 `json:"total"`
|
||||
Page int `json:"page"`
|
||||
PageSize int `json:"pageSize"`
|
||||
}
|
||||
|
||||
// GetApiList 获取API列表
|
||||
func (s *ApiService) GetApiList(ctx context.Context, req *GetApiListRequest) (*GetApiListResponse, error) {
|
||||
filters := make(map[string]string)
|
||||
if req.Path != "" {
|
||||
filters["path"] = req.Path
|
||||
}
|
||||
if req.Description != "" {
|
||||
filters["description"] = req.Description
|
||||
}
|
||||
if req.Method != "" {
|
||||
filters["method"] = req.Method
|
||||
}
|
||||
if req.ApiGroup != "" {
|
||||
filters["api_group"] = req.ApiGroup
|
||||
}
|
||||
|
||||
apis, total, err := s.uc.GetAPIInfoList(ctx, req.Page, req.PageSize, filters, req.OrderKey, req.Desc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
list := make([]*ApiInfo, len(apis))
|
||||
for i, api := range apis {
|
||||
list[i] = toApiInfo(api)
|
||||
}
|
||||
|
||||
return &GetApiListResponse{
|
||||
List: list,
|
||||
Total: total,
|
||||
Page: req.Page,
|
||||
PageSize: req.PageSize,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetAllApis 获取所有API
|
||||
func (s *ApiService) GetAllApis(ctx context.Context, authorityID uint) ([]*ApiInfo, error) {
|
||||
apis, err := s.uc.GetAllApis(ctx, authorityID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
list := make([]*ApiInfo, len(apis))
|
||||
for i, api := range apis {
|
||||
list[i] = toApiInfo(api)
|
||||
}
|
||||
return list, nil
|
||||
}
|
||||
|
||||
// GetApiGroups 获取API分组
|
||||
func (s *ApiService) GetApiGroups(ctx context.Context) ([]string, map[string]string, error) {
|
||||
return s.uc.GetApiGroups(ctx)
|
||||
}
|
||||
|
||||
// SyncApiResponse 同步API响应
|
||||
type SyncApiResponse struct {
|
||||
NewApis []*ApiInfo `json:"newApis"`
|
||||
DeleteApis []*ApiInfo `json:"deleteApis"`
|
||||
IgnoreApis []*ApiInfo `json:"ignoreApis"`
|
||||
}
|
||||
|
||||
// SyncApi 同步API
|
||||
func (s *ApiService) SyncApi(ctx context.Context) (*SyncApiResponse, error) {
|
||||
newApis, deleteApis, ignoreApis, err := s.uc.SyncApi(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp := &SyncApiResponse{
|
||||
NewApis: make([]*ApiInfo, len(newApis)),
|
||||
DeleteApis: make([]*ApiInfo, len(deleteApis)),
|
||||
IgnoreApis: make([]*ApiInfo, len(ignoreApis)),
|
||||
}
|
||||
for i, api := range newApis {
|
||||
resp.NewApis[i] = toApiInfo(api)
|
||||
}
|
||||
for i, api := range deleteApis {
|
||||
resp.DeleteApis[i] = toApiInfo(api)
|
||||
}
|
||||
for i, api := range ignoreApis {
|
||||
resp.IgnoreApis[i] = toApiInfo(api)
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// IgnoreApiRequest 忽略API请求
|
||||
type IgnoreApiRequest struct {
|
||||
Path string `json:"path"`
|
||||
Method string `json:"method"`
|
||||
Flag bool `json:"flag"`
|
||||
}
|
||||
|
||||
// IgnoreApi 忽略API
|
||||
func (s *ApiService) IgnoreApi(ctx context.Context, req *IgnoreApiRequest) error {
|
||||
return s.uc.IgnoreApi(ctx, req.Path, req.Method, req.Flag)
|
||||
}
|
||||
|
||||
// EnterSyncApiRequest 确认同步API请求
|
||||
type EnterSyncApiRequest struct {
|
||||
NewApis []*ApiInfo `json:"newApis"`
|
||||
DeleteApis []*ApiInfo `json:"deleteApis"`
|
||||
}
|
||||
|
||||
// EnterSyncApi 确认同步API
|
||||
func (s *ApiService) EnterSyncApi(ctx context.Context, req *EnterSyncApiRequest) error {
|
||||
newApis := make([]*system.Api, len(req.NewApis))
|
||||
for i, api := range req.NewApis {
|
||||
newApis[i] = &system.Api{
|
||||
Path: api.Path,
|
||||
Description: api.Description,
|
||||
ApiGroup: api.ApiGroup,
|
||||
Method: api.Method,
|
||||
}
|
||||
}
|
||||
deleteApis := make([]*system.Api, len(req.DeleteApis))
|
||||
for i, api := range req.DeleteApis {
|
||||
deleteApis[i] = &system.Api{
|
||||
ID: api.ID,
|
||||
Path: api.Path,
|
||||
Method: api.Method,
|
||||
}
|
||||
}
|
||||
return s.uc.EnterSyncApi(ctx, newApis, deleteApis)
|
||||
}
|
||||
|
||||
// FreshCasbin 刷新Casbin缓存
|
||||
func (s *ApiService) FreshCasbin() error {
|
||||
return s.uc.FreshCasbin()
|
||||
}
|
||||
|
||||
// 转换函数
|
||||
func toApiInfo(api *system.Api) *ApiInfo {
|
||||
return &ApiInfo{
|
||||
ID: api.ID,
|
||||
Path: api.Path,
|
||||
Description: api.Description,
|
||||
ApiGroup: api.ApiGroup,
|
||||
Method: api.Method,
|
||||
}
|
||||
}
|
||||
|
||||
// RegisterRoutes 注册路由
|
||||
func (s *ApiService) RegisterRoutes(srv *http.Server) {
|
||||
r := srv.Route("/")
|
||||
|
||||
r.POST("/api/createApi", s.handleCreateApi)
|
||||
r.POST("/api/updateApi", s.handleUpdateApi)
|
||||
r.POST("/api/deleteApi", s.handleDeleteApi)
|
||||
r.DELETE("/api/deleteApisByIds", s.handleDeleteApisByIds)
|
||||
r.POST("/api/getApiList", s.handleGetApiList)
|
||||
r.POST("/api/getApiById", s.handleGetApiById)
|
||||
r.POST("/api/getAllApis", s.handleGetAllApis)
|
||||
r.GET("/api/getApiGroups", s.handleGetApiGroups)
|
||||
r.GET("/api/syncApi", s.handleSyncApi)
|
||||
r.POST("/api/ignoreApi", s.handleIgnoreApi)
|
||||
r.POST("/api/enterSyncApi", s.handleEnterSyncApi)
|
||||
r.GET("/api/freshCasbin", s.handleFreshCasbin)
|
||||
}
|
||||
|
||||
// HTTP Handlers
|
||||
func (s *ApiService) handleCreateApi(ctx http.Context) error {
|
||||
var req CreateApiRequest
|
||||
if err := ctx.Bind(&req); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.CreateApi(ctx, &req); err != nil {
|
||||
return errors.BadRequest("CREATE_FAILED", err.Error())
|
||||
}
|
||||
return ctx.Result(200, map[string]any{"code": 0, "msg": "创建成功"})
|
||||
}
|
||||
|
||||
func (s *ApiService) handleUpdateApi(ctx http.Context) error {
|
||||
var req UpdateApiRequest
|
||||
if err := ctx.Bind(&req); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.UpdateApi(ctx, &req); err != nil {
|
||||
return errors.BadRequest("UPDATE_FAILED", err.Error())
|
||||
}
|
||||
return ctx.Result(200, map[string]any{"code": 0, "msg": "修改成功"})
|
||||
}
|
||||
|
||||
func (s *ApiService) handleDeleteApi(ctx http.Context) error {
|
||||
var req struct {
|
||||
ID uint `json:"ID"`
|
||||
}
|
||||
if err := ctx.Bind(&req); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.DeleteApi(ctx, req.ID); err != nil {
|
||||
return errors.BadRequest("DELETE_FAILED", err.Error())
|
||||
}
|
||||
return ctx.Result(200, map[string]any{"code": 0, "msg": "删除成功"})
|
||||
}
|
||||
|
||||
func (s *ApiService) handleDeleteApisByIds(ctx http.Context) error {
|
||||
var req DeleteApisByIdsRequest
|
||||
if err := ctx.Bind(&req); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.DeleteApisByIds(ctx, &req); err != nil {
|
||||
return errors.BadRequest("DELETE_FAILED", err.Error())
|
||||
}
|
||||
return ctx.Result(200, map[string]any{"code": 0, "msg": "删除成功"})
|
||||
}
|
||||
|
||||
func (s *ApiService) handleGetApiList(ctx http.Context) error {
|
||||
var req GetApiListRequest
|
||||
if err := ctx.Bind(&req); err != nil {
|
||||
return err
|
||||
}
|
||||
resp, err := s.GetApiList(ctx, &req)
|
||||
if err != nil {
|
||||
return errors.InternalServer("LIST_ERROR", err.Error())
|
||||
}
|
||||
return ctx.Result(200, map[string]any{"code": 0, "msg": "获取成功", "data": resp})
|
||||
}
|
||||
|
||||
func (s *ApiService) handleGetApiById(ctx http.Context) error {
|
||||
var req struct {
|
||||
ID uint `json:"ID"`
|
||||
}
|
||||
if err := ctx.Bind(&req); err != nil {
|
||||
return err
|
||||
}
|
||||
resp, err := s.GetApiById(ctx, req.ID)
|
||||
if err != nil {
|
||||
return errors.NotFound("NOT_FOUND", err.Error())
|
||||
}
|
||||
return ctx.Result(200, map[string]any{"code": 0, "msg": "获取成功", "data": map[string]any{"api": resp}})
|
||||
}
|
||||
|
||||
func (s *ApiService) handleGetAllApis(ctx http.Context) error {
|
||||
authorityID := middleware.GetAuthorityID(ctx)
|
||||
resp, err := s.GetAllApis(ctx, authorityID)
|
||||
if err != nil {
|
||||
return errors.InternalServer("LIST_ERROR", err.Error())
|
||||
}
|
||||
return ctx.Result(200, map[string]any{"code": 0, "msg": "获取成功", "data": map[string]any{"apis": resp}})
|
||||
}
|
||||
|
||||
func (s *ApiService) handleGetApiGroups(ctx http.Context) error {
|
||||
groups, apiGroupMap, err := s.GetApiGroups(ctx)
|
||||
if err != nil {
|
||||
return errors.InternalServer("LIST_ERROR", err.Error())
|
||||
}
|
||||
return ctx.Result(200, map[string]any{"code": 0, "msg": "获取成功", "data": map[string]any{"groups": groups, "apiGroupMap": apiGroupMap}})
|
||||
}
|
||||
|
||||
func (s *ApiService) handleSyncApi(ctx http.Context) error {
|
||||
resp, err := s.SyncApi(ctx)
|
||||
if err != nil {
|
||||
return errors.InternalServer("SYNC_ERROR", err.Error())
|
||||
}
|
||||
return ctx.Result(200, map[string]any{"code": 0, "data": resp})
|
||||
}
|
||||
|
||||
func (s *ApiService) handleIgnoreApi(ctx http.Context) error {
|
||||
var req IgnoreApiRequest
|
||||
if err := ctx.Bind(&req); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.IgnoreApi(ctx, &req); err != nil {
|
||||
return errors.BadRequest("IGNORE_FAILED", err.Error())
|
||||
}
|
||||
return ctx.Result(200, map[string]any{"code": 0, "msg": "操作成功"})
|
||||
}
|
||||
|
||||
func (s *ApiService) handleEnterSyncApi(ctx http.Context) error {
|
||||
var req EnterSyncApiRequest
|
||||
if err := ctx.Bind(&req); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.EnterSyncApi(ctx, &req); err != nil {
|
||||
return errors.BadRequest("SYNC_FAILED", err.Error())
|
||||
}
|
||||
return ctx.Result(200, map[string]any{"code": 0, "msg": "同步成功"})
|
||||
}
|
||||
|
||||
func (s *ApiService) handleFreshCasbin(ctx http.Context) error {
|
||||
if err := s.FreshCasbin(); err != nil {
|
||||
return errors.InternalServer("FRESH_FAILED", err.Error())
|
||||
}
|
||||
return ctx.Result(200, map[string]any{"code": 0, "msg": "刷新成功"})
|
||||
}
|
||||
@@ -0,0 +1,440 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"kra/internal/biz/system"
|
||||
"kra/internal/server/middleware"
|
||||
|
||||
"github.com/go-kratos/kratos/v2/errors"
|
||||
"github.com/go-kratos/kratos/v2/transport/http"
|
||||
)
|
||||
|
||||
// MenuService 菜单服务
|
||||
type MenuService struct {
|
||||
uc *system.MenuUsecase
|
||||
}
|
||||
|
||||
// NewMenuService 创建菜单服务
|
||||
func NewMenuService(uc *system.MenuUsecase) *MenuService {
|
||||
return &MenuService{uc: uc}
|
||||
}
|
||||
|
||||
// MenuInfo 菜单信息
|
||||
type MenuInfo struct {
|
||||
ID uint `json:"ID"`
|
||||
ParentId uint `json:"parentId"`
|
||||
Path string `json:"path"`
|
||||
Name string `json:"name"`
|
||||
Hidden bool `json:"hidden"`
|
||||
Component string `json:"component"`
|
||||
Sort int `json:"sort"`
|
||||
Meta MenuMeta `json:"meta"`
|
||||
AuthorityId uint `json:"authorityId,omitempty"`
|
||||
MenuId uint `json:"menuId,omitempty"`
|
||||
Parameters []*MenuParamInfo `json:"parameters,omitempty"`
|
||||
MenuBtn []*MenuBtnInfo `json:"menuBtn,omitempty"`
|
||||
Btns map[string]uint `json:"btns,omitempty"`
|
||||
Children []*MenuInfo `json:"children,omitempty"`
|
||||
}
|
||||
|
||||
// MenuMeta 菜单元信息
|
||||
type MenuMeta struct {
|
||||
ActiveName string `json:"activeName"`
|
||||
KeepAlive bool `json:"keepAlive"`
|
||||
DefaultMenu bool `json:"defaultMenu"`
|
||||
Title string `json:"title"`
|
||||
Icon string `json:"icon"`
|
||||
CloseTab bool `json:"closeTab"`
|
||||
TransitionType string `json:"transitionType"`
|
||||
}
|
||||
|
||||
// MenuParamInfo 菜单参数信息
|
||||
type MenuParamInfo struct {
|
||||
ID uint `json:"ID"`
|
||||
SysBaseMenuID uint `json:"sysBaseMenuId"`
|
||||
Type string `json:"type"`
|
||||
Key string `json:"key"`
|
||||
Value string `json:"value"`
|
||||
}
|
||||
|
||||
// MenuBtnInfo 菜单按钮信息
|
||||
type MenuBtnInfo struct {
|
||||
ID uint `json:"ID"`
|
||||
SysBaseMenuID uint `json:"sysBaseMenuId"`
|
||||
Name string `json:"name"`
|
||||
Desc string `json:"desc"`
|
||||
}
|
||||
|
||||
// BaseMenuRequest 基础菜单请求
|
||||
type BaseMenuRequest struct {
|
||||
ID uint `json:"ID"`
|
||||
ParentId uint `json:"parentId"`
|
||||
Path string `json:"path"`
|
||||
Name string `json:"name"`
|
||||
Hidden bool `json:"hidden"`
|
||||
Component string `json:"component"`
|
||||
Sort int `json:"sort"`
|
||||
Meta MenuMeta `json:"meta"`
|
||||
Parameters []*MenuParamInfo `json:"parameters"`
|
||||
MenuBtn []*MenuBtnInfo `json:"menuBtn"`
|
||||
}
|
||||
|
||||
// GetMenu 获取用户动态路由
|
||||
func (s *MenuService) GetMenu(ctx context.Context, authorityId uint) ([]*MenuInfo, error) {
|
||||
menus, err := s.uc.GetMenuTree(ctx, authorityId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return toMenuInfoList(menus), nil
|
||||
}
|
||||
|
||||
// GetBaseMenuTree 获取基础菜单树
|
||||
func (s *MenuService) GetBaseMenuTree(ctx context.Context, authorityId uint) ([]*MenuInfo, error) {
|
||||
menus, err := s.uc.GetBaseMenuTree(ctx, authorityId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return toBaseMenuInfoList(menus), nil
|
||||
}
|
||||
|
||||
// GetMenuList 获取菜单列表
|
||||
func (s *MenuService) GetMenuList(ctx context.Context, authorityId uint) ([]*MenuInfo, error) {
|
||||
menus, err := s.uc.GetInfoList(ctx, authorityId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return toBaseMenuInfoList(menus), nil
|
||||
}
|
||||
|
||||
// AddBaseMenu 添加基础菜单
|
||||
func (s *MenuService) AddBaseMenu(ctx context.Context, req *BaseMenuRequest) error {
|
||||
menu := toBaseMenu(req)
|
||||
return s.uc.AddBaseMenu(ctx, menu)
|
||||
}
|
||||
|
||||
// UpdateBaseMenu 更新基础菜单
|
||||
func (s *MenuService) UpdateBaseMenu(ctx context.Context, req *BaseMenuRequest) error {
|
||||
menu := toBaseMenu(req)
|
||||
return s.uc.UpdateBaseMenu(ctx, menu)
|
||||
}
|
||||
|
||||
// DeleteBaseMenu 删除基础菜单
|
||||
func (s *MenuService) DeleteBaseMenu(ctx context.Context, id uint) error {
|
||||
return s.uc.DeleteBaseMenu(ctx, id)
|
||||
}
|
||||
|
||||
// GetBaseMenuById 根据ID获取基础菜单
|
||||
func (s *MenuService) GetBaseMenuById(ctx context.Context, id uint) (*MenuInfo, error) {
|
||||
menu, err := s.uc.GetBaseMenuById(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return toBaseMenuInfo(menu), nil
|
||||
}
|
||||
|
||||
// AddMenuAuthorityRequest 添加菜单权限请求
|
||||
type AddMenuAuthorityRequest struct {
|
||||
AuthorityId uint `json:"authorityId"`
|
||||
Menus []*BaseMenuRequest `json:"menus"`
|
||||
}
|
||||
|
||||
// AddMenuAuthority 添加菜单权限
|
||||
func (s *MenuService) AddMenuAuthority(ctx context.Context, adminAuthorityID uint, req *AddMenuAuthorityRequest) error {
|
||||
menus := make([]*system.BaseMenu, len(req.Menus))
|
||||
for i, m := range req.Menus {
|
||||
menus[i] = toBaseMenu(m)
|
||||
}
|
||||
return s.uc.AddMenuAuthority(ctx, menus, adminAuthorityID, req.AuthorityId)
|
||||
}
|
||||
|
||||
// GetMenuAuthority 获取角色菜单权限
|
||||
func (s *MenuService) GetMenuAuthority(ctx context.Context, authorityId uint) ([]*MenuInfo, error) {
|
||||
menus, err := s.uc.GetMenuAuthority(ctx, authorityId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return toMenuInfoList(menus), nil
|
||||
}
|
||||
|
||||
// 转换函数
|
||||
func toBaseMenu(req *BaseMenuRequest) *system.BaseMenu {
|
||||
menu := &system.BaseMenu{
|
||||
ID: req.ID,
|
||||
ParentId: req.ParentId,
|
||||
Path: req.Path,
|
||||
Name: req.Name,
|
||||
Hidden: req.Hidden,
|
||||
Component: req.Component,
|
||||
Sort: req.Sort,
|
||||
ActiveName: req.Meta.ActiveName,
|
||||
KeepAlive: req.Meta.KeepAlive,
|
||||
DefaultMenu: req.Meta.DefaultMenu,
|
||||
Title: req.Meta.Title,
|
||||
Icon: req.Meta.Icon,
|
||||
CloseTab: req.Meta.CloseTab,
|
||||
TransitionType: req.Meta.TransitionType,
|
||||
}
|
||||
|
||||
if len(req.Parameters) > 0 {
|
||||
menu.Parameters = make([]*system.MenuParameter, len(req.Parameters))
|
||||
for i, p := range req.Parameters {
|
||||
menu.Parameters[i] = &system.MenuParameter{
|
||||
ID: p.ID,
|
||||
SysBaseMenuID: p.SysBaseMenuID,
|
||||
Type: p.Type,
|
||||
Key: p.Key,
|
||||
Value: p.Value,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(req.MenuBtn) > 0 {
|
||||
menu.MenuBtn = make([]*system.MenuBtn, len(req.MenuBtn))
|
||||
for i, b := range req.MenuBtn {
|
||||
menu.MenuBtn[i] = &system.MenuBtn{
|
||||
ID: b.ID,
|
||||
SysBaseMenuID: b.SysBaseMenuID,
|
||||
Name: b.Name,
|
||||
Desc: b.Desc,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return menu
|
||||
}
|
||||
|
||||
func toMenuInfoList(menus []*system.Menu) []*MenuInfo {
|
||||
if menus == nil {
|
||||
return []*MenuInfo{}
|
||||
}
|
||||
result := make([]*MenuInfo, len(menus))
|
||||
for i, m := range menus {
|
||||
result[i] = toMenuInfo(m)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func toMenuInfo(m *system.Menu) *MenuInfo {
|
||||
info := &MenuInfo{
|
||||
ID: m.ID,
|
||||
ParentId: m.ParentId,
|
||||
Path: m.Path,
|
||||
Name: m.Name,
|
||||
Hidden: m.Hidden,
|
||||
Component: m.Component,
|
||||
Sort: m.Sort,
|
||||
AuthorityId: m.AuthorityId,
|
||||
MenuId: m.MenuId,
|
||||
Btns: m.Btns,
|
||||
Meta: MenuMeta{
|
||||
ActiveName: m.ActiveName,
|
||||
KeepAlive: m.KeepAlive,
|
||||
DefaultMenu: m.DefaultMenu,
|
||||
Title: m.Title,
|
||||
Icon: m.Icon,
|
||||
CloseTab: m.CloseTab,
|
||||
TransitionType: m.TransitionType,
|
||||
},
|
||||
}
|
||||
|
||||
if len(m.Parameters) > 0 {
|
||||
info.Parameters = make([]*MenuParamInfo, len(m.Parameters))
|
||||
for i, p := range m.Parameters {
|
||||
info.Parameters[i] = &MenuParamInfo{
|
||||
ID: p.ID,
|
||||
SysBaseMenuID: p.SysBaseMenuID,
|
||||
Type: p.Type,
|
||||
Key: p.Key,
|
||||
Value: p.Value,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(m.Children) > 0 {
|
||||
info.Children = toMenuInfoList(m.Children)
|
||||
}
|
||||
|
||||
return info
|
||||
}
|
||||
|
||||
func toBaseMenuInfoList(menus []*system.BaseMenu) []*MenuInfo {
|
||||
if menus == nil {
|
||||
return []*MenuInfo{}
|
||||
}
|
||||
result := make([]*MenuInfo, len(menus))
|
||||
for i, m := range menus {
|
||||
result[i] = toBaseMenuInfo(m)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func toBaseMenuInfo(m *system.BaseMenu) *MenuInfo {
|
||||
info := &MenuInfo{
|
||||
ID: m.ID,
|
||||
ParentId: m.ParentId,
|
||||
Path: m.Path,
|
||||
Name: m.Name,
|
||||
Hidden: m.Hidden,
|
||||
Component: m.Component,
|
||||
Sort: m.Sort,
|
||||
Meta: MenuMeta{
|
||||
ActiveName: m.ActiveName,
|
||||
KeepAlive: m.KeepAlive,
|
||||
DefaultMenu: m.DefaultMenu,
|
||||
Title: m.Title,
|
||||
Icon: m.Icon,
|
||||
CloseTab: m.CloseTab,
|
||||
TransitionType: m.TransitionType,
|
||||
},
|
||||
}
|
||||
|
||||
if len(m.Parameters) > 0 {
|
||||
info.Parameters = make([]*MenuParamInfo, len(m.Parameters))
|
||||
for i, p := range m.Parameters {
|
||||
info.Parameters[i] = &MenuParamInfo{
|
||||
ID: p.ID,
|
||||
SysBaseMenuID: p.SysBaseMenuID,
|
||||
Type: p.Type,
|
||||
Key: p.Key,
|
||||
Value: p.Value,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(m.MenuBtn) > 0 {
|
||||
info.MenuBtn = make([]*MenuBtnInfo, len(m.MenuBtn))
|
||||
for i, b := range m.MenuBtn {
|
||||
info.MenuBtn[i] = &MenuBtnInfo{
|
||||
ID: b.ID,
|
||||
SysBaseMenuID: b.SysBaseMenuID,
|
||||
Name: b.Name,
|
||||
Desc: b.Desc,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(m.Children) > 0 {
|
||||
info.Children = toBaseMenuInfoList(m.Children)
|
||||
}
|
||||
|
||||
return info
|
||||
}
|
||||
|
||||
// RegisterRoutes 注册路由
|
||||
func (s *MenuService) RegisterRoutes(srv *http.Server) {
|
||||
r := srv.Route("/")
|
||||
|
||||
r.POST("/menu/getMenu", s.handleGetMenu)
|
||||
r.POST("/menu/getBaseMenuTree", s.handleGetBaseMenuTree)
|
||||
r.POST("/menu/getMenuList", s.handleGetMenuList)
|
||||
r.POST("/menu/addBaseMenu", s.handleAddBaseMenu)
|
||||
r.POST("/menu/updateBaseMenu", s.handleUpdateBaseMenu)
|
||||
r.POST("/menu/deleteBaseMenu", s.handleDeleteBaseMenu)
|
||||
r.POST("/menu/getBaseMenuById", s.handleGetBaseMenuById)
|
||||
r.POST("/menu/addMenuAuthority", s.handleAddMenuAuthority)
|
||||
r.POST("/menu/getMenuAuthority", s.handleGetMenuAuthority)
|
||||
}
|
||||
|
||||
// HTTP Handlers
|
||||
func (s *MenuService) handleGetMenu(ctx http.Context) error {
|
||||
authorityId := middleware.GetAuthorityID(ctx)
|
||||
menus, err := s.GetMenu(ctx, authorityId)
|
||||
if err != nil {
|
||||
return errors.InternalServer("GET_MENU_FAILED", err.Error())
|
||||
}
|
||||
return ctx.Result(200, map[string]any{"code": 0, "msg": "获取成功", "data": map[string]any{"menus": menus}})
|
||||
}
|
||||
|
||||
func (s *MenuService) handleGetBaseMenuTree(ctx http.Context) error {
|
||||
authorityId := middleware.GetAuthorityID(ctx)
|
||||
menus, err := s.GetBaseMenuTree(ctx, authorityId)
|
||||
if err != nil {
|
||||
return errors.InternalServer("GET_MENU_FAILED", err.Error())
|
||||
}
|
||||
return ctx.Result(200, map[string]any{"code": 0, "msg": "获取成功", "data": map[string]any{"menus": menus}})
|
||||
}
|
||||
|
||||
func (s *MenuService) handleGetMenuList(ctx http.Context) error {
|
||||
authorityId := middleware.GetAuthorityID(ctx)
|
||||
menus, err := s.GetMenuList(ctx, authorityId)
|
||||
if err != nil {
|
||||
return errors.InternalServer("GET_MENU_FAILED", err.Error())
|
||||
}
|
||||
return ctx.Result(200, map[string]any{"code": 0, "msg": "获取成功", "data": menus})
|
||||
}
|
||||
|
||||
func (s *MenuService) handleAddBaseMenu(ctx http.Context) error {
|
||||
var req BaseMenuRequest
|
||||
if err := ctx.Bind(&req); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.AddBaseMenu(ctx, &req); err != nil {
|
||||
return errors.BadRequest("ADD_MENU_FAILED", err.Error())
|
||||
}
|
||||
return ctx.Result(200, map[string]any{"code": 0, "msg": "添加成功"})
|
||||
}
|
||||
|
||||
func (s *MenuService) handleUpdateBaseMenu(ctx http.Context) error {
|
||||
var req BaseMenuRequest
|
||||
if err := ctx.Bind(&req); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.UpdateBaseMenu(ctx, &req); err != nil {
|
||||
return errors.BadRequest("UPDATE_MENU_FAILED", err.Error())
|
||||
}
|
||||
return ctx.Result(200, map[string]any{"code": 0, "msg": "更新成功"})
|
||||
}
|
||||
|
||||
func (s *MenuService) handleDeleteBaseMenu(ctx http.Context) error {
|
||||
var req struct {
|
||||
ID uint `json:"ID"`
|
||||
}
|
||||
if err := ctx.Bind(&req); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.DeleteBaseMenu(ctx, req.ID); err != nil {
|
||||
return errors.BadRequest("DELETE_MENU_FAILED", err.Error())
|
||||
}
|
||||
return ctx.Result(200, map[string]any{"code": 0, "msg": "删除成功"})
|
||||
}
|
||||
|
||||
func (s *MenuService) handleGetBaseMenuById(ctx http.Context) error {
|
||||
var req struct {
|
||||
ID uint `json:"ID"`
|
||||
}
|
||||
if err := ctx.Bind(&req); err != nil {
|
||||
return err
|
||||
}
|
||||
menu, err := s.GetBaseMenuById(ctx, req.ID)
|
||||
if err != nil {
|
||||
return errors.NotFound("MENU_NOT_FOUND", err.Error())
|
||||
}
|
||||
return ctx.Result(200, map[string]any{"code": 0, "msg": "获取成功", "data": map[string]any{"menu": menu}})
|
||||
}
|
||||
|
||||
func (s *MenuService) handleAddMenuAuthority(ctx http.Context) error {
|
||||
var req AddMenuAuthorityRequest
|
||||
if err := ctx.Bind(&req); err != nil {
|
||||
return err
|
||||
}
|
||||
adminAuthorityID := middleware.GetAuthorityID(ctx)
|
||||
if err := s.AddMenuAuthority(ctx, adminAuthorityID, &req); err != nil {
|
||||
return errors.BadRequest("ADD_AUTHORITY_FAILED", err.Error())
|
||||
}
|
||||
return ctx.Result(200, map[string]any{"code": 0, "msg": "添加成功"})
|
||||
}
|
||||
|
||||
func (s *MenuService) handleGetMenuAuthority(ctx http.Context) error {
|
||||
var req struct {
|
||||
AuthorityId uint `json:"authorityId"`
|
||||
}
|
||||
if err := ctx.Bind(&req); err != nil {
|
||||
return err
|
||||
}
|
||||
menus, err := s.GetMenuAuthority(ctx, req.AuthorityId)
|
||||
if err != nil {
|
||||
return errors.InternalServer("GET_AUTHORITY_FAILED", err.Error())
|
||||
}
|
||||
return ctx.Result(200, map[string]any{"code": 0, "msg": "获取成功", "data": map[string]any{"menus": menus}})
|
||||
}
|
||||
Reference in New Issue
Block a user