mirror of
https://gitee.com/xiangheng/x_admin.git
synced 2025-12-24 08:12:55 +08:00
调整权限
This commit is contained in:
@@ -0,0 +1,127 @@
|
||||
package {{{ .ModuleName }}}
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"x_admin/core/request"
|
||||
"x_admin/core/response"
|
||||
"x_admin/util"
|
||||
)
|
||||
|
||||
|
||||
type {{{ title (toCamelCase .ModuleName) }}}Handler struct {}
|
||||
|
||||
// @Summary {{{ .FunctionName }}}列表
|
||||
// @Tags {{{ .ModuleName }}}-{{{ .FunctionName }}}
|
||||
// @Produce json
|
||||
// @Param Token header string true "token"
|
||||
// @Param PageNo query int true "页码"
|
||||
// @Param PageSize query int true "每页数量"
|
||||
{{{- range .Columns }}}
|
||||
{{{- if .IsQuery }}}
|
||||
// @Param {{{ toCamelCase .GoField }}} query {{{ .GoType }}} false "{{{ .ColumnComment }}}."
|
||||
{{{- end }}}
|
||||
{{{- end }}}
|
||||
// @Success 200 {object} []{{{ title (toCamelCase .EntityName) }}}Resp "成功"
|
||||
// @Failure 400 {object} string "请求错误"
|
||||
// @Router /api/admin/{{{ .ModuleName }}}/list [get]
|
||||
func (hd {{{ title (toCamelCase .ModuleName) }}}Handler) List(c *gin.Context) {
|
||||
var page request.PageReq
|
||||
var listReq {{{ title (toCamelCase .EntityName) }}}ListReq
|
||||
if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &page)) {
|
||||
return
|
||||
}
|
||||
if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &listReq)) {
|
||||
return
|
||||
}
|
||||
res, err := Service.List(page, listReq)
|
||||
response.CheckAndRespWithData(c, res, err)
|
||||
}
|
||||
|
||||
// @Summary {{{ .FunctionName }}}列表-所有
|
||||
// @Tags {{{ .ModuleName }}}-{{{ .FunctionName }}}
|
||||
// @Produce json
|
||||
// @Success 200 {object} []{{{ title (toCamelCase .EntityName) }}}Resp "成功"
|
||||
// @Router /api/admin/{{{ .ModuleName }}}/listAll [get]
|
||||
func (hd {{{ title (toCamelCase .ModuleName) }}}Handler) ListAll(c *gin.Context) {
|
||||
res, err := Service.ListAll()
|
||||
if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &listReq)) {
|
||||
return
|
||||
}
|
||||
response.CheckAndRespWithData(c, res, err)
|
||||
}
|
||||
|
||||
// @Summary {{{ .FunctionName }}}详情
|
||||
// @Tags {{{ .ModuleName }}}-{{{ .FunctionName }}}
|
||||
// @Produce json
|
||||
// @Param Token header string true "token"
|
||||
{{{- range .Columns }}}
|
||||
{{{- if .IsPk }}}
|
||||
// @Param {{{ toCamelCase .GoField }}} query {{{ .GoType }}} false "{{{ .ColumnComment }}}."
|
||||
{{{- end }}}
|
||||
{{{- end }}}
|
||||
// @Success 200 {object} {{{ title (toCamelCase .EntityName) }}}Resp "成功"
|
||||
// @Router /api/admin/{{{ .ModuleName }}}/detail [get]
|
||||
func (hd {{{ title (toCamelCase .ModuleName) }}}Handler) Detail(c *gin.Context) {
|
||||
var detailReq {{{ title (toCamelCase .EntityName) }}}DetailReq
|
||||
if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &detailReq)) {
|
||||
return
|
||||
}
|
||||
res, err := Service.Detail(detailReq.{{{ title (toCamelCase .PrimaryKey) }}})
|
||||
response.CheckAndRespWithData(c, res, err)
|
||||
}
|
||||
|
||||
|
||||
// @Summary {{{ .FunctionName }}}新增
|
||||
// @Tags {{{ .ModuleName }}}-{{{ .FunctionName }}}
|
||||
// @Produce json
|
||||
// @Param Token header string true "token"
|
||||
{{{- range .Columns }}}
|
||||
{{{- if .IsInsert }}}
|
||||
// @Param {{{ toCamelCase .GoField }}} body {{{ .GoType }}} false "{{{ .ColumnComment }}}."
|
||||
{{{- end }}}
|
||||
{{{- end }}}
|
||||
// @Success 200 {object} response.RespType "成功"
|
||||
// @Router /api/admin/{{{ .ModuleName }}}/add [post]
|
||||
func (hd {{{ title (toCamelCase .ModuleName) }}}Handler) Add(c *gin.Context) {
|
||||
var addReq {{{ title (toCamelCase .EntityName) }}}AddReq
|
||||
if response.IsFailWithResp(c, util.VerifyUtil.VerifyBody(c, &addReq)) {
|
||||
return
|
||||
}
|
||||
response.CheckAndResp(c, Service.Add(addReq))
|
||||
}
|
||||
// @Summary {{{ .FunctionName }}}编辑
|
||||
// @Tags {{{ .ModuleName }}}-{{{ .FunctionName }}}
|
||||
// @Produce json
|
||||
// @Param Token header string true "token"
|
||||
{{{- range .Columns }}}
|
||||
{{{- if .IsEdit }}}
|
||||
// @Param {{{ toCamelCase .GoField }}} body {{{ .GoType }}} false "{{{ .ColumnComment }}}."
|
||||
{{{- end }}}
|
||||
{{{- end }}}
|
||||
// @Success 200 {object} response.RespType "成功"
|
||||
// @Router /api/admin/{{{ .ModuleName }}}/edit [post]
|
||||
func (hd {{{ title (toCamelCase .ModuleName) }}}Handler) Edit(c *gin.Context) {
|
||||
var editReq {{{ title (toCamelCase .EntityName) }}}EditReq
|
||||
if response.IsFailWithResp(c, util.VerifyUtil.VerifyBody(c, &editReq)) {
|
||||
return
|
||||
}
|
||||
response.CheckAndResp(c, Service.Edit(editReq))
|
||||
}
|
||||
// @Summary {{{ .FunctionName }}}删除
|
||||
// @Tags {{{ .ModuleName }}}-{{{ .FunctionName }}}
|
||||
// @Produce json
|
||||
// @Param Token header string true "token"
|
||||
{{{- range .Columns }}}
|
||||
{{{- if .IsPk }}}
|
||||
// @Param {{{ toCamelCase .GoField }}} body {{{ .GoType }}} false "{{{ .ColumnComment }}}."
|
||||
{{{- end }}}
|
||||
{{{- end }}}
|
||||
// @Success 200 {object} response.RespType "成功"
|
||||
// @Router /api/admin/{{{ .ModuleName }}}/del [post]
|
||||
func (hd {{{ title (toCamelCase .ModuleName) }}}Handler) Del(c *gin.Context) {
|
||||
var delReq {{{ title (toCamelCase .EntityName) }}}DelReq
|
||||
if response.IsFailWithResp(c, util.VerifyUtil.VerifyBody(c, &delReq)) {
|
||||
return
|
||||
}
|
||||
response.CheckAndResp(c, Service.Del(delReq.{{{ title (toCamelCase .PrimaryKey) }}}))
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package model
|
||||
|
||||
//{{{ title (toCamelCase .EntityName) }}} {{{ .FunctionName }}}实体
|
||||
type {{{ title (toCamelCase .EntityName) }}} struct {
|
||||
{{{- range .Columns }}}
|
||||
{{{- if not (contains $.SubTableFields .ColumnName) }}}
|
||||
{{{ if eq .GoType "core.TsTime" }}}
|
||||
{{{ title (toCamelCase .GoField) }}} int64 `gorm:"{{{ if eq .GoField "create_time" }}}autoCreateTime;{{{ else }}}{{{if eq .GoField "update_time"}}}autoUpdateTime;{{{ end }}}{{{ end }}}comment:'{{{ .ColumnComment }}}'"` // {{{ .ColumnComment }}}
|
||||
{{{ else }}}
|
||||
{{{ title (toCamelCase .GoField) }}} {{{ .GoType }}} `gorm:"{{{ if .IsPk }}}primarykey;{{{ end }}}comment:'{{{ .ColumnComment }}}'"` // {{{ .ColumnComment }}}
|
||||
{{{- end }}}
|
||||
{{{- end }}}
|
||||
{{{- end }}}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"x_admin/core"
|
||||
"x_admin/middleware"
|
||||
"x_admin/admin/{{{ .ModuleName }}}"
|
||||
)
|
||||
|
||||
/**
|
||||
集成
|
||||
1. 导入
|
||||
- 请先提交git避免文件覆盖!!!
|
||||
- 下载并解压压缩包后,直接复制server、admin文件夹到项目根目录即可
|
||||
|
||||
2. 注册路由
|
||||
请在 admin/entry.go 文件引入{{{ title (toCamelCase .ModuleName) }}}Route注册路由
|
||||
|
||||
3. 后台手动添加菜单和按钮
|
||||
{{{ .ModuleName }}}:add
|
||||
{{{.ModuleName }}}:edit
|
||||
{{{.ModuleName }}}:del
|
||||
{{{.ModuleName }}}:list
|
||||
{{{.ModuleName }}}:listAll
|
||||
{{{.ModuleName }}}:detail
|
||||
*/
|
||||
|
||||
|
||||
// {{{ title (toCamelCase .ModuleName) }}}Route(rg)
|
||||
func {{{ title (toCamelCase .ModuleName) }}}Route(rg *gin.RouterGroup) {
|
||||
handle := {{{ .ModuleName}}}.{{{ title (toCamelCase .EntityName) }}}Handler{}
|
||||
|
||||
rg = rg.Group("/", middleware.TokenAuth())
|
||||
rg.GET("/{{{ .ModuleName }}}/list", handle.List)
|
||||
rg.GET("/{{{ .ModuleName }}}/listAll", handle.ListAll)
|
||||
rg.GET("/{{{ .ModuleName }}}/detail", handle.Detail)
|
||||
rg.POST("/{{{ .ModuleName }}}/add", handle.Add)
|
||||
rg.POST("/{{{ .ModuleName }}}/edit", handle.Edit)
|
||||
rg.POST("/{{{ .ModuleName }}}/del", handle.Del)
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package {{{ .ModuleName }}}
|
||||
|
||||
|
||||
//{{{ title (toCamelCase .EntityName) }}}ListReq {{{ .FunctionName }}}列表参数
|
||||
type {{{ title (toCamelCase .EntityName) }}}ListReq struct {
|
||||
{{{- range .Columns }}}
|
||||
{{{- if .IsQuery }}}
|
||||
{{{ title (toCamelCase .GoField) }}} {{{ .GoType }}} `form:"{{{ toCamelCase .GoField }}}"` // {{{ .ColumnComment }}}
|
||||
{{{- end }}}
|
||||
{{{- end }}}
|
||||
}
|
||||
|
||||
//{{{ title (toCamelCase .EntityName) }}}DetailReq {{{ .FunctionName }}}详情参数
|
||||
type {{{ title (toCamelCase .EntityName) }}}DetailReq struct {
|
||||
{{{- range .Columns }}}
|
||||
{{{- if .IsPk }}}
|
||||
{{{ title (toCamelCase .GoField) }}} {{{ .GoType }}} `form:"{{{ toCamelCase .GoField }}}"` // {{{ .ColumnComment }}}
|
||||
{{{- end }}}
|
||||
{{{- end }}}
|
||||
}
|
||||
|
||||
//{{{ title (toCamelCase .EntityName) }}}AddReq {{{ .FunctionName }}}新增参数
|
||||
type {{{ title (toCamelCase .EntityName) }}}AddReq struct {
|
||||
{{{- range .Columns }}}
|
||||
{{{- if .IsInsert }}}
|
||||
{{{ title (toCamelCase .GoField) }}} {{{ .GoType }}} `form:"{{{ toCamelCase .GoField }}}"` // {{{ .ColumnComment }}}
|
||||
{{{- end }}}
|
||||
{{{- end }}}
|
||||
}
|
||||
|
||||
//{{{ title (toCamelCase .EntityName) }}}EditReq {{{ .FunctionName }}}编辑参数
|
||||
type {{{ title (toCamelCase .EntityName) }}}EditReq struct {
|
||||
{{{- range .Columns }}}
|
||||
{{{- if .IsEdit }}}
|
||||
{{{ title (toCamelCase .GoField) }}} {{{ .GoType }}} `form:"{{{ toCamelCase .GoField }}}"` // {{{ .ColumnComment }}}
|
||||
{{{- end }}}
|
||||
{{{- end }}}
|
||||
}
|
||||
|
||||
//{{{ title (toCamelCase .EntityName) }}}DelReq {{{ .FunctionName }}}新增参数
|
||||
type {{{ title (toCamelCase .EntityName) }}}DelReq struct {
|
||||
{{{- range .Columns }}}
|
||||
{{{- if .IsPk }}}
|
||||
{{{ title (toCamelCase .GoField) }}} {{{ .GoType }}} `form:"{{{ toCamelCase .GoField }}}"` // {{{ .ColumnComment }}}
|
||||
{{{- end }}}
|
||||
{{{- end }}}
|
||||
}
|
||||
|
||||
//{{{ title (toCamelCase .EntityName) }}}Resp {{{ .FunctionName }}}返回信息
|
||||
type {{{ title (toCamelCase .EntityName) }}}Resp struct {
|
||||
{{{- range .Columns }}}
|
||||
{{{- if or .IsList .IsPk }}}
|
||||
{{{ title (toCamelCase .GoField) }}} {{{ .GoType }}} `json:"{{{ toCamelCase .GoField }}}" structs:"{{{ toCamelCase .GoField }}}"` // {{{ .ColumnComment }}}
|
||||
{{{- end }}}
|
||||
{{{- end }}}
|
||||
}
|
||||
157
server/admin/generator/tpl_utils/templates/gocode/service.go.tpl
Normal file
157
server/admin/generator/tpl_utils/templates/gocode/service.go.tpl
Normal file
@@ -0,0 +1,157 @@
|
||||
package {{{ .ModuleName }}}
|
||||
|
||||
import (
|
||||
"x_admin/core"
|
||||
"x_admin/core/request"
|
||||
"x_admin/core/response"
|
||||
"x_admin/model"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
import "x_admin/core"
|
||||
|
||||
type I{{{ title (toCamelCase .EntityName) }}}Service interface {
|
||||
List(page request.PageReq, listReq {{{ title (toCamelCase .EntityName) }}}ListReq) (res response.PageResp, e error)
|
||||
ListAll() (res []{{{ title (toCamelCase .EntityName) }}}Resp, e error)
|
||||
|
||||
Detail(id int) (res {{{ title (toCamelCase .EntityName) }}}Resp, e error)
|
||||
Add(addReq {{{ title (toCamelCase .EntityName) }}}AddReq) (e error)
|
||||
Edit(editReq {{{ title (toCamelCase .EntityName) }}}EditReq) (e error)
|
||||
Del(id int) (e error)
|
||||
}
|
||||
var Service=New{{{ title (toCamelCase .EntityName) }}}Service()
|
||||
//New{{{ title (toCamelCase .EntityName) }}}Service 初始化
|
||||
func New{{{ title (toCamelCase .EntityName) }}}Service() *{{{ toCamelCase .EntityName }}}Service {
|
||||
db := core.GetDB()
|
||||
return &{{{ toCamelCase .EntityName }}}Service{db: db}
|
||||
}
|
||||
|
||||
//{{{ toCamelCase .EntityName }}}Service {{{ .FunctionName }}}服务实现类
|
||||
type {{{ toCamelCase .EntityName }}}Service struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
//List {{{ .FunctionName }}}列表
|
||||
func (service {{{ toCamelCase .EntityName }}}Service) List(page request.PageReq, listReq {{{ title (toCamelCase .EntityName) }}}ListReq) (res response.PageResp, e error) {
|
||||
// 分页信息
|
||||
limit := page.PageSize
|
||||
offset := page.PageSize * (page.PageNo - 1)
|
||||
// 查询
|
||||
dbModel := service.db.Model(&model.{{{ title (toCamelCase .EntityName) }}}{})
|
||||
{{{- range .Columns }}}
|
||||
{{{- if .IsQuery }}}
|
||||
{{{- $queryOpr := index $.ModelOprMap .QueryType }}}
|
||||
{{{- if and (eq .GoType "string") (eq $queryOpr "like") }}}
|
||||
if listReq.{{{ title (toCamelCase .ColumnName) }}} != "" {
|
||||
dbModel = dbModel.Where("{{{ .ColumnName }}} like ?", "%"+listReq.{{{ title (toCamelCase .ColumnName) }}}+"%")
|
||||
}
|
||||
{{{- else }}}
|
||||
if listReq.{{{ title (toCamelCase .ColumnName) }}} {{{ if eq .GoType "string" }}}!= ""{{{ else }}}> 0{{{ end }}} {
|
||||
dbModel = dbModel.Where("{{{ .ColumnName }}} = ?", listReq.{{{ title (toCamelCase .ColumnName) }}})
|
||||
}
|
||||
{{{- end }}}
|
||||
{{{- end }}}
|
||||
{{{- end }}}
|
||||
{{{- if contains .AllFields "is_delete" }}}
|
||||
dbModel = dbModel.Where("is_delete = ?", 0)
|
||||
{{{- end }}}
|
||||
// 总数
|
||||
var count int64
|
||||
err := dbModel.Count(&count).Error
|
||||
if e = response.CheckErr(err, "List Count err"); e != nil {
|
||||
return
|
||||
}
|
||||
// 数据
|
||||
var objs []model.{{{ title (toCamelCase .EntityName) }}}
|
||||
err = dbModel.Limit(limit).Offset(offset).Order("id desc").Find(&objs).Error
|
||||
if e = response.CheckErr(err, "List Find err"); e != nil {
|
||||
return
|
||||
}
|
||||
resps := []{{{ title (toCamelCase .EntityName) }}}Resp{}
|
||||
response.Copy(&resps, objs)
|
||||
return response.PageResp{
|
||||
PageNo: page.PageNo,
|
||||
PageSize: page.PageSize,
|
||||
Count: count,
|
||||
Lists: resps,
|
||||
}, nil
|
||||
}
|
||||
//ListAll {{{ .FunctionName }}}列表
|
||||
func (service {{{ toCamelCase .EntityName }}}Service) ListAll() (res []{{{ title (toCamelCase .EntityName) }}}Resp, e error) {
|
||||
var objs []model.{{{ title (toCamelCase .EntityName) }}}
|
||||
|
||||
err := service.db.Find(&objs).Error
|
||||
if e = response.CheckErr(err, "ListAll Find err"); e != nil {
|
||||
return
|
||||
}
|
||||
response.Copy(&res, objs)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
//Detail {{{ .FunctionName }}}详情
|
||||
func (service {{{ toCamelCase .EntityName }}}Service) Detail(id int) (res {{{ title (toCamelCase .EntityName) }}}Resp, e error) {
|
||||
var obj model.{{{ title (toCamelCase .EntityName) }}}
|
||||
err := service.db.Where("{{{ $.PrimaryKey }}} = ?{{{ if contains .AllFields "is_delete" }}} AND is_delete = ?{{{ end }}}", id{{{ if contains .AllFields "is_delete" }}}, 0{{{ end }}}).Limit(1).First(&obj).Error
|
||||
if e = response.CheckErrDBNotRecord(err, "数据不存在!"); e != nil {
|
||||
return
|
||||
}
|
||||
if e = response.CheckErr(err, "Detail First err"); e != nil {
|
||||
return
|
||||
}
|
||||
response.Copy(&res, obj)
|
||||
{{{- range .Columns }}}
|
||||
{{{- if and .IsEdit (contains (slice "image" "avatar" "logo" "img") .GoField) }}}
|
||||
res.Avatar = util.UrlUtil.ToAbsoluteUrl(res.Avatar)
|
||||
{{{- end }}}
|
||||
{{{- end }}}
|
||||
return
|
||||
}
|
||||
|
||||
//Add {{{ .FunctionName }}}新增
|
||||
func (service {{{ toCamelCase .EntityName }}}Service) Add(addReq {{{ title (toCamelCase .EntityName) }}}AddReq) (e error) {
|
||||
var obj model.{{{ title (toCamelCase .EntityName) }}}
|
||||
response.Copy(&obj, addReq)
|
||||
err := service.db.Create(&obj).Error
|
||||
e = response.CheckErr(err, "Add Create err")
|
||||
return
|
||||
}
|
||||
|
||||
//Edit {{{ .FunctionName }}}编辑
|
||||
func (service {{{ toCamelCase .EntityName }}}Service) Edit(editReq {{{ title (toCamelCase .EntityName) }}}EditReq) (e error) {
|
||||
var obj model.{{{ title (toCamelCase .EntityName) }}}
|
||||
err := service.db.Where("{{{ $.PrimaryKey }}} = ?{{{ if contains .AllFields "is_delete" }}} AND is_delete = ?{{{ end }}}", editReq.Id{{{ if contains .AllFields "is_delete" }}}, 0{{{ end }}}).Limit(1).First(&obj).Error
|
||||
// 校验
|
||||
if e = response.CheckErrDBNotRecord(err, "数据不存在!"); e != nil {
|
||||
return
|
||||
}
|
||||
if e = response.CheckErr(err, "Edit First err"); e != nil {
|
||||
return
|
||||
}
|
||||
// 更新
|
||||
response.Copy(&obj, editReq)
|
||||
err = service.db.Model(&obj).Updates(obj).Error
|
||||
e = response.CheckErr(err, "Edit Updates err")
|
||||
return
|
||||
}
|
||||
|
||||
//Del {{{ .FunctionName }}}删除
|
||||
func (service {{{ toCamelCase .EntityName }}}Service) Del(id int) (e error) {
|
||||
var obj model.{{{ title (toCamelCase .EntityName) }}}
|
||||
err := service.db.Where("{{{ $.PrimaryKey }}} = ?{{{ if contains .AllFields "is_delete" }}} AND is_delete = ?{{{ end }}}", id{{{ if contains .AllFields "is_delete" }}}, 0{{{ end }}}).Limit(1).First(&obj).Error
|
||||
// 校验
|
||||
if e = response.CheckErrDBNotRecord(err, "数据不存在!"); e != nil {
|
||||
return
|
||||
}
|
||||
if e = response.CheckErr(err, "Del First err"); e != nil {
|
||||
return
|
||||
}
|
||||
// 删除
|
||||
{{{- if contains .AllFields "is_delete" }}}
|
||||
obj.IsDelete = 1
|
||||
err = service.db.Save(&obj).Error
|
||||
e = response.CheckErr(err, "Del Save err")
|
||||
{{{- else }}}
|
||||
err = service.db.Delete(&obj).Error
|
||||
e = response.CheckErr(err, "Del Delete err")
|
||||
{{{- end }}}
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user