123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- package controllers
- import (
- "cc-officialweb/models"
- "cc-officialweb/unity"
- "encoding/json"
- beego "github.com/beego/beego/v2/server/web"
- "github.com/go-playground/validator/v10"
- "strconv"
- )
- type LinksController struct {
- beego.Controller
- }
- // AddLinks 添加友情链接
- func (l *LinksController) AddLinks() {
- var linksDto models.LinkDto
- var link models.Link
- err := json.Unmarshal(l.Ctx.Input.RequestBody, &linksDto)
- if err != nil {
- l.Data["json"] = &JSON{Code: 101, Msg: "json解析失败"}
- l.ServeJSON()
- return
- }
- link.Title = linksDto.Title
- link.Url = linksDto.Url
- link.Sort = linksDto.Sort
- _, err = unity.Add(&link)
- if err != nil {
- l.Data["json"] = &JSON{Code: 101, Msg: "添加失败"}
- l.ServeJSON()
- return
- } else {
- l.Data["json"] = &JSON{Code: 200, Msg: "添加成功", Data: nil}
- l.ServeJSON()
- }
- }
- // UpdateLinks 更新友情链接
- func (l *LinksController) UpdateLinks() {
- var linksDto models.LinkDto
- var link models.Link
- err := json.Unmarshal(l.Ctx.Input.RequestBody, &linksDto)
- if err != nil {
- l.Data["json"] = &JSON{Code: 101, Msg: "json解析失败"}
- l.ServeJSON()
- return
- }
- link.Title = linksDto.Title
- link.Url = linksDto.Url
- link.Sort = linksDto.Sort
- _, err = unity.UpdateById(linksDto.ID, &link)
- if err != nil {
- l.Data["json"] = &JSON{Code: 102, Msg: err.Error()}
- l.ServeJSON()
- return
- } else {
- l.Data["json"] = &JSON{Code: 200, Msg: "修改成功"}
- l.ServeJSON()
- }
- }
- // DeleteLinksById 根据id删除产品
- func (l *LinksController) DeleteLinksById() {
- id := l.GetString("id")
- atoi, _ := strconv.Atoi(id)
- validate := validator.New()
- err2 := validate.Var(atoi, "required")
- if err2 != nil {
- l.Data["json"] = &JSON{Code: 103, Msg: "id不能为空"}
- l.ServeJSON()
- return
- }
- _, err := unity.DeleteById(atoi, models.Link{})
- if err != nil {
- l.Data["json"] = map[string]interface{}{"code": 101, "msg": "删除失败"}
- l.ServeJSON()
- return
- } else {
- l.Data["json"] = map[string]interface{}{"code": 200, "msg": "删除成功"}
- l.ServeJSON()
- }
- }
- // GetAllLinks 获取所有产品信息
- func (l *LinksController) GetAllLinks() {
- var page unity.PageParams
- err := json.Unmarshal(l.Ctx.Input.RequestBody, &page)
- if err != nil {
- l.Data["json"] = &JSON{Code: 101, Msg: "参数错误"}
- l.ServeJSON()
- return
- }
- result, total, current, err := unity.Paginate(page, models.Link{})
- if err != nil {
- l.Data["json"] = &JSON{Code: 101, Msg: "查询失败"}
- l.ServeJSON()
- return
- } else {
- l.Data["json"] = &JSON{Code: 200, Msg: "查询成功", Data: &JSONS{
- Current: current,
- Size: total,
- Data: result,
- }}
- l.ServeJSON()
- return
- }
- }
|