基础项目
Some checks failed
CI / init (pull_request) Has been cancelled
CI / Frontend node 18.16.0 (pull_request) Has been cancelled
CI / Backend go (1.22) (pull_request) Has been cancelled
CI / release-pr (pull_request) Has been cancelled
CI / devops-test (1.22, 18.16.0) (pull_request) Has been cancelled
CI / release-please (pull_request) Has been cancelled
CI / devops-prod (1.22, 18.x) (pull_request) Has been cancelled
CI / docker (pull_request) Has been cancelled

This commit is contained in:
2026-04-26 15:32:21 +08:00
parent cc40d743cb
commit 1e33640629
102 changed files with 4088 additions and 197 deletions

View File

@@ -134,6 +134,9 @@ func (apiService *ApiService) IgnoreApi(ignoreApi system.SysIgnoreApi) (err erro
}
func (apiService *ApiService) EnterSyncApi(syncApis systemRes.SysSyncApis) (err error) {
if len(syncApis.NewApis) == 0 && len(syncApis.DeleteApis) == 0 {
return apiService.SyncApiToDB()
}
return global.GVA_DB.Transaction(func(tx *gorm.DB) error {
var txErr error
if len(syncApis.NewApis) > 0 {
@@ -153,6 +156,87 @@ func (apiService *ApiService) EnterSyncApi(syncApis systemRes.SysSyncApis) (err
})
}
func (apiService *ApiService) SyncApiToDB() (err error) {
return global.GVA_DB.Transaction(func(tx *gorm.DB) error {
var dbApis []system.SysApi
if err := tx.Find(&dbApis).Error; err != nil {
return err
}
var ignores []system.SysIgnoreApi
if err := tx.Find(&ignores).Error; err != nil {
return err
}
ignoreMap := make(map[string]bool, len(ignores))
for i := range ignores {
ignoreMap[apiRouteKey(ignores[i].Path, ignores[i].Method)] = true
}
dbMap := make(map[string]system.SysApi, len(dbApis))
for i := range dbApis {
dbMap[apiRouteKey(dbApis[i].Path, dbApis[i].Method)] = dbApis[i]
}
routeMap := make(map[string]system.SysApi, len(global.GVA_ROUTERS))
for i := range global.GVA_ROUTERS {
path := global.GVA_ROUTERS[i].Path
method := global.GVA_ROUTERS[i].Method
key := apiRouteKey(path, method)
if ignoreMap[key] {
continue
}
routeMap[key] = system.SysApi{
Path: path,
Method: method,
ApiGroup: defaultApiGroup(path),
Description: defaultApiDescription(method, path),
}
}
newApis := make([]system.SysApi, 0)
for key, routeApi := range routeMap {
if _, ok := dbMap[key]; !ok {
newApis = append(newApis, routeApi)
}
}
if len(newApis) > 0 {
if err := tx.Create(&newApis).Error; err != nil {
return err
}
}
for i := range dbApis {
key := apiRouteKey(dbApis[i].Path, dbApis[i].Method)
if _, ok := routeMap[key]; ok {
continue
}
CasbinServiceApp.ClearCasbin(1, dbApis[i].Path, dbApis[i].Method)
if err := tx.Delete(&system.SysApi{}, "path = ? AND method = ?", dbApis[i].Path, dbApis[i].Method).Error; err != nil {
return err
}
}
return nil
})
}
func apiRouteKey(path, method string) string {
return method + " " + path
}
func defaultApiGroup(path string) string {
pathArr := strings.Split(strings.Trim(path, "/"), "/")
if len(pathArr) == 0 || pathArr[0] == "" {
return "default"
}
return pathArr[0]
}
func defaultApiDescription(method, path string) string {
return method + " " + path
}
//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteApi
//@description: 删除基础api