Files
thinkweb/templates/tmpl/controller.html
ZiShuo c2dfa9fbac init
2024-04-10 18:56:50 +08:00

208 lines
4.3 KiB
HTML

package controller
/**
* {{.moduleTitle}}管理-控制器
* @author ZStudio {{.author}}
* @since {{.since}}
* @File : {{.moduleName}}
*/
import (
"github.com/darkit/thinkweb/app/dto"
"github.com/darkit/thinkweb/app/model"
"github.com/darkit/thinkweb/app/service"
"github.com/darkit/thinkweb/utils"
"github.com/darkit/thinkweb/utils/common"
"github.com/darkit/thinkweb/utils/gconv"
"github.com/darkit/thinkweb/utils/response"
"github.com/gin-gonic/gin"
"net/http"
)
var {{.entityName}} = new({{.moduleName}}Ctl)
type {{.moduleName}}Ctl struct{}
func (c *{{.moduleName}}Ctl) Index(ctx *gin.Context) {
// 模板渲染
response.BuildTpl(ctx, "{{.moduleName}}_index.html").WriteTpl()
}
func (c *{{.moduleName}}Ctl) List(ctx *gin.Context) {
// 参数绑定
var req *dto.{{.entityName}}PageReq
if err := ctx.ShouldBind(&req); err != nil {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 调用获取列表方法
list, count, err := service.{{.entityName}}.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 *{{.moduleName}}Ctl) Edit(ctx *gin.Context) {
id := gconv.Int(ctx.Query("id"))
if id > 0 {
// 编辑
info := &model.{{.entityName}}{Id: id}
has, err := info.Get()
if !has || err != nil {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
{{range $key, $val := .columnList}}
{{if ne $val.columnImage false}}
// {{$val.columnTitle}}
if info.{{$val.columnName2}} != "" {
info.{{$val.columnName2}} = utils.GetImageUrl(info.{{$val.columnName2}})
}
{{end}}
{{end}}
// 渲染模板
response.BuildTpl(ctx, "{{.moduleName}}_edit.html").WriteTpl(gin.H{
"info": info,
})
} else {
// 添加
response.BuildTpl(ctx, "{{.moduleName}}_edit.html").WriteTpl()
}
}
func (c *{{.moduleName}}Ctl) Add(ctx *gin.Context) {
// 参数绑定
var req *dto.{{.entityName}}AddReq
if err := ctx.ShouldBind(&req); err != nil {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 调用添加方法
rows, err := service.{{.entityName}}.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 *{{.moduleName}}Ctl) Update(ctx *gin.Context) {
// 参数绑定
var req *dto.{{.entityName}}UpdateReq
if err := ctx.ShouldBind(&req); err != nil {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 调用更新方法
rows, err := service.{{.entityName}}.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 *{{.moduleName}}Ctl) 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.{{.entityName}}.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: "删除成功",
})
}
{{range $key, $val := .columnList}}
{{if ne $val.columnSwitch false}}
func (c *{{$.moduleName}}Ctl) {{$val.columnName2}}(ctx *gin.Context) {
// 参数绑定
var req *dto.{{$.entityName}}{{$val.columnName2}}Req
if err := ctx.ShouldBind(&req); err != nil {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 调用设置状态方法
rows, err := service.{{$.entityName}}.{{$val.columnName2}}(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: "设置成功",
})
}
{{end}}
{{end}}