ferry/apis/system/menu.go

227 lines
5.8 KiB
Go
Raw Normal View History

2020-07-13 20:33:20 +08:00
package system
import (
2020-07-14 14:07:44 +08:00
"ferry/models/system"
2020-07-13 20:33:20 +08:00
"ferry/tools"
"ferry/tools/app"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
)
2020-07-17 15:51:05 +08:00
/*
@Author : lanyulei
*/
2020-07-13 20:33:20 +08:00
// @Summary Menu列表数据
// @Description 获取JSON
// @Tags 菜单
// @Param menuName query string false "menuName"
// @Success 200 {string} string "{"code": 200, "data": [...]}"
// @Success 200 {string} string "{"code": -1, "message": "抱歉未找到相关信息"}"
// @Router /api/v1/menulist [get]
// @Security Bearer
func GetMenuList(c *gin.Context) {
2020-08-31 23:43:23 +08:00
var (
err error
Menu system.Menu
result []system.Menu
)
2020-07-13 20:33:20 +08:00
Menu.MenuName = c.Request.FormValue("menuName")
Menu.Visible = c.Request.FormValue("visible")
Menu.Title = c.Request.FormValue("title")
2020-08-31 23:43:23 +08:00
if Menu.Title == "" {
result, err = Menu.SetMenu()
} else {
result, err = Menu.GetPage()
}
2020-07-16 01:22:01 +08:00
if err != nil {
app.Error(c, -1, err, "")
return
}
2020-07-13 20:33:20 +08:00
app.OK(c, result, "")
}
// @Summary Menu列表数据
// @Description 获取JSON
// @Tags 菜单
// @Param menuName query string false "menuName"
// @Success 200 {string} string "{"code": 200, "data": [...]}"
// @Success 200 {string} string "{"code": -1, "message": "抱歉未找到相关信息"}"
// @Router /api/v1/menu [get]
// @Security Bearer
func GetMenu(c *gin.Context) {
2020-07-14 14:07:44 +08:00
var data system.Menu
2020-07-14 14:47:17 +08:00
id, _ := tools.StringToInt(c.Param("id"))
2020-07-13 20:33:20 +08:00
data.MenuId = id
result, err := data.GetByMenuId()
2020-07-16 01:22:01 +08:00
if err != nil {
app.Error(c, -1, err, "")
return
}
2020-07-13 20:33:20 +08:00
app.OK(c, result, "")
}
func GetMenuTreeRoleselect(c *gin.Context) {
2020-07-14 14:07:44 +08:00
var Menu system.Menu
var SysRole system.SysRole
2020-07-14 14:47:17 +08:00
id, _ := tools.StringToInt(c.Param("roleId"))
2020-07-13 20:33:20 +08:00
SysRole.RoleId = id
result, err := Menu.SetMenuLable()
2020-07-16 01:22:01 +08:00
if err != nil {
app.Error(c, -1, err, "")
return
}
2020-07-13 20:33:20 +08:00
menuIds := make([]int, 0)
if id != 0 {
menuIds, err = SysRole.GetRoleMeunId()
2020-07-16 01:22:01 +08:00
if err != nil {
app.Error(c, -1, err, "")
return
}
2020-07-13 20:33:20 +08:00
}
app.Custum(c, gin.H{
"code": 200,
"menus": result,
"checkedKeys": menuIds,
})
}
// @Summary 获取菜单树
// @Description 获取JSON
// @Tags 菜单
// @Accept application/x-www-form-urlencoded
// @Product application/x-www-form-urlencoded
// @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
// @Router /api/v1/menuTreeselect [get]
// @Security Bearer
func GetMenuTreeelect(c *gin.Context) {
2020-07-14 14:07:44 +08:00
var data system.Menu
2020-07-13 20:33:20 +08:00
result, err := data.SetMenuLable()
2020-07-16 01:22:01 +08:00
if err != nil {
app.Error(c, -1, err, "")
return
}
2020-07-13 20:33:20 +08:00
app.OK(c, result, "")
}
// @Summary 创建菜单
// @Description 获取JSON
// @Tags 菜单
// @Accept application/x-www-form-urlencoded
// @Product application/x-www-form-urlencoded
// @Param menuName formData string true "menuName"
// @Param Path formData string false "Path"
// @Param Action formData string true "Action"
// @Param Permission formData string true "Permission"
// @Param ParentId formData string true "ParentId"
// @Param IsDel formData string true "IsDel"
// @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
// @Router /api/v1/menu [post]
// @Security Bearer
func InsertMenu(c *gin.Context) {
2020-07-14 14:07:44 +08:00
var data system.Menu
2020-07-13 20:33:20 +08:00
err := c.BindWith(&data, binding.JSON)
2020-07-16 01:22:01 +08:00
if err != nil {
app.Error(c, -1, err, "")
return
}
2020-07-13 20:33:20 +08:00
data.CreateBy = tools.GetUserIdStr(c)
result, err := data.Create()
2020-07-16 01:22:01 +08:00
if err != nil {
app.Error(c, -1, err, "")
return
}
2020-07-13 20:33:20 +08:00
app.OK(c, result, "")
}
// @Summary 修改菜单
// @Description 获取JSON
// @Tags 菜单
// @Accept application/x-www-form-urlencoded
// @Product application/x-www-form-urlencoded
// @Param id path int true "id"
2022-10-25 18:16:04 +08:00
// @Param data body system.Menu true "body"
2020-07-13 20:33:20 +08:00
// @Success 200 {string} string "{"code": 200, "message": "修改成功"}"
// @Success 200 {string} string "{"code": -1, "message": "修改失败"}"
// @Router /api/v1/menu/{id} [put]
// @Security Bearer
func UpdateMenu(c *gin.Context) {
2020-07-14 14:07:44 +08:00
var data system.Menu
2020-07-16 01:22:01 +08:00
err := c.BindWith(&data, binding.JSON)
if err != nil {
app.Error(c, -1, err, "")
return
}
2020-07-13 20:33:20 +08:00
data.UpdateBy = tools.GetUserIdStr(c)
2020-07-16 01:22:01 +08:00
_, err = data.Update(data.MenuId)
if err != nil {
app.Error(c, -1, err, "")
return
}
2020-07-13 20:33:20 +08:00
app.OK(c, "", "修改成功")
}
// @Summary 删除菜单
// @Description 删除数据
// @Tags 菜单
// @Param id path int true "id"
// @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
// @Success 200 {string} string "{"code": -1, "message": "删除失败"}"
// @Router /api/v1/menu/{id} [delete]
func DeleteMenu(c *gin.Context) {
2020-07-14 14:07:44 +08:00
var data system.Menu
2020-07-14 14:47:17 +08:00
id, _ := tools.StringToInt(c.Param("id"))
2020-07-16 01:22:01 +08:00
2020-07-13 20:33:20 +08:00
data.UpdateBy = tools.GetUserIdStr(c)
2020-07-14 14:47:17 +08:00
_, err := data.Delete(id)
2020-07-16 01:22:01 +08:00
if err != nil {
app.Error(c, -1, err, "")
return
}
2020-07-13 20:33:20 +08:00
app.OK(c, "", "删除成功")
}
// @Summary 根据角色名称获取菜单列表数据(左菜单使用)
// @Description 获取JSON
// @Tags 菜单
// @Param id path int true "id"
// @Success 200 {string} string "{"code": 200, "data": [...]}"
// @Success 200 {string} string "{"code": -1, "message": "抱歉未找到相关信息"}"
// @Router /api/v1/menurole [get]
// @Security Bearer
func GetMenuRole(c *gin.Context) {
2020-07-14 14:07:44 +08:00
var Menu system.Menu
2020-07-13 20:33:20 +08:00
result, err := Menu.SetMenuRole(tools.GetRoleName(c))
2020-07-16 01:22:01 +08:00
if err != nil {
app.Error(c, -1, err, "")
return
}
2020-07-13 20:33:20 +08:00
app.OK(c, result, "")
}
// @Summary 获取角色对应的菜单id数组
// @Description 获取JSON
// @Tags 菜单
// @Param id path int true "id"
// @Success 200 {string} string "{"code": 200, "data": [...]}"
// @Success 200 {string} string "{"code": -1, "message": "抱歉未找到相关信息"}"
// @Router /api/v1/menuids/{id} [get]
// @Security Bearer
func GetMenuIDS(c *gin.Context) {
2020-07-14 14:07:44 +08:00
var data system.RoleMenu
2020-07-13 20:33:20 +08:00
data.RoleName = c.GetString("role")
data.UpdateBy = tools.GetUserIdStr(c)
result, err := data.GetIDS()
2020-07-16 01:22:01 +08:00
if err != nil {
app.Error(c, -1, err, "")
return
}
2020-07-13 20:33:20 +08:00
app.OK(c, result, "")
}