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

231 lines
4.4 KiB
Go

/**
* 演示一管理-控制器
* @author
* @since 2022-04-19
* @File : example
*/
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"
)
var Example = new(exampleCtl)
type exampleCtl struct{}
func (c *exampleCtl) Index(ctx *gin.Context) {
// 模板渲染
response.BuildTpl(ctx, "example_index.html").WriteTpl()
}
func (c *exampleCtl) T(ctx *gin.Context) {
// 模板渲染
ctx.String(http.StatusOK, "TTTT")
}
func (c *exampleCtl) List(ctx *gin.Context) {
// 参数绑定
var req *dto.ExamplePageReq
if err := ctx.ShouldBind(&req); err != nil {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 调用获取列表方法
list, count, err := service.Example.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,
Data: list,
Msg: "操作成功",
Count: count,
})
}
func (c *exampleCtl) Edit(ctx *gin.Context) {
id := gconv.Int(ctx.Query("id"))
if id > 0 {
// 编辑
info := &model.Example{Id: id}
has, err := info.Get()
if !has || err != nil {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 头像
if info.Avatar != "" {
info.Avatar = utils.GetImageUrl(info.Avatar)
}
// 渲染模板
response.BuildTpl(ctx, "example_edit.html").WriteTpl(gin.H{
"info": info,
})
} else {
// 添加
response.BuildTpl(ctx, "example_edit.html").WriteTpl()
}
}
func (c *exampleCtl) Add(ctx *gin.Context) {
// 参数绑定
var req *dto.ExampleAddReq
if err := ctx.ShouldBind(&req); err != nil {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 调用添加方法
rows, err := service.Example.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 *exampleCtl) Update(ctx *gin.Context) {
// 参数绑定
var req *dto.ExampleUpdateReq
if err := ctx.ShouldBind(&req); err != nil {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 调用更新方法
rows, err := service.Example.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 *exampleCtl) 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.Example.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 *exampleCtl) Status(ctx *gin.Context) {
// 参数绑定
var req *dto.ExampleStatusReq
if err := ctx.ShouldBind(&req); err != nil {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 调用设置状态方法
rows, err := service.Example.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: "设置成功",
})
}
func (c *exampleCtl) IsVip(ctx *gin.Context) {
// 参数绑定
var req *dto.ExampleIsVipReq
if err := ctx.ShouldBind(&req); err != nil {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 调用设置状态方法
rows, err := service.Example.IsVip(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: "设置成功",
})
}