feat: 页面优化2

This commit is contained in:
wolter 2025-07-18 15:27:26 +08:00
parent 93044a47c7
commit b20417cb60
1 changed files with 28 additions and 101 deletions

View File

@ -114,7 +114,6 @@
margin-right: 15px; margin-right: 15px;
} }
/* 选中状态样式 */
.payment-option.selected { .payment-option.selected {
border-color: #007BFF; border-color: #007BFF;
background-color: #f0f7ff; background-color: #f0f7ff;
@ -125,7 +124,7 @@
{{if eq .code 200 }} {{if eq .code 200 }}
<body> <body>
<!-- 页面内容 --> <!-- 页面内容 -->
<div class="payment-info"> <div class="payment-info" id="payment-info">
<h2>订单支付</h2> <h2>订单支付</h2>
<p>{{.desc}}</p> <p>{{.desc}}</p>
<p>交易号:{{ .id }}</p> <p>交易号:{{ .id }}</p>
@ -137,12 +136,12 @@
<div id="pay"></div> <div id="pay"></div>
<!-- 支付方式列表 --> <!-- 支付方式列表 -->
<ul id="payment-list"></ul> <ul id="payment-list"></ul>
</div>
<!-- Loading状态 --> <!-- Loading状态 -->
<div id="loading" class="loading-container" style="display: none;"> <div id="loading" class="loading-container" style="display: none;">
<div class="loading-spinner"></div> <div class="loading-spinner"></div>
<p class="loading-text">支付处理中,请稍候...</p> <p class="loading-text">支付处理中,请稍等...</p>
</div>
</div> </div>
</body> </body>
@ -161,94 +160,30 @@
return null; return null;
} }
// 检查是否是支付回调
function isPaymentCallback() {
return getQueryParam('callback') === 'true';
}
// 获取预设的支付方式
function getPresetPaymentMethod() {
return getQueryParam('preset_method') || null;
}
// 检查订单状态
function checkOrderStatus(orderNo) {
return fetch(`/pay/front/api/v1/payPage/status?no=${orderNo}`, {
method: 'GET',
})
.then(response => {
if (!response.ok) {
throw new Error('网络响应不正常');
}
return response.json();
})
.then(data => {
if (data.code !== 200) {
throw new Error(data.message || '获取订单状态失败');
}
return data;
});
}
// 显示Loading状态 // 显示Loading状态
function showLoading() { function showLoading() {
document.getElementById('payment-info').style.display = 'none';
document.getElementById('pay-container').style.display = 'none'; document.getElementById('pay-container').style.display = 'none';
document.getElementById('loading').style.display = 'flex'; document.getElementById('loading').style.display = 'flex';
} }
// 隐藏Loading状态
function hideLoading() {
document.getElementById('pay-container').style.display = 'block';
document.getElementById('loading').style.display = 'none';
}
// 处理支付回调 // 处理支付回调
function handlePaymentCallback() { function handlePaymentCallback() {
const orderNo = getQueryParam('no');
if (!orderNo) {
console.error('订单号不存在');
return;
}
showLoading(); showLoading();
const loadingText = document.querySelector('.loading-text');
loadingText.textContent = `支付处理中,请稍等...`;
// 检查订单状态 // 1秒后刷新页面
checkOrderStatus(orderNo)
.then(data => {
if (data.data.status === 'SUCCESS') {
// 支付成功,跳转到指定链接
if (data.data.redirectUrl) {
setTimeout(() => { setTimeout(() => {
window.location.href = data.data.redirectUrl; clearInterval(dotInterval);
window.location.reload();
}, 1000); }, 1000);
} else {
hideLoading();
alert('支付成功!');
}
} else if (data.data.status === 'PENDING') {
// 支付处理中1秒后再次检查
setTimeout(() => {
handlePaymentCallback();
}, 1000);
} else {
// 支付失败或其他状态
hideLoading();
alert('支付未完成: ' + (data.data.message || '未知错误'));
}
})
.catch(error => {
hideLoading();
console.error('检查订单状态失败:', error);
alert('检查订单状态失败: ' + error.message);
});
} }
// 获取支付方式列表 // 获取支付方式列表
function fetchPaymentMethods() { function fetchPaymentMethods() {
const id = getQueryParam('no'); const id = getQueryParam('no');
const presetMethod = getPresetPaymentMethod(); console.log('Order no:', id);
console.log('Order no:', id, 'Preset method:', presetMethod);
if (id) { if (id) {
fetch(`/pay/front/api/v1/payPage/list?id=${id}`, { fetch(`/pay/front/api/v1/payPage/list?id=${id}`, {
method: 'POST', method: 'POST',
@ -272,11 +207,11 @@
const pay = document.getElementById('pay'); const pay = document.getElementById('pay');
pay.innerHTML = '<h3>支付环境异常,请检查</h3>'; pay.innerHTML = '<h3>支付环境异常,请检查</h3>';
} else if (data.data.length === 1) { } else if (data.data.length === 1) {
// 只有一个支付方式时,显示支付中状态并直接跳转 // 只有一个支付方式时,显示空白页并直接跳转
showLoading(); showLoading();
window.location.href = `/pay/front/api/v1/payPage/submit?pay_channel_id=${data.data[0].pay_channel_id}&no=${id}`; window.location.href = `/pay/front/api/v1/payPage/submit?pay_channel_id=${data.data[0].pay_channel_id}&no=${id}`;
} else { } else {
renderPaymentMethods(data.data, presetMethod); renderPaymentMethods(data.data);
} }
}) })
.catch(error => { .catch(error => {
@ -290,14 +225,15 @@
} }
// 渲染支付方式列表 // 渲染支付方式列表
function renderPaymentMethods(paymentMethods, presetMethod) { function renderPaymentMethods(paymentMethods) {
const pay = document.getElementById('pay'); const pay = document.getElementById('pay');
pay.innerHTML = '<h3>请选择支付方式</h3>'; pay.innerHTML = '<h3>请选择支付方式</h3>';
const paymentList = document.getElementById('payment-list'); const paymentList = document.getElementById('payment-list');
paymentList.innerHTML = ''; paymentList.innerHTML = '';
let hasPresetMethod = false; // 标记是否是第一个支付方式
let isFirstMethod = true;
paymentMethods.forEach(method => { paymentMethods.forEach(method => {
const listItem = document.createElement('li'); const listItem = document.createElement('li');
@ -309,11 +245,11 @@
radioInput.value = method.pay_channel_id; radioInput.value = method.pay_channel_id;
radioInput.id = `method-${method.pay_channel_id}`; radioInput.id = `method-${method.pay_channel_id}`;
// 检查是否是预设的支付方式 // 如果是第一个支付方式,默认选中
if (presetMethod && (method.pay_channel_id === presetMethod || method.pay_name.includes(presetMethod))) { if(isFirstMethod) {
radioInput.checked = true; radioInput.checked = true;
listItem.classList.add('selected'); listItem.classList.add('selected');
hasPresetMethod = true; isFirstMethod = false;
} }
const label = document.createElement('label'); const label = document.createElement('label');
@ -344,15 +280,6 @@
}); });
}); });
// 如果没有匹配的预设支付方式,默认选中第一个
if (!hasPresetMethod && paymentMethods.length > 0) {
const firstItem = document.querySelector('.payment-option');
if (firstItem) {
firstItem.classList.add('selected');
firstItem.querySelector('input[type="radio"]').checked = true;
}
}
// 添加提交按钮 // 添加提交按钮
const submitButton = document.createElement('button'); const submitButton = document.createElement('button');
submitButton.type = 'button'; submitButton.type = 'button';
@ -377,7 +304,7 @@
// 页面加载时执行 // 页面加载时执行
window.onload = function() { window.onload = function() {
if (isPaymentCallback()) { if (getQueryParam('return')) {
// 如果是支付回调,处理支付结果 // 如果是支付回调,处理支付结果
handlePaymentCallback(); handlePaymentCallback();
} else { } else {