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.

146 lines
3.7 KiB

2 years ago
package admin
import (
"encoding/json"
"github.com/beego/beego/v2/core/logs"
"github.com/beego/beego/v2/core/validation"
"strconv"
"viviman.top/controllers"
"viviman.top/dto"
"viviman.top/models"
"viviman.top/vo"
)
2 years ago
// RoleController 角色 API
2 years ago
type RoleController struct {
controllers.BaseController
}
func (c *RoleController) URLMapping() {
c.Mapping("Post", c.Post)
c.Mapping("GetAll", c.GetAll)
c.Mapping("Put", c.Put)
c.Mapping("Delete", c.Delete)
}
2 years ago
// GetOne @Title 获取单个角色
2 years ago
// @Description 获取单个角色
// @Param id path int true "角色ID"
// @Success 200 {object} models.Role
// @router /:id [get]
func (c *RoleController) GetOne() {
id := c.Ctx.Input.Param(":id")
id64, _ := strconv.ParseInt(id, 10, 64)
role := models.GetOneRole(id64)
c.Data["json"] = controllers.SuccessData(role)
2 years ago
_ = c.ServeJSON()
2 years ago
}
2 years ago
// GetAll @Title 角色列表
2 years ago
// @Description 角色列表
// @Success 200 {object} controllers.Result
// @router / [get]
func (c *RoleController) GetAll() {
total, list := models.GetAllRole(c.GetParams())
c.Data["json"] = controllers.SuccessData(vo.ResultList{Content: list, TotalElements: total})
2 years ago
_ = c.ServeJSON()
2 years ago
}
2 years ago
// Post @Title 角色添加
2 years ago
// @Description 角色添加
// @Success 200 {object} controllers.Result
// @router / [post]
func (c *RoleController) Post() {
var model models.SysRole
valid := validation.Validation{}
2 years ago
err := json.Unmarshal(c.Ctx.Input.RequestBody, &model)
if err != nil {
c.Data["json"] = controllers.ErrMsg("转译时,发生异常!")
_ = c.ServeJSON()
return
}
2 years ago
b, _ := valid.Valid(&model)
if !b {
for _, err := range valid.Errors {
c.Data["json"] = controllers.ErrMsg(err.Message)
}
}
_, e := models.AddRole(&model)
if e != nil {
c.Data["json"] = controllers.ErrMsg(e.Error())
}
c.Data["json"] = controllers.SuccessData("操作成功")
2 years ago
_ = c.ServeJSON()
2 years ago
}
2 years ago
// Put @router / [put]
2 years ago
func (c *RoleController) Put() {
var model models.SysRole
valid := validation.Validation{}
2 years ago
err := json.Unmarshal(c.Ctx.Input.RequestBody, &model)
if err != nil {
c.Data["json"] = controllers.ErrMsg("转译时,发生异常!")
_ = c.ServeJSON()
return
}
2 years ago
b, _ := valid.Valid(&model)
if !b {
for _, err := range valid.Errors {
c.Data["json"] = controllers.ErrMsg(err.Message)
}
}
e := models.UpdateByRole(&model)
if e != nil {
c.Data["json"] = controllers.ErrMsg(e.Error())
}
c.Data["json"] = controllers.SuccessData("操作成功")
2 years ago
_ = c.ServeJSON()
2 years ago
}
2 years ago
// Delete @Title 角色删除
2 years ago
// @Description 角色删除
// @Success 200 {object} controllers.Result
// @router / [delete]
func (c *RoleController) Delete() {
var ids []int64
2 years ago
err := json.Unmarshal(c.Ctx.Input.RequestBody, &ids)
if err != nil {
c.Data["json"] = controllers.ErrMsg("转译时,发生异常!")
_ = c.ServeJSON()
return
}
2 years ago
logs.Info(ids)
e := models.DelByRole(ids)
if e != nil {
c.Data["json"] = controllers.ErrMsg(e.Error())
}
c.Data["json"] = controllers.SuccessData("操作成功")
2 years ago
_ = c.ServeJSON()
2 years ago
}
2 years ago
// Menu @Title 角色菜单更新
2 years ago
// @Description 角色菜单更新
// @Success 200 {object} controllers.Result
// @router /menu [put]
func (c *RoleController) Menu() {
var model dto.RoleMenu
valid := validation.Validation{}
2 years ago
err := json.Unmarshal(c.Ctx.Input.RequestBody, &model)
if err != nil {
c.Data["json"] = controllers.ErrMsg("转译时,发生异常!")
_ = c.ServeJSON()
return
}
2 years ago
logs.Info("=======menu======")
logs.Info(model)
b, _ := valid.Valid(&model)
if !b {
for _, err := range valid.Errors {
c.Data["json"] = controllers.ErrMsg(err.Message)
}
}
models.BatchRoleMenuAdd(model)
c.Data["json"] = controllers.SuccessData("操作成功")
2 years ago
_ = c.ServeJSON()
2 years ago
}