wangzhibo
2025-10-23 cad64c1de2c45856b0774933c5801b05b64984e8
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
<template>
    <view class="page-wrapper">
        <div class="bg-top"></div>
 
        <div class="content-wrapper">
            <view class="p-4 w-95-percent">
                <uni-segmented-control :current="currentTab" :values="tabs" @clickItem="onTabChange" style-type="button"
                    active-color="#3b82f6" class="mb-4" />
            </view>
            <!-- 加载提示 -->
            <view v-if="loading" class="text-center text-gray-500 py-8">
                加载中...
            </view>
            <!-- 预约信息卡片列表 -->
            <scroll-view scroll-y class="h-calc-100vh-120px w-95-percent">
                <view v-for="(item, index) in filteredsitebooking" :key="index"
                    class="bg-white rounded-lg shadow p-4 mb-4 relative">
                    <!-- 取消按钮(仅 已预约 状态显示) -->
                    <view v-if="item.status === '已预约'" class="absolute top-2 right-2">
                        <button class="bg-red-500 text-white text-sm px-2 py-1 rounded"
                            @click="cancelReservation(item)">
                            取消预约
                        </button>
 
                    </view>
                    <!-- 卡片内容 -->
                    <view class="space-y-2">
                        <view class="flex justify-between">
                            <text class="font-bold text-lg">{{ item.sitename }}</text>
                            <text v-if="item.status != '已预约'" class="text-sm text-gray-500">{{ item.status }}</text>
                        </view>
                        <view class="text-sm space-y-2">
                            <text class="text-gray-600">预约日期:</text>
                            <text>{{ item.visitdate }}</text>
                        </view>
                        <view class="text-sm space-y-2">
                            <text class="text-gray-600">时段:</text>
                            <text>{{ item.visittimeam && item.visittimeam !== '' ? '上午(09:00-12:00)' : '下午(13:00-17:00)' }}</text>
                        </view>
                        <view class="text-sm space-y-2">
                            <text class="text-gray-600">预约人:</text>
                            <text>{{ item.contactperson }}</text>
                        </view>
                        <view class="text-sm space-y-2">
                            <text class="text-gray-600">电话:</text>
                            <text>{{ item.contactphone }}</text>
                        </view>
                        <view class="text-sm space-y-2">
                            <text class="text-gray-600">同行人数:</text>
                            <text>{{ item.personcount }} 人</text>
                        </view>
                        <view v-if="item.status === '已使用'" class="text-sm space-y-2">
                            <text class="text-gray-600">使用时间:</text>
                            <text>{{ item.usedtime }}</text>
                        </view>
                        <view v-if="item.status === '已取消'" class="text-sm space-y-2">
                            <text class="text-gray-600">取消时间:</text>
                            <text>{{ item.canceltime }}</text>
                        </view>
                        <button v-if="item.status === '已预约'"
                            class="bg-blue-500 text-white text-base px-2 py-1 rounded space-y-2"
                            @click="verifyReservation(item)">
                            现场核销
                        </button>
                    </view>
                </view>
                <!-- 无数据提示 -->
                <view v-if="!filteredsitebooking.length" class="text-center text-gray-500 py-8">
                    暂无{{ tabs[currentTab] }}的预约信息
                </view>
            </scroll-view>
        </div>
        <div class="bg-bottom"></div>
    </view>
</template>
 
