增加自动化生成表单功能,修改自动化生成文件的文件夹名称更加规范化,css样式调整。
This commit is contained in:
@@ -0,0 +1,130 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gin-vue-admin/global/response"
|
||||
"gin-vue-admin/model"
|
||||
"gin-vue-admin/model/request"
|
||||
resp "gin-vue-admin/model/response"
|
||||
"gin-vue-admin/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// @Tags {{.StructName}}
|
||||
// @Summary 创建{{.StructName}}
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data body model.{{.StructName}} true "创建{{.StructName}}"
|
||||
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
|
||||
// @Router /{{.Abbreviation}}/create{{.StructName}} [post]
|
||||
func Create{{.StructName}}(c *gin.Context) {
|
||||
var {{.Abbreviation}} model.{{.StructName}}
|
||||
_ = c.ShouldBindJSON(&{{.Abbreviation}})
|
||||
err := service.Create{{.StructName}}({{.Abbreviation}})
|
||||
if err != nil {
|
||||
response.FailWithMessage(fmt.Sprintf("创建失败,%v", err), c)
|
||||
} else {
|
||||
response.OkWithMessage("创建成功", c)
|
||||
}
|
||||
}
|
||||
|
||||
// @Tags {{.StructName}}
|
||||
// @Summary 删除{{.StructName}}
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data body model.{{.StructName}} true "删除{{.StructName}}"
|
||||
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
|
||||
// @Router /{{.Abbreviation}}/delete{{.StructName}} [delete]
|
||||
func Delete{{.StructName}}(c *gin.Context) {
|
||||
var {{.Abbreviation}} model.{{.StructName}}
|
||||
_ = c.ShouldBindJSON(&{{.Abbreviation}})
|
||||
err := service.Delete{{.StructName}}({{.Abbreviation}})
|
||||
if err != nil {
|
||||
response.FailWithMessage(fmt.Sprintf("删除失败,%v", err), c)
|
||||
} else {
|
||||
response.OkWithMessage("删除成功", c)
|
||||
}
|
||||
}
|
||||
|
||||
// @Tags {{.StructName}}
|
||||
// @Summary 批量删除{{.StructName}}
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data body request.IdsReq true "批量删除{{.StructName}}"
|
||||
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
|
||||
// @Router /{{.Abbreviation}}/delete{{.StructName}}ByIds [delete]
|
||||
func Delete{{.StructName}}ByIds(c *gin.Context) {
|
||||
var IDS request.IdsReq
|
||||
_ = c.ShouldBindJSON(&IDS)
|
||||
err := service.Delete{{.StructName}}ByIds(IDS)
|
||||
if err != nil {
|
||||
response.FailWithMessage(fmt.Sprintf("删除失败,%v", err), c)
|
||||
} else {
|
||||
response.OkWithMessage("删除成功", c)
|
||||
}
|
||||
}
|
||||
|
||||
// @Tags {{.StructName}}
|
||||
// @Summary 更新{{.StructName}}
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data body model.{{.StructName}} true "更新{{.StructName}}"
|
||||
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
|
||||
// @Router /{{.Abbreviation}}/update{{.StructName}} [put]
|
||||
func Update{{.StructName}}(c *gin.Context) {
|
||||
var {{.Abbreviation}} model.{{.StructName}}
|
||||
_ = c.ShouldBindJSON(&{{.Abbreviation}})
|
||||
err := service.Update{{.StructName}}(&{{.Abbreviation}})
|
||||
if err != nil {
|
||||
response.FailWithMessage(fmt.Sprintf("更新失败,%v", err), c)
|
||||
} else {
|
||||
response.OkWithMessage("更新成功", c)
|
||||
}
|
||||
}
|
||||
|
||||
// @Tags {{.StructName}}
|
||||
// @Summary 用id查询{{.StructName}}
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data body model.{{.StructName}} true "用id查询{{.StructName}}"
|
||||
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
|
||||
// @Router /{{.Abbreviation}}/find{{.StructName}} [get]
|
||||
func Find{{.StructName}}(c *gin.Context) {
|
||||
var {{.Abbreviation}} model.{{.StructName}}
|
||||
_ = c.ShouldBindQuery(&{{.Abbreviation}})
|
||||
err, re{{.Abbreviation}} := service.Get{{.StructName}}({{.Abbreviation}}.ID)
|
||||
if err != nil {
|
||||
response.FailWithMessage(fmt.Sprintf("查询失败,%v", err), c)
|
||||
} else {
|
||||
response.OkWithData(gin.H{"re{{.Abbreviation}}": re{{.Abbreviation}}}, c)
|
||||
}
|
||||
}
|
||||
|
||||
// @Tags {{.StructName}}
|
||||
// @Summary 分页获取{{.StructName}}列表
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data body request.{{.StructName}}Search true "分页获取{{.StructName}}列表"
|
||||
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
|
||||
// @Router /{{.Abbreviation}}/get{{.StructName}}List [get]
|
||||
func Get{{.StructName}}List(c *gin.Context) {
|
||||
var pageInfo request.{{.StructName}}Search
|
||||
_ = c.ShouldBindQuery(&pageInfo)
|
||||
err, list, total := service.Get{{.StructName}}InfoList(pageInfo)
|
||||
if err != nil {
|
||||
response.FailWithMessage(fmt.Sprintf("获取数据失败,%v", err), c)
|
||||
} else {
|
||||
response.OkWithData(resp.PageResult{
|
||||
List: list,
|
||||
Total: total,
|
||||
Page: pageInfo.Page,
|
||||
PageSize: pageInfo.PageSize,
|
||||
}, c)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
// 自动生成模板{{.StructName}}
|
||||
package model
|
||||
|
||||
import (
|
||||
"gin-vue-admin/global"
|
||||
)
|
||||
|
||||
// 如果含有time.Time 请自行import time包
|
||||
type {{.StructName}} struct {
|
||||
global.GVA_MODEL {{- range .Fields}}
|
||||
{{- if eq .FieldType "bool" }}
|
||||
{{.FieldName}} *{{.FieldType}} `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"column:{{.ColumnName}};comment:{{.Comment}}{{- if .DataType -}};type:{{.DataType}}{{- if eq .FieldType "string" -}}{{- if .DataTypeLong -}}({{.DataTypeLong}}){{- end -}}{{- end -}};{{- if .DataTypeLong -}}size:{{.DataTypeLong}};{{- end -}}{{- end -}}"`
|
||||
{{- else }}
|
||||
{{.FieldName}} {{.FieldType}} `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"column:{{.ColumnName}};comment:{{.Comment}}{{- if .DataType -}};type:{{.DataType}}{{- if eq .FieldType "string" -}}{{- if .DataTypeLong -}}({{.DataTypeLong}}){{- end -}}{{- end -}};{{- if .DataTypeLong -}}size:{{.DataTypeLong}};{{- end -}}{{- end -}}"`
|
||||
{{- end }} {{- end }}
|
||||
}
|
||||
|
||||
{{ if .TableName }}
|
||||
func ({{.StructName}}) TableName() string {
|
||||
return "{{.TableName}}"
|
||||
}
|
||||
{{ end }}
|
||||
@@ -0,0 +1,8 @@
|
||||
package request
|
||||
|
||||
import "gin-vue-admin/model"
|
||||
|
||||
type {{.StructName}}Search struct{
|
||||
model.{{.StructName}}
|
||||
PageInfo
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"gin-vue-admin/api/v1"
|
||||
"gin-vue-admin/middleware"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func Init{{.StructName}}Router(Router *gin.RouterGroup) {
|
||||
{{.StructName}}Router := Router.Group("{{.Abbreviation}}").Use(middleware.JWTAuth()).Use(middleware.CasbinHandler()).Use(middleware.OperationRecord())
|
||||
{
|
||||
{{.StructName}}Router.POST("create{{.StructName}}", v1.Create{{.StructName}}) // 新建{{.StructName}}
|
||||
{{.StructName}}Router.DELETE("delete{{.StructName}}", v1.Delete{{.StructName}}) // 删除{{.StructName}}
|
||||
{{.StructName}}Router.DELETE("delete{{.StructName}}ByIds", v1.Delete{{.StructName}}ByIds) // 批量删除{{.StructName}}
|
||||
{{.StructName}}Router.PUT("update{{.StructName}}", v1.Update{{.StructName}}) // 更新{{.StructName}}
|
||||
{{.StructName}}Router.GET("find{{.StructName}}", v1.Find{{.StructName}}) // 根据ID获取{{.StructName}}
|
||||
{{.StructName}}Router.GET("get{{.StructName}}List", v1.Get{{.StructName}}List) // 获取{{.StructName}}列表
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"gin-vue-admin/global"
|
||||
"gin-vue-admin/model"
|
||||
"gin-vue-admin/model/request"
|
||||
)
|
||||
|
||||
// @title Create{{.StructName}}
|
||||
// @description create a {{.StructName}}
|
||||
// @param {{.Abbreviation}} model.{{.StructName}}
|
||||
// @auth (2020/04/05 20:22)
|
||||
// @return err error
|
||||
|
||||
func Create{{.StructName}}({{.Abbreviation}} model.{{.StructName}}) (err error) {
|
||||
err = global.GVA_DB.Create(&{{.Abbreviation}}).Error
|
||||
return err
|
||||
}
|
||||
|
||||
// @title Delete{{.StructName}}
|
||||
// @description delete a {{.StructName}}
|
||||
// @auth (2020/04/05 20:22)
|
||||
// @param {{.Abbreviation}} model.{{.StructName}}
|
||||
// @return error
|
||||
|
||||
func Delete{{.StructName}}({{.Abbreviation}} model.{{.StructName}}) (err error) {
|
||||
err = global.GVA_DB.Delete({{.Abbreviation}}).Error
|
||||
return err
|
||||
}
|
||||
|
||||
// @title Delete{{.StructName}}ByIds
|
||||
// @description delete {{.StructName}}s
|
||||
// @auth (2020/04/05 20:22)
|
||||
// @param {{.Abbreviation}} model.{{.StructName}}
|
||||
// @return error
|
||||
|
||||
func Delete{{.StructName}}ByIds(ids request.IdsReq) (err error) {
|
||||
err = global.GVA_DB.Delete(&[]model.{{.StructName}}{},"id in ?",ids.Ids).Error
|
||||
return err
|
||||
}
|
||||
|
||||
// @title Update{{.StructName}}
|
||||
// @description update a {{.StructName}}
|
||||
// @param {{.Abbreviation}} *model.{{.StructName}}
|
||||
// @auth (2020/04/05 20:22)
|
||||
// @return error
|
||||
|
||||
func Update{{.StructName}}({{.Abbreviation}} *model.{{.StructName}}) (err error) {
|
||||
err = global.GVA_DB.Save({{.Abbreviation}}).Error
|
||||
return err
|
||||
}
|
||||
|
||||
// @title Get{{.StructName}}
|
||||
// @description get the info of a {{.StructName}}
|
||||
// @auth (2020/04/05 20:22)
|
||||
// @param id uint
|
||||
// @return error
|
||||
// @return {{.StructName}} {{.StructName}}
|
||||
|
||||
func Get{{.StructName}}(id uint) (err error, {{.Abbreviation}} model.{{.StructName}}) {
|
||||
err = global.GVA_DB.Where("id = ?", id).First(&{{.Abbreviation}}).Error
|
||||
return
|
||||
}
|
||||
|
||||
// @title Get{{.StructName}}InfoList
|
||||
// @description get {{.StructName}} list by pagination, 分页获取{{.StructName}}
|
||||
// @auth (2020/04/05 20:22)
|
||||
// @param info PageInfo
|
||||
// @return error
|
||||
|
||||
func Get{{.StructName}}InfoList(info request.{{.StructName}}Search) (err error, list interface{}, total int64) {
|
||||
limit := info.PageSize
|
||||
offset := info.PageSize * (info.Page - 1)
|
||||
// 创建db
|
||||
db := global.GVA_DB.Model(&model.{{.StructName}}{})
|
||||
var {{.Abbreviation}}s []model.{{.StructName}}
|
||||
// 如果有条件搜索 下方会自动创建搜索语句
|
||||
{{- range .Fields}}
|
||||
{{- if .FieldSearchType}}
|
||||
{{- if eq .FieldType "string" }}
|
||||
if info.{{.FieldName}} != "" {
|
||||
db = db.Where("`{{.ColumnName}}` {{.FieldSearchType}} ?",{{if eq .FieldSearchType "LIKE"}}"%"+ {{ end }}info.{{.FieldName}}{{if eq .FieldSearchType "LIKE"}}+"%"{{ end }})
|
||||
}
|
||||
{{- else if eq .FieldType "bool" }}
|
||||
if info.{{.FieldName}} != nil {
|
||||
db = db.Where("`{{.ColumnName}}` {{.FieldSearchType}} ?",{{if eq .FieldSearchType "LIKE"}}"%"+{{ end }}info.{{.FieldName}}{{if eq .FieldSearchType "LIKE"}}+"%"{{ end }})
|
||||
}
|
||||
{{- else if eq .FieldType "int" }}
|
||||
if info.{{.FieldName}} != 0 {
|
||||
db = db.Where("`{{.ColumnName}}` {{.FieldSearchType}} ?",{{if eq .FieldSearchType "LIKE"}}"%"+{{ end }}info.{{.FieldName}}{{if eq .FieldSearchType "LIKE"}}+"%"{{ end }})
|
||||
}
|
||||
{{- else if eq .FieldType "float64" }}
|
||||
if info.{{.FieldName}} != 0 {
|
||||
db = db.Where("`{{.ColumnName}}` {{.FieldSearchType}} ?",{{if eq .FieldSearchType "LIKE"}}"%"+{{ end }}info.{{.FieldName}}{{if eq .FieldSearchType "LIKE"}}+"%"{{ end }})
|
||||
}
|
||||
{{- else if eq .FieldType "time.Time" }}
|
||||
if !info.{{.FieldName}}.IsZero() {
|
||||
db = db.Where("`{{.ColumnName}}` {{.FieldSearchType}} ?",{{if eq .FieldSearchType "LIKE"}}"%"+{{ end }}info.{{.FieldName}}{{if eq .FieldSearchType "LIKE"}}+"%"{{ end }})
|
||||
}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
err = db.Count(&total).Error
|
||||
err = db.Limit(limit).Offset(offset).Find(&{{.Abbreviation}}s).Error
|
||||
return err, {{.Abbreviation}}s, total
|
||||
}
|
||||
Reference in New Issue
Block a user