优化,完善代码

This commit is contained in:
2026-05-14 14:42:51 +08:00
parent 79a21ff0a5
commit 16c91facac
26 changed files with 1792 additions and 1322 deletions

View File

@@ -1,13 +1,20 @@
<template>
<view class="pending-bill-page">
<!-- 自定义导航栏 -->
<custom-navbar title="水电费待付" />
<scroll-view scroll-y class="scroll-content" :refresher-enabled="true" :refresher-triggered="isRefreshing"
@refresherrefresh="refresh" @scrolltolower="loadMore">
<DateFilter :start="startDate" :end="endDate" @update:start="startDate = $event"
@update:end="endDate = $event" @change="onDateFilterChange" />
<scroll-view
scroll-y
class="scroll-content"
:refresher-enabled="true"
:refresher-triggered="isRefreshing"
@refresherrefresh="refresh"
@scrolltolower="loadMore"
>
<view v-if="bills.length > 0">
<view class="bill-item" v-for="item in bills" :key="item.billNo">
<!-- 左侧勾选 + 信息 -->
<view class="bill-left">
<u-checkbox v-model="item.checked" @change="toggleCheck(item)" size="32"
active-color="#EA414A" />
@@ -17,10 +24,10 @@
</view>
</view>
<!-- 右侧金额 + 支付按钮 -->
<view class="bill-right">
<text class="amount">{{ formatMoney(item.billAmount) }}</text>
<button class="pay-btn" @click="goPayTest([item])">去支付
<button class="pay-btn" @click="goPay([item])" :disabled="isPaying">
去支付
</button>
</view>
</view>
@@ -33,15 +40,15 @@
<u-loadmore :status="loadStatus" />
</scroll-view>
<!-- 底部批量支付栏 -->
<view v-if="selectedBills.length > 0" class="batch-pay-bar">
<view class="batch-pay-bar-left"><text>已选 {{ selectedBills.length }} </text></view>
<view class="batch-pay-bar-center">
合计<text class="sumAmount">{{ formatMoney(sumAmount) }}</text>
</view>
<view class="batch-pay-bar-right">
<button class="bottomPayBtn" color="linear-gradient(90deg, #007aff, #00aaff)" type="primary"
size="small" @click="goPayTest(selectedBills)">
<button class="bottomPayBtn" type="primary" size="small"
@click="goPay(selectedBills)"
:disabled="isPaying">
批量缴费
</button>
</view>
@@ -50,16 +57,23 @@
</template>
<script>
import DateFilter from '../../components/DatePicker/DateFilter.vue';
export default {
components: { DateFilter },
data() {
return {
pageNo: 1,
pageSize: 10,
bills: [],
isRefreshing: false,
loadStatus: 'loadmore',
loadStatus: 'more', // ✅ 修复为正确状态
sumAmount: 0,
selectedBills: [], // 勾选的账单集合
selectedBills: [],
startDate: "",
endDate: "",
minDate: "2000-01-01",
maxDate: "2199-12-31",
isPaying: false
};
},
onLoad(options) {
@@ -68,166 +82,174 @@
onShow() {
this.$checkToken(this.$getToken())
},
// ❌ 已删除冲突的 onReachBottom
methods: {
loadBills() {
if (this.loadStatus !== 'loadmore') return;
// ✅ 正确判断状态
if (this.loadStatus !== 'more') return;
this.loadStatus = 'loading';
const token = this.$getToken();
let url = '/bill/pageQueryWaeBill'
this.$u.post(url, {
pageNo: this.pageNo,
pageSize: this.pageSize,
startDate: this.startDate,
endDate: this.endDate,
billStatus: '待缴费'
}, {
WT: token
}).then(
result => {
const rows = result.data.result || [];
}).then(res => {
const rows = res.data.result || [];
rows.forEach(item => {
item.checked = false;
});
if (this.pageNo === 1) this.bills = [];
this.bills = this.bills.concat(rows);
if (this.pageNo === 1) this.bills = [];
this.bills = this.bills.concat(rows);
if (rows.length < this.pageSize) {
this.loadStatus = 'nomore';
} else {
this.pageNo++;
this.loadStatus = 'loadmore';
}
})
this.updateSelected();
if (rows.length < this.pageSize) {
this.loadStatus = 'nomore';
} else {
this.pageNo++;
this.loadStatus = 'more'; // ✅ 恢复可加载
}
}).catch(err => {
console.log("获取水电费账单失败:", err);
this.loadStatus = 'more'; // ✅ 异常恢复
})
},
refresh() {
this.isRefreshing = true;
this.pageNo = 1;
this.selectedBills = [];
this.sumAmount = 0;
setTimeout(() => {
this.loadBills();
this.isRefreshing = false;
}, 1000);
}, 500);
},
loadMore() {
this.loadBills()
},
onDateFilterChange({ start, end }) {
this.loadStatus = 'more'
this.refresh();
},
formatMoney(val) {
if (val === null || val === undefined || isNaN(val)) return '—';
val = Number(val);
if (val >= 10000) {
return '¥' + (val / 10000).toFixed(2) + '万';
}
return '¥' + val.toFixed(2);
},
toggleCheck(item) {
item.checked = !item.checked;
if (item.checked && !this.selectedBills.some(b => b.id === item.id)) {
this.selectedBills.push(item);
if (item.checked) {
if (!this.selectedBills.some(b => b.billNo === item.billNo)) {
this.selectedBills.push(item);
}
} else {
// 取消勾选时从已选数组移除
this.selectedBills = this.selectedBills.filter(b => b.id !== item.id);
this.selectedBills = this.selectedBills.filter(b => b.billNo !== item.billNo);
}
let sumFee = 0;
this.selectedBills.forEach(b => sumFee += b.billAmount);
this.sumAmount = sumFee;
this.calcTotalAmount();
},
calcTotalAmount() {
let sum = 0;
this.selectedBills.forEach(b => {
sum += Number(b.billAmount || 0);
});
this.sumAmount = sum;
},
updateSelected() {
if (!Array.isArray(this.bills)) return;
this.selectedBills = this.bills.filter(b => b.checked);
},
goPayTest(billList) {
if (!billList || billList.length === 0) return;
// 模拟后台生成订单返回
const order = {
timeStamp: String(Date.now()), // 时间戳
nonceStr: Math.random().toString(36).substr(2, 15), // 随机字符串
package: 'prepay_id=TEST1234567890', // 模拟预支付ID
signType: 'MD5',
paySign: 'TEST_SIGN', // 模拟签名
};
// 调用微信支付接口(测试)
uni.requestPayment({
...order,
success: () => {
uni.showToast({
title: '支付成功(测试)',
icon: 'success'
});
// 清空勾选状态
billList.forEach(b => b.checked = false);
this.updateSelected();
// 刷新列表(可选)
this.loadBills();
},
fail: () => {
uni.showToast({
title: '支付失败(测试)',
icon: 'none'
});
}
});
this.calcTotalAmount();
},
async goPay(billList) {
if (this.isPaying) return;
if (!billList || billList.length === 0) return;
try {
// 1. 请求后台生成订单
const res = await uni.request({
url: 'https://api.example.com/payments/create',
method: 'POST',
data: {
userId: this.userId,
bills: billList.map(b => b.id),
}
});
this.isPaying = true;
const token = this.$getToken()
if (res[1].data.code !== 0) {
uni.showToast({
title: res[1].data.msg,
icon: 'none'
});
try {
let unpaidBillVos = []
let tempSumAmount = 0
billList.forEach(bill => {
let tempBill = {
billNo: bill.billNo,
bizType: 'wae',
amount: bill.billAmount
}
tempSumAmount += Number(bill.billAmount)
unpaidBillVos.push(tempBill)
})
const params = {
amount: tempSumAmount,
billVoList: unpaidBillVos
}
let url = '/bill/pay'
const res = await this.$u.post(url, params, { WT: token });
if (res.code !== 200) {
uni.showToast({ title: res.msg, icon: 'none' });
this.isPaying = false;
return;
}
const order = res[1].data.data; // 后台返回的支付参数
const order = res.data;
let orderId = order.mainOrderId
// 2. 调用微信支付
uni.requestPayment({
timeStamp: order.timeStamp,
nonceStr: order.nonceStr,
package: order.package,
signType: order.signType,
paySign: order.paySign,
timeStamp: order.miniPayRequest.timeStamp,
nonceStr: order.miniPayRequest.nonceStr,
package: order.miniPayRequest.packageStr,
signType: order.miniPayRequest.signType,
paySign: order.miniPayRequest.paySign,
success: () => {
uni.showToast({
title: '支付成功',
icon: 'success'
});
// 更新勾选状态
billList.forEach(b => b.checked = false);
this.updateSelected();
// 刷新列表
this.loadBills();
this.refresh();
this.isPaying = false;
},
fail: () => {
uni.showToast({
title: '支付失败',
icon: 'none'
});
fail: (err) => {
this.payFailCallback(err, orderId);
this.isPaying = false;
}
});
} catch (error) {
console.error('支付请求失败', error);
uni.showToast({
title: '支付请求失败',
icon: 'none'
});
console.error('支付请求异常', error);
uni.showToast({ title: '支付请求失败', icon: 'none' });
this.isPaying = false;
}
},
// ✅ 修复支付失败回调(参数+接口格式)
payFailCallback(err, orderId) {
let reason = "支付失败";
if (err.errMsg?.includes("cancel")) {
reason = "您已取消支付";
} else if (err.errMsg?.includes("fail")) {
reason = "支付失败,请重试";
}
this.$u.get(`/bill/paycallback`, {
orderId: orderId,
payResult: "FAIL"
}, {
WT: this.$getToken()
}).then(res => {
console.log("回调成功")
}).catch(err => {
console.log("回调失败", err)
})
uni.showToast({ title: reason, icon: 'none' });
}
}
};
</script>
@@ -237,10 +259,12 @@
background: #f7f8fa;
min-height: 100vh;
padding-top: 175rpx;
/* 自定义导航栏高度 */
box-sizing: border-box;
}
/* ✅ 核心修复:给滚动区域固定高度 */
.scroll-content {
height: calc(100vh - 175rpx - 110rpx);
padding: 20rpx;
box-sizing: border-box;
}
@@ -262,6 +286,7 @@
.bill-info {
display: flex;
flex-direction: column;
margin-left: 12rpx;
.bill-name {
font-size: 30rpx;
@@ -300,16 +325,12 @@
padding: 0 40rpx;
height: 60rpx;
line-height: 60rpx;
border-radius: 6rpx; // 圆角
border-radius: 6rpx;
font-size: 30rpx;
color: #fff;
background-color: #F34038;
text-align: center;
}
.btn-text {
padding-top: 13rpx;
}
}
.batch-pay-bar {
@@ -317,7 +338,7 @@
bottom: 0;
left: 0;
right: 0;
height: 170rpx;
height: 110rpx;
padding: 0 30rpx;
background-color: #fff;
display: flex;
@@ -358,6 +379,11 @@
}
}
button[disabled] {
opacity: 0.5 !important;
background-color: #ccc !important;
color: #fff !important;
}
.empty {
margin-top: 200rpx;