966ddc9219
CI / verify (pull_request) Has been cancelled
Co-authored-by: Cursor <cursoragent@cursor.com>
60 lines
2.0 KiB
TypeScript
60 lines
2.0 KiB
TypeScript
export type CheckoutContext = {
|
|
productId?: string;
|
|
qty?: string;
|
|
addressId?: string;
|
|
cross?: boolean;
|
|
select?: boolean;
|
|
};
|
|
|
|
export function buildQuery(ctx: CheckoutContext): string {
|
|
const parts: string[] = [];
|
|
if (ctx.productId) parts.push(`productId=${encodeURIComponent(ctx.productId)}`);
|
|
if (ctx.qty) parts.push(`qty=${encodeURIComponent(ctx.qty)}`);
|
|
if (ctx.addressId) parts.push(`addressId=${encodeURIComponent(ctx.addressId)}`);
|
|
if (ctx.cross) parts.push('cross=1');
|
|
if (ctx.select) parts.push('select=1');
|
|
return parts.join('&');
|
|
}
|
|
|
|
export function buildOrderConfirmUrl(ctx: CheckoutContext): string {
|
|
const qs = buildQuery(ctx);
|
|
return qs ? `/pages/order-confirm/index?${qs}` : '/pages/order-confirm/index';
|
|
}
|
|
|
|
export function buildAddressListUrl(ctx: CheckoutContext): string {
|
|
const qs = buildQuery({ ...ctx, select: true });
|
|
return qs ? `/pages/addresses/index?${qs}` : '/pages/addresses/index';
|
|
}
|
|
|
|
export function buildAddressEditUrl(id: string | undefined, ctx: CheckoutContext): string {
|
|
const base = id ? `/pages/address-edit/index?id=${encodeURIComponent(id)}` : '/pages/address-edit/index';
|
|
const extra = buildQuery(ctx);
|
|
if (!extra) return base;
|
|
return `${base}${base.includes('?') ? '&' : '?'}${extra}`;
|
|
}
|
|
|
|
export function buildPayUrl(params: {
|
|
orderId: string;
|
|
productId?: string;
|
|
qty?: string;
|
|
addressId?: string;
|
|
cross?: boolean;
|
|
}): string {
|
|
const parts = [`orderId=${encodeURIComponent(params.orderId)}`];
|
|
if (params.productId) parts.push(`productId=${encodeURIComponent(params.productId)}`);
|
|
if (params.qty) parts.push(`qty=${encodeURIComponent(params.qty)}`);
|
|
if (params.addressId) parts.push(`addressId=${encodeURIComponent(params.addressId)}`);
|
|
if (params.cross) parts.push('cross=1');
|
|
return `/pages/pay/index?${parts.join('&')}`;
|
|
}
|
|
|
|
export function readCheckoutContext(params: Record<string, string | undefined>): CheckoutContext {
|
|
return {
|
|
productId: params.productId,
|
|
qty: params.qty,
|
|
addressId: params.addressId,
|
|
cross: params.cross === '1',
|
|
select: params.select === '1',
|
|
};
|
|
}
|