105 lines
2.7 KiB
JavaScript
105 lines
2.7 KiB
JavaScript
function e(e, t = {}) {
|
|
let n = {
|
|
...e,
|
|
...t,
|
|
};
|
|
return {
|
|
product_id: Number(n.product_id),
|
|
config_options: n.config_options ?? {},
|
|
qty: Math.max(1, Number(n.qty ?? 1)),
|
|
customfield: n.customfield ?? {},
|
|
self_defined_field: n.self_defined_field ?? {},
|
|
};
|
|
}
|
|
function t(e) {
|
|
return JSON.parse(JSON.stringify(e));
|
|
}
|
|
function n(n) {
|
|
return n.map((n) => t(e(n)));
|
|
}
|
|
function r(e) {
|
|
return Array.isArray(e)
|
|
? e.map(r)
|
|
: e && typeof e == `object`
|
|
? Object.fromEntries(
|
|
Object.entries(e)
|
|
.filter(([, e]) => e !== void 0)
|
|
.sort(([e], [t]) => e.localeCompare(t))
|
|
.map(([e, t]) => [e, r(t)]),
|
|
)
|
|
: e;
|
|
}
|
|
function i(t) {
|
|
return JSON.stringify(r(e(t)));
|
|
}
|
|
function a(e, t) {
|
|
if (!e) return;
|
|
let n = e.item.self_defined_field ?? {};
|
|
if (Object.keys(n).some((e) => !t.includes(e)))
|
|
throw Error(`商品自定义字段已调整,请返回购物车删除该项后重新配置`);
|
|
}
|
|
var o = null;
|
|
async function s(e) {
|
|
let t = new TextEncoder().encode(i(e));
|
|
return Array.from(
|
|
new Uint8Array(await crypto.subtle.digest(`SHA-256`, t)),
|
|
(e) => e.toString(16).padStart(2, `0`),
|
|
).join(``);
|
|
}
|
|
async function c(e, t) {
|
|
let n = [...t];
|
|
o = {
|
|
fingerprints: await Promise.all(e.map(s)),
|
|
indices: n,
|
|
};
|
|
}
|
|
async function l(e) {
|
|
let t = o;
|
|
if (!t) return null;
|
|
o = null;
|
|
let n = await Promise.all(e.map(s)),
|
|
r = new Set();
|
|
return t.indices.flatMap((e) => {
|
|
let i = t.fingerprints
|
|
.slice(0, e)
|
|
.filter((n) => n === t.fingerprints[e]).length,
|
|
a = n.flatMap((n, r) => (n === t.fingerprints[e] ? [r] : []))[i];
|
|
return a === void 0 || r.has(a) ? [] : (r.add(a), [a]);
|
|
});
|
|
}
|
|
function u(n, r) {
|
|
let a = r ? t(r.item) : null,
|
|
c = !1;
|
|
return async (l, u) => {
|
|
if (!r || !a) throw Error(`购物车编辑上下文缺失`);
|
|
if (c) throw Error(`正在保存,请勿重复操作`);
|
|
c = !0;
|
|
let d = t(e(a, l));
|
|
try {
|
|
let c = (await n.get(`/cart`)).list?.[r.position];
|
|
if (!c || i(c) !== i(a))
|
|
throw Error(`购物车商品已变化,请返回购物车刷新后重新编辑`);
|
|
if (!u()) throw Error(`配置已改变,请重新确认报价后保存`);
|
|
let l = await n.put(`/cart/${r.position}`, {
|
|
...d,
|
|
expected_item: e(a),
|
|
}),
|
|
f = a;
|
|
a = t({
|
|
...a,
|
|
...d,
|
|
});
|
|
let p = o;
|
|
return (
|
|
p &&
|
|
p.fingerprints[r.position] === (await s(f)) &&
|
|
(p.fingerprints[r.position] = await s(a)),
|
|
l
|
|
);
|
|
} finally {
|
|
c = !1;
|
|
}
|
|
};
|
|
}
|
|
export { l as a, c as i, t as n, a as o, u as r, n as t };
|