Files
console-dev/app/lib/product-fields.js
T
2026-11-06 00:04:38 +08:00

215 lines
5.3 KiB
JavaScript

function e(e) {
return e && typeof e == `object` && !Array.isArray(e) ? e : null;
}
function t(e) {
return typeof e == `string` || typeof e == `number` ? String(e) : ``;
}
function n(e) {
return e === !0 || e === 1 || e === `1`;
}
function r(n) {
return (
Array.isArray(n)
? n.flatMap((n) => {
let r = e(n);
return r
? [
{
value: t(r.value ?? r.id ?? r.option_value),
label: t(r.label ?? r.name ?? r.option_name ?? r.value),
},
]
: [t(n)];
})
: t(n).split(/[,\n]/)
)
.map((e) =>
typeof e == `string`
? {
value: e.trim(),
label: e.trim(),
}
: e,
)
.filter((e) => e.value !== ``);
}
function i(e) {
let n = t(e).trim();
if (!n) return {};
let r = ``,
i = n[0],
a = {
"(": `)`,
"[": `]`,
"{": `}`,
"<": `>`,
};
if (/[^a-zA-Z0-9\\\s\0]/.test(i)) {
let e = a[i] ?? i,
t = 1,
o = -1;
for (let r = 1; r < n.length; r++) {
if (n[r] === `\\`) {
r++;
continue;
}
if ((a[i] && n[r] === i && t++, n[r] === e && --t === 0)) {
o = r;
break;
}
a[i] || (t = 1);
}
if (o > 0 && /^[a-zA-Z\s]*$/.test(n.slice(o + 1)))
((r = n.slice(o + 1).replace(/\s/g, ``)),
(n = n.slice(1, o)),
`\\^$.*+?()[]{}|/`.includes(e) || (n = n.split(`\\${e}`).join(e)));
else if (`/#~%!`.includes(i)) throw Error(`自定义字段验证规则无效`);
}
if (!/^[imsuxADSUXJnr]*$/.test(r))
throw Error(`自定义字段验证规则修饰符无效`);
let o = /^\(\?([imsx]+)\)/.exec(n);
o && ((r += o[1]), (n = n.slice(o[0].length)));
let s = [...new Set(r.replace(/[^imsu]/g, ``))].join(``);
if (
/\(\?(?:[>('|&R0-9]|P[=>]|[imsx-]+:)|\(\*|\\[KCG]|(?:[+*?]|\{\d+(?:,\d*)?\})\+/.test(
n,
) ||
r.includes(`r`) ||
(r.includes(`J`) && /\(\?</.test(n))
)
return {
pattern: n,
patternFlags: s || void 0,
patternServerOnly: !0,
};
let c = ``,
l = !1;
for (let e = 0; e < n.length; e++) {
let t = n[e];
if (t === `\\`) {
let t = n[++e],
r = {
A: `(?<![\\s\\S])`,
z: `(?![\\s\\S])`,
Z: `(?=\\n?(?![\\s\\S]))`,
};
c += !l && t && r[t] ? r[t] : `\\${t ?? ``}`;
continue;
}
if (
(t === `[` && !l ? (l = !0) : t === `]` && l && (l = !1),
!l && r.includes(`x`))
) {
if (/\s/.test(t)) continue;
if (t === `#`) {
for (
;
e < n.length &&
n[e] !==
`
`;
)
e++;
continue;
}
}
if (!l && t === `$` && r.includes(`D`) && !s.includes(`m`)) {
c += `(?![\\s\\S])`;
continue;
}
if (!l && t === `(` && r.includes(`n`) && n[e + 1] !== `?`) {
c += `(?:`;
continue;
}
((c += t),
!l &&
r.includes(`U`) &&
(`*+`.includes(t) ||
(t === `?` && e > 0 && n[e - 1] !== `(`) ||
(t === `}` && /\{\d+(?:,\d*)?\}$/.test(c))) &&
(n[e + 1] === `?` ? e++ : (c += `?`)));
}
r.includes(`A`) && (c = `(?<![\\s\\S])(?:${c})`);
try {
new RegExp(c, s);
} catch {
throw Error(`自定义字段验证规则无效`);
}
return {
pattern: c,
patternFlags: s || void 0,
};
}
function a(a) {
let o = e(a),
s = Array.isArray(a) ? a : Array.isArray(o?.data) ? o.data : [],
c = [];
for (let a of s) {
let o = e(a);
if (!o || o.id === void 0) continue;
let s = t(o.field_type || o.type).toLowerCase();
if (
![
`text`,
`password`,
`textarea`,
`dropdown`,
`checkbox`,
`tickbox`,
`link`,
`explain`,
].includes(s)
)
continue;
let l =
s === `dropdown`
? `select`
: s === `checkbox` || s === `tickbox`
? `boolean`
: s;
c.push({
id: String(o.id),
label: t(o.field_name || o.name).trim() || `自定义字段`,
kind: l,
required: n(o.is_required ?? o.required),
...i(o.regexpr),
description: t(o.description).trim() || void 0,
explain: t(o.explain_content).trim() || void 0,
options: l === `select` ? r(o.field_option ?? o.options) : [],
});
}
return c.filter((e, t, n) => n.findIndex((t) => t.id === e.id) === t);
}
function o(e) {
return Object.fromEntries(
e.filter((e) => e.kind !== `explain`).map((e) => [e.id, (e.kind, ``)]),
);
}
function s(e, t) {
for (let n of e) {
if (n.kind === `explain`) continue;
let e = t[n.id],
r =
n.kind === `boolean`
? e === !0 || e === 1 || e === `1`
? `1`
: ``
: String(e ?? ``).trim();
if (n.required && !r) throw Error(`请填写${n.label}`);
if (r) {
if (n.kind === `select` && !n.options.some((e) => e.value === r))
throw Error(`${n.label}选项无效`);
if (n.pattern && !n.patternServerOnly) {
let e;
try {
e = new RegExp(n.pattern, n.patternFlags);
} catch {
throw Error(`${n.label}验证规则无效`);
}
if (!e.test(r)) throw Error(`${n.label}格式不正确`);
}
}
}
}
export { s as i, i as n, a as r, o as t };