<script>
    import {
        ref,
        computed,
        onMounted
    } from 'vue'
    import {
        apiService
    } from '@/apiservice/apiService'
    import {
        useUserStore
    } from '@/stores/user'
 
    const userStore = useUserStore()
 
    export default {
        setup() {
            // Tab 数据
            const tabs = ['已预约', '已使用', '已取消', '已过期'];
            const currentTab = ref(0);
            let sitebooking = ref([]);
            const loading = ref(false);
 
            // 获取预约列表
            const fetchsitebooking = async () => {
                loading.value = true;
                try {
                    const res = await apiService.bookingListByUuid(userStore.uuid)
                    if (res.code === 1000) {
                        sitebooking.value = res.data
 
                    } else {
                        console.warn('查询失败:', res.message)
                    }
 
                } catch (error) {
                    console.error('获取预约信息错误:', error);
                    uni.showToast({
                        title: '网络错误',
                        icon: 'none'
                    });
                } finally {
                    loading.value = false;
                }
            };
 
            // 根据当前 Tab 过滤数据
            const filteredsitebooking = computed(() => {
                return sitebooking.value.filter(item => item.status === tabs[currentTab.value]);
            });
 
            const getNowString = () => {
                const now = new Date();
                const pad = (n) => n.toString().padStart(2, '0');
                return `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())} ` +
                    `${pad(now.getHours())}:${pad(now.getMinutes())}:${pad(now.getSeconds())}`;
            };
 
            // Tab 切换
            const onTabChange = (e) => {
                currentTab.value = e.currentIndex;
            };
 
            // 取消预约
            const cancelReservation = async (item) => {
                uni.showModal({
                    title: '提示',
                    content: '确定取消预约吗?',
                    success: async (res) => {
                        if (res.confirm) {
                            try {
                                console.log(item);
 
                                const cancelRes = await apiService.bookingCancel({
                                    id: item.id,
                                    canceltime: getNowString(),
                                    status: '已取消'
                                });
 
                                console.log(cancelRes);
 
                                if (cancelRes.code === 1000) {
                                    uni.showToast({
                                        title: '预约已取消',
                                        icon: 'success'
                                    });
                                    // 刷新数据
                                    await fetchsitebooking();
                                } else {
                                    uni.showToast({
                                        title: cancelRes.message || '取消失败',
                                        icon: 'none'
                                    });
                                }
                            } catch (error) {
                                console.error('取消预约错误:', error);
                                uni.showToast({
                                    title: '取消失败,请重试',
                                    icon: 'none'
                                });
                            }
                        }
                    }
                });
            };
            // 现场核销
            const verifyReservation = async (item) => {
                try {
                    console.log(item)
                    const scanRes = await new Promise((resolve, reject) => {
                        uni.scanCode({
                            scanType: ['qrCode'],
                            success: resolve,
                            fail: reject
                        });
                    });
 
                    console.log(scanRes.result)
                    const match = scanRes.result.match(/farm\.evcc\.top.*?sitename=([^&]+)/);
 
                    if (!match) {
                        uni.showToast({
                            title: '二维码无效,请扫描正确的核销码',
                            icon: 'none'
                        });
                        return;
                    }
 
                    const qrSitename = decodeURIComponent(match[1]);
 
                    // 2️⃣ 获取当前时间
                    const now = new Date();
                    const todayStr = now.toISOString().split('T')[0]; // yyyy-mm-dd
                    const currentHour = now.getHours();
                    const isAm = !!item.visittimeam && item.visittimeam !== '';
 
                    // 3️⃣ 按顺序验证所有条件
                    let message = '';
 
                    if (qrSitename !== item.sitename) {
                        message = `二维码场馆不匹配(扫码场馆:${qrSitename})`;
                    } else if (item.visitdate !== todayStr) {
                        message = `未到预约日期(预约日期:${item.visitdate})`;
                    } else if (isAm && currentHour >= 12) {
                        message = '上午时段已过,无法核销';
                    } else if (!isAm && currentHour < 12) {
                        message = '还未到下午时段,请稍后核销';
                    }
 
                    // 如果有 message,直接提示客户,不做任何请求
                    if (message) {
                        uni.showModal({
                            title: '无法核销',
                            content: message,
                            showCancel: false,
                            confirmText: '我知道了'
                        });
                        return;
                    }
                    const verifyRes = await apiService.bookingUse({
                        id: item.id,
                        usedtime: getNowString(),
                        status: '已使用'
                    })
 
                    if (verifyRes.code === 1000) {
                        uni.showModal({
                            title: '核销成功',
                            content: '预约已核销,请工作人员确认',
                            showCancel: false,
                            confirmText: '确定',
                            success: async () => {
                                await fetchsitebooking(); // 刷新数据
                            }
                        });
                    } else {
                        uni.showToast({
                            title: verifyRes.message || '核销失败',
                            icon: 'none'
                        });
                    }
                } catch (error) {
                    console.error('核销错误:', error);
                    uni.showToast({
                        title: '核销失败,请重试',
                        icon: 'none'
                    });
                }
            };
 
            // 页面加载时获取数据
            onMounted(() => {
                fetchsitebooking();
            });
 
            return {
                tabs,
                currentTab,
                sitebooking,
                filteredsitebooking,
                loading,
                onTabChange,
                cancelReservation,
                verifyReservation
            };
        }
    };
