2
0
mirror of https://github.com/acepanel/panel.git synced 2026-02-04 09:13:49 +08:00

feat: add language api

This commit is contained in:
耗子
2024-05-26 13:37:58 +08:00
parent 96806d4e94
commit 6e0d93257f
7 changed files with 37 additions and 8 deletions

View File

@@ -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

View File

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

View File

@@ -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",

View File

@@ -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"
}
}

View File

@@ -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"
}
}

View File

@@ -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:

View File

@@ -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)