import { ref } from 'vue' import { useToast } from './useToast' import { GetSettings, SaveSettings } from '@wails/go/app/App' import { types } from '@wails/go/models' export interface RetryStage { maxElapsed: number interval: number } export interface Settings { timeApiUrl: string activity: string traceEnabled: boolean traceMaxBody: number logLevel: string concurrency: number attempts: number retryStages: RetryStage[] seckillMode: number // 0=标准, 1=激进 xiaodianPageSize: number proxyEnabled: boolean proxyUrl: string proxyApi: string autoFetchProxyOnSchedule: boolean } const defaultRetryStages: RetryStage[] = [ { maxElapsed: 4, interval: 120 }, { maxElapsed: 12, interval: 200 }, { maxElapsed: 30, interval: 500 }, { maxElapsed: 75, interval: 1000 } ] const settings = ref({ timeApiUrl: '', activity: '', traceEnabled: false, traceMaxBody: 0, logLevel: 'info', concurrency: 150, attempts: 2, retryStages: [...defaultRetryStages], seckillMode: 1, xiaodianPageSize: 200, proxyEnabled: false, proxyUrl: '', proxyApi: '', autoFetchProxyOnSchedule: false }) export function useSettings() { const { showToast } = useToast() const loadSettings = async () => { try { const cfg = await GetSettings() settings.value = { timeApiUrl: cfg.timeApiUrl || '', activity: cfg.activity || '', traceEnabled: !!cfg.traceEnabled, traceMaxBody: cfg.traceMaxBody || 0, logLevel: cfg.logLevel || 'info', concurrency: cfg.concurrency || 150, attempts: cfg.attempts || 2, retryStages: cfg.retryStages && cfg.retryStages.length > 0 ? cfg.retryStages : [...defaultRetryStages], seckillMode: cfg.seckillMode ?? 1, xiaodianPageSize: cfg.xiaodianPageSize || 200, proxyEnabled: !!cfg.proxyEnabled, proxyUrl: cfg.proxyUrl || '', proxyApi: cfg.proxyApi || '', autoFetchProxyOnSchedule: !!cfg.autoFetchProxyOnSchedule } } catch (e: any) { showToast('读取设置失败', e.message, 'error') } } const saveSettings = async (newSettings: Settings) => { try { const d = await SaveSettings(types.Config.createFrom(newSettings)) if (d.success) { settings.value = { ...newSettings } showToast('设置已保存', '配置已生效', 'success') return true } else { showToast('保存失败', '后端未返回成功状态', 'error') return false } } catch (e: any) { showToast('保存失败', e.message, 'error') return false } } return { settings, loadSettings, saveSettings } }