fix: pay
All checks were successful
构建并部署前端到生产环境 / build-and-deploy (push) Successful in 52s

This commit is contained in:
luo
2026-06-26 16:33:33 +08:00
parent 4dc86889c5
commit 59e4e1203e
4 changed files with 237 additions and 16 deletions

View File

@@ -1,4 +1,5 @@
const ALIPAY_PENDING_REFRESH_KEY = 'pending_alipay_payment_refresh';
const ALIPAY_PAYMENT_DATA_KEY = 'pending_alipay_payment_data';
export const PAYMENT_REFRESH_TARGETS = {
PACKAGE_ORDER: 'package-order',
@@ -13,8 +14,22 @@ export function isValidWechatPayConfig(payConfig) {
}
export function getAlipayPaymentUrl(paymentLink) {
if (!paymentLink) return '';
return paymentLink.copy_link || paymentLink.qr_link || '';
return getAlipayPaymentUrls(paymentLink).copyUrl;
}
export function getAlipayPaymentUrls(paymentLink) {
if (!paymentLink) {
return { copyUrl: '', qrUrl: '' };
}
if (typeof paymentLink === 'string') {
return { copyUrl: paymentLink, qrUrl: paymentLink };
}
return {
copyUrl: paymentLink.copy_link || paymentLink.qr_link || '',
qrUrl: paymentLink.qr_link || paymentLink.copy_link || ''
};
}
export function isValidAlipayPaymentLink(paymentLink) {
@@ -39,28 +54,73 @@ export function consumePendingPaymentRefresh(target) {
return true;
}
export function getPendingAlipayPaymentData() {
return uni.getStorageSync(ALIPAY_PAYMENT_DATA_KEY) || null;
}
function getAlipayPaymentPageUrl(refreshTarget) {
const params = [`t=${Date.now()}`];
if (refreshTarget) {
params.push(`from=${encodeURIComponent(refreshTarget)}`);
}
return `/pages/alipay-payment/alipay-payment?${params.join('&')}`;
}
function getAlipayPaymentBrowserUrl(pageUrl) {
if (typeof window === 'undefined' || !window.location) {
return pageUrl;
}
const { origin, pathname } = window.location;
const pagePath = pageUrl.replace(/^\//, '');
const basePath = pathname.includes('/pages/')
? pathname.slice(0, pathname.indexOf('/pages/'))
: pathname.replace(/\/[^/]*$/, '');
return `${origin}${basePath}/${pagePath}`;
}
function navigateToAlipayPaymentPage(pageUrl, resolve, reject) {
uni.navigateTo({
url: pageUrl,
success: () => resolve({ redirecting: true }),
fail: () => {
uni.redirectTo({
url: pageUrl,
success: () => resolve({ redirecting: true }),
fail: () => {
if (typeof window !== 'undefined' && window.location) {
window.location.href = getAlipayPaymentBrowserUrl(pageUrl);
resolve({ redirecting: true });
return;
}
reject(new Error('\u6253\u5f00\u652f\u4ed8\u5b9d\u652f\u4ed8\u9875\u5931\u8d25'));
}
});
}
});
}
export function openAlipayPayment(paymentLink, refreshTarget) {
return new Promise((resolve, reject) => {
const paymentUrl = getAlipayPaymentUrl(paymentLink);
const { copyUrl, qrUrl } = getAlipayPaymentUrls(paymentLink);
if (!paymentUrl) {
if (!copyUrl && !qrUrl) {
reject(new Error('\u652f\u4ed8\u94fe\u63a5\u65e0\u6548'));
return;
}
markPendingPaymentRefresh(refreshTarget);
if (typeof window !== 'undefined' && window.location) {
window.location.href = paymentUrl;
resolve({ redirecting: true });
return;
}
uni.setClipboardData({
data: paymentUrl,
success: () => resolve({ copied: true }),
fail: () => reject(new Error('\u5f53\u524d\u73af\u5883\u6682\u4e0d\u652f\u6301\u6253\u5f00\u652f\u4ed8\u94fe\u63a5'))
uni.setStorageSync(ALIPAY_PAYMENT_DATA_KEY, {
copyUrl,
qrUrl,
refreshTarget,
timestamp: Date.now()
});
navigateToAlipayPaymentPage(getAlipayPaymentPageUrl(refreshTarget), resolve, reject);
});
}