refactor: 常量化订单状态 + 支付渠道/提供商分离显示 + H5自动跳转

- 新增 src/lib/constants.ts,集中管理 ORDER_STATUS / PAYMENT_TYPE / PAYMENT_PREFIX 等常量
- 后端 service/status/timeout/limits 全量替换魔法字符串为 ORDER_STATUS.*
- PaymentTypeMeta 新增 provider 字段,分离 sublabel(选择器展示)与 provider(提供商名称)
- getPaymentDisplayInfo() 返回 { channel, provider } 用于用户端/管理端展示
- 支持通过 PAYMENT_SUBLABEL_* 环境变量覆盖默认 sublabel
- PaymentQRCode: H5 支付自动跳转(含易支付微信 weixin:// scheme 兜底)
- 订单列表/详情页:显示可读的渠道名+提供商,不再暴露内部标识符

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
erio
2026-03-06 17:34:42 +08:00
parent 3829d0e52e
commit 254ead1908
14 changed files with 250 additions and 94 deletions

View File

@@ -9,6 +9,7 @@ import {
getPaymentIconSrc,
getPaymentChannelLabel,
} from '@/lib/pay-utils';
import { TERMINAL_STATUSES } from '@/lib/constants';
interface PaymentQRCodeProps {
orderId: string;
@@ -36,7 +37,6 @@ const TEXT_BACK = '\u8FD4\u56DE';
const TEXT_CANCEL_ORDER = '\u53D6\u6D88\u8BA2\u5355';
const TEXT_H5_HINT =
'\u652F\u4ED8\u5B8C\u6210\u540E\u8BF7\u8FD4\u56DE\u6B64\u9875\u9762\uFF0C\u7CFB\u7EDF\u5C06\u81EA\u52A8\u786E\u8BA4';
const TERMINAL_STATUSES = new Set(['COMPLETED', 'FAILED', 'CANCELLED', 'EXPIRED', 'REFUNDED', 'REFUND_FAILED']);
export default function PaymentQRCode({
orderId,
@@ -62,6 +62,7 @@ export default function PaymentQRCode({
const [qrDataUrl, setQrDataUrl] = useState('');
const [imageLoading, setImageLoading] = useState(false);
const [cancelBlocked, setCancelBlocked] = useState(false);
const [redirected, setRedirected] = useState(false);
// Stripe Payment Element state
const [stripeLoaded, setStripeLoaded] = useState(false);
@@ -80,6 +81,24 @@ export default function PaymentQRCode({
// alipay_direct 使用电脑网站支付payUrl 是跳转链接不是二维码内容
const isRedirect = isRedirectPayment(paymentType);
// 移动端可用的跳转链接:优先 payUrl其次尝试 qrCode微信 weixin:// 协议可直接唤起)
const mobileRedirectUrl = payUrl || (qrCode && /^(https?:|weixin:)/i.test(qrCode) ? qrCode : null);
// 自动跳转redirect 支付方式 或 移动端 H5
const shouldAutoRedirect = !expired && !isStripeType(paymentType) && ((isRedirect && payUrl) || (isMobile && mobileRedirectUrl));
useEffect(() => {
if (!shouldAutoRedirect || redirected) return;
const url = isRedirect ? payUrl! : mobileRedirectUrl!;
setRedirected(true);
// embedded iframe 不能 location.href 跳转,用 window.open
if (isEmbedded) {
window.open(url, '_blank');
} else {
window.location.href = url;
}
}, [shouldAutoRedirect, redirected, isRedirect, payUrl, mobileRedirectUrl, isEmbedded]);
const qrPayload = useMemo(() => {
if (isRedirect && !qrCode) return '';
const value = (qrCode || payUrl || '').trim();
@@ -448,31 +467,22 @@ export default function PaymentQRCode({
</>
)}
</div>
) : isMobile && payUrl ? (
) : shouldAutoRedirect ? (
<>
<div className="flex items-center justify-center py-6">
<div className={`h-8 w-8 animate-spin rounded-full border-2 border-t-transparent`} style={{ borderColor: meta.color, borderTopColor: 'transparent' }} />
<span className={['ml-3 text-sm', dark ? 'text-slate-400' : 'text-gray-500'].join(' ')}>
{channelLabel}...
</span>
</div>
<a
href={payUrl}
href={isRedirect ? payUrl! : mobileRedirectUrl!}
target={isEmbedded ? '_blank' : '_self'}
rel="noopener noreferrer"
className={`flex w-full items-center justify-center gap-2 rounded-lg py-3 font-medium text-white shadow-md ${iconBgClass}`}
>
<img src={iconSrc} alt={channelLabel} className="h-5 w-5 brightness-0 invert" />
{`打开${channelLabel}支付`}
</a>
<p className={['text-center text-sm', dark ? 'text-slate-400' : 'text-gray-500'].join(' ')}>
{TEXT_H5_HINT}
</p>
</>
) : isRedirect && payUrl ? (
<>
<a
href={payUrl}
target="_blank"
rel="noopener noreferrer"
className={`flex w-full items-center justify-center gap-2 rounded-lg py-3 font-medium text-white shadow-md ${meta.buttonClass}`}
>
{iconSrc && <img src={iconSrc} alt={channelLabel} className="h-5 w-5 brightness-0 invert" />}
{`前往${channelLabel}收银台`}
{redirected ? `未跳转?点击前往${channelLabel}` : `前往${channelLabel}支付`}
</a>
<p className={['text-center text-sm', dark ? 'text-slate-400' : 'text-gray-500'].join(' ')}>
{TEXT_H5_HINT}