Skip to content

Commit b757fd7

Browse files
committed
feat: 完善i18n回滚功能
1 parent a4217d1 commit b757fd7

File tree

4 files changed

+120
-2
lines changed

4 files changed

+120
-2
lines changed

server/go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ require (
3636
github.com/swaggo/gin-swagger v1.6.0
3737
github.com/swaggo/swag v1.16.2
3838
github.com/tencentyun/cos-go-sdk-v5 v0.7.42
39+
github.com/tidwall/gjson v1.17.3
40+
github.com/tidwall/sjson v1.2.5
3941
github.com/unrolled/secure v1.13.0
4042
github.com/xuri/excelize/v2 v2.8.0
4143
go.mongodb.org/mongo-driver v1.12.1
@@ -129,6 +131,8 @@ require (
129131
github.com/spf13/pflag v1.0.5 // indirect
130132
github.com/subosito/gotenv v1.4.2 // indirect
131133
github.com/therootcompany/xz v1.0.1 // indirect
134+
github.com/tidwall/match v1.1.1 // indirect
135+
github.com/tidwall/pretty v1.2.0 // indirect
132136
github.com/tklauser/go-sysconf v0.3.11 // indirect
133137
github.com/tklauser/numcpus v0.6.0 // indirect
134138
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect

server/go.sum

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,16 @@ github.com/tencentyun/cos-go-sdk-v5 v0.7.42 h1:Up1704BJjI5orycXKjpVpvuOInt9GC5pq
464464
github.com/tencentyun/cos-go-sdk-v5 v0.7.42/go.mod h1:LUFnaqRmGk6pEHOaRmdn2dCZR2j0cSsM5xowWFPTPao=
465465
github.com/therootcompany/xz v1.0.1 h1:CmOtsn1CbtmyYiusbfmhmkpAAETj0wBIH6kCYaX+xzw=
466466
github.com/therootcompany/xz v1.0.1/go.mod h1:3K3UH1yCKgBneZYhuQUvJ9HPD19UEXEI0BWbMn8qNMY=
467+
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
468+
github.com/tidwall/gjson v1.17.3 h1:bwWLZU7icoKRG+C+0PNwIKC6FCJO/Q3p2pZvuP0jN94=
469+
github.com/tidwall/gjson v1.17.3/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
470+
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
471+
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
467472
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
473+
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
474+
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
475+
github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
476+
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
468477
github.com/tklauser/go-sysconf v0.3.11 h1:89WgdJhk5SNwJfu+GKyYveZ4IaJ7xAkecBo+KdJV0CM=
469478
github.com/tklauser/go-sysconf v0.3.11/go.mod h1:GqXfhXY3kiPa0nAXPDIQIWzJbMCB7AmcWpGR8lSZfqI=
470479
github.com/tklauser/numcpus v0.6.0 h1:kebhY2Qt+3U6RNK7UqpYNA+tJ23IBEGKkB7JQBfDYms=

server/resource/lang/zh.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,6 @@
295295
"pluginInstall": "插件安装",
296296
"pluginMarket": "插件市场",
297297
"pluginSystem": "插件系统",
298-
"ppppClients": "clients表",
299298
"resumableUpload": "断点续传(插件版)",
300299
"resume": "断点续传(插件版)",
301300
"roleManage": "角色管理",

server/service/system/auto_code_history.go

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"github.com/flipped-aurora/gin-vue-admin/server/utils/ast"
88
"github.com/pkg/errors"
9+
"os"
910
"path"
1011
"path/filepath"
1112
"strconv"
@@ -17,7 +18,7 @@ import (
1718
model "github.com/flipped-aurora/gin-vue-admin/server/model/system"
1819
request "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
1920
"github.com/flipped-aurora/gin-vue-admin/server/utils"
20-
21+
"github.com/tidwall/sjson"
2122
"go.uber.org/zap"
2223
)
2324

@@ -79,12 +80,34 @@ func (s *autoCodeHistory) RollBack(ctx context.Context, info request.SysAutoHist
7980
if err != nil {
8081
global.GVA_LOG.Error("ClearTag DeleteApiByIds:", zap.Error(err))
8182
}
83+
localPath := path.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "resource", "lang")
84+
files, err := s.readJSONFiles(localPath)
85+
if err != nil {
86+
return err
87+
}
88+
for _, file := range files {
89+
err := s.reWriteI18nJson(file, "api", history.Package, history.StructName)
90+
if err != nil {
91+
return err
92+
}
93+
}
8294
} // 清除API表
8395
if info.DeleteMenu {
8496
err = BaseMenuServiceApp.DeleteBaseMenu(int(history.MenuID))
8597
if err != nil {
8698
return errors.Wrap(err, "删除菜单失败!")
8799
}
100+
localPath := path.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "resource", "lang")
101+
files, err := s.readJSONFiles(localPath)
102+
if err != nil {
103+
return err
104+
}
105+
for _, file := range files {
106+
err := s.reWriteI18nJson(file, "menu", history.Package, history.StructName)
107+
if err != nil {
108+
return err
109+
}
110+
}
88111
} // 清除菜单表
89112
if info.DeleteTable {
90113
err = s.DropTable(history.BusinessDB, history.Table)
@@ -178,6 +201,17 @@ func (s *autoCodeHistory) RollBack(ctx context.Context, info request.SysAutoHist
178201
if err != nil {
179202
return errors.Wrap(err, "更新失败!")
180203
}
204+
localPath := path.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Web, "locales")
205+
files, err := s.readJSONFiles(localPath)
206+
if err != nil {
207+
return err
208+
}
209+
for _, file := range files {
210+
err := s.reWriteI18nJson(file, "web", history.Package, history.StructName)
211+
if err != nil {
212+
return err
213+
}
214+
}
181215
return nil
182216
}
183217

