From 09a80bc7bcf6de285094296d5c8c5d3fcc221045 Mon Sep 17 00:00:00 2001 From: Odboy Date: Mon, 5 Feb 2024 09:56:49 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8F=9C=E5=8D=95=E8=B7=AF=E7=94=B1=E5=8A=A8?= =?UTF-8?q?=E6=80=81=E5=8A=A0=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application-macDev.yml | 17 ++++ ...ation-dev.yml => application-windowDev.yml | 0 application.yml | 2 +- src/infra/gin.go | 2 +- src/modules/system/dao/api.go | 10 +- src/modules/system/dao/menu.go | 16 ++++ src/modules/system/domain/api.go | 2 +- .../system/{rest => domain}/dto/kickout.go | 0 .../system/{rest => domain}/dto/login.go | 0 src/modules/system/domain/dto/menu_item.go | 15 +++ src/modules/system/domain/menu.go | 18 ++-- src/modules/system/rest/auth.go | 6 +- src/modules/system/rest/menu.go | 91 ++++++++++++++++++- .../{system.user.api.js => system.menu.js} | 7 +- .../{system.router.api.js => system.user.js} | 0 web/src/router/routes.js | 2 +- .../store/modules/d2admin/modules/account.js | 2 +- web/src/store/modules/d2admin/modules/user.js | 2 +- 18 files changed, 163 insertions(+), 29 deletions(-) create mode 100644 application-macDev.yml rename application-dev.yml => application-windowDev.yml (100%) create mode 100644 src/modules/system/dao/menu.go rename src/modules/system/{rest => domain}/dto/kickout.go (100%) rename src/modules/system/{rest => domain}/dto/login.go (100%) create mode 100644 src/modules/system/domain/dto/menu_item.go rename web/src/api/modules/{system.user.api.js => system.menu.js} (56%) rename web/src/api/modules/{system.router.api.js => system.user.js} (100%) diff --git a/application-macDev.yml b/application-macDev.yml new file mode 100644 index 0000000..1508b29 --- /dev/null +++ b/application-macDev.yml @@ -0,0 +1,17 @@ +server: + port: 8001 +datasource: + host: 127.0.0.1 + port: 3306 + username: root + password: 123456 + database: d2admin +redis: + host: 127.0.0.1 + port: 6379 + password: 123456 + database: 1 + pool: + size: 1024 +jwt: + secret: 123456 diff --git a/application-dev.yml b/application-windowDev.yml similarity index 100% rename from application-dev.yml rename to application-windowDev.yml diff --git a/application.yml b/application.yml index c166b54..0527fed 100644 --- a/application.yml +++ b/application.yml @@ -1,2 +1,2 @@ -active: dev +active: macDev debug: true \ No newline at end of file diff --git a/src/infra/gin.go b/src/infra/gin.go index 92b22d6..b90ff1e 100644 --- a/src/infra/gin.go +++ b/src/infra/gin.go @@ -55,7 +55,7 @@ func RunServer() { apiGroup := r.Group("/api/v1") apiGroup.Use(jwt.AuthMiddleware()) { - routes := dao.ApiDao{}.GetAllRouter() + routes := dao.ApiDao{}.GetAllApi() for _, route := range routes { if route.ApiMethod == "GET" { apiGroup.GET(route.ApiPath, InnerRouters[route.ApiName]) diff --git a/src/modules/system/dao/api.go b/src/modules/system/dao/api.go index 0a9ba59..027fbfb 100644 --- a/src/modules/system/dao/api.go +++ b/src/modules/system/dao/api.go @@ -8,9 +8,9 @@ import ( type ApiDao struct { } -func (ApiDao) GetAllRouter() []domain.Api { - var routers []domain.Api - // select * from system_router where router_status = 1 - database.DB.Model(domain.Api{ApiStatus: 1}).Find(&routers) - return routers +func (ApiDao) GetAllApi() []domain.Api { + var apis []domain.Api + // select * from system_api where api_status = 1 + database.DB.Model(domain.Api{ApiStatus: 1}).Find(&apis) + return apis } diff --git a/src/modules/system/dao/menu.go b/src/modules/system/dao/menu.go new file mode 100644 index 0000000..b78708d --- /dev/null +++ b/src/modules/system/dao/menu.go @@ -0,0 +1,16 @@ +package dao + +import ( + "d2-admin-service/src/infra/database" + "d2-admin-service/src/modules/system/domain" +) + +type MenuDao struct { +} + +func (MenuDao) GetAllMenu() []domain.Menu { + var menus []domain.Menu + // select * from system_menu + database.DB.Find(&menus) + return menus +} diff --git a/src/modules/system/domain/api.go b/src/modules/system/domain/api.go index 06d6136..cc416b1 100644 --- a/src/modules/system/domain/api.go +++ b/src/modules/system/domain/api.go @@ -8,7 +8,7 @@ type Api struct { ApiPath string `gorm:"not null;unique_index:index_npm"` // 接口路径, 例如: /api/v1/getUser ApiMethod string `gorm:"not null;unique_index:index_npm"` // GET、POST ApiDesc string `gorm:"not null"` // 接口说明 - ApiStatus int `gorm:"not null"` // 接口是否可用 + ApiStatus uint `gorm:"not null"` // 接口是否可用 } func (Api) TableName() string { diff --git a/src/modules/system/rest/dto/kickout.go b/src/modules/system/domain/dto/kickout.go similarity index 100% rename from src/modules/system/rest/dto/kickout.go rename to src/modules/system/domain/dto/kickout.go diff --git a/src/modules/system/rest/dto/login.go b/src/modules/system/domain/dto/login.go similarity index 100% rename from src/modules/system/rest/dto/login.go rename to src/modules/system/domain/dto/login.go diff --git a/src/modules/system/domain/dto/menu_item.go b/src/modules/system/domain/dto/menu_item.go new file mode 100644 index 0000000..2879685 --- /dev/null +++ b/src/modules/system/domain/dto/menu_item.go @@ -0,0 +1,15 @@ +package dto + +type MenuItem struct { + ID uint `json:"id"` + MenuParentId uint `json:"menuParentId"` + MenuTitle string `json:"menuTitle"` + MenuIcon string `json:"menuIcon"` + MenuPath string `json:"menuPath"` + RouterPath string `json:"routerPath"` + RouterName string `json:"routerName"` + RouterAuth uint `json:"routerAuth"` + RouterHidden uint `json:"routerHidden"` + RouterComponentPath string `json:"routerComponentPath"` + Children []*MenuItem `json:"children"` // 子菜单项 +} diff --git a/src/modules/system/domain/menu.go b/src/modules/system/domain/menu.go index 0f2f833..f9b666a 100644 --- a/src/modules/system/domain/menu.go +++ b/src/modules/system/domain/menu.go @@ -4,15 +4,15 @@ import "github.com/jinzhu/gorm" type Menu struct { gorm.Model - MenuParentId int `gorm:"null"` - MenuTitle string `gorm:"not null"` - MenuIcon string `gorm:"not null;default:''"` - MenuPath string `gorm:"not null"` - RouterPath string `gorm:"not null"` - RouterName string `gorm:"not null"` - RouterAuth int `gorm:"not null"` - RouterHidden int `gorm:"not null"` - RouterComponentPath string `gorm:"not null"` + MenuParentId uint `gorm:"null" json:"menuParentId"` + MenuTitle string `gorm:"not null" json:"menuTitle"` + MenuIcon string `gorm:"not null;default:''" json:"menuIcon"` + MenuPath string `gorm:"not null" json:"menuPath"` + RouterPath string `gorm:"not null" json:"routerPath"` + RouterName string `gorm:"not null" json:"routerName"` + RouterAuth uint `gorm:"not null" json:"routerAuth"` + RouterHidden uint `gorm:"not null" json:"routerHidden"` + RouterComponentPath string `gorm:"not null" json:"routerComponentPath"` } func (Menu) TableName() string { diff --git a/src/modules/system/rest/auth.go b/src/modules/system/rest/auth.go index cb0efe8..e1303f6 100644 --- a/src/modules/system/rest/auth.go +++ b/src/modules/system/rest/auth.go @@ -7,7 +7,7 @@ import ( "d2-admin-service/src/infra/redistool" "d2-admin-service/src/infra/resp" "d2-admin-service/src/modules/system/dao" - "d2-admin-service/src/modules/system/rest/dto" + dto2 "d2-admin-service/src/modules/system/domain/dto" "github.com/gin-gonic/gin" "net/http" "time" @@ -21,7 +21,7 @@ func (AuthController) Login(c *gin.Context) { userDao := dao.UserDao{} // 声明接收的变量 - var json dto.LoginDTO + var json dto2.LoginDTO // 将request的body中的数据,自动按照json格式解析到结构体 if err := c.ShouldBindJSON(&json); err != nil { c.JSON(http.StatusBadRequest, resp.ResolveParamsError) @@ -62,7 +62,7 @@ func (AuthController) KickOut(c *gin.Context) { c.JSON(http.StatusBadRequest, resp.NoOperationPermissionError) return } - var json dto.KickOutDTO + var json dto2.KickOutDTO if err := c.ShouldBindJSON(&json); err != nil { c.JSON(http.StatusBadRequest, resp.ResolveParamsError) return diff --git a/src/modules/system/rest/menu.go b/src/modules/system/rest/menu.go index 85974ff..0475d00 100644 --- a/src/modules/system/rest/menu.go +++ b/src/modules/system/rest/menu.go @@ -1,11 +1,98 @@ package rest -import "github.com/gin-gonic/gin" +import ( + "d2-admin-service/src/infra/resp" + "d2-admin-service/src/modules/system/dao" + "d2-admin-service/src/modules/system/domain/dto" + "github.com/gin-gonic/gin" + "net/http" +) type MenuController struct { } -func (MenuController) TreeMenu(c *gin.Context) {} +func (MenuController) TreeMenu(c *gin.Context) { + menuDao := dao.MenuDao{} + menus := menuDao.GetAllMenu() + + var newMenus []dto.MenuItem + for _, menu := range menus { + menuItem := dto.MenuItem{} + menuItem.ID = menu.ID + menuItem.MenuParentId = menu.MenuParentId + menuItem.MenuTitle = menu.MenuTitle + menuItem.MenuIcon = menu.MenuIcon + menuItem.MenuPath = menu.MenuPath + menuItem.RouterAuth = menu.RouterAuth + menuItem.RouterHidden = menu.RouterHidden + menuItem.RouterName = menu.RouterName + menuItem.RouterPath = menu.RouterPath + menuItem.RouterComponentPath = menu.RouterComponentPath + newMenus = append(newMenus, menuItem) + } + c.JSON(http.StatusOK, resp.Success(buildTree(newMenus))) +} func (MenuController) CreateMenu(c *gin.Context) {} func (MenuController) DeleteMenu(c *gin.Context) {} func (MenuController) ModifyMenu(c *gin.Context) {} + +// 递归构建无限级树结构 +func buildTree(menuItems []dto.MenuItem) []*dto.MenuItem { + var rootNodes []*dto.MenuItem + + for _, item := range menuItems { + if item.MenuParentId == 0 { // 根节点 + rootNodes = append(rootNodes, &item) + } else { + // 查找并插入到相应父节点的子菜单中 + insertIntoParent(&item, menuItems) + } + } + + // 对每个根节点进行递归处理其子节点 + for i := range rootNodes { + rootNodes[i].Children = buildChildren(rootNodes[i], menuItems) + } + + return rootNodes +} + +// 将一个菜单项插入到其父节点的子菜单列表中(如果父节点已存在于菜单列表中) +func insertIntoParent(item *dto.MenuItem, menuItems []dto.MenuItem) { + for i := range menuItems { + if menuItems[i].ID == item.MenuParentId { + menuItems[i].Children = append(menuItems[i].Children, item) + return + } + } +} + +// 递归构建某个节点的所有子节点 +func buildChildren(parent *dto.MenuItem, menuItems []dto.MenuItem) []*dto.MenuItem { + var children []*dto.MenuItem + processedIds := make(map[uint]bool) + + for _, item := range menuItems { + if item.MenuParentId == parent.ID && !processedIds[item.ID] { // 防止重复和无限递归 + child := item + processedIds[child.ID] = true // 标记为已处理 + + // 使用剩余未处理的菜单项构建子节点 + child.Children = buildChildren(&child, filterUnprocessed(menuItems, processedIds)) // 递归处理子节点 + children = append(children, &child) + } + } + + return children +} + +// 创建一个新的切片,其中不包含已处理过的菜单项 +func filterUnprocessed(menuItems []dto.MenuItem, processedIds map[uint]bool) []dto.MenuItem { + var unprocessedItems []dto.MenuItem + for _, item := range menuItems { + if !processedIds[item.ID] { + unprocessedItems = append(unprocessedItems, item) + } + } + return unprocessedItems +} diff --git a/web/src/api/modules/system.user.api.js b/web/src/api/modules/system.menu.js similarity index 56% rename from web/src/api/modules/system.user.api.js rename to web/src/api/modules/system.menu.js index ed24bac..3be13cb 100644 --- a/web/src/api/modules/system.user.api.js +++ b/web/src/api/modules/system.menu.js @@ -1,12 +1,11 @@ export default ({ service, request, tools }) => ({ /** - * @description 登录 - * @param {Object} data 登录携带的信息 + * @description 获取所有菜单 */ - login (data = {}) { + queryAllMenus (data = {}) { // 接口请求 return request({ - url: '/login', + url: '/queryAllMenus', method: 'post', data }) diff --git a/web/src/api/modules/system.router.api.js b/web/src/api/modules/system.user.js similarity index 100% rename from web/src/api/modules/system.router.api.js rename to web/src/api/modules/system.user.js diff --git a/web/src/router/routes.js b/web/src/router/routes.js index 522e59f..0d100bf 100644 --- a/web/src/router/routes.js +++ b/web/src/router/routes.js @@ -74,7 +74,7 @@ const frameIn = [ auth: true }, component: _import('demo/page3') - }, + } ] } ] diff --git a/web/src/store/modules/d2admin/modules/account.js b/web/src/store/modules/d2admin/modules/account.js index 0ab503e..7f07f86 100644 --- a/web/src/store/modules/d2admin/modules/account.js +++ b/web/src/store/modules/d2admin/modules/account.js @@ -27,7 +27,7 @@ export default { util.cookies.set('username', res.username) util.cookies.set('name', res.name) util.cookies.set('token', res.token) - console.log("=============== 设置用户token") + console.log('=============== 设置用户token') // 设置 vuex 用户信息 await dispatch('d2admin/user/set', { name: res.name }, { root: true }) // 用户登录后从持久化数据加载一系列的设置 diff --git a/web/src/store/modules/d2admin/modules/user.js b/web/src/store/modules/d2admin/modules/user.js index b4048e8..08788fa 100644 --- a/web/src/store/modules/d2admin/modules/user.js +++ b/web/src/store/modules/d2admin/modules/user.js @@ -20,7 +20,7 @@ export default { value: info, user: true }, { root: true }) - console.log("================ 配置用户信息") + console.log('================ 配置用户信息') }, /** * @description 从数据库取用户数据