feat(config): 支持共享代理配置文件,发布 0.5.2
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/wailsapp/wails/v2/pkg/runtime"
|
||||
|
||||
"douyu_go/internal/config"
|
||||
"douyu_go/internal/logger"
|
||||
"douyu_go/internal/types"
|
||||
)
|
||||
|
||||
// LoadSharedProxyConfig 从指定文件读取代理配置并立即应用到当前实例。
|
||||
func (a *App) LoadSharedProxyConfig(path string) types.ErrorResult {
|
||||
path = strings.TrimSpace(path)
|
||||
if path == "" {
|
||||
path = strings.TrimSpace(a.getCfg().SharedProxyConfigFile)
|
||||
}
|
||||
shared, err := config.LoadSharedProxyConfig(path)
|
||||
if err != nil {
|
||||
logger.LogEvent("配置", "warn", "加载共享代理配置失败: %v", err)
|
||||
return types.ErrorResult{Error: err.Error()}
|
||||
}
|
||||
cfg := a.getCfg()
|
||||
cfg.SharedProxyConfigFile = path
|
||||
config.ApplySharedProxyConfig(&cfg, shared)
|
||||
if err := config.SaveSharedProxyConfigPointer(path); err != nil {
|
||||
logger.LogEvent("配置", "warn", "保存共享代理配置路径失败: %v", err)
|
||||
}
|
||||
result := a.SaveSettings(cfg)
|
||||
if result.Success {
|
||||
logger.LogEvent("配置", "success", "共享代理配置已加载并应用: %s", path)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// SaveSharedProxyConfig 将当前实例的代理配置写入共享文件。
|
||||
func (a *App) SaveSharedProxyConfig(path string) types.ErrorResult {
|
||||
path = strings.TrimSpace(path)
|
||||
if path == "" {
|
||||
path = strings.TrimSpace(a.getCfg().SharedProxyConfigFile)
|
||||
}
|
||||
if path == "" {
|
||||
return types.ErrorResult{Error: "请先选择共享代理配置文件"}
|
||||
}
|
||||
cfg := a.getCfg()
|
||||
if err := config.SaveSharedProxyConfig(path, cfg); err != nil {
|
||||
logger.LogEvent("配置", "error", "保存共享代理配置失败: %v", err)
|
||||
return types.ErrorResult{Error: err.Error()}
|
||||
}
|
||||
cfg.SharedProxyConfigFile = path
|
||||
if err := config.SaveSharedProxyConfigPointer(path); err != nil {
|
||||
logger.LogEvent("配置", "warn", "保存共享代理配置路径失败: %v", err)
|
||||
}
|
||||
result := a.SaveSettings(cfg)
|
||||
if result.Success {
|
||||
logger.LogEvent("配置", "success", "共享代理配置已保存: %s", path)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// SelectSharedProxyConfigFile 打开系统文件选择器,供前端选择共享代理配置。
|
||||
func (a *App) SelectSharedProxyConfigFile(save bool) types.FileDialogResult {
|
||||
cfg := a.getCfg()
|
||||
defaultDir := strings.TrimSpace(filepath.Dir(cfg.SharedProxyConfigFile))
|
||||
if defaultDir == "." || defaultDir == "" {
|
||||
defaultDir = a.paths.DataDir
|
||||
}
|
||||
if info, err := os.Stat(defaultDir); err != nil || !info.IsDir() {
|
||||
defaultDir = a.paths.DataDir
|
||||
}
|
||||
filter := runtime.FileFilter{DisplayName: "JSON 配置 (*.json)", Pattern: "*.json"}
|
||||
var (
|
||||
path string
|
||||
err error
|
||||
)
|
||||
if save {
|
||||
path, err = runtime.SaveFileDialog(a.ctx, runtime.SaveDialogOptions{
|
||||
DefaultDirectory: defaultDir,
|
||||
DefaultFilename: "douyu-proxy-config.json",
|
||||
Title: "保存共享代理配置",
|
||||
Filters: []runtime.FileFilter{filter},
|
||||
})
|
||||
} else {
|
||||
path, err = runtime.OpenFileDialog(a.ctx, runtime.OpenDialogOptions{
|
||||
DefaultDirectory: defaultDir,
|
||||
Title: "选择共享代理配置",
|
||||
Filters: []runtime.FileFilter{filter},
|
||||
})
|
||||
}
|
||||
if err != nil {
|
||||
return types.FileDialogResult{Error: err.Error()}
|
||||
}
|
||||
path = strings.TrimSpace(path)
|
||||
if path != "" && !strings.EqualFold(filepath.Ext(path), ".json") {
|
||||
path += ".json"
|
||||
}
|
||||
return types.FileDialogResult{Success: true, Path: path}
|
||||
}
|
||||
Reference in New Issue
Block a user