Files
ArmedPolice/app/api/role.go

157 lines
3.9 KiB
Go
Raw Normal View History

2021-11-04 15:15:11 +08:00
package api
import (
"ArmedPolice/app/controller/role"
"ArmedPolice/app/service"
"github.com/gin-gonic/gin"
)
type Role struct{}
type roleForm struct {
ParentID uint64 `json:"parent_id" form:"parent_id"`
Name string `json:"name" form:"name" binding:"required"`
Remark string `json:"remark" form:"remark" binding:"required"`
Sort int `json:"sort" form:"sort"`
}
/**
2021-11-04 15:44:35 +08:00
* @apiDefine Role 角色管理
2021-11-04 15:15:11 +08:00
*/
func (*Role) List(c *gin.Context) {
data, err := role.NewInstance()(getSession()(c).(*service.Session)).List()
APIResponse(err, data)(c)
}
func (a *Role) Add(c *gin.Context) {
form := new(roleForm)
if err := bind(form)(c); err != nil {
APIFailure(err.(error))(c)
return
}
err := role.NewInstance()(getSession()(c).(*service.Session)).Form(&role.InstanceParams{
ParentID: form.ParentID, Name: form.Name, Remark: form.Remark, Sort: form.Sort,
})
APIResponse(err)(c)
}
func (a *Role) Edit(c *gin.Context) {
form := &struct {
IDStringForm
roleForm
}{}
if err := bind(form)(c); err != nil {
APIFailure(err.(error))(c)
return
}
err := role.NewInstance()(getSession()(c).(*service.Session)).Form(&role.InstanceParams{
ID: form.Convert(), ParentID: form.ParentID, Name: form.Name, Remark: form.Remark, Sort: form.Sort,
})
APIResponse(err)(c)
}
func (a *Role) Delete(c *gin.Context) {
form := new(IDStringForm)
if err := bind(form)(c); err != nil {
APIFailure(err.(error))(c)
return
}
err := role.NewInstance()(getSession()(c).(*service.Session)).Delete(form.Convert())
APIResponse(err)(c)
}
/**
2021-11-04 15:44:35 +08:00
* @api {post} /api/v1/role/menus 角色菜单
2021-11-04 15:15:11 +08:00
* @apiVersion 1.0.0
* @apiName RoleMenu
* @apiGroup Role
*
* @apiHeader {string} x-token token
*
* @apiSuccess (200) {Number} code 成功响应状态码!
* @apiSuccess (200) {String} msg 成功提示
* @apiSuccess (200) {Array} data 具体信息
* @apiSuccess (200) {Number} data.id 菜单ID
* @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": [
* "id": 1,
* "parent_id": 0,
* "name": "系统管理",
* "kind": 1,
* "link": "1"
* "component": ""
* "children": [
* {
* "id": 2,
* "parent_id": 1,
* "name": "用户管理",
* "kind": 1,
* "link": "1"
* "component": ""
* "children": [],
* }
* ]
* ]
* }
*/
func (a *Role) Menu(c *gin.Context) {
form := &struct {
RoleID uint64 `json:"role_id" form:"role_id" binding:"required"`
}{}
if err := bind(form)(c); err != nil {
APIFailure(err.(error))(c)
return
}
data, err := role.NewMenu()(getSession()(c).(*service.Session)).List(form.RoleID)
APIResponse(err, data)(c)
}
/**
2021-11-04 15:44:35 +08:00
* @api {post} /api/v1/role/menu/bind 角色菜单绑定
2021-11-04 15:15:11 +08:00
* @apiVersion 1.0.0
* @apiName RoleMenuBind
* @apiGroup Role
*
* @apiHeader {string} x-token token
*
* @apiParam {Number} role_id 角色ID
* @apiParam {Array.Number} menu_ids 菜单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 *Role) MenuBind(c *gin.Context) {
form := &struct {
RoleID uint64 `json:"role_id" form:"role_id" binding:"required"`
MenuIDs []uint64 `json:"menu_ids" form:"menu_ids" binding:"required"`
}{}
if err := bind(form)(c); err != nil {
APIFailure(err.(error))(c)
return
}
err := role.NewMenu()(getSession()(c).(*service.Session)).Bind(form.RoleID, form.MenuIDs)
APIResponse(err)(c)
}