Files
Simon f13b34c59e feat(web): 字段库页 + 界面单位换算 + 因子目录参数化 + 选股策略条件表单
字段库与单位:
- `/fields`:字段库管理页(中文名/说明可改、可停用;kind 是引擎事实不可改);
  单位只在字段自己的阶梯里选(总市值 = 万元/亿元),越界 422 原样展示。
- `lib/units.ts`:界面单位 ⇄ 基准单位换算集中一处,条件输入按界面单位回显、
  提交前换回基准单位(引擎只认基准单位,库里存的也永远是基准单位)。
- `SelectionStrategyForm` 取代 `StrategyParamsForm`:一个策略只定义「怎么选」
  (股票池 + 因子 + 过滤条件),条件字段来自字段库接口而不是前端硬编码枚举。

因子参数化(名字即身份,界面不许藏):
- `/factors` 新增「参数」列与展开行:精确引擎键、每个参数的允许范围、来源、依赖列
  (依赖列标明「引擎事实,不可改」);说明文案从「不可修改」改为
  「改参数 = 新建参数化因子 = 新身份,旧因子/既有策略不变义」。
- 「新建参数化因子」卡片:模板下拉 + 受控窗口(min/max)+ 方向枚举下拉,
  实时预览规范键/中文名/渲染公式;后端 422 的原文原样展示,不静默截断。
- 因子下拉显示中文名(含参数)与短键,**value 一律是完整引擎键**;
  停用的因子从各页候选里消失(/strategies、/selection、/signals、/factors/compose),
  既有策略/归档仍按名字解析;`/factors/compose` 顺手修了勾选框点击目标过小。
- `/fields` 加提示:字段库只列内置因子名,参数化因子在 /factors 管理,
  选股策略条件下拉里会一并出现。

tsc --noEmit 0 error;verify_ui_alignment 8 页 160 项全过(另跑 3 个选因子页 51/54)。
2026-10-01 16:38:29 +08:00

102 lines
3.7 KiB
TypeScript

/**
* 后端 API 客户端。
*
* 默认走同源代理:页面请求相对路径 /api/*,由 next.config.ts 的 rewrites 转发到后端
* (默认 http://127.0.0.1:8000,可用环境变量 BACKEND_API_URL 覆盖)。
* 需要直连(不走代理)时可用 NEXT_PUBLIC_API_BASE 显式指定后端地址(含 /api)。
*/
const explicitBase = process.env.NEXT_PUBLIC_API_BASE?.replace(/\/$/, "");
const BASE = explicitBase ?? "/api";
export async function apiGet<T>(path: string): Promise<T> {
const resp = await fetch(`${BASE}${path}`);
if (!resp.ok) throw new Error(`GET ${path} → ${resp.status}: ${await resp.text()}`);
return (await resp.json()) as T;
}
/**
* GET 并**同时拿到响应头**。
*
* 为什么需要:列表接口用 `X-Total-Count` 暴露「过滤后总数」,而 body 必须保持
* `list[...]` 形状(既有页面依赖)。只读 body 的 `apiGet` 拿不到这个信息,
* 会让前端无法区分「这就是全部」和「只是最近 N 条」——那正是 §7 要避免的静默截断。
*/
export async function apiGetWithHeaders<T>(
path: string
): Promise<{ data: T; headers: Headers }> {
const resp = await fetch(`${BASE}${path}`);
if (!resp.ok) throw new Error(`GET ${path} → ${resp.status}: ${await resp.text()}`);
return { data: (await resp.json()) as T, headers: resp.headers };
}
/**
* GET 原始文本。
*
* 导出归档时用:直接落盘**后端存储的那份 JSON**,而不是前端再 `JSON.stringify`
* 一遍(后者会因字段顺序/缩进差异而与归档原文不同,且多一次内存拷贝)。
*/
export async function apiGetText(path: string): Promise<string> {
const resp = await fetch(`${BASE}${path}`);
if (!resp.ok) throw new Error(`GET ${path} → ${resp.status}: ${await resp.text()}`);
return await resp.text();
}
export async function apiPost<T>(path: string, body: unknown): Promise<T> {
const resp = await fetch(`${BASE}${path}`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
if (!resp.ok) {
const text = await resp.text();
throw new Error(`POST ${path} → ${resp.status}: ${text.slice(0, 300)}`);
}
return (await resp.json()) as T;
}
export async function apiPut<T>(path: string, body: unknown): Promise<T> {
const resp = await fetch(`${BASE}${path}`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
if (!resp.ok) {
const text = await resp.text();
throw new Error(`PUT ${path} → ${resp.status}: ${text.slice(0, 300)}`);
}
return (await resp.json()) as T;
}
/**
* PATCH:局部更新(如「启用/停用因子」只改 enabled,不该把整行 PUT 回去)。
*
* 为什么单列:因子参数化新增了 `PATCH /api/factors {name, enabled}` ——
* 名字里有括号/等号/逗号,放路径会被代理折腾,所以放在 body 里用 PATCH。
*/
export async function apiPatch<T>(path: string, body: unknown): Promise<T> {
const resp = await fetch(`${BASE}${path}`, {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
if (!resp.ok) {
const text = await resp.text();
throw new Error(`PATCH ${path} → ${resp.status}: ${text.slice(0, 300)}`);
}
return (await resp.json()) as T;
}
export async function apiDelete<T = { deleted?: string }>(path: string): Promise<T> {
const resp = await fetch(`${BASE}${path}`, { method: "DELETE" });
if (!resp.ok) {
const text = await resp.text();
throw new Error(`DELETE ${path} → ${resp.status}: ${text.slice(0, 300)}`);
}
return (await resp.json().catch(() => ({}))) as T;
}
export interface ApiError {
status: number;
message: string;
}