ticketRows.ts 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. /**
  2. * 施工回填「一账号一通路」行模型与校验/提交适配。
  3. * 通路=客户侧接入(含接入口令)→ 路径/出口 → 境外出口资源;共享=挂靠前序境外资源(可选再共用口令)。
  4. */
  5. import type {
  6. BackfillInIp, BackfillInRoute, ConstructionGuide, IpRow, RouteRow,
  7. } from "./api/ticket";
  8. export const BANDWIDTH_TIERS = [5, 10, 30];
  9. export const IP_TYPES: { code: string; label: string }[] = [
  10. { code: "fixed_ip", label: "固定 IP" },
  11. { code: "nat_forward", label: "NAT 共享出口" },
  12. ];
  13. export const OVERSEAS_IP_MAX_ROWS = 4;
  14. export const ROUTE_MAX_ROWS = 12;
  15. export const PATH_MAX_ROWS = ROUTE_MAX_ROWS;
  16. export type PathDraft = {
  17. key: string;
  18. open_this_time: boolean;
  19. /** 空=独立境外资源;非空=挂靠主通路 key */
  20. share_egress_of: string;
  21. /** 与 share_egress_of 联用:同时共用主通路接入口令 */
  22. share_credential: boolean;
  23. require_cpid: boolean;
  24. tunnel_encap: "none" | "vxlan";
  25. sub_account_id: string;
  26. network_account_name: string;
  27. intranet_ip: string;
  28. subnet_mask: string;
  29. gateway: string;
  30. vlan: string;
  31. pin: string;
  32. cpid: string;
  33. proxy_host: string;
  34. proxy_port: string;
  35. vni: string;
  36. src_endpoint: string;
  37. dst_endpoint: string;
  38. egress_pop: string;
  39. down_mbps: string;
  40. up_mbps: string;
  41. overseas_ip: string;
  42. ip_type: string;
  43. bandwidth_mbps: string;
  44. pin_registered?: boolean;
  45. cpid_registered?: boolean;
  46. };
  47. /** @deprecated 兼容旧调用;新代码用 PathDraft */
  48. export type RouteRowDraft = PathDraft & { /* alias shape */ };
  49. export type BackfillIpRowDraft = {
  50. key: string;
  51. egress: string;
  52. overseas_ip: string;
  53. ip_type: string;
  54. bandwidth_mbps: string;
  55. pin: string;
  56. cpid: string;
  57. sub_account_ids?: string[];
  58. };
  59. let seq = 0;
  60. const key = (p: string) => `${p}-${(seq += 1)}`;
  61. export const newPathDraft = (): PathDraft => ({
  62. key: key("path"),
  63. open_this_time: true,
  64. share_egress_of: "",
  65. share_credential: false,
  66. require_cpid: false,
  67. tunnel_encap: "none",
  68. sub_account_id: "",
  69. network_account_name: "",
  70. intranet_ip: "",
  71. subnet_mask: "",
  72. gateway: "",
  73. vlan: "",
  74. pin: "",
  75. cpid: "",
  76. proxy_host: "",
  77. proxy_port: "",
  78. vni: "",
  79. src_endpoint: "",
  80. dst_endpoint: "",
  81. egress_pop: "",
  82. down_mbps: "",
  83. up_mbps: "",
  84. overseas_ip: "",
  85. ip_type: "fixed_ip",
  86. bandwidth_mbps: "5",
  87. });
  88. export const newRouteRow = newPathDraft;
  89. export const newBackfillIpRow = (ip_type = "fixed_ip", bandwidth_mbps = "5"): BackfillIpRowDraft => ({
  90. key: key("bip"),
  91. egress: "",
  92. overseas_ip: "",
  93. ip_type,
  94. bandwidth_mbps,
  95. pin: "",
  96. cpid: "",
  97. });
  98. export function exitNormKey(s: string): string {
  99. return s.replace(/\s+/g, "").toLowerCase();
  100. }
  101. const blank = (v: string | undefined | null) => !v || !v.trim();
  102. const posNum = (v: string) => /^\d+(\.\d+)?$/.test(v.trim()) && Number(v) > 0;
  103. function isIpLike(v: string): boolean {
  104. const raw = v.trim().replace(/\/\d+$/, "");
  105. if (raw.includes(":")) return true;
  106. const m = raw.split(".");
  107. return m.length === 4 && m.every((o) => /^\d{1,3}$/.test(o) && Number(o) <= 255);
  108. }
  109. function isVoidPath(r: PathDraft): boolean {
  110. return blank(r.sub_account_id) && blank(r.intranet_ip) && blank(r.overseas_ip) && blank(r.proxy_host);
  111. }
  112. /** 套餐账号数 → 预生成通路行数(1‥PATH_MAX_ROWS)。 */
  113. export function suggestPathCount(guide: ConstructionGuide | null | undefined): number {
  114. const raw = Number(
  115. guide?.defaults?.suggest_seat_count
  116. ?? guide?.plan?.quota_slots
  117. ?? guide?.plan?.sub_account_quota
  118. ?? 0,
  119. );
  120. const n = Number.isFinite(raw) ? Math.trunc(raw) : 0;
  121. if (n < 1) return 1;
  122. return Math.min(n, PATH_MAX_ROWS);
  123. }
  124. /** 按套餐席位数预生成空白通路(可带核定出口/建议带宽)。 */
  125. export function seedPathRows(
  126. count: number,
  127. opts?: {
  128. egress_pop?: string;
  129. down_mbps?: string;
  130. up_mbps?: string;
  131. bandwidth_mbps?: string;
  132. },
  133. ): PathDraft[] {
  134. const n = Math.max(1, Math.min(Math.trunc(count) || 1, PATH_MAX_ROWS));
  135. return Array.from({ length: n }, () => {
  136. const d = newPathDraft();
  137. if (opts?.egress_pop) d.egress_pop = opts.egress_pop;
  138. if (opts?.down_mbps) d.down_mbps = opts.down_mbps;
  139. if (opts?.up_mbps) d.up_mbps = opts.up_mbps;
  140. if (opts?.bandwidth_mbps) d.bandwidth_mbps = opts.bandwidth_mbps;
  141. return d;
  142. });
  143. }
  144. /** 通路卡片收起时的一行摘要(展示用 1 基序号由调用方拼)。 */
  145. export function pathCardSummary(r: PathDraft, paths: PathDraft[]): string {
  146. const bits: string[] = [];
  147. const acct = r.network_account_name.trim() || r.sub_account_id.trim();
  148. bits.push(acct || "未填账号");
  149. if (r.intranet_ip.trim()) bits.push(r.intranet_ip.trim());
  150. if (r.share_egress_of.trim()) {
  151. const p = paths.find((x) => x.key === r.share_egress_of);
  152. const n = p ? paths.indexOf(p) + 1 : 0;
  153. bits.push(n > 0 ? `挂靠通路 ${n}` : "挂靠前序");
  154. if (p?.overseas_ip.trim()) bits.push(p.overseas_ip.trim());
  155. } else {
  156. if (r.overseas_ip.trim()) bits.push(r.overseas_ip.trim());
  157. else if (r.egress_pop.trim()) bits.push(r.egress_pop.trim());
  158. }
  159. if (r.pin.trim() || r.pin_registered) bits.push("PIN 已填");
  160. return bits.join(" · ");
  161. }
  162. /** 通路几乎空白:默认展开;已有内容且多条时默认收起。 */
  163. export function isPathSparse(r: PathDraft): boolean {
  164. return !r.sub_account_id.trim() && !r.intranet_ip.trim() && !r.overseas_ip.trim()
  165. && !r.network_account_name.trim() && !r.share_egress_of.trim();
  166. }
  167. /** 主通路=未挂靠前序境外资源的独立通路(席位须全开,不再按「本次开通」过滤)。 */
  168. export function primaryPaths(paths: PathDraft[]): PathDraft[] {
  169. return paths.filter((p) => !p.share_egress_of.trim());
  170. }
  171. export function validatePathRows(
  172. paths: PathDraft[],
  173. require: boolean,
  174. allowedExits: string[] = [],
  175. involveOverseasIp = true,
  176. ): string | null {
  177. if (paths.length > PATH_MAX_ROWS) return `网络通路最多 ${PATH_MAX_ROWS} 条。`;
  178. /**
  179. * 2026-11-23 定案:套餐席位=网络侧必须开通的通路;成功/部分成功回填须**全席填齐**。
  180. * 「一次只开一部分账号」原口径混淆了系统登录分账号与网络侧席位,已废止。
  181. */
  182. if (require) {
  183. for (let i = 0; i < paths.length; i++) {
  184. if (isVoidPath(paths[i]) || isPathSparse(paths[i])) {
  185. return `套餐 ${paths.length} 席须全部开通回填,第 ${i + 1} 条通路尚未填写。`;
  186. }
  187. }
  188. }
  189. const kept = require ? paths : paths.filter((r) => !isVoidPath(r));
  190. const allowKeys = new Set(allowedExits.map(exitNormKey));
  191. const byKey = new Map(paths.map((p) => [p.key, p]));
  192. const seenIp = new Set<string>();
  193. for (const r of kept) {
  194. const n = paths.indexOf(r) + 1;
  195. const shareKey = r.share_egress_of.trim();
  196. const primary = shareKey ? byKey.get(shareKey) : null;
  197. if (shareKey) {
  198. if (!primary || primary.share_egress_of.trim()) {
  199. return `第 ${n} 条通路的挂靠目标无效(须为独立主通路)。`;
  200. }
  201. if (paths.indexOf(primary) >= paths.indexOf(r)) {
  202. return `第 ${n} 条通路只能挂靠更靠前的主通路。`;
  203. }
  204. }
  205. const miss: string[] = [];
  206. if (blank(r.sub_account_id)) miss.push("分账号编号");
  207. if (blank(r.network_account_name)) miss.push("网络接入账号");
  208. if (blank(r.intranet_ip)) miss.push("内网 IP");
  209. if (blank(r.subnet_mask)) miss.push("子网掩码");
  210. if (blank(r.gateway)) miss.push("默认网关");
  211. if (blank(r.vlan)) miss.push("接入 VLAN");
  212. if (blank(r.proxy_host)) miss.push("正向代理地址");
  213. if (blank(r.proxy_port)) miss.push("正向代理端口");
  214. if (blank(r.egress_pop)) miss.push("出口 PoP");
  215. if (blank(r.down_mbps)) miss.push("下行实测");
  216. if (blank(r.up_mbps)) miss.push("上行实测");
  217. if (!shareKey || !r.share_credential) {
  218. if (blank(r.pin) && !r.pin_registered) miss.push("接入口令(PIN)");
  219. }
  220. if (r.require_cpid && !shareKey && blank(r.cpid) && !r.cpid_registered) miss.push("CPID");
  221. if (r.tunnel_encap === "vxlan") {
  222. if (blank(r.vni)) miss.push("VNI");
  223. if (blank(r.src_endpoint)) miss.push("隧道本端");
  224. if (blank(r.dst_endpoint)) miss.push("隧道对端");
  225. }
  226. if (involveOverseasIp && !shareKey) {
  227. if (blank(r.overseas_ip)) miss.push("境外出口 IP");
  228. if (blank(r.ip_type)) miss.push("接入方式");
  229. if (blank(r.bandwidth_mbps)) miss.push("申报带宽档位");
  230. }
  231. if (miss.length) return `第 ${n} 条通路缺${miss.join("、")}。`;
  232. for (const f of ["intranet_ip", "subnet_mask", "gateway"] as const) {
  233. if (!isIpLike(r[f])) return `第 ${n} 条通路的${f}「${r[f]}」不是合法 IP。`;
  234. }
  235. const port = Number(r.proxy_port);
  236. if (!Number.isInteger(port) || port < 1 || port > 65535) {
  237. return `第 ${n} 条通路的代理端口「${r.proxy_port}」须在 1~65535。`;
  238. }
  239. if (!posNum(r.down_mbps) || !posNum(r.up_mbps)) {
  240. return `第 ${n} 条通路的上下行实测须为正数。`;
  241. }
  242. if (allowKeys.size && r.egress_pop.trim() && !allowKeys.has(exitNormKey(r.egress_pop))) {
  243. return `第 ${n} 条通路出口 PoP「${r.egress_pop}」不在本单订阅核定出口内(须为:${allowedExits.join("、")})。`;
  244. }
  245. if (involveOverseasIp && !shareKey) {
  246. if (!isIpLike(r.overseas_ip)) return `第 ${n} 条通路境外 IP「${r.overseas_ip}」不是合法 IP。`;
  247. if (seenIp.has(r.overseas_ip.trim())) return `境外 IP「${r.overseas_ip}」重复登记。`;
  248. seenIp.add(r.overseas_ip.trim());
  249. if (!BANDWIDTH_TIERS.includes(Number(r.bandwidth_mbps))) {
  250. return `第 ${n} 条通路带宽档位只能是 5/10/30 Mbps。`;
  251. }
  252. }
  253. }
  254. if (require && !kept.length) return "套餐席位通路尚未填写。";
  255. return null;
  256. }
  257. /** 兼容旧名。 */
  258. export function validateRouteRows(
  259. rows: PathDraft[],
  260. require: boolean,
  261. allowedExits: string[] = [],
  262. ): string | null {
  263. return validatePathRows(rows, require, allowedExits, true);
  264. }
  265. export function validateBackfillIpRows(
  266. _rows: BackfillIpRowDraft[],
  267. _allowedExits: string[] = [],
  268. ): string | null {
  269. return null; // 境外资源已并入通路校验
  270. }
  271. export function normalizeBuilderUserId(raw: string | undefined | null): string | null {
  272. const v = (raw ?? "").trim();
  273. return /^\d+$/.test(v) ? v : null;
  274. }
  275. export function parseLineExits(raw: string | string[] | null | undefined): string[] {
  276. if (Array.isArray(raw)) return raw.map((x) => String(x).trim()).filter(Boolean);
  277. if (!raw || !String(raw).trim()) return [];
  278. try {
  279. const v = JSON.parse(String(raw));
  280. return Array.isArray(v) ? v.map((x) => String(x).trim()).filter(Boolean) : [];
  281. } catch {
  282. return [];
  283. }
  284. }
  285. export function resolveApprovedLineExits(detail: {
  286. order?: { line_exits?: string | string[] | null } | null;
  287. construction_guide?: {
  288. plan?: { line_exits?: string | string[] | null } | null;
  289. defaults?: { line_exits?: string[] | null } | null;
  290. } | null;
  291. }): string[] {
  292. const fromOrder = parseLineExits(detail.order?.line_exits);
  293. if (fromOrder.length) return fromOrder;
  294. const fromPlan = parseLineExits(detail.construction_guide?.plan?.line_exits);
  295. if (fromPlan.length) return fromPlan;
  296. return parseLineExits(detail.construction_guide?.defaults?.line_exits);
  297. }
  298. export function derivePinBindMode(paths: PathDraft[]): "shared" | "per_account" {
  299. return paths.some((p) => !isVoidPath(p) && p.share_credential && p.share_egress_of.trim())
  300. ? "shared"
  301. : "per_account";
  302. }
  303. /** 通路 → 后端 route_rows + ip_rows + pin_bind_mode(提交行一律 open_this_time=true)。 */
  304. export function buildPathSubmit(paths: PathDraft[]): {
  305. route_rows: BackfillInRoute[];
  306. ip_rows: Array<BackfillInIp & { sub_account_ids: string[] }>;
  307. pin_bind_mode: "shared" | "per_account";
  308. } {
  309. const opened = paths.filter((r) => !isVoidPath(r));
  310. const byKey = new Map(opened.map((p) => [p.key, p]));
  311. const pin_bind_mode = derivePinBindMode(opened);
  312. const route_rows: BackfillInRoute[] = opened.map((r) => {
  313. const primary = r.share_egress_of.trim() ? byKey.get(r.share_egress_of.trim()) : null;
  314. const shareCredOf = r.share_credential && primary ? (primary.sub_account_id.trim() || null) : null;
  315. return {
  316. sub_account_id: r.sub_account_id.trim(),
  317. network_account_name: r.network_account_name.trim() || null,
  318. intranet_ip: r.intranet_ip.trim(),
  319. subnet_mask: r.subnet_mask.trim(),
  320. gateway: r.gateway.trim(),
  321. vlan: r.vlan.trim(),
  322. tunnel_encap: r.tunnel_encap,
  323. vni: r.tunnel_encap === "vxlan" ? r.vni.trim() : "",
  324. src_endpoint: r.tunnel_encap === "vxlan" ? r.src_endpoint.trim() : "",
  325. dst_endpoint: r.tunnel_encap === "vxlan" ? r.dst_endpoint.trim() : "",
  326. egress_pop: r.egress_pop.trim(),
  327. down_mbps: Number(r.down_mbps),
  328. up_mbps: Number(r.up_mbps),
  329. proxy_host: r.proxy_host.trim(),
  330. proxy_port: Number(r.proxy_port),
  331. pin: shareCredOf ? "" : r.pin,
  332. cpid: shareCredOf ? "" : r.cpid.trim(),
  333. require_cpid: r.require_cpid,
  334. share_credential_of: shareCredOf,
  335. open_this_time: true,
  336. };
  337. });
  338. /** 按境外 IP 聚合:独立通路各一条;挂靠行并入主通路 IP。 */
  339. type Acc = {
  340. egress: string;
  341. overseas_ip: string;
  342. ip_type: string;
  343. bandwidth_mbps: number;
  344. sub_account_ids: string[];
  345. pin: string;
  346. cpid: string;
  347. };
  348. const groups = new Map<string, Acc>();
  349. for (const r of opened) {
  350. const primary = r.share_egress_of.trim() ? byKey.get(r.share_egress_of.trim()) : r;
  351. if (!primary || blank(primary.overseas_ip)) continue;
  352. const ipKey = primary.overseas_ip.trim();
  353. let g = groups.get(ipKey);
  354. if (!g) {
  355. const multi = opened.some(
  356. (x) => x.share_egress_of && byKey.get(x.share_egress_of.trim())?.overseas_ip.trim() === ipKey,
  357. );
  358. g = {
  359. egress: primary.egress_pop.trim(),
  360. overseas_ip: ipKey,
  361. ip_type: multi || primary.ip_type === "nat_forward" ? "nat_forward" : (primary.ip_type || "fixed_ip"),
  362. bandwidth_mbps: Number(primary.bandwidth_mbps) || 5,
  363. sub_account_ids: [],
  364. pin: primary.pin,
  365. cpid: primary.cpid.trim(),
  366. };
  367. groups.set(ipKey, g);
  368. }
  369. const acc = r.sub_account_id.trim();
  370. if (acc && !g.sub_account_ids.includes(acc)) g.sub_account_ids.push(acc);
  371. }
  372. const ip_rows = [...groups.values()].map((g) => ({
  373. egress: g.egress,
  374. overseas_ip: g.overseas_ip,
  375. ip_type: g.sub_account_ids.length > 1 ? "nat_forward" : g.ip_type,
  376. bandwidth_mbps: g.bandwidth_mbps,
  377. pin: g.pin || undefined,
  378. cpid: g.cpid || undefined,
  379. action: "allocate" as const,
  380. sub_account_ids: g.sub_account_ids,
  381. }));
  382. return { route_rows, ip_rows, pin_bind_mode };
  383. }
  384. export function buildRouteRows(rows: PathDraft[]): BackfillInRoute[] {
  385. return buildPathSubmit(rows).route_rows;
  386. }
  387. export function buildBackfillIpRows(rows: BackfillIpRowDraft[]): BackfillInIp[] {
  388. return rows.filter((r) => !blank(r.overseas_ip)).map((r) => ({
  389. egress: r.egress.trim(),
  390. overseas_ip: r.overseas_ip.trim(),
  391. ip_type: r.ip_type,
  392. bandwidth_mbps: Number(r.bandwidth_mbps),
  393. pin: r.pin,
  394. cpid: r.cpid.trim(),
  395. }));
  396. }
  397. export function pathsFromDetail(
  398. routes: RouteRow[],
  399. ips: IpRow[],
  400. ): PathDraft[] {
  401. if (!routes.length) return [newPathDraft()];
  402. const ipByAcc = new Map<string, IpRow>();
  403. const multiOwners = new Map<string, string[]>();
  404. for (const ip of ips) {
  405. const accs = [...(ip.sub_account_ids ?? [])];
  406. if (!accs.length && routes.length === 1 && ips.length === 1) {
  407. const only = routes[0].sub_account_id ?? "";
  408. if (only) accs.push(only);
  409. }
  410. for (const a of accs) {
  411. if (a) ipByAcc.set(a, ip);
  412. }
  413. if (accs.length > 1 && ip.overseas_ip) {
  414. multiOwners.set(ip.overseas_ip, accs);
  415. }
  416. }
  417. const pathKeys = new Map<string, string>();
  418. const drafts: PathDraft[] = routes.map((r) => {
  419. const d = newPathDraft();
  420. pathKeys.set(r.sub_account_id ?? "", d.key);
  421. const ip = ipByAcc.get(r.sub_account_id ?? "");
  422. const owners = ip?.overseas_ip ? multiOwners.get(ip.overseas_ip) : undefined;
  423. const isPrimary = !owners || owners[0] === r.sub_account_id;
  424. const primaryAcc = owners?.[0];
  425. return {
  426. ...d,
  427. sub_account_id: r.sub_account_id ?? "",
  428. network_account_name: r.network_account_name ?? "",
  429. intranet_ip: r.intranet_ip ?? "",
  430. subnet_mask: r.subnet_mask ?? "",
  431. gateway: r.gateway ?? "",
  432. vlan: r.vlan ?? "",
  433. tunnel_encap: (r as RouteRow & { tunnel_encap?: string }).tunnel_encap === "vxlan" ? "vxlan" : "none",
  434. vni: r.vni ?? "",
  435. src_endpoint: r.src_endpoint ?? "",
  436. dst_endpoint: r.dst_endpoint ?? "",
  437. egress_pop: r.egress_pop ?? "",
  438. down_mbps: r.down_mbps ?? "",
  439. up_mbps: r.up_mbps ?? "",
  440. proxy_host: (r as RouteRow & { proxy_host?: string }).proxy_host ?? "",
  441. proxy_port: (r as RouteRow & { proxy_port?: string }).proxy_port ?? "",
  442. pin_registered: (r as RouteRow & { pin_registered?: string }).pin_registered === "true"
  443. || (r as RouteRow & { pin_registered?: string }).pin_registered === "1",
  444. cpid_registered: (r as RouteRow & { cpid_registered?: string }).cpid_registered === "true"
  445. || (r as RouteRow & { cpid_registered?: string }).cpid_registered === "1",
  446. overseas_ip: isPrimary ? (ip?.overseas_ip ?? "") : "",
  447. ip_type: ip?.ip_type || "fixed_ip",
  448. bandwidth_mbps: ip?.bandwidth_mbps || "5",
  449. share_egress_of: !isPrimary && primaryAcc ? (pathKeys.get(primaryAcc) || "") : "",
  450. share_credential: false,
  451. open_this_time: true,
  452. };
  453. });
  454. // 第二遍:挂靠 key(第一遍时 primary 的 key 可能尚未写入 map 顺序问题——按账号再补)
  455. for (const d of drafts) {
  456. if (!d.share_egress_of && d.overseas_ip === "") {
  457. const ip = ipByAcc.get(d.sub_account_id);
  458. const owners = ip?.overseas_ip ? multiOwners.get(ip.overseas_ip) : undefined;
  459. if (owners && owners[0] !== d.sub_account_id) {
  460. const pk = drafts.find((x) => x.sub_account_id === owners[0])?.key;
  461. if (pk) d.share_egress_of = pk;
  462. }
  463. }
  464. }
  465. return drafts;
  466. }
  467. export function routeRowsFromDetail(rows: RouteRow[]): PathDraft[] {
  468. return pathsFromDetail(rows, []);
  469. }
  470. const txt = (v: unknown) => (v == null ? "" : String(v));
  471. const flagOn = (v: unknown) => v === true || v === "true" || v === "1";
  472. /**
  473. * 服务端回填草稿 → 通路表单。PIN/CPID 明文不回,只带 registered 标记;
  474. * `share_credential_of`(账号 ID)还原为表单内挂靠 key。
  475. */
  476. export function pathsFromDraft(
  477. routeRows: Array<Partial<BackfillInRoute> & {
  478. pin_registered?: string | boolean | null;
  479. cpid_registered?: string | boolean | null;
  480. pin_cipher?: string | null;
  481. cpid_cipher?: string | null;
  482. open_this_time?: boolean | null;
  483. }> | null | undefined,
  484. ipRows?: Array<Partial<BackfillInIp> & {
  485. pin_registered?: string | boolean | null;
  486. cpid_registered?: string | boolean | null;
  487. }> | null,
  488. ): PathDraft[] {
  489. const routes = routeRows ?? [];
  490. if (!routes.length) return [newPathDraft()];
  491. const asRoutes: RouteRow[] = routes.map((r, i) => ({
  492. id: `draft-r-${i}`,
  493. sub_account_id: txt(r.sub_account_id) || null,
  494. network_account_name: txt(r.network_account_name) || null,
  495. intranet_ip: txt(r.intranet_ip) || null,
  496. subnet_mask: txt(r.subnet_mask) || null,
  497. gateway: txt(r.gateway) || null,
  498. vlan: txt(r.vlan) || null,
  499. tunnel_encap: r.tunnel_encap === "vxlan" ? "vxlan" : "none",
  500. vni: txt(r.vni) || null,
  501. src_endpoint: txt(r.src_endpoint) || null,
  502. dst_endpoint: txt(r.dst_endpoint) || null,
  503. egress_pop: txt(r.egress_pop) || null,
  504. down_mbps: txt(r.down_mbps) || null,
  505. up_mbps: txt(r.up_mbps) || null,
  506. proxy_host: txt(r.proxy_host) || null,
  507. proxy_port: txt(r.proxy_port) || null,
  508. pin_registered: flagOn(r.pin_registered) || Boolean(txt(r.pin_cipher)) ? "true" : "false",
  509. cpid_registered: flagOn(r.cpid_registered) || Boolean(txt(r.cpid_cipher)) ? "true" : "false",
  510. }));
  511. const asIps: IpRow[] = (ipRows ?? []).map((ip, i) => ({
  512. id: `draft-ip-${i}`,
  513. row_no: String(i + 1),
  514. egress: txt(ip.egress) || null,
  515. overseas_ip: txt(ip.overseas_ip) || null,
  516. ip_type: txt(ip.ip_type) || "fixed_ip",
  517. bandwidth_mbps: txt(ip.bandwidth_mbps) || "5",
  518. pin_registered: flagOn(ip.pin_registered) ? "true" : "false",
  519. cpid_registered: flagOn(ip.cpid_registered) ? "true" : "false",
  520. sub_account_ids: Array.isArray(ip.sub_account_ids)
  521. ? ip.sub_account_ids.map((x) => txt(x)).filter(Boolean)
  522. : [],
  523. }));
  524. const drafts = pathsFromDetail(asRoutes, asIps);
  525. // share_credential_of=主通路账号 ID → 表单挂靠 key
  526. for (let i = 0; i < drafts.length; i++) {
  527. const credOf = txt(routes[i]?.share_credential_of);
  528. if (!credOf) continue;
  529. const primary = drafts.find((x) => x.sub_account_id === credOf);
  530. if (!primary) continue;
  531. drafts[i].share_egress_of = primary.key;
  532. drafts[i].share_credential = true;
  533. }
  534. for (let i = 0; i < drafts.length; i++) {
  535. drafts[i].open_this_time = true;
  536. if (flagOn(routes[i]?.require_cpid)) drafts[i].require_cpid = true;
  537. }
  538. return drafts;
  539. }
  540. export function backfillIpRowsFromDetail(rows: IpRow[]): BackfillIpRowDraft[] {
  541. return rows.map((r) => ({
  542. key: key("bip"),
  543. egress: r.egress ?? "",
  544. overseas_ip: r.overseas_ip ?? "",
  545. ip_type: r.ip_type || "fixed_ip",
  546. bandwidth_mbps: r.bandwidth_mbps || "5",
  547. pin: "",
  548. cpid: "",
  549. sub_account_ids: r.sub_account_ids,
  550. }));
  551. }
  552. /** 字段元数据(通路卡分段渲染)。 */
  553. export const PATH_CPE_FIELDS: { key: keyof PathDraft; label: string; ph?: string; hint?: string; scannable?: boolean; type?: string }[] = [
  554. { key: "sub_account_id", label: "分账号编号", ph: "ACC-0001", scannable: true, hint: "本系统分账号主键" },
  555. { key: "network_account_name", label: "网络接入账号", ph: "proxy-user-01", scannable: true, hint: "正向代理/网管侧认证用户名" },
  556. { key: "intranet_ip", label: "客户侧内网 IP", ph: "10.20.30.11", hint: "CPE/LAN 侧地址" },
  557. { key: "subnet_mask", label: "子网掩码", ph: "255.255.255.0" },
  558. { key: "gateway", label: "默认网关", ph: "10.20.30.1" },
  559. { key: "vlan", label: "接入 VLAN", ph: "120", hint: "客户侧 L2 分段,与 VNI 无包含关系" },
  560. { key: "pin", label: "接入口令(PIN)", ph: "只写不读", type: "password", hint: "与网络接入账号配对的口令;一账号一 PIN" },
  561. { key: "cpid", label: "CPID", ph: "选填", hint: "有则录;默认非必填" },
  562. { key: "proxy_host", label: "正向代理地址", ph: "proxy.example.com", hint: "客户侧接入点" },
  563. { key: "proxy_port", label: "正向代理端口", ph: "8080" },
  564. ];
  565. export const PATH_TUNNEL_FIELDS: { key: keyof PathDraft; label: string; ph?: string; hint?: string }[] = [
  566. { key: "vni", label: "隧道 VNI", ph: "50120", hint: "VXLAN 网络标识(与 VLAN 不同层)" },
  567. { key: "src_endpoint", label: "隧道本端(VTEP/PE)", hint: "客户侧隧道端点" },
  568. { key: "dst_endpoint", label: "隧道对端(VTEP/PE)", hint: "对端/PoP 侧隧道端点" },
  569. { key: "egress_pop", label: "出口 PoP", ph: "请选择核定出口", hint: "须∈订阅核定出口(KLT-G20406)" },
  570. { key: "down_mbps", label: "下行实测 Mbps", ph: "100", hint: "开通实测,非申报档位" },
  571. { key: "up_mbps", label: "上行实测 Mbps", ph: "50" },
  572. ];
  573. // 旧 ROUTE_FIELDS 兼容(部分页面可能仍引用)
  574. export type RouteFieldDef = {
  575. key: keyof PathDraft;
  576. label: string;
  577. ph?: string;
  578. group: "cpe" | "tunnel";
  579. scannable?: boolean;
  580. hint?: string;
  581. };
  582. export const ROUTE_FIELDS: RouteFieldDef[] = [
  583. ...PATH_CPE_FIELDS.filter((f) => !["pin", "cpid", "proxy_host", "proxy_port"].includes(f.key as string)).map((f) => ({ ...f, group: "cpe" as const })),
  584. ...PATH_TUNNEL_FIELDS.map((f) => ({ ...f, group: "tunnel" as const })),
  585. ];
  586. export const ROUTE_GROUP_LABELS = {
  587. cpe: "客户侧接入",
  588. tunnel: "路径与出口",
  589. };
  590. export const IP_ROW_FIELDS: { key: keyof BackfillIpRowDraft; label: string; ph?: string; hint?: string }[] = [
  591. { key: "overseas_ip", label: "境外出口 IP", ph: "203.0.113.7" },
  592. { key: "ip_type", label: "接入方式" },
  593. { key: "bandwidth_mbps", label: "申报带宽档位" },
  594. ];
  595. export const IP_TYPE_LABELS: Record<string, string> = {
  596. fixed_ip: "固定 IP",
  597. nat_forward: "NAT 共享出口",
  598. };