payCard.vue
9.45 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<!--
* @Date: 2023-12-20 14:11:11
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2025-08-11 10:15:24
* @FilePath: /jgdl/src/components/paycard.vue
* @Description: 文件描述
-->
<template>
<div class="pay-card">
<nut-action-sheet v-model:visible="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>
<!-- 协议勾选区域 -->
<div v-if="!hasAgreed" class="agreement-section">
<nut-checkbox v-model="isChecked" class="agreement-checkbox">
<view class="checkbox-text">
<text>我已阅读并同意</text>
<text class="agreement-link" @tap.stop="showProtocol">
《用户服务协议》
</text>
</view>
</nut-checkbox>
</div>
<nut-button
block
color="#fb923c"
:disabled="(!hasAgreed && !isChecked) || isPaymentLoading"
:loading="isPaymentLoading"
@tap="goToPay"
>
{{ isPaymentLoading ? '处理中...' : '立即支付' }}
</nut-button>
</view>
</nut-action-sheet>
<!-- 用户服务协议弹框 -->
<UserServiceAgreementModal
v-model:visible="protocolVisible"
@confirm="() => { protocolVisible = false; isChecked = true; }"
@cancel="protocolVisible = false"
/>
</div>
</template>
<script setup>
import Taro from '@tarojs/taro'
import { ref, watch, onMounted, onUnmounted } from 'vue'
import { getCurrentPageUrl } from "@/utils/weapp";
import { payAPI, payCheckAPI, getProfileAPI, updateProfileAPI } from '@/api/index'
import { useUserStore } from '@/stores/user'
import UserServiceAgreementModal from '@/components/UserServiceAgreementModal.vue'
/**
* 格式化时间
* @param {*} seconds
*/
function formatTime(seconds) {
const hours = Math.floor(seconds / 3600); // 计算小时数
const minutes = Math.floor((seconds % 3600) / 60); // 计算分钟数
const remainingSeconds = seconds % 60; // 计算剩余的秒数
const formattedHours = String(hours).padStart(2, "0"); // 格式化小时数,保证两位数
const formattedMinutes = String(minutes).padStart(2, "0"); // 格式化分钟数,保证两位数
const formattedSeconds = String(remainingSeconds).padStart(2, "0"); // 格式化剩余的秒数,保证两位数
return `${formattedHours}:${formattedMinutes}:${formattedSeconds}`;
}
const props = defineProps({
visible: {
type: Boolean,
default: false,
},
data: {
type: Object,
default: () => ({}),
},
});
const emit = defineEmits(['close', 'paySuccess']);
const userStore = useUserStore()
const visible = ref(false);
// 协议相关状态
const isChecked = ref(false);
const hasAgreed = ref(false);
const protocolVisible = ref(false);
// 支付loading状态
const isPaymentLoading = ref(false);
const onClose = () => {
visible.value = false;
}
const id = ref('');
const price = ref('');
const remain_time = ref('');
let timeId = null;
watch(
() => props.visible,
async (val) => {
visible.value = val;
if (val) {
id.value = props.data.id;
price.value = props.data.price;
remain_time.value = props.data.remain_time;
// 检查用户协议状态
await checkAgreementStatus();
}
}
)
watch(
() => visible.value,
(val) => {
if (!val) {
emit('close');
}
}
)
onMounted(() => {
// 进入页面后,开始倒计时
timeId = setInterval(() => {
remain_time.value ? remain_time.value -= 1 : 0;
if (remain_time.value === 0) { // 倒计时结束
clearInterval(timeId);
visible.value = false;
}
}, 1000);
})
onUnmounted(() => {
timeId && clearInterval(timeId);
})
/**
* 检查用户是否已同意过协议
*/
const checkAgreementStatus = async () => {
try {
// 调用API获取用户信息
const result = await getProfileAPI()
if (result.code && result.data) {
hasAgreed.value = result.data.is_signed || false
// 更新用户store中的状态
if (userStore.userInfo) {
userStore.userInfo.is_signed = result.data.is_signed
}
} else {
// 如果API调用失败,使用store中的数据作为备选
hasAgreed.value = userStore.userInfo?.is_signed || false
}
} catch (error) {
console.error('获取用户协议状态失败:', error)
// 如果API调用失败,使用store中的数据作为备选
hasAgreed.value = userStore.userInfo?.is_signed || false
}
}
/**
* 显示支付协议
*/
const showProtocol = () => {
protocolVisible.value = true
}
/**
* 处理协议同意
*/
const handleAgreeProtocol = async () => {
if (!isChecked.value) return
try {
// 调用API更新用户协议状态
const result = await updateProfileAPI({
is_signed: true
})
if (result.code) {
hasAgreed.value = true
// 更新用户store中的状态
if (userStore.userInfo) {
userStore.userInfo.is_signed = true
}
}
} catch (error) {
console.error('更新协议状态失败:', error)
throw error // 重新抛出错误,让goToPay函数处理
}
}
const goToPay = async () => {
// 防止重复点击
if (isPaymentLoading.value) return
// 检查协议状态
if (!hasAgreed.value && !isChecked.value) {
Taro.showToast({
title: '请先同意支付协议',
icon: 'none'
})
return
}
// 设置loading状态
isPaymentLoading.value = true
try {
// 如果用户勾选了协议但还未提交,先处理协议同意
if (!hasAgreed.value && isChecked.value) {
await handleAgreeProtocol()
}
if (price.value > 0) { // 金额大于0
// 获取支付参数
const { code, data } = await payAPI({ order_id: id.value });
if (code) {
let pay = data.payargs;
// 触发微信支付操作
Taro.requestPayment({
timeStamp: pay.timeStamp,
nonceStr: pay.nonceStr,
package: pay.package,
signType: pay.signType,
paySign: pay.paySign,
success: async () => {
visible.value = false; // 关闭支付弹框
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/myOrders/index') { // 我的订单打开
// 发出支付成功事件,通知父组件更新订单状态
emit('paySuccess', { orderId: id.value });
}
if (current_page === 'pages/productDetail/index') { // 详情页打开
// 跳转订单成功页
Taro.navigateTo({
url: '/pages/myOrders/index',
});
}
if (current_page === 'pages/setAuthCar/index') { // 发布认证页打开
// 发出支付成功事件,通知父组件更新订单状态
emit('paySuccess', { orderId: id.value });
}
}
},
fail: () => {
let current_page = getCurrentPageUrl();
if (current_page === 'pages/setAuthCar/index') { // 发布认证页打开
Taro.showModal({
title: '温馨提示',
content: '需要付费才能认证',
showCancel: false,
});
}
},
complete: () => {
// 支付完成后清除loading状态
isPaymentLoading.value = false
}
});
}
}
} catch (error) {
console.error('支付过程出错:', error)
Taro.showToast({
title: '支付失败,请重试',
icon: 'error'
})
} finally {
// 确保loading状态被清除
isPaymentLoading.value = false
}
}
</script>
<style lang="less">
.pay-card {
.agreement-section {
margin: 20rpx 0;
padding: 0 20rpx;
.agreement-checkbox {
// display: flex;
// align-items: center;
// justify-content: center;
:deep(.nut-checkbox__label) {
margin-left: 8rpx;
}
.checkbox-text {
font-size: 28rpx;
color: #666;
.agreement-link {
color: #fb923c;
text-decoration: underline;
margin-left: 8rpx;
}
}
}
}
}
.protocol-container {
display: flex;
flex-direction: column;
height: 100%;
background-color: #fff;
.protocol-header {
display: flex;
align-items: center;
justify-content: space-between;
padding: 20rpx 30rpx;
border-bottom: 1rpx solid #eee;
.protocol-title {
font-size: 36rpx;
font-weight: bold;
color: #333;
}
.close-btn {
width: 60rpx;
height: 60rpx;
display: flex;
align-items: center;
justify-content: center;
.close-text {
font-size: 48rpx;
color: #999;
line-height: 1;
}
}
}
.protocol-scroll {
flex: 1;
.protocol-body {
padding: 30rpx;
.protocol-text {
font-size: 28rpx;
line-height: 1.6;
color: #333;
white-space: pre-line;
}
}
}
}
</style>