mirror of
https://gitlab.52pay.top/go/easygoadmin.git
synced 2025-09-26 22:47:58 +08:00
216 lines
4.0 KiB
Go
216 lines
4.0 KiB
Go
/**
|
|
* 广告管理-控制器
|
|
* @author
|
|
* @since 2021/11/15
|
|
* @File : ad
|
|
*/
|
|
package controller
|
|
|
|
import (
|
|
"easygoadmin/app/dto"
|
|
"easygoadmin/app/model"
|
|
"easygoadmin/app/service"
|
|
"easygoadmin/utils"
|
|
"easygoadmin/utils/common"
|
|
"easygoadmin/utils/gconv"
|
|
"easygoadmin/utils/response"
|
|
"github.com/gin-gonic/gin"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
var Ad = new(adCtl)
|
|
|
|
type adCtl struct{}
|
|
|
|
func (c *adCtl) Index(ctx *gin.Context) {
|
|
// 渲染模板
|
|
response.BuildTpl(ctx, "ad_index.html").WriteTpl()
|
|
}
|
|
|
|
func (c *adCtl) List(ctx *gin.Context) {
|
|
// 参数
|
|
var req *dto.AdPageReq
|
|
if err := ctx.ShouldBind(&req); err != nil {
|
|
ctx.JSON(http.StatusOK, common.JsonResult{
|
|
Code: -1,
|
|
Msg: err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
// 调用分页查询方法
|
|
list, count, err := service.Ad.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 *adCtl) Edit(ctx *gin.Context) {
|
|
// 查询记录
|
|
id := gconv.Int(ctx.Query("id"))
|
|
if id > 0 {
|
|
info := &model.Ad{Id: id}
|
|
has, err := info.Get()
|
|
if !has || err != nil {
|
|
ctx.JSON(http.StatusOK, common.JsonResult{
|
|
Code: -1,
|
|
Msg: err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
// 广告图片
|
|
if info.Cover != "" {
|
|
info.Cover = utils.GetImageUrl(info.Cover)
|
|
}
|
|
|
|
// 富文本图片替换处理
|
|
if info.Content != "" {
|
|
info.Content = strings.ReplaceAll(info.Content, "[IMG_URL]", utils.ImageUrl())
|
|
}
|
|
|
|
// 广告位列表
|
|
list := make([]model.AdSort, 0)
|
|
utils.XormDb.Where("mark=1").Find(&list)
|
|
adSortList := make(map[int]string, 0)
|
|
for _, v := range list {
|
|
adSortList[v.Id] = v.Description
|
|
}
|
|
|
|
// 渲染模板
|
|
response.BuildTpl(ctx, "ad_edit.html").WriteTpl(gin.H{
|
|
"info": info,
|
|
"typeList": common.AD_TYPE_LIST,
|
|
"adSortList": adSortList,
|
|
})
|
|
} else {
|
|
// 添加
|
|
// 渲染模板
|
|
response.BuildTpl(ctx, "ad_edit.html").WriteTpl()
|
|
}
|
|
}
|
|
|
|
func (c *adCtl) Add(ctx *gin.Context) {
|
|
// 参数
|
|
var req *dto.AdAddReq
|
|
if err := ctx.ShouldBind(&req); err != nil {
|
|
ctx.JSON(http.StatusOK, common.JsonResult{
|
|
Code: -1,
|
|
Msg: err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
// 调用添加方法
|
|
rows, err := service.Ad.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 *adCtl) Update(ctx *gin.Context) {
|
|
// 参数
|
|
var req *dto.AdUpdateReq
|
|
if err := ctx.ShouldBind(&req); err != nil {
|
|
ctx.JSON(http.StatusOK, common.JsonResult{
|
|
Code: -1,
|
|
Msg: err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
// 调用更新方法
|
|
rows, err := service.Ad.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 *adCtl) 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.Ad.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 *adCtl) Status(ctx *gin.Context) {
|
|
// 参数绑定
|
|
var req *dto.AdStatusReq
|
|
if err := ctx.ShouldBind(&req); err != nil {
|
|
ctx.JSON(http.StatusOK, common.JsonResult{
|
|
Code: -1,
|
|
Msg: err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
// 调用设置状态方法
|
|
rows, err := service.Ad.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: "设置成功",
|
|
})
|
|
}
|