@@ -215,3 +249,75 @@ func (s *autoCodeHistory) DropTable(BusinessDb, tableName string) error {
215249
return global.GVA_DB.Exec("DROP TABLE " + tableName).Error
216250
}
217251
}
252+
253+
func (s *autoCodeHistory) readJSONFiles(dir string) ([]string, error) {
254+
var files []string
255+
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
256+
if err != nil {
257+
return err
258+
}
259+
if !info.IsDir() && filepath.Ext(path) == ".json" {
260+
files = append(files, path)
261+
}
262+
return nil
263+
})
264+
if err != nil {
265+
return nil, err
266+
}
267+
return files, nil
268+
}
269+
270+
func (s *autoCodeHistory) reWriteI18nJson(file string, flag string, packageName, structName string) error {
271+
data, err := os.ReadFile(file)
272+
if err != nil {
273+
return err
274+
}
275+
originJson := string(data)
276+
switch flag {
277+
case "api":
278+
var err error
279+
originJson, err = sjson.Delete(originJson, fmt.Sprintf("system.api.group.%s%s", packageName, structName))
280+
if err != nil {
281+
return err
282+
}
283+
originJson, err = sjson.Delete(originJson, fmt.Sprintf("system.api.desc.add%s%s", packageName, structName))
284+
if err != nil {
285+
return err
286+
}
287+
originJson, err = sjson.Delete(originJson, fmt.Sprintf("system.api.desc.delete%s%s", packageName, structName))
288+
if err != nil {
289+
return err
290+
}
291+
originJson, err = sjson.Delete(originJson, fmt.Sprintf("system.api.desc.batch%s%s", packageName, structName))
292+
if err != nil {
293+
return err
294+
}
295+
originJson, err = sjson.Delete(originJson, fmt.Sprintf("system.api.desc.update%s%s", packageName, structName))
296+
if err != nil {
297+
return err
298+
}
299+
originJson, err = sjson.Delete(originJson, fmt.Sprintf("system.api.desc.find%s%s", packageName, structName))
300+
if err != nil {
301+
return err
302+
}
303+
originJson, err = sjson.Delete(originJson, fmt.Sprintf("system.api.desc.list%s%s", packageName, structName))
304+
if err != nil {
305+
return err
306+
}
307+
case "menu":
308+
originJson, err = sjson.Delete(originJson, fmt.Sprintf("system.menu.%s%s", packageName, structName))
309+
if err != nil {
310+
return err
311+
}
312+
case "web":
313+
originJson, err = sjson.Delete(originJson, fmt.Sprintf("%s.%s", packageName, structName))
314+
if err != nil {
315+
return err
316+
}
317+
}
318+
err = os.WriteFile(file, []byte(originJson), 0666)
319+
if err != nil {
320+
return err
321+
}
322+
return nil
323+
}

0 commit comments

Comments
 (0)