Files
device-voice-h5/pages/silence-extension/silence-extension.vue
邹进 06ca8af218
Some checks failed
构建并部署前端到测试环境 / build-and-deploy (push) Has been cancelled
feat: H5 延期订单可作废,未完成订单直接进入详情继续支付或作废
- 延期订单详情待支付时显示作废订单,作废后可重新办理延期
- 延期页有未完成订单时直接打开该订单详情;作废返回后重新查资格
2026-11-07 20:33:42 +09:00

506 lines
14 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<view class="container silence-page">
<view v-if="loading" class="loading-state">
<up-loading-icon text="加载中" size="30"></up-loading-icon>
</view>
<view v-else-if="blockedMessage" class="card blocked-card">
<view class="blocked-title">{{ blockedMessage }}</view>
<view v-if="pendingOrderNo" class="blocked-desc">未完成订单:{{ pendingOrderNo }}</view>
<view class="blocked-actions">
<view v-if="pendingOrderNo" class="btn-apple btn-primary" @tap="openPendingOrder">去支付或作废该订单</view>
<view class="btn-apple btn-secondary" @tap="goBack">返回</view>
</view>
</view>
<template v-else-if="eligibility">
<view class="card">
<view class="section-title">当前沉默期</view>
<view class="info-row flex-row-sb">
<view class="info-label">当前截止日期</view>
<view class="info-value">{{ eligibility.original_deadline || '-' }}</view>
</view>
<view v-if="eligibility.overdue_days > 0" class="info-row flex-row-sb">
<view class="info-label">已过期</view>
<view class="info-value danger">{{ eligibility.overdue_days }} 天</view>
</view>
<view class="info-row flex-row-sb">
<view class="info-label">可延期天数</view>
<view class="info-value">{{ eligibility.min_days }}~{{ eligibility.max_days }} 天</view>
</view>
<view v-if="eligibility.extended_days > 0" class="info-row flex-row-sb">
<view class="info-label">已累计延期</view>
<view class="info-value">{{ eligibility.extended_days }} 天</view>
</view>
</view>
<view class="card">
<view class="section-title">选择延期天数</view>
<view class="option-list">
<view
v-for="option in options"
:key="option.billed_days"
class="option-item"
:class="{ active: !customMode && selectedDays === option.billed_days }"
@tap="selectOption(option)"
>
<view class="option-days">{{ option.billed_days }} 天</view>
<view class="option-amount">¥{{ formatMoney(option.amount) }}</view>
<view class="option-deadline">延至 {{ option.new_deadline }}</view>
</view>
</view>
<view class="custom-row" :class="{ active: customMode }">
<view class="custom-label" @tap="enableCustom">自定义天数</view>
<input
class="custom-input"
type="number"
:value="customDays"
:placeholder="`${eligibility.min_days}~${eligibility.max_days}`"
@focus="enableCustom"
@input="onCustomInput"
/>
<text class="custom-unit">天</text>
</view>
<view v-if="customError" class="custom-error">{{ customError }}</view>
<view v-else-if="customMode && quoting" class="custom-hint">试算中...</view>
</view>
<view v-if="summary" class="card summary-card">
<view class="info-row flex-row-sb">
<view class="info-label">计费天数</view>
<view class="info-value">{{ summary.billed_days }} 天</view>
</view>
<view class="info-row flex-row-sb">
<view class="info-label">新截止日期</view>
<view class="info-value">{{ summary.new_deadline }}</view>
</view>
<view class="info-row flex-row-sb">
<view class="info-label">应付金额</view>
<view class="info-value amount">¥{{ formatMoney(summary.amount) }}</view>
</view>
<view class="summary-tip">按每月 30 天计费,不足 30 天按 30 天计。</view>
</view>
<view class="card">
<view class="section-title">支付方式</view>
<view v-if="paymentOptions.length === 0" class="empty-desc">当前资产暂不支持在线延期,请联系客服办理</view>
<view
v-for="method in paymentOptions"
:key="method.value"
class="payment-item flex-row-sb"
:class="{ active: paymentMethod === method.value }"
@tap="paymentMethod = method.value"
>
<text>{{ method.label }}</text>
<text class="payment-check">{{ paymentMethod === method.value ? '✓' : '' }}</text>
</view>
</view>
<view class="footer-actions">
<view class="orders-link" @tap="openOrders">我的延期订单</view>
<view
class="btn-apple btn-primary submit-btn"
:class="{ disabled: !canSubmit }"
@tap="submit"
>{{ submitting ? '提交中...' : '确认支付' }}</view>
</view>
</template>
</view>
</template>
<script setup>
import { computed, ref } from 'vue';
import { onLoad, onShow } from '@dcloudio/uni-app';
import { assetApi, packageSilenceApi } from '@/api/index.js';
import { useUserStore } from '@/store/index.js';
import { formatMoney } from '@/utils/display.js';
import { consumePendingPaymentRefresh } from '@/utils/payment.js';
import {
createRequestIdKeeper,
getSilenceErrorMessage,
getSilencePaymentOptions,
getSilenceReasonText,
validateExtensionDays
} from '@/utils/silence.js';
import { openSilencePayment, SILENCE_REFRESH_TARGET } from '@/utils/silence-payment.js';
const userStore = useUserStore();
const packageUsageId = ref(0);
const loading = ref(true);
const eligibility = ref(null);
const blockedMessage = ref('');
const pendingOrderNo = ref('');
const paymentOptions = ref([]);
const paymentMethod = ref('');
const selectedDays = ref(0);
const customMode = ref(false);
const customDays = ref('');
const customError = ref('');
const quoting = ref(false);
const quoteResult = ref(null);
const submitting = ref(false);
const lastOrderId = ref(0);
const requestIds = createRequestIdKeeper();
let quoteTimer = null;
let quoteSeq = 0;
const options = computed(() => (Array.isArray(eligibility.value?.options) ? eligibility.value.options : []));
const chosenDays = computed(() => (customMode.value ? Number(customDays.value) : selectedDays.value));
const summary = computed(() => {
if (customMode.value) return customError.value ? null : quoteResult.value;
return options.value.find((option) => option.billed_days === selectedDays.value) || null;
});
const canSubmit = computed(() => (
!submitting.value && !!summary.value && !!paymentMethod.value && !(customMode.value && quoting.value)
));
const showToast = (title) => uni.showToast({ title, icon: 'none', duration: 2500 });
const loadEligibility = async () => {
loading.value = true;
blockedMessage.value = '';
pendingOrderNo.value = '';
try {
const [data, info] = await Promise.all([
packageSilenceApi.getEligibility(packageUsageId.value),
assetApi.getInfo(userStore.state.identifier).catch(() => null)
]);
eligibility.value = data;
paymentOptions.value = getSilencePaymentOptions(info?.allowed_payment_methods);
if (!paymentOptions.value.some((option) => option.value === paymentMethod.value)) {
paymentMethod.value = paymentOptions.value[0]?.value || '';
}
if (data?.pending_order_no) {
pendingOrderNo.value = data.pending_order_no;
blockedMessage.value = '已有未完成的延期订单,暂不能新建';
} else if (!data?.can_extend) {
blockedMessage.value = getSilenceReasonText(data?.reason);
} else if (!selectedDays.value) {
selectedDays.value = options.value[0]?.billed_days || 0;
if (!selectedDays.value) customMode.value = true;
}
} catch (error) {
blockedMessage.value = getSilenceErrorMessage(error, '查询延期资格失败,请稍后重试');
} finally {
loading.value = false;
}
};
const selectOption = (option) => {
customMode.value = false;
customError.value = '';
selectedDays.value = option.billed_days;
};
const enableCustom = () => {
customMode.value = true;
};
const runQuote = async (days) => {
const seq = ++quoteSeq;
quoting.value = true;
try {
const data = await packageSilenceApi.quote(packageUsageId.value, days);
if (seq !== quoteSeq) return;
quoteResult.value = data;
} catch (error) {
if (seq !== quoteSeq) return;
quoteResult.value = null;
customError.value = getSilenceErrorMessage(error, '试算失败,请稍后重试');
} finally {
if (seq === quoteSeq) quoting.value = false;
}
};
const onCustomInput = (event) => {
customMode.value = true;
customDays.value = String(event?.detail?.value ?? '').trim();
quoteResult.value = null;
clearTimeout(quoteTimer);
quoteSeq += 1;
quoting.value = false;
customError.value = customDays.value
? validateExtensionDays(customDays.value, eligibility.value?.min_days, eligibility.value?.max_days)
: '';
if (!customDays.value || customError.value) return;
const days = Number(customDays.value);
quoteTimer = setTimeout(() => runQuote(days), 500);
};
const submit = async () => {
if (!canSubmit.value) {
if (!paymentMethod.value) showToast('请选择支付方式');
else if (!summary.value) showToast('请选择延期天数');
return;
}
const days = chosenDays.value;
const message = validateExtensionDays(days, eligibility.value?.min_days, eligibility.value?.max_days);
if (message) {
showToast(message);
return;
}
submitting.value = true;
try {
const order = await packageSilenceApi.createOrder({
package_usage_id: packageUsageId.value,
days,
payment_method: paymentMethod.value,
request_id: requestIds.get(days, paymentMethod.value)
});
lastOrderId.value = order?.id || 0;
if (!order?.qr_content) {
showToast('订单已创建,请在"我的延期订单"中继续支付');
openOrders();
return;
}
await openSilencePayment(order, SILENCE_REFRESH_TARGET.EXTENSION);
} catch (error) {
// 网络异常时后端可能已建单:保留 request_id,客户再次点击会拿到同一笔订单。
if (!error?.paymentResultUnknown) requestIds.reset();
showToast(getSilenceErrorMessage(error, '下单失败,请稍后重试'));
if (Number(error?.code) === 1271) await loadEligibility();
} finally {
submitting.value = false;
}
};
const checkLastOrder = async () => {
if (!lastOrderId.value) return;
try {
const order = await packageSilenceApi.getOrder(lastOrderId.value);
if (order?.status === 'completed') {
uni.showToast({ title: '延期成功', icon: 'success' });
requestIds.reset();
uni.setStorageSync('silence_extension_completed', Date.now());
setTimeout(() => uni.navigateBack(), 1200);
return;
}
if (order?.status === 'paid') {
showToast('支付成功,延期处理中');
return;
}
uni.navigateTo({ url: `/pages/silence-orders/detail?id=${lastOrderId.value}` });
} catch (error) {
showToast(getSilenceErrorMessage(error, '查询订单失败'));
}
};
// 有未完成订单时直接打开该订单详情:可继续支付,也可作废后重新办理
const openPendingOrder = async () => {
try {
const data = await packageSilenceApi.getOrders({ order_no: pendingOrderNo.value, page: 1, page_size: 1 });
const target = Array.isArray(data?.items) ? data.items[0] : null;
if (target?.id) {
uni.navigateTo({ url: `/pages/silence-orders/detail?id=${target.id}` });
return;
}
} catch (error) {
console.error('查询未完成延期订单失败', error);
}
openOrders();
};
const openOrders = () => {
uni.navigateTo({ url: '/pages/silence-orders/silence-orders' });
};
const goBack = () => uni.navigateBack();
onLoad((query) => {
packageUsageId.value = Number(query?.package_usage_id) || 0;
if (!packageUsageId.value) {
loading.value = false;
blockedMessage.value = '缺少套餐信息,请从套餐历史进入';
return;
}
loadEligibility();
});
onShow(() => {
// 从订单详情作废未完成订单后返回:重新查资格,允许重新下单
if (pendingOrderNo.value && packageUsageId.value) {
loadEligibility();
return;
}
if (consumePendingPaymentRefresh(SILENCE_REFRESH_TARGET.EXTENSION)) checkLastOrder();
});
</script>
<style lang="scss" scoped>
.silence-page {
padding-bottom: 180rpx;
}
.loading-state {
display: flex;
justify-content: center;
padding: 200rpx 0;
}
.section-title {
margin-bottom: var(--space-md);
font-size: 30rpx;
font-weight: 600;
color: var(--text-primary);
}
.info-row {
padding: 10rpx 0;
font-size: 26rpx;
}
.info-label {
color: var(--text-tertiary);
}
.info-value {
color: var(--text-primary);
&.danger {
color: var(--danger);
}
&.amount {
font-size: 34rpx;
font-weight: 700;
color: var(--danger);
}
}
.option-list {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 20rpx;
}
.option-item {
padding: 24rpx;
border: 2rpx solid var(--border-color, #e5e5ea);
border-radius: 20rpx;
&.active {
border-color: var(--primary);
background: rgba(85, 171, 92, 0.08);
}
}
.option-days {
font-size: 30rpx;
font-weight: 600;
}
.option-amount {
margin-top: 8rpx;
font-size: 28rpx;
color: var(--danger);
}
.option-deadline {
margin-top: 8rpx;
font-size: 22rpx;
color: var(--text-tertiary);
}
.custom-row {
display: flex;
align-items: center;
gap: 16rpx;
margin-top: 24rpx;
padding: 20rpx 24rpx;
border: 2rpx solid var(--border-color, #e5e5ea);
border-radius: 20rpx;
&.active {
border-color: var(--primary);
}
}
.custom-label {
font-size: 26rpx;
white-space: nowrap;
}
.custom-input {
flex: 1;
font-size: 28rpx;
}
.custom-error {
margin-top: 12rpx;
font-size: 24rpx;
color: var(--danger);
}
.custom-hint,
.summary-tip,
.empty-desc {
margin-top: 12rpx;
font-size: 24rpx;
color: var(--text-tertiary);
}
.payment-item {
padding: 24rpx 0;
font-size: 28rpx;
border-bottom: 1rpx solid var(--border-color, #e5e5ea);
&.active {
color: var(--primary-dark);
}
}
.payment-check {
color: var(--primary);
}
.footer-actions {
position: fixed;
right: 0;
bottom: 0;
left: 0;
display: flex;
align-items: center;
justify-content: space-between;
padding: 24rpx 32rpx calc(24rpx + env(safe-area-inset-bottom));
background: var(--bg-primary);
box-shadow: 0 -4rpx 16rpx rgba(0, 0, 0, 0.06);
}
.orders-link {
font-size: 26rpx;
color: var(--primary-dark);
}
.submit-btn {
min-width: 280rpx;
text-align: center;
&.disabled {
opacity: 0.5;
}
}
.blocked-card {
margin-top: 40rpx;
text-align: center;
}
.blocked-title {
font-size: 30rpx;
color: var(--text-primary);
}
.blocked-desc {
margin-top: 16rpx;
font-size: 24rpx;
color: var(--text-tertiary);
}
.blocked-actions {
display: flex;
flex-direction: column;
gap: 20rpx;
margin-top: 40rpx;
}
</style>