Files
opsxsql/backend/internal/handler/preferences.go
T
logan 8aaf2271b8 feat: gh-ost真实集成验证通过 + 登录页全面中文化 + 外观设置跨浏览器同步修复
gh-ost集成:
- handler根据instance_id查DB获取连接信息(host/port/user/password)
- 修复gh-ost参数: 去掉不存在的--dry-run(默认noop), 加--execute才真执行
- 加--skip-port-validation(端口映射环境必须)
- 加--allow-on-master(单机MySQL直接执行)
- 加--initially-drop-ghost-table/old-table/socket-file(清理残留)
- GhOstTask增加Port字段, buildArgs同步更新
- dry-run真实验证通过: 连接1.94.55.133:9005, 3行表, 3.85s完成

登录页中文化:
- 5个登录组件(SplitView/CenteredCard/SidePanel/FullImage/MinimalDark)
- 46处英文文案全部替换为中文
- 平台标题统一为'SQL审核平台'

外观设置同步修复:
- GetPublicLoginStyle: GORM First()复用主键导致第二次查询失败
- 改用Take()+重置结构体, 修复theme始终返回dark的bug
- UpdateUserPreferences: 同步写入SystemSetting表(login_style+default_theme)
2026-05-27 00:06:13 +08:00

85 lines
2.3 KiB
Go

package handler
import (
"encoding/json"
"net/http"
"sql-platform/internal/middleware"
"sql-platform/internal/model"
"github.com/gin-gonic/gin"
)
// GET /api/v1/user/preferences
func GetUserPreferences(c *gin.Context) {
userID, _, _ := middleware.GetCurrentUser(c)
var user model.User
if err := model.DB.First(&user, userID).Error; err != nil {
c.JSON(http.StatusNotFound, gin.H{"detail": "User not found"})
return
}
prefs := map[string]string{}
if user.Preferences != nil && *user.Preferences != "" {
json.Unmarshal([]byte(*user.Preferences), &prefs)
}
c.JSON(http.StatusOK, prefs)
}
// PUT /api/v1/user/preferences
func UpdateUserPreferences(c *gin.Context) {
userID, _, _ := middleware.GetCurrentUser(c)
var prefs map[string]string
if err := c.ShouldBindJSON(&prefs); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"detail": err.Error()})
return
}
data, _ := json.Marshal(prefs)
str := string(data)
result := model.DB.Model(&model.User{}).Where("id = ?", userID).Update("preferences", str)
if result.Error != nil {
c.JSON(http.StatusInternalServerError, gin.H{"detail": result.Error.Error()})
return
}
// Sync login_style and theme to SystemSetting so public endpoint picks them up
syncToSystemSetting := func(key, value string) {
var s model.SystemSetting
if model.DB.Where("`key` = ?", key).First(&s).Error == nil {
model.DB.Model(&s).Update("value", value)
} else {
model.DB.Create(&model.SystemSetting{
Key: key, Value: value, ValueType: "string",
Group: "appearance", Label: key, SortOrder: 0,
})
}
}
if v, ok := prefs["login_style"]; ok {
syncToSystemSetting("login_style", v)
}
if v, ok := prefs["theme"]; ok {
syncToSystemSetting("default_theme", v)
}
c.JSON(http.StatusOK, gin.H{"message": "Preferences updated", "preferences": prefs})
}
// GET /api/v1/public/login-style (no auth required)
func GetPublicLoginStyle(c *gin.Context) {
style := "split"
theme := "dark"
var s model.SystemSetting
if model.DB.Where("`key` = ?", "login_style").Take(&s).Error == nil && s.Value != "" {
style = s.Value
}
s = model.SystemSetting{} // reset to avoid GORM reusing primary key
if model.DB.Where("`key` = ?", "default_theme").Take(&s).Error == nil && s.Value != "" {
theme = s.Value
}
c.JSON(http.StatusOK, gin.H{"login_style": style, "theme": theme})
}