You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
3.3 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package menu
import (
"github.com/gin-gonic/gin"
"recook/internal/api/manage/user"
"recook/internal/back"
"recook/internal/dbc"
"recook/internal/model/menu"
"recook/tools"
)
type createMenuParam struct {
Id uint `json:"id"`
Name string `json:"name" validate:"required"` //菜单名称
Parentid int `json:"parentid"` //父级菜单id
Path string `json:"path"` //前端跳转路径
Action string `json:"action"` //后台api路径
Icon string `json:"icon"` //图标
Component string `json:"component"` //react组件名称
Listorder int `json:"listorder"` //排序
Display string `json:"display"` //是否显示1正常2禁用
}
type deleteMenuParam struct {
ID uint `json:"id"` //需要删除的id
}
//创建一个菜单,编辑
func CreateMenu(c *gin.Context) {
var p createMenuParam
err := tools.Params(&p, c)
if err != nil {
back.Fail(c, err.Error())
return
}
//判断菜单名称是否重复
var menuData menu.RecookMenu
dbc.DB.First(&menuData, "name=? and id!=?", p.Name, p.Id)
if menuData.ID > 0 {
back.Fail(c, "菜单名称不能重复")
return
}
menuData = menu.RecookMenu{}
//判断上级id是否存在
if p.Parentid != 0 {
err = dbc.DB.First(&menuData, "id=?", p.Parentid).Error
if err != nil {
back.Fail(c, "上级id不存在")
return
}
}
if p.Id > 0 {
//正式更新
dbc.DB.Table("recook_menu").Where("id=?", p.Id).Updates(map[string]interface{}{
"name": p.Name,
"parentid": p.Parentid,
"path": p.Path,
"action": p.Action,
"icon": p.Icon,
"component": p.Component,
"listorder": p.Listorder,
"display": p.Display,
})
} else {
//正式插入数据
param := menu.RecookMenu{
Name: p.Name,
Parentid: p.Parentid,
Path: p.Path,
Action: p.Action,
Icon: p.Icon,
Component: p.Component,
Listorder: p.Listorder,
Display: p.Display,
}
dbc.DB.Create(&param)
}
back.Suc(c, "操作成功", nil)
}
//菜单的删除
func DeleteMenu(c *gin.Context) {
var p deleteMenuParam
err := tools.Params(&p, c)
if err != nil {
back.Fail(c, err.Error())
return
}
if p.ID <= 0 {
back.Fail(c, "id不能小于0")
return
}
//判断这个菜单下面是否有子菜单
var tempMenu menu.RecookMenu
dbc.DB.First(&tempMenu, "parentid=?", p.ID)
if tempMenu.ID > 0 {
//说明有子菜单,不给他删除
back.Fail(c, "该菜单下面有子菜单,无法删除")
return
}
//正式去删除
dbc.DB.Delete(menu.RecookMenu{}, "id=?", p.ID)
back.Suc(c, "删除成功", nil)
}
//菜单的查询,主订单,主id。
func MenuList(c *gin.Context) {
TreeList := getMenu(0)
back.Suc(c, "获取成功", TreeList)
}
/*
递归获取树形菜单
*/
func getMenu(pid int) []*user.TreeList {
var menu []menu.RecookMenu
dbc.DB.Where("parentid = ?", pid).Order("listorder asc").Find(&menu, "display=1")
treeList := []*user.TreeList{}
for _, v := range menu {
child := getMenu(v.ID)
node := &user.TreeList{
ID: v.ID,
Name: v.Name,
Parentid: v.Parentid,
Path: v.Path,
Action: v.Action,
Icon: v.Icon,
Component: v.Component,
Listorder: v.Listorder,
Display: v.Display,
}
node.Children = child
treeList = append(treeList, node)
}
return treeList
}