@@ -3,11 +3,10 @@ package system
33import (
44 "errors"
55 "fmt"
6-
76 "github.com/flipped-aurora/gin-vue-admin/server/global"
87 "github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
98 "github.com/flipped-aurora/gin-vue-admin/server/model/system"
10-
9+ systemRes "github.com/flipped-aurora/gin-vue-admin/server/model/system/response"
1110 "gorm.io/gorm"
1211)
1312
@@ -28,6 +27,112 @@ func (apiService *ApiService) CreateApi(api system.SysApi) (err error) {
2827 return global .GVA_DB .Create (& api ).Error
2928}
3029
30+ func (apiService * ApiService ) GetApiGroups () (groups []string , err error ) {
31+ err = global .GVA_DB .Model (& system.SysApi {}).Select ("DISTINCT api_group" ).Pluck ("api_group" , & groups ).Error
32+ return
33+ }
34+
35+ func (apiService * ApiService ) SyncApi () (newApis , deleteApis , ignoreApis []system.SysApi , err error ) {
36+ newApis = make ([]system.SysApi , 0 )
37+ deleteApis = make ([]system.SysApi , 0 )
38+ ignoreApis = make ([]system.SysApi , 0 )
39+ var apis []system.SysApi
40+ err = global .GVA_DB .Find (& apis ).Error
41+ if err != nil {
42+ return
43+ }
44+ var ignores []system.SysIgnoreApi
45+ err = global .GVA_DB .Find (& ignores ).Error
46+ if err != nil {
47+ return
48+ }
49+
50+ for i := range ignores {
51+ ignoreApis = append (ignoreApis , system.SysApi {
52+ Path : ignores [i ].Path ,
53+ Description : "" ,
54+ ApiGroup : "" ,
55+ Method : ignores [i ].Method ,
56+ })
57+ }
58+
59+ var cacheApis []system.SysApi
60+ for i := range global .GVA_ROUTERS {
61+ ignoresFlag := false
62+ for j := range ignores {
63+ if ignores [j ].Path == global .GVA_ROUTERS [i ].Path && ignores [j ].Method == global .GVA_ROUTERS [i ].Method {
64+ ignoresFlag = true
65+ }
66+ }
67+ if ! ignoresFlag {
68+ cacheApis = append (cacheApis , system.SysApi {
69+ Path : global .GVA_ROUTERS [i ].Path ,
70+ Method : global .GVA_ROUTERS [i ].Method ,
71+ })
72+ }
73+ }
74+
75+ //对比数据库中的api和内存中的api,如果数据库中的api不存在于内存中,则把api放入删除数组,如果内存中的api不存在于数据库中,则把api放入新增数组
76+ for i := range cacheApis {
77+ var flag bool
78+ // 如果存在于内存不存在于api数组中
79+ for j := range apis {
80+ if cacheApis [i ].Path == apis [j ].Path && cacheApis [i ].Method == apis [j ].Method {
81+ flag = true
82+ }
83+ }
84+ if ! flag {
85+ newApis = append (newApis , system.SysApi {
86+ Path : cacheApis [i ].Path ,
87+ Description : "" ,
88+ ApiGroup : "" ,
89+ Method : cacheApis [i ].Method ,
90+ })
91+ }
92+ }
93+
94+ for i := range apis {
95+ var flag bool
96+ // 如果存在于api数组不存在于内存
97+ for j := range cacheApis {
98+ if cacheApis [j ].Path == apis [i ].Path && cacheApis [j ].Method == apis [i ].Method {
99+ flag = true
100+ }
101+ }
102+ if ! flag {
103+ deleteApis = append (deleteApis , apis [i ])
104+ }
105+ }
106+ return
107+ }
108+
109+ func (apiService * ApiService ) IgnoreApi (ignoreApi system.SysIgnoreApi ) (err error ) {
110+ if ignoreApi .Flag {
111+ return global .GVA_DB .Create (& ignoreApi ).Error
112+ }
113+ return global .GVA_DB .Unscoped ().Delete (& ignoreApi , "path = ? AND method = ?" , ignoreApi .Path , ignoreApi .Method ).Error
114+ }
115+
116+ func (apiService * ApiService ) EnterSyncApi (syncApis systemRes.SysSyncApis ) (err error ) {
117+ return global .GVA_DB .Transaction (func (tx * gorm.DB ) error {
118+ var txErr error
119+ if syncApis .NewApis != nil && len (syncApis .NewApis ) > 0 {
120+ txErr = tx .Create (& syncApis .NewApis ).Error
121+ if txErr != nil {
122+ return txErr
123+ }
124+ }
125+ for i := range syncApis .DeleteApis {
126+ CasbinServiceApp .ClearCasbin (1 , syncApis .DeleteApis [i ].Path , syncApis .DeleteApis [i ].Method )
127+ txErr = tx .Delete (& system.SysApi {}, "path = ? AND method = ?" , syncApis .DeleteApis [i ].Path , syncApis .DeleteApis [i ].Method ).Error
128+ if txErr != nil {
129+ return txErr
130+ }
131+ }
132+ return nil
133+ })
134+ }
135+
31136//@author: [piexlmax](https://github.com/piexlmax)
32137//@function: DeleteApi
33138//@description: 删除基础api
@@ -44,9 +149,7 @@ func (apiService *ApiService) DeleteApi(api system.SysApi) (err error) {
44149 if err != nil {
45150 return err
46151 }
47- if ! CasbinServiceApp .ClearCasbin (1 , entity .Path , entity .Method ) {
48- return errors .New ("ClearCasbin 失败" )
49- }
152+ CasbinServiceApp .ClearCasbin (1 , entity .Path , entity .Method )
50153 return nil
51154}
52155
0 commit comments