| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502 |
- /**
- * 施工回填「一账号一通路」行模型与校验/提交适配。
- * 通路=客户侧接入(含接入口令)→ 路径/出口 → 境外出口资源;共享=挂靠前序境外资源(可选再共用口令)。
- */
- import type { BackfillInIp, BackfillInRoute, IpRow, RouteRow } from "./api/ticket";
- export const BANDWIDTH_TIERS = [5, 10, 30];
- export const IP_TYPES: { code: string; label: string }[] = [
- { code: "fixed_ip", label: "固定 IP" },
- { code: "nat_forward", label: "NAT 共享出口" },
- ];
- export const OVERSEAS_IP_MAX_ROWS = 4;
- export const ROUTE_MAX_ROWS = 12;
- export const PATH_MAX_ROWS = ROUTE_MAX_ROWS;
- export type PathDraft = {
- key: string;
- open_this_time: boolean;
- /** 空=独立境外资源;非空=挂靠主通路 key */
- share_egress_of: string;
- /** 与 share_egress_of 联用:同时共用主通路接入口令 */
- share_credential: boolean;
- require_cpid: boolean;
- tunnel_encap: "none" | "vxlan";
- sub_account_id: string;
- network_account_name: string;
- intranet_ip: string;
- subnet_mask: string;
- gateway: string;
- vlan: string;
- pin: string;
- cpid: string;
- proxy_host: string;
- proxy_port: string;
- vni: string;
- src_endpoint: string;
- dst_endpoint: string;
- egress_pop: string;
- down_mbps: string;
- up_mbps: string;
- overseas_ip: string;
- ip_type: string;
- bandwidth_mbps: string;
- pin_registered?: boolean;
- cpid_registered?: boolean;
- };
- /** @deprecated 兼容旧调用;新代码用 PathDraft */
- export type RouteRowDraft = PathDraft & { /* alias shape */ };
- export type BackfillIpRowDraft = {
- key: string;
- egress: string;
- overseas_ip: string;
- ip_type: string;
- bandwidth_mbps: string;
- pin: string;
- cpid: string;
- sub_account_ids?: string[];
- };
- let seq = 0;
- const key = (p: string) => `${p}-${(seq += 1)}`;
- export const newPathDraft = (): PathDraft => ({
- key: key("path"),
- open_this_time: true,
- share_egress_of: "",
- share_credential: false,
- require_cpid: false,
- tunnel_encap: "none",
- sub_account_id: "",
- network_account_name: "",
- intranet_ip: "",
- subnet_mask: "",
- gateway: "",
- vlan: "",
- pin: "",
- cpid: "",
- proxy_host: "",
- proxy_port: "",
- vni: "",
- src_endpoint: "",
- dst_endpoint: "",
- egress_pop: "",
- down_mbps: "",
- up_mbps: "",
- overseas_ip: "",
- ip_type: "fixed_ip",
- bandwidth_mbps: "5",
- });
- export const newRouteRow = newPathDraft;
- export const newBackfillIpRow = (ip_type = "fixed_ip", bandwidth_mbps = "5"): BackfillIpRowDraft => ({
- key: key("bip"),
- egress: "",
- overseas_ip: "",
- ip_type,
- bandwidth_mbps,
- pin: "",
- cpid: "",
- });
- export function exitNormKey(s: string): string {
- return s.replace(/\s+/g, "").toLowerCase();
- }
- const blank = (v: string | undefined | null) => !v || !v.trim();
- const posNum = (v: string) => /^\d+(\.\d+)?$/.test(v.trim()) && Number(v) > 0;
- function isIpLike(v: string): boolean {
- const raw = v.trim().replace(/\/\d+$/, "");
- if (raw.includes(":")) return true;
- const m = raw.split(".");
- return m.length === 4 && m.every((o) => /^\d{1,3}$/.test(o) && Number(o) <= 255);
- }
- function isVoidPath(r: PathDraft): boolean {
- return blank(r.sub_account_id) && blank(r.intranet_ip) && blank(r.overseas_ip) && blank(r.proxy_host);
- }
- /** 解析主通路:独立配置且本次开通。 */
- export function primaryPaths(paths: PathDraft[]): PathDraft[] {
- return paths.filter((p) => p.open_this_time && !p.share_egress_of.trim());
- }
- export function validatePathRows(
- paths: PathDraft[],
- require: boolean,
- allowedExits: string[] = [],
- involveOverseasIp = true,
- ): string | null {
- const kept = paths.filter((r) => !isVoidPath(r));
- if (kept.length > PATH_MAX_ROWS) return `网络通路最多 ${PATH_MAX_ROWS} 条。`;
- const allowKeys = new Set(allowedExits.map(exitNormKey));
- const byKey = new Map(paths.map((p) => [p.key, p]));
- const seenIp = new Set<string>();
- for (const r of kept) {
- if (!r.open_this_time) continue;
- const n = paths.indexOf(r) + 1;
- const shareKey = r.share_egress_of.trim();
- const primary = shareKey ? byKey.get(shareKey) : null;
- if (shareKey) {
- if (!primary || !primary.open_this_time || primary.share_egress_of.trim()) {
- return `第 ${n} 条通路的挂靠目标无效(须为本次开通的独立主通路)。`;
- }
- if (paths.indexOf(primary) >= paths.indexOf(r)) {
- return `第 ${n} 条通路只能挂靠更靠前的主通路。`;
- }
- }
- const miss: string[] = [];
- if (blank(r.sub_account_id)) miss.push("分账号编号");
- if (blank(r.network_account_name)) miss.push("网络接入账号");
- if (blank(r.intranet_ip)) miss.push("内网 IP");
- if (blank(r.subnet_mask)) miss.push("子网掩码");
- if (blank(r.gateway)) miss.push("默认网关");
- if (blank(r.vlan)) miss.push("接入 VLAN");
- if (blank(r.proxy_host)) miss.push("正向代理地址");
- if (blank(r.proxy_port)) miss.push("正向代理端口");
- if (blank(r.egress_pop)) miss.push("出口 PoP");
- if (blank(r.down_mbps)) miss.push("下行实测");
- if (blank(r.up_mbps)) miss.push("上行实测");
- if (!shareKey || !r.share_credential) {
- if (blank(r.pin) && !r.pin_registered) miss.push("接入口令(PIN)");
- }
- if (r.require_cpid && !shareKey && blank(r.cpid) && !r.cpid_registered) miss.push("CPID");
- if (r.tunnel_encap === "vxlan") {
- if (blank(r.vni)) miss.push("VNI");
- if (blank(r.src_endpoint)) miss.push("隧道本端");
- if (blank(r.dst_endpoint)) miss.push("隧道对端");
- }
- if (involveOverseasIp && !shareKey) {
- if (blank(r.overseas_ip)) miss.push("境外出口 IP");
- if (blank(r.ip_type)) miss.push("接入方式");
- if (blank(r.bandwidth_mbps)) miss.push("申报带宽档位");
- }
- if (miss.length) return `第 ${n} 条通路缺${miss.join("、")}。`;
- for (const f of ["intranet_ip", "subnet_mask", "gateway"] as const) {
- if (!isIpLike(r[f])) return `第 ${n} 条通路的${f}「${r[f]}」不是合法 IP。`;
- }
- const port = Number(r.proxy_port);
- if (!Number.isInteger(port) || port < 1 || port > 65535) {
- return `第 ${n} 条通路的代理端口「${r.proxy_port}」须在 1~65535。`;
- }
- if (!posNum(r.down_mbps) || !posNum(r.up_mbps)) {
- return `第 ${n} 条通路的上下行实测须为正数。`;
- }
- if (allowKeys.size && r.egress_pop.trim() && !allowKeys.has(exitNormKey(r.egress_pop))) {
- return `第 ${n} 条通路出口 PoP「${r.egress_pop}」不在本单订阅核定出口内(须为:${allowedExits.join("、")})。`;
- }
- if (involveOverseasIp && !shareKey) {
- if (!isIpLike(r.overseas_ip)) return `第 ${n} 条通路境外 IP「${r.overseas_ip}」不是合法 IP。`;
- if (seenIp.has(r.overseas_ip.trim())) return `境外 IP「${r.overseas_ip}」重复登记。`;
- seenIp.add(r.overseas_ip.trim());
- if (!BANDWIDTH_TIERS.includes(Number(r.bandwidth_mbps))) {
- return `第 ${n} 条通路带宽档位只能是 5/10/30 Mbps。`;
- }
- }
- }
- if (require && !kept.some((r) => r.open_this_time)) {
- return kept.length
- ? "勾选至少一条「本次开通」通路并填齐。"
- : "至少填写一条网络通路。";
- }
- return null;
- }
- /** 兼容旧名。 */
- export function validateRouteRows(
- rows: PathDraft[],
- require: boolean,
- allowedExits: string[] = [],
- ): string | null {
- return validatePathRows(rows, require, allowedExits, true);
- }
- export function validateBackfillIpRows(
- _rows: BackfillIpRowDraft[],
- _allowedExits: string[] = [],
- ): string | null {
- return null; // 境外资源已并入通路校验
- }
- export function normalizeBuilderUserId(raw: string | undefined | null): string | null {
- const v = (raw ?? "").trim();
- return /^\d+$/.test(v) ? v : null;
- }
- export function parseLineExits(raw: string | string[] | null | undefined): string[] {
- if (Array.isArray(raw)) return raw.map((x) => String(x).trim()).filter(Boolean);
- if (!raw || !String(raw).trim()) return [];
- try {
- const v = JSON.parse(String(raw));
- return Array.isArray(v) ? v.map((x) => String(x).trim()).filter(Boolean) : [];
- } catch {
- return [];
- }
- }
- export function resolveApprovedLineExits(detail: {
- order?: { line_exits?: string | string[] | null } | null;
- construction_guide?: {
- plan?: { line_exits?: string | string[] | null } | null;
- defaults?: { line_exits?: string[] | null } | null;
- } | null;
- }): string[] {
- const fromOrder = parseLineExits(detail.order?.line_exits);
- if (fromOrder.length) return fromOrder;
- const fromPlan = parseLineExits(detail.construction_guide?.plan?.line_exits);
- if (fromPlan.length) return fromPlan;
- return parseLineExits(detail.construction_guide?.defaults?.line_exits);
- }
- export function derivePinBindMode(paths: PathDraft[]): "shared" | "per_account" {
- return paths.some((p) => p.open_this_time && p.share_credential && p.share_egress_of.trim())
- ? "shared"
- : "per_account";
- }
- /** 通路 → 后端 route_rows + ip_rows + pin_bind_mode */
- export function buildPathSubmit(paths: PathDraft[]): {
- route_rows: BackfillInRoute[];
- ip_rows: Array<BackfillInIp & { sub_account_ids: string[] }>;
- pin_bind_mode: "shared" | "per_account";
- } {
- const opened = paths.filter((r) => !isVoidPath(r) && r.open_this_time);
- const byKey = new Map(opened.map((p) => [p.key, p]));
- const pin_bind_mode = derivePinBindMode(opened);
- const route_rows: BackfillInRoute[] = opened.map((r) => {
- const primary = r.share_egress_of.trim() ? byKey.get(r.share_egress_of.trim()) : null;
- const shareCredOf = r.share_credential && primary ? (primary.sub_account_id.trim() || null) : null;
- return {
- sub_account_id: r.sub_account_id.trim(),
- network_account_name: r.network_account_name.trim() || null,
- intranet_ip: r.intranet_ip.trim(),
- subnet_mask: r.subnet_mask.trim(),
- gateway: r.gateway.trim(),
- vlan: r.vlan.trim(),
- tunnel_encap: r.tunnel_encap,
- vni: r.tunnel_encap === "vxlan" ? r.vni.trim() : "",
- src_endpoint: r.tunnel_encap === "vxlan" ? r.src_endpoint.trim() : "",
- dst_endpoint: r.tunnel_encap === "vxlan" ? r.dst_endpoint.trim() : "",
- egress_pop: r.egress_pop.trim(),
- down_mbps: Number(r.down_mbps),
- up_mbps: Number(r.up_mbps),
- proxy_host: r.proxy_host.trim(),
- proxy_port: Number(r.proxy_port),
- pin: shareCredOf ? "" : r.pin,
- cpid: shareCredOf ? "" : r.cpid.trim(),
- require_cpid: r.require_cpid,
- share_credential_of: shareCredOf,
- open_this_time: true,
- };
- });
- /** 按境外 IP 聚合:独立通路各一条;挂靠行并入主通路 IP。 */
- type Acc = {
- egress: string;
- overseas_ip: string;
- ip_type: string;
- bandwidth_mbps: number;
- sub_account_ids: string[];
- pin: string;
- cpid: string;
- };
- const groups = new Map<string, Acc>();
- for (const r of opened) {
- const primary = r.share_egress_of.trim() ? byKey.get(r.share_egress_of.trim()) : r;
- if (!primary || blank(primary.overseas_ip)) continue;
- const ipKey = primary.overseas_ip.trim();
- let g = groups.get(ipKey);
- if (!g) {
- const multi = opened.some(
- (x) => x.share_egress_of && byKey.get(x.share_egress_of.trim())?.overseas_ip.trim() === ipKey,
- );
- g = {
- egress: primary.egress_pop.trim(),
- overseas_ip: ipKey,
- ip_type: multi || primary.ip_type === "nat_forward" ? "nat_forward" : (primary.ip_type || "fixed_ip"),
- bandwidth_mbps: Number(primary.bandwidth_mbps) || 5,
- sub_account_ids: [],
- pin: primary.pin,
- cpid: primary.cpid.trim(),
- };
- groups.set(ipKey, g);
- }
- const acc = r.sub_account_id.trim();
- if (acc && !g.sub_account_ids.includes(acc)) g.sub_account_ids.push(acc);
- }
- const ip_rows = [...groups.values()].map((g) => ({
- egress: g.egress,
- overseas_ip: g.overseas_ip,
- ip_type: g.sub_account_ids.length > 1 ? "nat_forward" : g.ip_type,
- bandwidth_mbps: g.bandwidth_mbps,
- pin: g.pin || undefined,
- cpid: g.cpid || undefined,
- action: "allocate" as const,
- sub_account_ids: g.sub_account_ids,
- }));
- return { route_rows, ip_rows, pin_bind_mode };
- }
- export function buildRouteRows(rows: PathDraft[]): BackfillInRoute[] {
- return buildPathSubmit(rows).route_rows;
- }
- export function buildBackfillIpRows(rows: BackfillIpRowDraft[]): BackfillInIp[] {
- return rows.filter((r) => !blank(r.overseas_ip)).map((r) => ({
- egress: r.egress.trim(),
- overseas_ip: r.overseas_ip.trim(),
- ip_type: r.ip_type,
- bandwidth_mbps: Number(r.bandwidth_mbps),
- pin: r.pin,
- cpid: r.cpid.trim(),
- }));
- }
- export function pathsFromDetail(
- routes: RouteRow[],
- ips: IpRow[],
- ): PathDraft[] {
- if (!routes.length) return [newPathDraft()];
- const ipByAcc = new Map<string, IpRow>();
- const multiOwners = new Map<string, string[]>();
- for (const ip of ips) {
- const accs = [...(ip.sub_account_ids ?? [])];
- if (!accs.length && routes.length === 1 && ips.length === 1) {
- const only = routes[0].sub_account_id ?? "";
- if (only) accs.push(only);
- }
- for (const a of accs) {
- if (a) ipByAcc.set(a, ip);
- }
- if (accs.length > 1 && ip.overseas_ip) {
- multiOwners.set(ip.overseas_ip, accs);
- }
- }
- const pathKeys = new Map<string, string>();
- const drafts: PathDraft[] = routes.map((r) => {
- const d = newPathDraft();
- pathKeys.set(r.sub_account_id ?? "", d.key);
- const ip = ipByAcc.get(r.sub_account_id ?? "");
- const owners = ip?.overseas_ip ? multiOwners.get(ip.overseas_ip) : undefined;
- const isPrimary = !owners || owners[0] === r.sub_account_id;
- const primaryAcc = owners?.[0];
- return {
- ...d,
- sub_account_id: r.sub_account_id ?? "",
- network_account_name: r.network_account_name ?? "",
- intranet_ip: r.intranet_ip ?? "",
- subnet_mask: r.subnet_mask ?? "",
- gateway: r.gateway ?? "",
- vlan: r.vlan ?? "",
- tunnel_encap: (r as RouteRow & { tunnel_encap?: string }).tunnel_encap === "vxlan" ? "vxlan" : "none",
- vni: r.vni ?? "",
- src_endpoint: r.src_endpoint ?? "",
- dst_endpoint: r.dst_endpoint ?? "",
- egress_pop: r.egress_pop ?? "",
- down_mbps: r.down_mbps ?? "",
- up_mbps: r.up_mbps ?? "",
- proxy_host: (r as RouteRow & { proxy_host?: string }).proxy_host ?? "",
- proxy_port: (r as RouteRow & { proxy_port?: string }).proxy_port ?? "",
- pin_registered: (r as RouteRow & { pin_registered?: string }).pin_registered === "true"
- || (r as RouteRow & { pin_registered?: string }).pin_registered === "1",
- cpid_registered: (r as RouteRow & { cpid_registered?: string }).cpid_registered === "true"
- || (r as RouteRow & { cpid_registered?: string }).cpid_registered === "1",
- overseas_ip: isPrimary ? (ip?.overseas_ip ?? "") : "",
- ip_type: ip?.ip_type || "fixed_ip",
- bandwidth_mbps: ip?.bandwidth_mbps || "5",
- share_egress_of: !isPrimary && primaryAcc ? (pathKeys.get(primaryAcc) || "") : "",
- share_credential: false,
- open_this_time: true,
- };
- });
- // 第二遍:挂靠 key(第一遍时 primary 的 key 可能尚未写入 map 顺序问题——按账号再补)
- for (const d of drafts) {
- if (!d.share_egress_of && d.overseas_ip === "") {
- const ip = ipByAcc.get(d.sub_account_id);
- const owners = ip?.overseas_ip ? multiOwners.get(ip.overseas_ip) : undefined;
- if (owners && owners[0] !== d.sub_account_id) {
- const pk = drafts.find((x) => x.sub_account_id === owners[0])?.key;
- if (pk) d.share_egress_of = pk;
- }
- }
- }
- return drafts;
- }
- export function routeRowsFromDetail(rows: RouteRow[]): PathDraft[] {
- return pathsFromDetail(rows, []);
- }
- export function backfillIpRowsFromDetail(rows: IpRow[]): BackfillIpRowDraft[] {
- return rows.map((r) => ({
- key: key("bip"),
- egress: r.egress ?? "",
- overseas_ip: r.overseas_ip ?? "",
- ip_type: r.ip_type || "fixed_ip",
- bandwidth_mbps: r.bandwidth_mbps || "5",
- pin: "",
- cpid: "",
- sub_account_ids: r.sub_account_ids,
- }));
- }
- /** 字段元数据(通路卡分段渲染)。 */
- export const PATH_CPE_FIELDS: { key: keyof PathDraft; label: string; ph?: string; hint?: string; scannable?: boolean; type?: string }[] = [
- { key: "sub_account_id", label: "分账号编号", ph: "ACC-0001", scannable: true, hint: "本系统分账号主键" },
- { key: "network_account_name", label: "网络接入账号", ph: "proxy-user-01", scannable: true, hint: "正向代理/网管侧认证用户名" },
- { key: "intranet_ip", label: "客户侧内网 IP", ph: "10.20.30.11", hint: "CPE/LAN 侧地址" },
- { key: "subnet_mask", label: "子网掩码", ph: "255.255.255.0" },
- { key: "gateway", label: "默认网关", ph: "10.20.30.1" },
- { key: "vlan", label: "接入 VLAN", ph: "120", hint: "客户侧 L2 分段,与 VNI 无包含关系" },
- { key: "pin", label: "接入口令(PIN)", ph: "只写不读", type: "password", hint: "与网络接入账号配对的口令;一账号一 PIN" },
- { key: "cpid", label: "CPID", ph: "选填", hint: "有则录;默认非必填" },
- { key: "proxy_host", label: "正向代理地址", ph: "proxy.example.com", hint: "客户侧接入点" },
- { key: "proxy_port", label: "正向代理端口", ph: "8080" },
- ];
- export const PATH_TUNNEL_FIELDS: { key: keyof PathDraft; label: string; ph?: string; hint?: string }[] = [
- { key: "vni", label: "隧道 VNI", ph: "50120", hint: "VXLAN 网络标识(与 VLAN 不同层)" },
- { key: "src_endpoint", label: "隧道本端(VTEP/PE)", hint: "客户侧隧道端点" },
- { key: "dst_endpoint", label: "隧道对端(VTEP/PE)", hint: "对端/PoP 侧隧道端点" },
- { key: "egress_pop", label: "出口 PoP", ph: "请选择核定出口", hint: "须∈订阅核定出口(KLT-G20406)" },
- { key: "down_mbps", label: "下行实测 Mbps", ph: "100", hint: "开通实测,非申报档位" },
- { key: "up_mbps", label: "上行实测 Mbps", ph: "50" },
- ];
- // 旧 ROUTE_FIELDS 兼容(部分页面可能仍引用)
- export type RouteFieldDef = {
- key: keyof PathDraft;
- label: string;
- ph?: string;
- group: "cpe" | "tunnel";
- scannable?: boolean;
- hint?: string;
- };
- export const ROUTE_FIELDS: RouteFieldDef[] = [
- ...PATH_CPE_FIELDS.filter((f) => !["pin", "cpid", "proxy_host", "proxy_port"].includes(f.key as string)).map((f) => ({ ...f, group: "cpe" as const })),
- ...PATH_TUNNEL_FIELDS.map((f) => ({ ...f, group: "tunnel" as const })),
- ];
- export const ROUTE_GROUP_LABELS = {
- cpe: "客户侧接入",
- tunnel: "路径与出口",
- };
- export const IP_ROW_FIELDS: { key: keyof BackfillIpRowDraft; label: string; ph?: string; hint?: string }[] = [
- { key: "overseas_ip", label: "境外出口 IP", ph: "203.0.113.7" },
- { key: "ip_type", label: "接入方式" },
- { key: "bandwidth_mbps", label: "申报带宽档位" },
- ];
- export const IP_TYPE_LABELS: Record<string, string> = {
- fixed_ip: "固定 IP",
- nat_forward: "NAT 共享出口",
- };
|