优化,完善代码

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

@@ -3,8 +3,19 @@
<!-- 自定义导航栏 -->
<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="flowList.length > 0">
<view class="bill-item" v-for="item in flowList" :key="item.billNo">
<!-- 左侧勾选 + 信息 -->
@@ -20,7 +31,8 @@
<!-- 右侧金额 + 支付按钮 -->
<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>
@@ -40,8 +52,8 @@
合计<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 +62,23 @@
</template>
<script>
import DateFilter from '../../components/DatePicker/DateFilter.vue';
export default {
components: { DateFilter },
data() {
return {
pageNo: 1,
pageSize: 10,
flowList: [],
isRefreshing: false,
loadStatus: 'loadmore',
loadStatus: 'more',
sumAmount: 0,
selectedBills: [], // 勾选的账单集合
selectedBills: [],
startDate: "",
endDate: "",
minDate: "2000-01-01",
maxDate: "2199-12-31",
isPaying: false
};
},
onLoad(options) {
@@ -70,167 +89,190 @@
},
methods: {
loadBills() {
if (this.loadStatus !== 'loadmore') return;
if (this.loadStatus !== 'more') return;
this.loadStatus = 'loading';
const token = this.$getToken()
let url = '/bill/pageQueryContractBill'
this.$u.post(url, {
pageNo: this.pageNo,
pageSize: this.pageSize,
startDate: this.startDate,
endDate: this.endDate,
billStatus: '待缴费'
}, {
WT: token
}).then(res => {
const rows = res.data.result || [];
console.log(rows)
rows.forEach(item => {
item.checked = false;
});
if (this.pageNo === 1) this.flowList = [];
this.flowList = this.flowList.concat(rows);
if (rows.length < this.pageSize) {
this.loadStatus = 'nomore';
} else {
this.pageNo++;
this.loadStatus = 'loadmore';
this.loadStatus = 'more';
}
}).catch(err => {
console.log("获取待付合同账单记录失败:", err)
this.loadStatus = 'loadmore';
this.loadStatus = 'more';
})
this.updateSelected();
},
onDateFilterChange({ start, end }) {
this.loadStatus = 'more'
this.refresh();
},
refresh() {
this.isRefreshing = true;
this.pageNo = 1;
this.selectedBills = [];
this.sumAmount = 0;
setTimeout(() => {
this.loadBills();
this.isRefreshing = false;
}, 1000);
}, 500);
},
loadMore() {
this.loadBills();
},
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);
this.selectedBills = this.flowList.filter(b => b.checked);
this.calcTotalAmount();
},
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
timeStamp: String(Date.now()),
nonceStr: Math.random().toString(36).substr(2, 15),
package: 'prepay_id=TEST1234567890',
signType: 'MD5',
paySign: 'TEST_SIGN', // 模拟签名
paySign: 'TEST_SIGN',
};
// 调用微信支付接口(测试)
uni.requestPayment({
...order,
success: () => {
uni.showToast({
title: '支付成功(测试)',
icon: 'success'
});
// 清空勾选状态
uni.showToast({ title: '支付成功(测试)', icon: 'success' });
billList.forEach(b => b.checked = false);
this.updateSelected();
// 刷新列表(可选)
this.loadBills();
this.refresh();
},
fail: () => {
uni.showToast({
title: '支付失败(测试)',
icon: 'none'
});
uni.showToast({ title: '支付失败(测试)', icon: 'none' });
}
});
},
updateBillStatus(billIds) {
const token = this.$getToken()
this.$u.post('/bill/updateRentBillStatus', { billIds }, { WT: token })
},
async goPay(billList) {
if (this.isPaying) return;
if (!billList || billList.length === 0) return;
this.isPaying = true;
const token = this.$getToken()
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),
}
});
let unpaidBillVos = []
let tempSumAmount = 0
billList.forEach(bill => {
unpaidBillVos.push({
billNo: bill.billNo,
bizType: 'rent',
amount: bill.billAmount
})
tempSumAmount += Number(bill.billAmount)
})
const res = await this.$u.post('/bill/pay', {
amount: tempSumAmount,
billVoList: unpaidBillVos
}, { WT: token });
if (res[1].data.code !== 0) {
uni.showToast({
title: res[1].data.msg,
icon: 'none'
});
if (res.code !== 200) {
uni.showToast({ title: res.msg, icon: 'none' });
this.isPaying = false;
return;
}
const order = res[1].data.data; // 后台返回的支付参数
// 2. 调用微信支付
const order = res.data;
let orderId = order.mainOrderId
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) {
console.log("触发支付失败回调")
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("回调成功", res)
}).catch(err => {
console.log("回调失败", err)
})
uni.showToast({ title: reason, icon: 'none' });
}
}
};
}
</script>
<style lang="scss" scoped>
@@ -238,10 +280,11 @@
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;
}
@@ -263,6 +306,7 @@
.bill-info {
display: flex;
flex-direction: column;
margin-left: 12rpx;
.bill-name {
font-size: 30rpx;
@@ -301,16 +345,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 {
@@ -318,7 +358,7 @@
bottom: 0;
left: 0;
right: 0;
height: 170rpx;
height: 110rpx;
padding: 0 30rpx;
background-color: #fff;
display: flex;
@@ -359,6 +399,11 @@
}
}
button[disabled] {
opacity: 0.5 !important;
background-color: #ccc !important;
color: #fff !important;
}
.empty {
margin-top: 200rpx;