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.

101 lines
2.5 KiB

2 years ago
package admin
import (
"encoding/json"
"github.com/beego/beego/v2/core/validation"
"viviman.top/controllers"
"viviman.top/models"
"viviman.top/vo"
)
2 years ago
// DictController 字典api
2 years ago
type DictController struct {
controllers.BaseController
}
func (c *DictController) URLMapping() {
c.Mapping("Post", c.Post)
//c.Mapping("GetOne", c.GetOne)
c.Mapping("GetAll", c.GetAll)
//c.Mapping("Put", c.Put)
//c.Mapping("Delete", c.Delete)
}
2 years ago
// GetAll @Title获取字典列表
2 years ago
// @Description 获取字典列表
// @Success 200 {object} controllers.Result
// @router / [get]
func (c *DictController) GetAll() {
total, list := models.GetAllDict(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 *DictController) Post() {
var dictModel models.SysDict
valid := validation.Validation{}
2 years ago
err := json.Unmarshal(c.Ctx.Input.RequestBody, &dictModel)
if err != nil {
c.Data["json"] = controllers.ErrMsg("转译时,发生异常!")
_ = c.ServeJSON()
return
}
2 years ago
b, _ := valid.Valid(&dictModel)
if !b {
for _, err := range valid.Errors {
c.Data["json"] = controllers.ErrMsg(err.Message)
}
}
_, e := models.AddDict(&dictModel)
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 @Title修改字典
2 years ago
// @Description 修改字典
// @Success 200 {object} controllers.Result
// @router / [put]
func (c *DictController) Put() {
var dictModel models.SysDict
valid := validation.Validation{}
2 years ago
err := json.Unmarshal(c.Ctx.Input.RequestBody, &dictModel)
if err != nil {
c.Data["json"] = controllers.ErrMsg("转译时,发生异常!")
_ = c.ServeJSON()
return
}
2 years ago
b, _ := valid.Valid(&dictModel)
if !b {
for _, err := range valid.Errors {
c.Data["json"] = controllers.ErrMsg(err.Message)
}
}
e := models.UpdateByDict(&dictModel)
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 /:id [delete]
func (c *DictController) Delete() {
id, _ := c.GetInt64(":id", 1)
e := models.DelByDict(id)
if e != nil {
c.Data["json"] = controllers.ErrMsg(e.Error())
}
c.Data["json"] = controllers.SuccessData("操作成功")
2 years ago
_ = c.ServeJSON()
2 years ago
}