Files
easygoadmin/app/controller/notice.go
yaoyilin 1b36bd8fbe feat: 初始化项目
初始化项目
2022-10-31 22:29:16 +08:00

206 lines
3.9 KiB
Go

/**
* 通知公告-控制器
* @author
* @since 2021/11/13
* @File : notice
*/
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 Notice = new(noticeCtl)
type noticeCtl struct{}
func (c *noticeCtl) Index(ctx *gin.Context) {
// 渲染模板
response.BuildTpl(ctx, "notice_index.html").WriteTpl(gin.H{
"sourceList": common.NOTICE_SOURCE_LIST,
})
}
func (c *noticeCtl) List(ctx *gin.Context) {
// 参数
var req *dto.NoticePageReq
if err := ctx.ShouldBind(&req); err != nil {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 调用分页查询方法
list, count, err := service.Notice.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 *noticeCtl) Edit(ctx *gin.Context) {
// 查询记录
id := gconv.Int(ctx.Query("id"))
if id > 0 {
info := &model.Notice{Id: id}
has, err := info.Get()
if !has || err != nil {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 富文本图片替换处理
if info.Content != "" {
info.Content = strings.ReplaceAll(info.Content, "[IMG_URL]", utils.ImageUrl())
}
// 渲染模板
response.BuildTpl(ctx, "notice_edit.html").WriteTpl(gin.H{
"info": info,
"sourceList": common.NOTICE_SOURCE_LIST,
})
} else {
// 渲染模板
response.BuildTpl(ctx, "notice_edit.html").WriteTpl(gin.H{
"sourceList": common.NOTICE_SOURCE_LIST,
})
}
}
func (c *noticeCtl) Add(ctx *gin.Context) {
// 参数
var req *dto.NoticeAddReq
if err := ctx.ShouldBind(&req); err != nil {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 调用添加方法
rows, err := service.Notice.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 *noticeCtl) Update(ctx *gin.Context) {
// 参数
var req *dto.NoticeUpdateReq
if err := ctx.ShouldBind(&req); err != nil {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 调用更新方法
rows, err := service.Notice.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 *noticeCtl) 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.Notice.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 *noticeCtl) Status(ctx *gin.Context) {
// 参数
var req *dto.NoticeStatusReq
if err := ctx.ShouldBind(&req); err != nil {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 调用设置状态方法
rows, err := service.Notice.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: "设置成功",
})
}