feat✨ 添加api管理功能

This commit is contained in:
wenjianzhang committed 2021-05-10 18:24:47 +08:00
1 parent cd29c1728d
commit e091603bc8
3 files changed
+191

No files matched your search

+34
View File
@@ -0,0 +1,34 @@
package models
import (
// "gorm.io/gorm"
"go-admin/common/models"
)
type SysApi struct {
models.Model
Name string `json:"name" gorm:"type:varchar(128);comment:名称"`
Title string `json:"title" gorm:"type:varchar(128);comment:标题"`
Path string `json:"path" gorm:"type:varchar(128);comment:地址"`
Paths string `json:"paths" gorm:"type:varchar(128);comment:Paths"`
Action string `json:"action" gorm:"type:varchar(16);comment:类型"`
ParentId string `json:"parentId" gorm:"type:smallint(6);comment:按钮id"`
Sort string `json:"sort" gorm:"type:tinyint(4);comment:排序"`
models.ModelTime
models.ControlBy
}
func (SysApi) TableName() string {
return "sys_api"
}
func (e *SysApi) Generate() models.ActiveRecord {
o := *e
return &o
}
func (e *SysApi) GetId() interface{} {
return e.Id
}
+31
View File
@@ -0,0 +1,31 @@
package router
import (
"github.com/gin-gonic/gin"
jwt "github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth"
"go-admin/app/admin/models"
"go-admin/app/admin/service/dto"
"go-admin/common/actions"
"go-admin/common/middleware"
)
func init() {
routerCheckRole = append(routerCheckRole, registerSysApiRouter)
}
// 需认证的路由代码
func registerSysApiRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) {
r := v1.Group("/sys-api").Use(authMiddleware.MiddlewareFunc()).Use(middleware.AuthCheckRole())
{
model := &models.SysApi{}
r.GET("", actions.PermissionAction(), actions.IndexAction(model, new(dto.SysApiSearch), func() interface{} {
list := make([]models.SysApi, 0)
return &list
}))
r.GET("/:id", actions.PermissionAction(), actions.ViewAction(new(dto.SysApiById), nil))
r.POST("", actions.CreateAction(new(dto.SysApiControl)))
r.PUT("/:id", actions.PermissionAction(), actions.UpdateAction(new(dto.SysApiControl)))
r.DELETE("", actions.PermissionAction(), actions.DeleteAction(new(dto.SysApiById)))
}
}
+126
View File
@@ -0,0 +1,126 @@
package dto
import (
"errors"
"github.com/gin-gonic/gin"
"github.com/go-admin-team/go-admin-core/sdk/api"
"go-admin/app/admin/models"
"go-admin/common/dto"
common "go-admin/common/models"
)
type SysApiSearch struct {
dto.Pagination `search:"-"`
Name string `form:"name" search:"type:exact;column:name;table:sys_api" comment:"名称"`
Title string `form:"title" search:"type:exact;column:title;table:sys_api" comment:"标题"`
Path string `form:"path" search:"type:exact;column:path;table:sys_api" comment:"地址"`
Action string `form:"action" search:"type:exact;column:action;table:sys_api" comment:"类型"`
ParentId string `form:"parentId" search:"type:exact;column:parent_id;table:sys_api" comment:"按钮id"`
}
func (m *SysApiSearch) GetNeedSearch() interface{} {
return *m
}
func (m *SysApiSearch) Generate() dto.Index {
o := *m
return &o
}
func (m *SysApiSearch) Bind(ctx *gin.Context) error {
log := api.GetRequestLogger(ctx)
err := ctx.ShouldBind(m)
if err != nil {
log.Errorf("ShouldBind error: %s", err.Error())
}
return err
}
type SysApiControl struct {
Id int `uri:"id" comment:"编码"` // 编码
Name string `json:"name" comment:"名称"`
Title string `json:"title" comment:"标题"`
Path string `json:"path" comment:"地址"`
Paths string `json:"paths" comment:""`
Action string `json:"action" comment:"类型"`
ParentId string `json:"parentId" comment:"按钮id"`
Sort string `json:"sort" comment:"排序"`
}
func (s *SysApiControl) Generate() dto.Control {
o := *s
return &o
}
func (s *SysApiControl) Bind(ctx *gin.Context) error {
log := api.GetRequestLogger(ctx)
err := ctx.ShouldBindUri(s)
if err != nil {
log.Errorf("ShouldBindUri error: %s", err.Error())
return errors.New("数据绑定出错")
}
err = ctx.ShouldBind(s)
if err != nil {
log.Errorf("ShouldBind error: %s", err.Error())
err = errors.New("数据绑定出错")
}
return err
}
func (s *SysApiControl) GenerateM() (common.ActiveRecord, error) {
return &models.SysApi{
Model: common.Model{Id: s.Id},
Name: s.Name,
Title: s.Title,
Path: s.Path,
Paths: s.Paths,
Action: s.Action,
ParentId: s.ParentId,
Sort: s.Sort,
}, nil
}
func (s *SysApiControl) GetId() interface{} {
return s.Id
}
type SysApiById struct {
dto.ObjectById
}
func (s *SysApiById) GetId() interface{} {
if len(s.Ids) > 0 {
s.Ids = append(s.Ids, s.Id)
return s.Ids
}
return s.Id
}
func (s *SysApiById) Bind(ctx *gin.Context) error {
log := api.GetRequestLogger(ctx)
err := ctx.ShouldBindUri(s)
if err != nil {
log.Errorf("ShouldBindUri error: %s", err.Error())
return errors.New("数据绑定出错")
}
err = ctx.ShouldBind(s)
if err != nil {
log.Errorf("ShouldBind error: %s", err.Error())
err = errors.New("数据绑定出错")
}
return err
}
func (s *SysApiById) SetUpdateBy(id int) {
}
func (s *SysApiById) Generate() dto.Control {
o := *s
return &o
}
func (s *SysApiById) GenerateM() (common.ActiveRecord, error) {
return &models.SysApi{}, nil
}