Files
easygoadmin/app/controller/link.go
yaoyilin dbd3c3c247 feat: 封装为包
封装为包
2022-10-31 23:55:00 +08:00

210 lines
4.1 KiB
Go

/**
* 友链管理-控制器
* @author
* @since 2021/11/13
* @File : link
*/
package controller
import (
"github.com/gin-gonic/gin"
"gitlab.52pay.top/go/easygoadmin/app/dto"
"gitlab.52pay.top/go/easygoadmin/app/model"
"gitlab.52pay.top/go/easygoadmin/app/service"
"gitlab.52pay.top/go/easygoadmin/utils"
"gitlab.52pay.top/go/easygoadmin/utils/common"
"gitlab.52pay.top/go/easygoadmin/utils/gconv"
"gitlab.52pay.top/go/easygoadmin/utils/response"
"net/http"
)
var Link = new(linkCtl)
type linkCtl struct{}
func (c *linkCtl) Index(ctx *gin.Context) {
// 渲染模板
response.BuildTpl(ctx, "link_index.html").WriteTpl(gin.H{
"typeList": common.LINK_TYPE_LIST,
"platformList": common.LINK_PLATFORM_LIST,
})
}
func (c *linkCtl) List(ctx *gin.Context) {
// 参数
var req *dto.LinkPageReq
if err := ctx.ShouldBind(&req); err != nil {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 调用分页查询方法
list, count, err := service.Link.GetList(req)
if err != nil {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 返回结果
ctx.JSON(http.StatusOK, common.JsonResult{
Code: 0,
Msg: "查询成功",
Data: list,
Count: count,
})
}
func (c *linkCtl) Edit(ctx *gin.Context) {
id := gconv.Int(ctx.Query("id"))
if id > 0 {
// 修改
info := &model.Link{Id: id}
has, err := info.Get()
if err != nil || !has {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 友链图片
if info.Image != "" {
info.Image = utils.GetImageUrl(info.Image)
}
// 渲染模板
response.BuildTpl(ctx, "link_edit.html").WriteTpl(gin.H{
"info": info,
"typeList": common.LINK_TYPE_LIST,
"formList": common.LINK_FORM_LIST,
"platformList": common.LINK_PLATFORM_LIST,
})
} else {
// 添加
response.BuildTpl(ctx, "link_edit.html").WriteTpl(gin.H{
"typeList": common.LINK_TYPE_LIST,
"formList": common.LINK_FORM_LIST,
"platformList": common.LINK_PLATFORM_LIST,
})
}
}
func (c *linkCtl) Add(ctx *gin.Context) {
// 参数
var req *dto.LinkAddReq
if err := ctx.ShouldBind(&req); err != nil {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 调用添加方法
rows, err := service.Link.Add(req, utils.Uid(ctx))
if err != nil || rows == 0 {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 返回结构
ctx.JSON(http.StatusOK, common.JsonResult{
Code: 0,
Msg: "添加成功",
})
}
func (c *linkCtl) Update(ctx *gin.Context) {
// 参数
var req *dto.LinkUpdateReq
if err := ctx.ShouldBind(&req); err != nil {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 调用更新方法
rows, err := service.Link.Update(req, utils.Uid(ctx))
if err != nil || rows == 0 {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 返回结果
ctx.JSON(http.StatusOK, common.JsonResult{
Code: 0,
Msg: "更新成功",
})
}
func (c *linkCtl) Delete(ctx *gin.Context) {
// 记录ID
ids := ctx.Param("ids")
if ids == "" {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: "记录ID不能为空",
})
return
}
// 调用删除方法
rows, err := service.Link.Delete(ids)
if err != nil || rows == 0 {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 返回结果
ctx.JSON(http.StatusOK, common.JsonResult{
Code: 0,
Msg: "删除成功",
})
}
func (c *linkCtl) Status(ctx *gin.Context) {
// 参数
var req *dto.LinkStatusReq
if err := ctx.ShouldBind(&req); err != nil {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 调用设置方法
rows, err := service.Link.Status(req, utils.Uid(ctx))
if err != nil || rows == 0 {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 返回结果
ctx.JSON(http.StatusOK, common.JsonResult{
Code: 0,
Msg: "设置成功",
})
}