Files
ozon-seller-kit/extensions/collector/src/storage/settings.ts
T
R524809 dc6d38c128 refactor(server): 移除鉴权并归档遗留路由至 legacy/
- 删除 auth.py 与 deps.py,各路由去除 get_current_user 依赖
- collection.py 更名为 materials.py,冻结链路(ozon/publish/shops/categories)移入 legacy/
- 扩展默认生图服务端口并入 8800 并自动迁移旧配置,水印默认文案改为 Panda Store
- 新增 docs/v2.1/backend-structure.md 后端结构盘点文档
2026-08-28 15:09:22 +08:00

80 lines
3.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 服务端设置(上传/生成用):后端地址 + 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<string, string> = {
'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<BackendSettings> {
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<void> {
// 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 });
}