Skip to content

Commit

Permalink
feat: add language api
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed May 26, 2024
1 parent 96806d4 commit 6e0d932
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 8 deletions.
5 changes: 5 additions & 0 deletions app/http/controllers/info_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 15 additions & 2 deletions app/http/controllers/setting_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -189,14 +190,26 @@ 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 {
return Error(ctx, http.StatusInternalServerError, out)
}
}

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)
Expand All @@ -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()
}

Expand Down
2 changes: 2 additions & 0 deletions app/http/requests/setting/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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",
Expand Down
7 changes: 5 additions & 2 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4042,6 +4042,9 @@ const docTemplate = `{
"entrance": {
"type": "string"
},
"language": {
"type": "string"
},
"name": {
"type": "string"
},
Expand Down Expand Up @@ -4274,10 +4277,10 @@ const docTemplate = `{
"requests.Copy": {
"type": "object",
"properties": {
"new": {
"source": {
"type": "string"
},
"old": {
"target": {
"type": "string"
}
}
Expand Down
7 changes: 5 additions & 2 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -4035,6 +4035,9 @@
"entrance": {
"type": "string"
},
"language": {
"type": "string"
},
"name": {
"type": "string"
},
Expand Down Expand Up @@ -4267,10 +4270,10 @@
"requests.Copy": {
"type": "object",
"properties": {
"new": {
"source": {
"type": "string"
},
"old": {
"target": {
"type": "string"
}
}
Expand Down
6 changes: 4 additions & 2 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ definitions:
type: string
entrance:
type: string
language:
type: string
name:
type: string
password:
Expand Down Expand Up @@ -356,9 +358,9 @@ definitions:
type: object
requests.Copy:
properties:
new:
source:
type: string
old:
target:
type: string
type: object
requests.Create:
Expand Down
1 change: 1 addition & 0 deletions routes/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 6e0d932

Please sign in to comment.