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.
100 lines
2.5 KiB
100 lines
2.5 KiB
package admin
|
|
|
|
import (
|
|
"encoding/json"
|
|
"github.com/beego/beego/v2/core/validation"
|
|
"viviman.top/controllers"
|
|
"viviman.top/models"
|
|
"viviman.top/vo"
|
|
)
|
|
|
|
// DictController 字典api
|
|
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)
|
|
}
|
|
|
|
// GetAll @Title获取字典列表
|
|
// @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})
|
|
_ = c.ServeJSON()
|
|
}
|
|
|
|
// Post @Title添加字典
|
|
// @Description 添加字典
|
|
// @Success 200 {object} controllers.Result
|
|
// @router / [post]
|
|
func (c *DictController) Post() {
|
|
var dictModel models.SysDict
|
|
valid := validation.Validation{}
|
|
err := json.Unmarshal(c.Ctx.Input.RequestBody, &dictModel)
|
|
if err != nil {
|
|
c.Data["json"] = controllers.ErrMsg("转译时,发生异常!")
|
|
_ = c.ServeJSON()
|
|
return
|
|
}
|
|
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("操作成功")
|
|
_ = c.ServeJSON()
|
|
}
|
|
|
|
// Put @Title修改字典
|
|
// @Description 修改字典
|
|
// @Success 200 {object} controllers.Result
|
|
// @router / [put]
|
|
func (c *DictController) Put() {
|
|
var dictModel models.SysDict
|
|
valid := validation.Validation{}
|
|
err := json.Unmarshal(c.Ctx.Input.RequestBody, &dictModel)
|
|
if err != nil {
|
|
c.Data["json"] = controllers.ErrMsg("转译时,发生异常!")
|
|
_ = c.ServeJSON()
|
|
return
|
|
}
|
|
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("操作成功")
|
|
_ = c.ServeJSON()
|
|
}
|
|
|
|
// Delete @Title删除字典
|
|
// @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("操作成功")
|
|
_ = c.ServeJSON()
|
|
}
|
|
|