63 lines
1.8 KiB
TypeScript
63 lines
1.8 KiB
TypeScript
import { showDialog, showToast } from 'vant'
|
|
|
|
import type { PaymentOrder } from '@/features/orders/api/orders'
|
|
import {
|
|
paymentPayURL,
|
|
useOrderPaymentCashier,
|
|
} from '@/features/orders/composables/useOrderPaymentCashier'
|
|
|
|
export { paymentPayURL as mobilePaymentPayURL }
|
|
|
|
/**
|
|
* 移动端支付收银台 Composable。
|
|
*
|
|
* 在通用 {@link useOrderPaymentCashier} 基础上注入 vant 的 toast 提示,
|
|
* 并处理微信 / 支付宝内嵌浏览器的直接跳转拉起支付。对外 API 名保持不变,
|
|
* Mobile 视图无需改动。
|
|
*/
|
|
|
|
export function useMobilePaymentCashier(options?: {
|
|
queryPayment?: (orderId: number) => Promise<PaymentOrder>
|
|
paidMessage?: string
|
|
}) {
|
|
function isInAppPaymentBrowser(): boolean {
|
|
if (typeof navigator === 'undefined') return false
|
|
const ua = navigator.userAgent || ''
|
|
return /MicroMessenger|AlipayClient/i.test(ua)
|
|
}
|
|
|
|
const cashier = useOrderPaymentCashier({
|
|
notifySuccess: message => showToast({ message, icon: 'passed' }),
|
|
notifyError: message => showToast({ message, icon: 'cross' }),
|
|
notifyInfo: message => showToast({ message, icon: 'info-o' }),
|
|
notifyFallback: message => {
|
|
void showDialog({
|
|
title: '订单支付',
|
|
message,
|
|
confirmButtonText: '知道了',
|
|
})
|
|
},
|
|
paidMessage: options?.paidMessage || '支付成功,正在进入群消息',
|
|
pollIntervalMs: 2500,
|
|
qrWidth: 220,
|
|
queryPayment: options?.queryPayment,
|
|
openExternalURL: url => {
|
|
if (isHTTPURL(url) && isInAppPaymentBrowser()) {
|
|
window.location.href = url
|
|
return true
|
|
}
|
|
return false
|
|
},
|
|
})
|
|
|
|
return {
|
|
...cashier,
|
|
// 保持 Mobile 视图既有调用名。
|
|
openMobilePaymentCashier: cashier.openPaymentCashier,
|
|
}
|
|
}
|
|
|
|
function isHTTPURL(value: string): boolean {
|
|
return /^https?:\/\//i.test(value)
|
|
}
|