/** * 服务端设置(上传/生成用):后端地址 + Bearer Token + 水印选项,持久化到 chrome.storage.local。 * v2.1 并入 ozon-seller-kit 后新增「商品上报」配置:reportEnabled / reportBaseUrl / studioBaseUrl。 */ /** 生成图水印:服务端在 AI 出图后、落盘前合成(与生图模型无关)。默认复刻 ozonSeller。 */ export interface WatermarkSettings { enabled: boolean; type: 'image' | 'text'; text: string; opacity: number; // 1-100(%) } export interface BackendSettings { baseUrl: string; token: string; watermark: WatermarkSettings; /** 商品上报(ozon-seller-kit 后台):开关 + 后台地址 + 试算页地址 */ reportEnabled: boolean; reportBaseUrl: string; studioBaseUrl: string; } const KEY = 'suite_backend_settings'; export const DEFAULT_BASE_URL = 'http://127.0.0.1:8800'; export const DEFAULT_REPORT_BASE_URL = 'http://127.0.0.1:8800'; export const DEFAULT_STUDIO_BASE_URL = 'http://localhost:8900'; const DEFAULT: BackendSettings = { baseUrl: DEFAULT_BASE_URL, token: '', watermark: { enabled: false, type: 'image', text: 'Panda Store', opacity: 30 }, reportEnabled: true, reportBaseUrl: DEFAULT_REPORT_BASE_URL, studioBaseUrl: DEFAULT_STUDIO_BASE_URL, }; /** 历史默认地址 → 当前默认地址(换端口后自动迁移用户已保存的设置)。 * v2.1 并入 OSK 后生图服务端与上报同源(8800),ISS 的 3300 存档自动迁移。 */ const MIGRATE: Record = { 'http://127.0.0.1:3300': DEFAULT_BASE_URL, 'http://localhost:3300': DEFAULT_BASE_URL, 'http://127.0.0.1:8810': DEFAULT_BASE_URL, 'http://127.0.0.1:7000': DEFAULT_BASE_URL, 'http://localhost:7000': DEFAULT_BASE_URL, 'http://127.0.0.1:7200': DEFAULT_BASE_URL, 'http://localhost:7200': DEFAULT_BASE_URL, }; export async function loadSettings(): Promise { const r = await chrome.storage.local.get(KEY); const saved = r[KEY] ?? {}; const baseUrl = MIGRATE[saved.baseUrl] ?? saved.baseUrl ?? DEFAULT.baseUrl; // 水印子对象深合并:老版本存储里没有 watermark,避免整对象覆盖丢默认值 const s: BackendSettings = { token: '', ...saved, baseUrl, watermark: { ...DEFAULT.watermark, ...(saved.watermark ?? {}) }, // 上报配置为 v2.1 新增字段:老存档缺失时兜底默认值 reportEnabled: saved.reportEnabled ?? DEFAULT.reportEnabled, reportBaseUrl: saved.reportBaseUrl ?? DEFAULT.reportBaseUrl, studioBaseUrl: saved.studioBaseUrl ?? DEFAULT.studioBaseUrl, }; if (baseUrl !== saved.baseUrl) await chrome.storage.local.set({ [KEY]: s }); // 迁移结果写回 return s; } export async function saveSettings(s: BackendSettings): Promise { // localhost 会被 Chrome 解析为 IPv6 ::1,若该端口被系统服务(如 macOS AirPlay)占用会 403, // 统一改写为 IPv4 的 127.0.0.1 s = { ...s, baseUrl: s.baseUrl.replace('//localhost:', '//127.0.0.1:'), reportBaseUrl: s.reportBaseUrl.replace('//localhost:', '//127.0.0.1:'), }; await chrome.storage.local.set({ [KEY]: s }); }