273 lines
9.4 KiB
Smarty
273 lines
9.4 KiB
Smarty
{{- if .IsAdd}}
|
||
// 在 {{.StructName}}Handler 中新增如下方法
|
||
// 请根据实际需求添加
|
||
|
||
{{- else}}
|
||
package {{.Package}}
|
||
|
||
import (
|
||
{{if not .OnlyTemplate}}
|
||
"{{.Module}}/pkg/response"
|
||
"{{.Module}}/internal/biz/{{.Package}}"
|
||
model "{{.Module}}/internal/data/model/{{.Package}}"
|
||
{{- if not .IsTree}}
|
||
{{.Package}}Req "{{.Module}}/internal/service/types/{{.Package}}/request"
|
||
{{- end }}
|
||
"github.com/gin-gonic/gin"
|
||
"github.com/go-kratos/kratos/v2/log"
|
||
{{- if .AutoCreateResource}}
|
||
"{{.Module}}/pkg/utils"
|
||
{{- end }}
|
||
{{- else}}
|
||
"{{.Module}}/pkg/response"
|
||
"github.com/gin-gonic/gin"
|
||
{{- end}}
|
||
)
|
||
|
||
// {{.StructName}}Handler {{.Description}}处理器
|
||
// 负责处理 HTTP 请求,调用 Biz 层 Usecase 完成业务逻辑
|
||
type {{.StructName}}Handler struct {
|
||
uc *{{.Package}}.{{.StructName}}Usecase
|
||
log *log.Helper
|
||
}
|
||
|
||
// New{{.StructName}}Handler 创建{{.Description}}处理器
|
||
// @param uc Biz 层 Usecase,包含业务逻辑
|
||
// @param logger 日志记录器
|
||
func New{{.StructName}}Handler(uc *{{.Package}}.{{.StructName}}Usecase, logger log.Logger) *{{.StructName}}Handler {
|
||
return &{{.StructName}}Handler{
|
||
uc: uc,
|
||
log: log.NewHelper(logger),
|
||
}
|
||
}
|
||
|
||
{{if not .OnlyTemplate}}
|
||
|
||
// Create{{.StructName}} 创建{{.Description}}
|
||
// @Tags {{.StructName}}
|
||
// @Summary 创建{{.Description}}
|
||
// @Security ApiKeyAuth
|
||
// @Accept application/json
|
||
// @Produce application/json
|
||
// @Param data body model.{{.StructName}} true "创建{{.Description}}"
|
||
// @Success 200 {object} response.Response{msg=string} "创建成功"
|
||
// @Router /{{.Abbreviation}}/create{{.StructName}} [post]
|
||
func (h *{{.StructName}}Handler) Create{{.StructName}}(c *gin.Context) {
|
||
ctx := c.Request.Context()
|
||
|
||
var {{.Abbreviation}} model.{{.StructName}}
|
||
if err := c.ShouldBindJSON(&{{.Abbreviation}}); err != nil {
|
||
response.FailWithMessage(err.Error(), c)
|
||
return
|
||
}
|
||
{{- if .AutoCreateResource }}
|
||
{{.Abbreviation}}.CreatedBy = utils.GetUserID(c)
|
||
{{- end }}
|
||
// 调用 Biz 层 Usecase 创建实体
|
||
if err := h.uc.Create(ctx, &{{.Abbreviation}}); err != nil {
|
||
h.log.WithContext(ctx).Errorf("创建失败: %v", err)
|
||
response.FailWithMessage("创建失败: "+err.Error(), c)
|
||
return
|
||
}
|
||
response.OkWithMessage("创建成功", c)
|
||
}
|
||
|
||
// Delete{{.StructName}} 删除{{.Description}}
|
||
// @Tags {{.StructName}}
|
||
// @Summary 删除{{.Description}}
|
||
// @Security ApiKeyAuth
|
||
// @Accept application/json
|
||
// @Produce application/json
|
||
// @Param data body model.{{.StructName}} true "删除{{.Description}}"
|
||
// @Success 200 {object} response.Response{msg=string} "删除成功"
|
||
// @Router /{{.Abbreviation}}/delete{{.StructName}} [delete]
|
||
func (h *{{.StructName}}Handler) Delete{{.StructName}}(c *gin.Context) {
|
||
ctx := c.Request.Context()
|
||
|
||
{{.PrimaryField.FieldJson}} := c.Query("{{.PrimaryField.FieldJson}}")
|
||
{{- if .AutoCreateResource }}
|
||
userID := utils.GetUserID(c)
|
||
{{- end }}
|
||
// 调用 Biz 层 Usecase 删除实体
|
||
if err := h.uc.Delete(ctx, {{.PrimaryField.FieldJson}}{{- if .AutoCreateResource -}}, userID{{- end -}}); err != nil {
|
||
h.log.WithContext(ctx).Errorf("删除失败: %v", err)
|
||
response.FailWithMessage("删除失败: "+err.Error(), c)
|
||
return
|
||
}
|
||
response.OkWithMessage("删除成功", c)
|
||
}
|
||
|
||
// Delete{{.StructName}}ByIds 批量删除{{.Description}}
|
||
// @Tags {{.StructName}}
|
||
// @Summary 批量删除{{.Description}}
|
||
// @Security ApiKeyAuth
|
||
// @Accept application/json
|
||
// @Produce application/json
|
||
// @Param {{.PrimaryField.FieldJson}}s query []{{.PrimaryField.FieldType}} true "批量删除{{.Description}}"
|
||
// @Success 200 {object} response.Response{msg=string} "批量删除成功"
|
||
// @Router /{{.Abbreviation}}/delete{{.StructName}}ByIds [delete]
|
||
func (h *{{.StructName}}Handler) Delete{{.StructName}}ByIds(c *gin.Context) {
|
||
ctx := c.Request.Context()
|
||
|
||
{{.PrimaryField.FieldJson}}s := c.QueryArray("{{.PrimaryField.FieldJson}}s[]")
|
||
{{- if .AutoCreateResource }}
|
||
userID := utils.GetUserID(c)
|
||
{{- end }}
|
||
// 调用 Biz 层 Usecase 批量删除实体
|
||
if err := h.uc.DeleteByIds(ctx, {{.PrimaryField.FieldJson}}s{{- if .AutoCreateResource }}, userID{{- end }}); err != nil {
|
||
h.log.WithContext(ctx).Errorf("批量删除失败: %v", err)
|
||
response.FailWithMessage("批量删除失败: "+err.Error(), c)
|
||
return
|
||
}
|
||
response.OkWithMessage("批量删除成功", c)
|
||
}
|
||
|
||
// Update{{.StructName}} 更新{{.Description}}
|
||
// @Tags {{.StructName}}
|
||
// @Summary 更新{{.Description}}
|
||
// @Security ApiKeyAuth
|
||
// @Accept application/json
|
||
// @Produce application/json
|
||
// @Param data body model.{{.StructName}} true "更新{{.Description}}"
|
||
// @Success 200 {object} response.Response{msg=string} "更新成功"
|
||
// @Router /{{.Abbreviation}}/update{{.StructName}} [put]
|
||
func (h *{{.StructName}}Handler) Update{{.StructName}}(c *gin.Context) {
|
||
ctx := c.Request.Context()
|
||
|
||
var {{.Abbreviation}} model.{{.StructName}}
|
||
if err := c.ShouldBindJSON(&{{.Abbreviation}}); err != nil {
|
||
response.FailWithMessage(err.Error(), c)
|
||
return
|
||
}
|
||
{{- if .AutoCreateResource }}
|
||
{{.Abbreviation}}.UpdatedBy = utils.GetUserID(c)
|
||
{{- end }}
|
||
// 调用 Biz 层 Usecase 更新实体
|
||
if err := h.uc.Update(ctx, &{{.Abbreviation}}); err != nil {
|
||
h.log.WithContext(ctx).Errorf("更新失败: %v", err)
|
||
response.FailWithMessage("更新失败: "+err.Error(), c)
|
||
return
|
||
}
|
||
response.OkWithMessage("更新成功", c)
|
||
}
|
||
|
||
// Find{{.StructName}} 用id查询{{.Description}}
|
||
// @Tags {{.StructName}}
|
||
// @Summary 用id查询{{.Description}}
|
||
// @Security ApiKeyAuth
|
||
// @Accept application/json
|
||
// @Produce application/json
|
||
// @Param {{.PrimaryField.FieldJson}} query {{.PrimaryField.FieldType}} true "用id查询{{.Description}}"
|
||
// @Success 200 {object} response.Response{data=model.{{.StructName}},msg=string} "查询成功"
|
||
// @Router /{{.Abbreviation}}/find{{.StructName}} [get]
|
||
func (h *{{.StructName}}Handler) Find{{.StructName}}(c *gin.Context) {
|
||
ctx := c.Request.Context()
|
||
|
||
{{.PrimaryField.FieldJson}} := c.Query("{{.PrimaryField.FieldJson}}")
|
||
// 调用 Biz 层 Usecase 查询实体
|
||
re{{.Abbreviation}}, err := h.uc.FindByID(ctx, {{.PrimaryField.FieldJson}})
|
||
if err != nil {
|
||
h.log.WithContext(ctx).Errorf("查询失败: %v", err)
|
||
response.FailWithMessage("查询失败: "+err.Error(), c)
|
||
return
|
||
}
|
||
response.OkWithData(re{{.Abbreviation}}, c)
|
||
}
|
||
|
||
{{- if .IsTree }}
|
||
// Get{{.StructName}}List 获取{{.Description}}列表(树形结构)
|
||
// @Tags {{.StructName}}
|
||
// @Summary 获取{{.Description}}列表(树形结构)
|
||
// @Security ApiKeyAuth
|
||
// @Accept application/json
|
||
// @Produce application/json
|
||
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
|
||
// @Router /{{.Abbreviation}}/get{{.StructName}}List [get]
|
||
func (h *{{.StructName}}Handler) Get{{.StructName}}List(c *gin.Context) {
|
||
ctx := c.Request.Context()
|
||
|
||
// 调用 Biz 层 Usecase 获取树形列表
|
||
list, err := h.uc.List(ctx)
|
||
if err != nil {
|
||
h.log.WithContext(ctx).Errorf("获取失败: %v", err)
|
||
response.FailWithMessage("获取失败: "+err.Error(), c)
|
||
return
|
||
}
|
||
response.OkWithDetailed(list, "获取成功", c)
|
||
}
|
||
{{- else }}
|
||
// Get{{.StructName}}List 分页获取{{.Description}}列表
|
||
// @Tags {{.StructName}}
|
||
// @Summary 分页获取{{.Description}}列表
|
||
// @Security ApiKeyAuth
|
||
// @Accept application/json
|
||
// @Produce application/json
|
||
// @Param data query {{.Package}}Req.{{.StructName}}Search true "分页获取{{.Description}}列表"
|
||
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
|
||
// @Router /{{.Abbreviation}}/get{{.StructName}}List [get]
|
||
func (h *{{.StructName}}Handler) Get{{.StructName}}List(c *gin.Context) {
|
||
ctx := c.Request.Context()
|
||
|
||
var pageInfo {{.Package}}Req.{{.StructName}}Search
|
||
if err := c.ShouldBindQuery(&pageInfo); err != nil {
|
||
response.FailWithMessage(err.Error(), c)
|
||
return
|
||
}
|
||
// 调用 Biz 层 Usecase 获取分页列表
|
||
list, total, err := h.uc.List(ctx, &pageInfo)
|
||
if err != nil {
|
||
h.log.WithContext(ctx).Errorf("获取失败: %v", err)
|
||
response.FailWithMessage("获取失败: "+err.Error(), c)
|
||
return
|
||
}
|
||
response.OkWithDetailed(response.PageResult{
|
||
List: list,
|
||
Total: total,
|
||
Page: pageInfo.Page,
|
||
PageSize: pageInfo.PageSize,
|
||
}, "获取成功", c)
|
||
}
|
||
{{- end }}
|
||
|
||
{{- if .HasDataSource }}
|
||
// Get{{.StructName}}DataSource 获取{{.StructName}}的数据源
|
||
// @Tags {{.StructName}}
|
||
// @Summary 获取{{.StructName}}的数据源
|
||
// @Accept application/json
|
||
// @Produce application/json
|
||
// @Success 200 {object} response.Response{data=object,msg=string} "查询成功"
|
||
// @Router /{{.Abbreviation}}/get{{.StructName}}DataSource [get]
|
||
func (h *{{.StructName}}Handler) Get{{.StructName}}DataSource(c *gin.Context) {
|
||
ctx := c.Request.Context()
|
||
|
||
// 调用 Biz 层 Usecase 获取数据源
|
||
dataSource, err := h.uc.GetDataSource(ctx)
|
||
if err != nil {
|
||
h.log.WithContext(ctx).Errorf("查询失败: %v", err)
|
||
response.FailWithMessage("查询失败: "+err.Error(), c)
|
||
return
|
||
}
|
||
response.OkWithData(dataSource, c)
|
||
}
|
||
{{- end }}
|
||
|
||
{{- end }}
|
||
|
||
// Get{{.StructName}}Public 不需要鉴权的{{.Description}}接口
|
||
// @Tags {{.StructName}}
|
||
// @Summary 不需要鉴权的{{.Description}}接口
|
||
// @Accept application/json
|
||
// @Produce application/json
|
||
// @Success 200 {object} response.Response{data=object,msg=string} "获取成功"
|
||
// @Router /{{.Abbreviation}}/get{{.StructName}}Public [get]
|
||
func (h *{{.StructName}}Handler) Get{{.StructName}}Public(c *gin.Context) {
|
||
ctx := c.Request.Context()
|
||
|
||
// 调用 Biz 层 Usecase 获取公开数据
|
||
h.uc.GetPublic(ctx)
|
||
response.OkWithDetailed(gin.H{
|
||
"info": "不需要鉴权的{{.Description}}接口信息",
|
||
}, "获取成功", c)
|
||
}
|
||
{{- end }}
|