feat✨ 添加api管理
This commit is contained in:
1 parent
70998c424d
commit
d0b2e8d03f
6 files changed
+395
-53
No files matched your search
@@ -0,0 +1,259 @@
|
||||
package apis
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth/user"
|
||||
_ "github.com/go-admin-team/go-admin-core/sdk/pkg/response"
|
||||
|
||||
"go-admin/app/admin/models"
|
||||
"go-admin/app/admin/service"
|
||||
"go-admin/app/admin/service/dto"
|
||||
"go-admin/common/actions"
|
||||
"go-admin/common/apis"
|
||||
)
|
||||
|
||||
type SysApi struct {
|
||||
apis.Api
|
||||
}
|
||||
|
||||
// GetSysApiList 获取接口管理列表
|
||||
// @Summary 获取接口管理列表
|
||||
// @Description 获取接口管理列表
|
||||
// @Tags 接口管理
|
||||
// @Param name query string false "名称"
|
||||
// @Param title query string false "标题"
|
||||
// @Param path query string false "地址"
|
||||
// @Param action query string false "类型"
|
||||
// @Param parentId query string false "按钮id"
|
||||
// @Param pageSize query int false "页条数"
|
||||
// @Param pageIndex query int false "页码"
|
||||
// @Success 200 {object} response.Response{data=response.Page{list=[]models.SysApi}} "{"code": 200, "data": [...]}"
|
||||
// @Router /api/v1/sys_api [get]
|
||||
// @Security Bearer
|
||||
func (e SysApi) GetSysApiList(c *gin.Context) {
|
||||
e.MakeContext(c)
|
||||
log := e.GetLogger()
|
||||
db, err := e.GetOrm()
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
d := new(dto.SysApiSearch)
|
||||
//查询列表
|
||||
err = d.Bind(c)
|
||||
if err != nil {
|
||||
log.Warnf("request body bind error, %s", err.Error())
|
||||
e.Error(http.StatusUnprocessableEntity, err, "参数验证失败")
|
||||
return
|
||||
}
|
||||
|
||||
//数据权限检查
|
||||
p := actions.GetPermissionFromContext(c)
|
||||
|
||||
list := make([]models.SysApi, 0)
|
||||
var count int64
|
||||
serviceSysApi := service.SysApi{}
|
||||
serviceSysApi.Log = log
|
||||
serviceSysApi.Orm = db
|
||||
err = serviceSysApi.GetSysApiPage(d, p, &list, &count)
|
||||
if err != nil {
|
||||
log.Errorf("Get SysApi Page error, %s", err.Error())
|
||||
e.Error(http.StatusInternalServerError, err, "查询失败")
|
||||
return
|
||||
}
|
||||
|
||||
e.PageOK(list, int(count), d.GetPageIndex(), d.GetPageSize(), "查询成功")
|
||||
}
|
||||
|
||||
// GetSysApi 获取接口管理
|
||||
// @Summary 获取接口管理
|
||||
// @Description 获取接口管理
|
||||
// @Tags 接口管理
|
||||
// @Param id path string false "id"
|
||||
// @Success 200 {object} response.Response{data=models.SysApi} "{"code": 200, "data": [...]}"
|
||||
// @Router /api/v1/sys_api/{id} [get]
|
||||
// @Security Bearer
|
||||
func (e SysApi) GetSysApi(c *gin.Context) {
|
||||
e.MakeContext(c)
|
||||
log := e.GetLogger()
|
||||
db, err := e.GetOrm()
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
control := new(dto.SysApiById)
|
||||
|
||||
//查看详情
|
||||
err = control.Bind(c)
|
||||
if err != nil {
|
||||
log.Warnf("request body bind error, %s", err.Error())
|
||||
e.Error(http.StatusUnprocessableEntity, err, "参数验证失败")
|
||||
return
|
||||
}
|
||||
var object models.SysApi
|
||||
|
||||
//数据权限检查
|
||||
p := actions.GetPermissionFromContext(c)
|
||||
|
||||
serviceSysApi := service.SysApi{}
|
||||
serviceSysApi.Log = log
|
||||
serviceSysApi.Orm = db
|
||||
err = serviceSysApi.GetSysApi(control, p, &object)
|
||||
if err != nil {
|
||||
log.Errorf("Get SysApi error, %s", err.Error())
|
||||
e.Error(http.StatusInternalServerError, err, "查询失败")
|
||||
return
|
||||
}
|
||||
|
||||
e.OK(object, "查看成功")
|
||||
}
|
||||
|
||||
// InsertSysApi 创建接口管理
|
||||
// @Summary 创建接口管理
|
||||
// @Description 创建接口管理
|
||||
// @Tags 接口管理
|
||||
// @Accept application/json
|
||||
// @Product application/json
|
||||
// @Param data body dto.SysApiControl true "data"
|
||||
// @Success 200 {object} response.Response "{"code": 200, "message": "添加成功"}"
|
||||
// @Router /api/v1/sys_api [post]
|
||||
// @Security Bearer
|
||||
//func (e SysApi) InsertSysApi(c *gin.Context) {
|
||||
// e.MakeContext(c)
|
||||
// log := e.GetLogger()
|
||||
// db, err := e.GetOrm()
|
||||
// if err != nil {
|
||||
// log.Error(err)
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// control := new(dto.SysApiControl)
|
||||
//
|
||||
// //新增操作
|
||||
// err = control.Bind(c)
|
||||
// if err != nil {
|
||||
// log.Warnf("request body bind error, %s", err.Error())
|
||||
// e.Error(http.StatusUnprocessableEntity, err, "参数验证失败")
|
||||
// return
|
||||
// }
|
||||
// object, err := control.GenerateM()
|
||||
// if err != nil {
|
||||
// log.Errorf("generate SysApi model error, %s", err.Error())
|
||||
// e.Error(http.StatusInternalServerError, err, "模型生成失败")
|
||||
// return
|
||||
// }
|
||||
// // 设置创建人
|
||||
// object.SetCreateBy(user.GetUserId(c))
|
||||
//
|
||||
// serviceSysApi := service.SysApi{}
|
||||
// serviceSysApi.Orm = db
|
||||
// serviceSysApi.Log = log
|
||||
// err = serviceSysApi.InsertSysApi(object.(*models.SysApi))
|
||||
// if err != nil {
|
||||
// log.Errorf("Insert SysApi error, %s", err.Error())
|
||||
// e.Error(http.StatusInternalServerError, err, "创建失败")
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// e.OK(object.GetId(), "创建成功")
|
||||
//}
|
||||
|
||||
// UpdateSysApi 修改接口管理
|
||||
// @Summary 修改接口管理
|
||||
// @Description 修改接口管理
|
||||
// @Tags 接口管理
|
||||
// @Accept application/json
|
||||
// @Product application/json
|
||||
// @Param data body dto.SysApiControl true "body"
|
||||
// @Success 200 {object} response.Response "{"code": 200, "message": "修改成功"}"
|
||||
// @Router /api/v1/sys_api/{id} [put]
|
||||
// @Security Bearer
|
||||
func (e SysApi) UpdateSysApi(c *gin.Context) {
|
||||
e.MakeContext(c)
|
||||
log := e.GetLogger()
|
||||
db, err := e.GetOrm()
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
control := new(dto.SysApiControl)
|
||||
|
||||
//更新操作
|
||||
err = control.Bind(c)
|
||||
if err != nil {
|
||||
log.Warnf("request body bind error, %s", err.Error())
|
||||
e.Error(http.StatusUnprocessableEntity, err, "参数验证失败")
|
||||
return
|
||||
}
|
||||
object, err := control.GenerateM()
|
||||
if err != nil {
|
||||
log.Errorf("generate SysApi model error, %s", err.Error())
|
||||
e.Error(http.StatusInternalServerError, err, "模型生成失败")
|
||||
return
|
||||
}
|
||||
object.SetUpdateBy(user.GetUserId(c))
|
||||
|
||||
//数据权限检查
|
||||
p := actions.GetPermissionFromContext(c)
|
||||
|
||||
serviceSysApi := service.SysApi{}
|
||||
serviceSysApi.Orm = db
|
||||
serviceSysApi.Log = log
|
||||
err = serviceSysApi.UpdateSysApi(object.(*models.SysApi), p)
|
||||
if err != nil {
|
||||
log.Errorf("Update SysApi error, %s", err.Error())
|
||||
e.Error(http.StatusInternalServerError, err, "更新失败")
|
||||
return
|
||||
}
|
||||
e.OK(object.GetId(), "更新成功")
|
||||
}
|
||||
|
||||
// DeleteSysApi 删除接口管理
|
||||
// @Summary 删除接口管理
|
||||
// @Description 删除接口管理
|
||||
// @Tags 接口管理
|
||||
// @Param ids body []int false "ids"
|
||||
// @Success 200 {object} response.Response "{"code": 200, "message": "删除成功"}"
|
||||
// @Router /api/v1/sys_api [delete]
|
||||
// @Security Bearer
|
||||
func (e SysApi) DeleteSysApi(c *gin.Context) {
|
||||
e.MakeContext(c)
|
||||
log := e.GetLogger()
|
||||
db, err := e.GetOrm()
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
control := new(dto.SysApiById)
|
||||
|
||||
//删除操作
|
||||
err = control.Bind(c)
|
||||
if err != nil {
|
||||
log.Warnf("request body bind error, %s", err.Error())
|
||||
e.Error(http.StatusUnprocessableEntity, err, "参数验证失败")
|
||||
return
|
||||
}
|
||||
|
||||
// 设置编辑人
|
||||
control.SetUpdateBy(user.GetUserId(c))
|
||||
|
||||
// 数据权限检查
|
||||
p := actions.GetPermissionFromContext(c)
|
||||
|
||||
serviceSysApi := service.SysApi{}
|
||||
serviceSysApi.Orm = db
|
||||
serviceSysApi.Log = log
|
||||
err = serviceSysApi.RemoveSysApi(control, p)
|
||||
if err != nil {
|
||||
log.Errorf("Remove SysApi error, %s", err.Error())
|
||||
e.Error(http.StatusInternalServerError, err, "删除失败")
|
||||
return
|
||||
}
|
||||
e.OK(control.GetId(), "删除成功")
|
||||
}
|
||||
@@ -3,20 +3,17 @@ package models
|
||||
import (
|
||||
// "gorm.io/gorm"
|
||||
|
||||
"errors"
|
||||
"github.com/go-admin-team/go-admin-core/sdk/pkg"
|
||||
"go-admin/common/models"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type SysApi struct {
|
||||
Id int `json:"id" gorm:"primaryKey;autoIncrement;comment:主键编码"`
|
||||
Name string `json:"name" gorm:"type:varchar(128);comment:名称"`
|
||||
Handle string `json:"handle" gorm:"type:varchar(128);comment:handle"`
|
||||
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 int `json:"parentId" gorm:"type:smallint(6);comment:按钮id"`
|
||||
ParentId int `json:"parentId" gorm:"comment:按钮id"`
|
||||
Sort int `json:"sort" gorm:"type:tinyint(4);comment:排序"`
|
||||
models.ModelTime
|
||||
models.ControlBy
|
||||
@@ -33,34 +30,4 @@ func (e *SysApi) Generate() models.ActiveRecord {
|
||||
|
||||
func (e *SysApi) GetId() interface{} {
|
||||
return e.Id
|
||||
}
|
||||
|
||||
func (e *SysApi) Create(tx *gorm.DB) (id int, err error) {
|
||||
result := tx.Table(e.TableName()).Create(&e)
|
||||
if result.Error != nil {
|
||||
err = result.Error
|
||||
return
|
||||
}
|
||||
err = e.InitPaths(tx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
id = e.Id
|
||||
return
|
||||
}
|
||||
|
||||
func (e *SysApi) InitPaths(tx *gorm.DB) (err error) {
|
||||
parentMenu := new(Menu)
|
||||
if e.ParentId != 0 {
|
||||
tx.Table("sys_api").Where("id = ?", e.ParentId).First(parentMenu)
|
||||
if parentMenu.Paths == "" {
|
||||
err = errors.New("父级paths异常,请尝试对当前节点父级菜单进行更新操作!")
|
||||
return
|
||||
}
|
||||
e.Paths = parentMenu.Paths + "/" + pkg.IntToString(e.Id)
|
||||
} else {
|
||||
e.Paths = "/0/" + pkg.IntToString(e.Id)
|
||||
}
|
||||
tx.Table("sys_api").Where("id = ?", e.ParentId).Update("paths", e.Paths)
|
||||
return
|
||||
}
|
||||
}
|
||||
+10
-14
@@ -3,9 +3,8 @@ 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/app/admin/apis"
|
||||
"go-admin/common/middleware"
|
||||
)
|
||||
|
||||
@@ -13,18 +12,15 @@ func init() {
|
||||
routerCheckRole = append(routerCheckRole, registerSysApiRouter)
|
||||
}
|
||||
|
||||
// 需认证的路由代码
|
||||
// registerSysApiRouter
|
||||
func registerSysApiRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) {
|
||||
api := apis.SysApi{}
|
||||
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)))
|
||||
r.GET("", api.GetSysApiList)
|
||||
r.GET("/:id", api.GetSysApi)
|
||||
//r.POST("", api.InsertSysApi)
|
||||
r.PUT("/:id", api.UpdateSysApi)
|
||||
//r.DELETE("", api.DeleteSysApi)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -39,7 +39,7 @@ func (m *SysApiSearch) Bind(ctx *gin.Context) error {
|
||||
|
||||
type SysApiControl struct {
|
||||
Id int `uri:"id" comment:"编码"` // 编码
|
||||
Name string `json:"name" comment:"名称"`
|
||||
Handle string `json:"handle" comment:"handle"`
|
||||
Title string `json:"title" comment:"标题"`
|
||||
Path string `json:"path" comment:"地址"`
|
||||
Paths string `json:"paths" comment:""`
|
||||
@@ -71,7 +71,7 @@ func (s *SysApiControl) Bind(ctx *gin.Context) error {
|
||||
func (s *SysApiControl) GenerateM() (common.ActiveRecord, error) {
|
||||
return &models.SysApi{
|
||||
Id: s.Id,
|
||||
Name: s.Name,
|
||||
Handle: s.Handle,
|
||||
Title: s.Title,
|
||||
Path: s.Path,
|
||||
Paths: s.Paths,
|
||||
@@ -123,4 +123,4 @@ func (s *SysApiById) Generate() dto.Control {
|
||||
|
||||
func (s *SysApiById) GenerateM() (common.ActiveRecord, error) {
|
||||
return &models.SysApi{}, nil
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/go-admin-team/go-admin-core/sdk/runtime"
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
"go-admin/app/admin/models"
|
||||
"go-admin/app/admin/service/dto"
|
||||
"go-admin/common/actions"
|
||||
cDto "go-admin/common/dto"
|
||||
"go-admin/common/service"
|
||||
)
|
||||
|
||||
type SysApi struct {
|
||||
service.Service
|
||||
}
|
||||
|
||||
// GetSysApiPage 获取SysApi列表
|
||||
func (e *SysApi) GetSysApiPage(c *dto.SysApiSearch, p *actions.DataPermission, list *[]models.SysApi, count *int64) error {
|
||||
var err error
|
||||
var data models.SysApi
|
||||
|
||||
err = e.Orm.Model(&data).
|
||||
Scopes(
|
||||
cDto.MakeCondition(c.GetNeedSearch()),
|
||||
cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
|
||||
actions.Permission(data.TableName(), p),
|
||||
).
|
||||
Find(list).Limit(-1).Offset(-1).
|
||||
Count(count).Error
|
||||
if err != nil {
|
||||
e.Log.Errorf("Service GetSysApiPage error:%s", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetSysApi 获取SysApi对象
|
||||
func (e *SysApi) GetSysApi(d *dto.SysApiById, p *actions.DataPermission, model *models.SysApi) error {
|
||||
var data models.SysApi
|
||||
|
||||
err := e.Orm.Model(&data).
|
||||
Scopes(
|
||||
actions.Permission(data.TableName(), p),
|
||||
).
|
||||
First(model, d.GetId()).Error
|
||||
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
err = errors.New("查看对象不存在或无权查看")
|
||||
e.Log.Errorf("Service GetSysApi error:%s", err)
|
||||
return err
|
||||
}
|
||||
if err != nil {
|
||||
e.Log.Errorf("db error:%s", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// InsertSysApi 创建SysApi对象
|
||||
func (e *SysApi) CheckStorageSysApi(c *[]runtime.Router) error {
|
||||
for _, v := range *c {
|
||||
err := e.Orm.Debug().Where(models.SysApi{Path: v.RelativePath, Action: v.HttpMethod}).Attrs(models.SysApi{Handle: v.Handler}).FirstOrCreate(&models.SysApi{}).Error
|
||||
if err != nil {
|
||||
err := fmt.Errorf("Service CheckStorageSysApi error: %s \r\n ", err.Error())
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateSysApi 修改SysApi对象
|
||||
func (e *SysApi) UpdateSysApi(c *models.SysApi, p *actions.DataPermission) error {
|
||||
db := e.Orm.Model(c).
|
||||
Scopes(
|
||||
actions.Permission(c.TableName(), p),
|
||||
).Where(c.GetId()).Updates(c)
|
||||
if err := db.Error; err != nil {
|
||||
e.Log.Errorf("Service UpdateSysApi error:%s", err)
|
||||
return err
|
||||
}
|
||||
if db.RowsAffected == 0 {
|
||||
return errors.New("无权更新该数据")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveSysApi 删除SysApi
|
||||
func (e *SysApi) RemoveSysApi(d *dto.SysApiById, p *actions.DataPermission) error {
|
||||
var data models.SysApi
|
||||
|
||||
db := e.Orm.Model(&data).
|
||||
Scopes(
|
||||
actions.Permission(data.TableName(), p),
|
||||
).Delete(&data, d.GetId())
|
||||
if err := db.Error; err != nil {
|
||||
e.Log.Errorf("Service RemoveSysApi error:%s", err)
|
||||
return err
|
||||
}
|
||||
if db.RowsAffected == 0 {
|
||||
return errors.New("无权删除该数据")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package models
|
||||
|
||||
type Routers struct {
|
||||
List []Router
|
||||
}
|
||||
|
||||
type Router struct {
|
||||
HttpMethod, RelativePath string
|
||||
}
|
||||
|
||||
func (e *Routers) Add(HttpMethod, RelativePath string) *Routers {
|
||||
e.List = append(e.List, Router{HttpMethod: HttpMethod,RelativePath: RelativePath})
|
||||
return e
|
||||
}
|
||||
Reference in new issue
Block a user