payCard.vue
4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<!--
* @Date: 2023-12-20 14:11:11
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2023-12-27 17:37:35
* @FilePath: /meihuaApp/src/components/payCard.vue
* @Description: 文件描述
-->
<template>
<div class="pay-card">
<nut-action-sheet v-model:visible="props.visible" title="" @close="onClose">
<view style="padding: 2rem 1rem; text-align: center;">
<view style="font-size: 32rpx;">实付金额</view>
<view style="color: red; margin: 10rpx 0;"><text style="font-size: 50rpx;">¥</text><text style="font-size: 80rpx;">{{ price }}</text></view>
<view style="font-size: 28rpx; margin-bottom: 20rpx;">支付剩余时间 <text style="color: red;">{{ formatTime(remain_time) }}</text></view>
<nut-button block color="#6A4925" @tap="goToPay">立即支付</nut-button>
</view>
</nut-action-sheet>
</div>
</template>
<script setup>
import Taro from '@tarojs/taro'
import { ref, watch, onMounted, onUnmounted } from 'vue'
import { getCurrentPageUrl } from "@/utils/weapp";
import { payAPI, payCheckAPI } from '@/api/index'
/**
* 格式化时间
* @param {*} seconds
*/
function formatTime(seconds) {
const hours = Math.floor(seconds / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
const remainingSeconds = seconds % 60;
let formattedTime = "";
if (hours > 0) {
formattedTime += hours.toString().padStart(2, "0") + ":";
}
if (minutes > 0 || hours > 0) {
formattedTime += minutes.toString().padStart(2, "0") + ":";
}
formattedTime += remainingSeconds.toString().padStart(2, "0");
return formattedTime;
}
const props = defineProps({
visible: {
type: Boolean,
default: false,
},
data: {
type: Object,
default: {},
},
});
const emit = defineEmits(['close']);
const onClose = () => {
emit('close');
}
const id = ref('');
const price = ref('');
const remain_time = ref('');
let timeId = null;
watch(
() => props.visible,
(val) => {
if (val) {
id.value = props.data.id;
price.value = props.data.price;
remain_time.value = props.data.remain_time;
} else {
}
}
)
onMounted(() => {
// 进入页面后,开始倒计时
timeId = setInterval(() => {
remain_time.value ? remain_time.value -= 1 : 0;
if (remain_time.value === 0) { // 倒计时结束
clearInterval(timeId);
emit('close');
}
}, 1000);
})
onUnmounted(() => {
timeId && clearInterval(timeId);
})
const goToPay = async () => {
// TODO:模拟代码
emit('close'); // 关闭支付弹框
//
Taro.showToast({
title: '支付成功',
icon: 'success',
duration: 1000
});
setTimeout(() => {
let current_page = getCurrentPageUrl();
if (current_page === 'pages/my/index') { // 我的页面打开
// 刷新当前页面
Taro.reLaunch({
url: '/pages/my/index?tab_index=1'
});
}
if (current_page === 'pages/detail/index') { // 订房确认页打开
// 跳转订单成功页
Taro.navigateTo({
url: '/pages/payInfo/index',
});
}
}, 1000);
// TODO:实际操作代码等待接口放上去
// // 获取支付参数
// const pay = await payAPI({ order_id: id.value });
// if (pay.code) {
// let pay = pay.data.payargs;
// // 触发微信支付操作
// wx.requestPayment({
// timeStamp: pay.timeStamp,
// nonceStr: pay.nonceStr,
// package: pay.package,
// signType: pay.signType,
// paySign: pay.paySign,
// success: async (result) => {
// emit('close'); // 关闭支付弹框
// Taro.showToast({
// title: '支付成功',
// icon: 'success',
// duration: 1000
// });
// // 支付成功后,调用检查接口
// const pay_success = await payCheckAPI({ order_id: id.value });
// if (pay_success.code) {
// let current_page = getCurrentPageUrl();
// if (current_page === 'pages/my/index') { // 我的页面打开
// // 刷新当前页面
// Taro.reLaunch({
// url: '/pages/my/index?tab_index=1'
// });
// }
// if (current_page === 'pages/detail/index') { // 订房确认页打开
// // 跳转订单成功页
// Taro.navigateTo({
// url: '/pages/payInfo/index',
// });
// }
// }
// }
// });
// }
}
</script>
<style lang="less">
</style>