</script>
 
<style>
    .min-h-screen {
        min-height: 100vh;
    }
 
    .bg-gray-100 {
        background-color: #f3f4f6;
    }
 
    .ml-5 {
        left: 10rpx;
    }
 
    .w-95-percent {
        width: 95%;
    }
 
    .mr-5 {
        right: 10rpx;
    }
 
    .mb-4 {
        margin-bottom: 1rem;
    }
 
    .text-center {
        text-align: center;
    }
 
    .text-gray-500 {
        color: #6b7280;
    }
 
    .text-gray-600 {
        color: #4b5563;
    }
 
    .py-8 {
        padding-top: 2rem;
        /* 32px */
        padding-bottom: 2rem;
    }
 
    .h-calc-100vh-120px {
        height: calc(100vh - 120px);
    }
 
    .px-4 {
        padding-left: 1rem;
        padding-right: 1rem;
    }
 
    .bg-white {
        background-color: #ffffff;
    }
 
    .rounded-lg {
        border-radius: 0.5rem;
        /* 8px */
    }
 
    .shadow {
        box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1), 0 1px 2px rgba(0, 0, 0, 0.06);
    }
 
    .p-4 {
        padding: 1rem;
    }
 
    .mb-4 {
        margin-bottom: 1rem;
    }
 
    .mt-10 {
        margin-top: 2rem;
    }
 
    .relative {
        position: relative;
    }
 
    .absolute {
        position: absolute;
    }
 
    .top-2 {
        top: 0.5rem;
        /* 8px */
    }
 
    .right-2 {
        right: 0.5rem;
        /* 8px */
    }
 
    .bg-red-500 {
        background-color: #ef4444;
    }
 
    .bg-blue-500 {
        background-color: #3b82f6;
    }
 
    .text-white {
        color: #ffffff;
    }
 
    .text-sm {
        font-size: 0.875rem;
        /* 14px */
        line-height: 1.25rem;
        /* 20px */
    }
 
    .text-base {
        font-size: 1rem;
        /* 14px */
        line-height: 1.5rem;
        /* 20px */
    }
 
    .px-2 {
        padding-left: 0.5rem;
        padding-right: 0.5rem;
    }
 
    .py-1 {
        padding-top: 0.25rem;
        padding-bottom: 0.25rem;
    }
 
    .rounded {
        border-radius: 0.25rem;
        /* 4px */
    }
 
    .space-y-2 {
        margin-top: 0.5rem;
        /* 8px */
    }
 
    .flex {
        display: flex;
    }
 
    .justify-between {
        justify-content: space-between;
    }
 
    .font-bold {
        font-weight: 700;
    }
 
    .text-lg {
        font-size: 1.125rem;
        /* 18px */
        line-height: 1.75rem;
        /* 28px */
    }
 
    .page-wrapper {
        position: relative;
        min-height: 100vh;
        /* 确保小内容时填满视口 */
        overflow: hidden;
    }
 
    /* 顶部背景 */
    .bg-top {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 500rpx;
        /* 可用固定像素或图片高度 */
        background: url('https://farm.evcc.top/Uploads/yuanboyuan/background/bg_top.jpg') no-repeat top center;
        background-size: cover;
        z-index: 0;
    }
 
    /* 底部背景 */
    .bg-bottom {
        position: absolute;
        bottom: 0;
        left: 0;
        width: 100%;
        height: 500rpx;
        /* 可用固定像素或图片高度 */
        background: url('https://farm.evcc.top/Uploads/yuanboyuan/background/bg_bottom.jpg') no-repeat bottom center;
        background-size: cover;
        z-index: 0;
    }
 
    /* 内容层 */
    .content-wrapper {
        position: relative;
        z-index: 1;
        padding: 2rem 1rem;
    }
</style>