From 6e0d93257f6f11163166b1ef4dfac8813e64e29f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=97=E5=AD=90?= Date: Sun, 26 May 2024 13:37:58 +0800 Subject: [PATCH] feat: add language api --- app/http/controllers/info_controller.go | 5 +++++ app/http/controllers/setting_controller.go | 17 +++++++++++++++-- app/http/requests/setting/update.go | 2 ++ docs/docs.go | 7 +++++-- docs/swagger.json | 7 +++++-- docs/swagger.yaml | 6 ++++-- routes/api.go | 1 + 7 files changed, 37 insertions(+), 8 deletions(-) diff --git a/app/http/controllers/info_controller.go b/app/http/controllers/info_controller.go index 0a3513cd..57e0516c 100644 --- a/app/http/controllers/info_controller.go +++ b/app/http/controllers/info_controller.go @@ -41,6 +41,11 @@ func (r *InfoController) Name(ctx http.Context) http.Response { }) } +// Language 获取面板语言 +func (r *InfoController) Language(ctx http.Context) http.Response { + return Success(ctx, facades.Config().GetString("app.locale")) +} + // HomePlugins 获取首页插件 func (r *InfoController) HomePlugins(ctx http.Context) http.Response { var plugins []models.Plugin diff --git a/app/http/controllers/setting_controller.go b/app/http/controllers/setting_controller.go index 36219e54..14d2ef16 100644 --- a/app/http/controllers/setting_controller.go +++ b/app/http/controllers/setting_controller.go @@ -60,6 +60,7 @@ func (r *SettingController) List(ctx http.Context) http.Response { return Success(ctx, http.Json{ "name": r.setting.Get(models.SettingKeyName), + "language": facades.Config().GetString("app.locale"), "entrance": facades.Config().GetString("http.entrance"), "ssl": facades.Config().GetBool("panel.ssl"), "website_path": r.setting.Get(models.SettingKeyWebsitePath), @@ -189,7 +190,6 @@ func (r *SettingController) Update(ctx http.Context) http.Response { }).Info("获取面板入口失败") return ErrorSystem(ctx) } - entrance := cast.ToString(updateRequest.Entrance) if oldEntrance != entrance { if out, err := tools.Exec("sed -i 's!APP_ENTRANCE=" + oldEntrance + "!APP_ENTRANCE=" + entrance + "!g' /www/panel/panel.conf"); err != nil { @@ -197,6 +197,19 @@ func (r *SettingController) Update(ctx http.Context) http.Response { } } + oldLanguage, err := tools.Exec(`cat /www/panel/panel.conf | grep APP_LOCALE | awk -F '=' '{print $2}' | tr -d '\n'`) + if err != nil { + facades.Log().Request(ctx.Request()).Tags("面板", "面板设置").With(map[string]any{ + "error": err.Error(), + }).Info("获取面板语言失败") + return ErrorSystem(ctx) + } + if oldLanguage != updateRequest.Language { + if out, err := tools.Exec("sed -i 's/APP_LOCALE=" + oldLanguage + "/APP_LOCALE=" + updateRequest.Language + "/g' /www/panel/panel.conf"); err != nil { + return Error(ctx, http.StatusInternalServerError, out) + } + } + if updateRequest.SSL { if out, err := tools.Exec("sed -i 's/APP_SSL=false/APP_SSL=true/g' /www/panel/panel.conf"); err != nil { return Error(ctx, http.StatusInternalServerError, out) @@ -207,7 +220,7 @@ func (r *SettingController) Update(ctx http.Context) http.Response { } } - if oldPort != port || oldEntrance != entrance || updateRequest.SSL != facades.Config().GetBool("panel.ssl") { + if oldPort != port || oldEntrance != entrance || oldLanguage != updateRequest.Language || updateRequest.SSL != facades.Config().GetBool("panel.ssl") { tools.RestartPanel() } diff --git a/app/http/requests/setting/update.go b/app/http/requests/setting/update.go index d072cf0b..3455f372 100644 --- a/app/http/requests/setting/update.go +++ b/app/http/requests/setting/update.go @@ -7,6 +7,7 @@ import ( type Update struct { Name string `form:"name" json:"name"` + Language string `form:"language" json:"language"` Port uint `form:"port" json:"port" filter:"uint"` BackupPath string `form:"backup_path" json:"backup_path"` WebsitePath string `form:"website_path" json:"website_path"` @@ -24,6 +25,7 @@ func (r *Update) Authorize(ctx http.Context) error { func (r *Update) Rules(ctx http.Context) map[string]string { return map[string]string{ "name": "required|string:2,20", + "language": "required|in:zh_CN,en", "port": "required|int:1000,65535", "backup_path": "required|string:2,255", "website_path": "required|string:2,255", diff --git a/docs/docs.go b/docs/docs.go index cb0b6fec..9dedea14 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -4042,6 +4042,9 @@ const docTemplate = `{ "entrance": { "type": "string" }, + "language": { + "type": "string" + }, "name": { "type": "string" }, @@ -4274,10 +4277,10 @@ const docTemplate = `{ "requests.Copy": { "type": "object", "properties": { - "new": { + "source": { "type": "string" }, - "old": { + "target": { "type": "string" } } diff --git a/docs/swagger.json b/docs/swagger.json index 98e48ac0..1b68b31e 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -4035,6 +4035,9 @@ "entrance": { "type": "string" }, + "language": { + "type": "string" + }, "name": { "type": "string" }, @@ -4267,10 +4270,10 @@ "requests.Copy": { "type": "object", "properties": { - "new": { + "source": { "type": "string" }, - "old": { + "target": { "type": "string" } } diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 79a8319f..984d40de 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -204,6 +204,8 @@ definitions: type: string entrance: type: string + language: + type: string name: type: string password: @@ -356,9 +358,9 @@ definitions: type: object requests.Copy: properties: - new: + source: type: string - old: + target: type: string type: object requests.Create: diff --git a/routes/api.go b/routes/api.go index 5856b078..5b194baa 100644 --- a/routes/api.go +++ b/routes/api.go @@ -13,6 +13,7 @@ func Api() { r.Prefix("info").Group(func(r route.Router) { infoController := controllers.NewInfoController() r.Get("name", infoController.Name) + r.Get("language", infoController.Language) r.Middleware(middleware.Jwt()).Get("homePlugins", infoController.HomePlugins) r.Middleware(middleware.Jwt()).Get("nowMonitor", infoController.NowMonitor) r.Middleware(middleware.Jwt()).Get("systemInfo", infoController.SystemInfo)