Files
ArmedPolice/app/api/menu.go

237 lines
6.3 KiB
Go
Raw Normal View History

2021-11-03 10:24:37 +08:00
package api
2021-11-04 15:15:11 +08:00
import (
"ArmedPolice/app/controller/menu"
"ArmedPolice/app/service"
"github.com/gin-gonic/gin"
)
2021-11-03 10:24:37 +08:00
type Menu struct{}
2021-11-04 15:15:11 +08:00
type (
// menuForm 菜单信息
menuForm struct {
ParentID uint64 `json:"parent_id" form:"parent_id"`
Name string `json:"name" form:"name" binding:"required"`
Kind int `json:"kind" form:"kind" binding:"required"`
Link string `json:"link" form:"link"`
Component string `json:"component" form:"component"`
Icon string `json:"icon" form:"icon"`
Auth int `json:"auth" form:"auth"`
Sort int `json:"sort" form:"sort"`
Status int `json:"status" form:"status"`
Remark string `json:"remark" form:"remark"`
}
)
/**
* @apiDefine Menu 菜单管理
*/
/**
* @api {get} /api/v1/menu/list 菜单列表
* @apiVersion 1.0.0
* @apiName MenuList
* @apiGroup Menu
*
* @apiHeader {string} x-token token
*
* @apiSuccess (200) {Number} code 成功响应状态码!
* @apiSuccess (200) {String} msg 成功提示
* @apiSuccess (200) {Array} data 具体信息
2021-11-08 15:52:46 +08:00
* @apiSuccess (200) {String} data.id 菜单ID
2021-11-04 15:15:11 +08:00
* @apiSuccess (200) {Number} data.parent_id 父级ID
* @apiSuccess (200) {String} data.name 菜单名称
* @apiSuccess (200) {Number} data.kind 类型1目录2菜单
* @apiSuccess (200) {String} data.link 访问地址
* @apiSuccess (200) {String} data.component 组件
* @apiSuccess (200) {Array} data.children="[]" 子集
*
* @apiSuccessExample {json} Success response:
* HTTPS 200 OK
* {
* "code": 200
* "msg": "ok"
* "data": [
2021-11-08 15:52:46 +08:00
* "id": "qeqwe",
2021-11-04 15:15:11 +08:00
* "parent_id": 0,
* "name": "系统管理",
* "kind": 1,
* "link": "1"
* "component": ""
* "children": [
* {
2021-11-08 15:52:46 +08:00
* "id": "23123asqw",
2021-11-04 15:15:11 +08:00
* "parent_id": 1,
* "name": "用户管理",
* "kind": 1,
* "link": "1"
* "component": ""
* "children": [],
* }
* ]
* ]
* }
*/
func (a *Menu) List(c *gin.Context) {
data, err := menu.NewInstance()(getSession()(c).(*service.Session)).List()
APIResponse(err, data)(c)
}
/**
* @api {post} /api/v1/menu/add 菜单添加
* @apiVersion 1.0.0
* @apiName MenuAdd
* @apiGroup Menu
*
* @apiHeader {string} x-token token
*
* @apiParam {Number} parent_id 父级ID
* @apiParam {String} name 菜单名
* @apiParam {Number} kind 菜单类型1目录2菜单
* @apiParam {String} link 访问地址
* @apiParam {String} component 页面组件
* @apiParam {String} icon 页面icon
* @apiParam {Number} auth 菜单权限0普通权限1超管权限
* @apiParam {Number} sort 排序从大到小
* @apiParam {Number} status 忘了干嘛没用
* @apiParam {String} remark 备注信息
*
* @apiSuccess (200) {Number} code 成功响应状态码!
* @apiSuccess (200) {String} msg 成功提示
*
* @apiSuccessExample {json} Success response:
* HTTPS 200 OK
* {
* "code": 200
* "msg": "ok"
* }
*/
func (a *Menu) Add(c *gin.Context) {
form := new(menuForm)
if err := bind(form)(c); err != nil {
APIFailure(err.(error))(c)
return
}
err := menu.NewInstance()(getSession()(c).(*service.Session)).Form(&menu.InstanceParams{
ParentID: form.ParentID, Name: form.Name, Kind: form.Kind, Link: form.Link, Component: form.Component,
Icon: form.Icon, Auth: form.Auth, Sort: form.Sort, Status: form.Status, Remark: form.Remark,
})
APIResponse(err)(c)
}
/**
* @api {post} /api/v1/menu/edit 菜单修改
* @apiVersion 1.0.0
* @apiName MenuEdit
* @apiGroup Menu
*
* @apiHeader {string} x-token token
*
* @apiParam {Number} id ID
* @apiParam {Number} parent_id 父级ID
* @apiParam {String} name 菜单名
* @apiParam {Number} kind 菜单类型1目录2菜单
* @apiParam {String} link 访问地址
* @apiParam {String} component 页面组件
* @apiParam {String} icon 页面icon
* @apiParam {Number} auth 菜单权限0普通权限1超管权限
* @apiParam {Number} sort 排序从大到小
* @apiParam {Number} status 禁用状态1启用2禁用
* @apiParam {String} remark 备注信息
*
* @apiSuccess (200) {Number} code 成功响应状态码!
* @apiSuccess (200) {String} msg 成功提示
*
* @apiSuccessExample {json} Success response:
* HTTPS 200 OK
* {
* "code": 200
* "msg": "ok"
* }
*/
func (a *Menu) Edit(c *gin.Context) {
form := &struct {
IDStringForm
menuForm
}{}
if err := bind(form)(c); err != nil {
APIFailure(err.(error))(c)
return
}
err := menu.NewInstance()(getSession()(c).(*service.Session)).Form(&menu.InstanceParams{
ID: form.Convert(), ParentID: form.ParentID, Name: form.Name, Kind: form.Kind, Link: form.Link, Component: form.Component,
Icon: form.Icon, Auth: form.Auth, Sort: form.Sort, Status: form.Status, Remark: form.Remark,
})
APIResponse(err)(c)
}
/**
* @api {post} /api/v1/menu/status 菜单状态
* @apiVersion 1.0.0
* @apiName MenuStatus
* @apiGroup Menu
*
* @apiHeader {string} x-token token
*
* @apiParam {Number} id ID
* @apiParam {Number} status 状态1启动2禁用
*
* @apiSuccess (200) {Number} code=200 成功响应状态码!
* @apiSuccess (200) {String} msg="ok" 成功提示
*
* @apiSuccessExample {json} Success response:
* HTTPS 200 OK
* {
* "code": 200
* "msg": "ok"
* "data": null
* }
*/
func (a *Menu) Status(c *gin.Context) {
form := &struct {
IDStringForm
Status int `json:"status" form:"status" binding:"required"`
}{}
if err := bind(form)(c); err != nil {
APIFailure(err.(error))(c)
return
}
err := menu.NewInstance()(getSession()(c).(*service.Session)).Status(form.Convert(), form.Status)
APIResponse(err)(c)
}
/**
* @api {post} /api/v1/menu/delete 删除菜单
* @apiVersion 1.0.0
* @apiName MenuDelete
* @apiGroup Menu
*
* @apiHeader {string} x-token token
*
* @apiParam {Number} id ID
*
* @apiSuccess (200) {Number} code=200 成功响应状态码!
* @apiSuccess (200) {String} msg="ok" 成功提示
*
* @apiSuccessExample {json} Success response:
* HTTPS 200 OK
* {
* "code": 200
* "msg": "ok"
* "data": null
* }
*/
func (a *Menu) Delete(c *gin.Context) {
form := new(IDStringForm)
if err := bind(form)(c); err != nil {
APIFailure(err.(error))(c)
return
}
err := menu.NewInstance()(getSession()(c).(*service.Session)).Delete(form.Convert())
APIResponse(err)(c)
}