preview.vue
8.48 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
<template>
<div class="preview-page">
<div class="app-time">
预约时间
</div>
<div @click="handleCheck(item)" v-for="(item, index) in shortcut" :key="index" class="d-checkbox">
<div class="icon-box">
<van-icon v-if="item.checked" name="https://cdn.ipadbiz.cn/bieyuan/dui@2x.png" />
</div>
<div class="text">{{ item.date }}</div>
<div>
剩余名额 <span style="font-size: 1.5rem; font-weight: bold;">{{ item.rest_count }}</span> 人
</div>
</div>
<div class="more-box">
<div @click="showMore" class="more">
<van-icon size="1.25rem" name="https://cdn.ipadbiz.cn/bieyuan/rili@2x.png"
style="vertical-align: sub;" /> 更多
</div>
</div>
<div class="bookingBox">
<van-row>
<van-col span="6" class="text">预约人数</van-col>
<van-col span="18" class="control">
<van-row style="line-height: 3rem;" gutter="10">
<van-col span="4" style="text-align: center;">
<div @click="clickMinus" class="minus">
<van-icon size="0.85rem" name="https://cdn.ipadbiz.cn/bieyuan/-@2x.png" />
</div>
</van-col>
<van-col span="12" style="text-align: center;">
<input v-model="num" type="number" class="input_num">
</van-col>
<van-col span="4" style="text-align: center;">
<span>人</span>
</van-col>
<van-col span="4" style="text-align: center;">
<div @click="clickPlus" class="plus">
<van-icon size="0.85rem" name="https://cdn.ipadbiz.cn/bieyuan/+@2x.png" />
</div>
</van-col>
</van-row>
</van-col>
</van-row>
</div>
<div style="padding-bottom: 2rem;">
<div @click="clickNext" class="next-btn">下一步</div>
</div>
<van-calendar
v-model:show="show"
:default-date="defaultDate"
type="multiple"
:max-range="7"
:formatter="formatter"
:max-date="maxDate"
color="#93663D"
@confirm="onConfirm"
/>
</div>
</template>
<script setup>
import { ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { Cookies, $, _, axios, storeToRefs, mainStore, Toast, useTitle } from '@/utils/generatePackage.js'
import dayjs from 'dayjs';
//import { } from '@/utils/generateModules.js'
//import { } from '@/utils/generateIcons.js'
//import { } from '@/composables'
import { showSuccessToast, showFailToast } from 'vant';
import { orderRestCountAPI } from '@/api/index'
const $route = useRoute();
const $router = useRouter();
useTitle($route.meta.title);
function getDaysInMonth(year, month) {
return new Date(year, month, 0).getDate();
}
onMounted(async () => {
const dates = $route.query.dates && JSON.parse($route.query.dates);
const { data } = await orderRestCountAPI({ dates: dates.join(',')});
shortcut.value = data;
// 上一页选择的预约人数
num.value = $route.query.count ? +$route.query.count : 1;
// 默认选中所有日期项
shortcut.value.forEach((item, index) => {
item.checked = true;
});
// 获取默认日期集合
selectedDates.value = shortcut.value.map(item => item.date);
// 默认勾选日历信息
let default_date = JSON.parse($route.query.dates);
defaultDate.value = [];
default_date.forEach((date) => {
defaultDate.value.push(dayjs(date).toDate())
});
});
const defaultDate = ref(null);
const shortcut = ref([]);
const handleCheck = (value) => {
value.checked = !value.checked;
}
const num = ref(1);
const show = ref(false);
const selectedDates = ref([]);
watch(
() => num.value,
(val) => {
// 控制输入值为 3 位
const maxLength = 3;
if (val.toString().length > maxLength) {
num.value = num.value.toString().slice(0, maxLength);
}
}
);
const asyncData = ref();
const showMore = async () => {
show.value = true;
const { data } = await orderRestCountAPI({ start_date: dayjs().format('YYYY-MM-DD'), end_date: dayjs(maxDate.value).format('YYYY-MM-DD') });
asyncData.value = data;
asyncData.value.forEach(item => {
item.format = item.date.split('-')
});
}
const formatter = computed(() => {
if (!asyncData.value) {
return (day) => day;
}
return (day) => {
const year = day.date.getFullYear();
const month = day.date.getMonth() + 1;
const date = day.date.getDate();
asyncData.value.forEach(item => {
const arr = item.format;
if (year === +arr[0] && month === +arr[1] && date === +arr[2]) {
// day.topInfo = '剩余';
day.bottomInfo = item.rest_count;
}
});
return day;
};
});
const onConfirm = async (dates) => {
show.value = false;
selectedDates.value = [];
dates.forEach((date) => {
selectedDates.value.push(dayjs(date).format('YYYY-MM-DD'))
defaultDate.value.push(dayjs(date).toDate())
});
// 后台请求数据
const { data } = await orderRestCountAPI({ dates : selectedDates.value.join(',') });
shortcut.value = data;
shortcut.value.forEach(item => {
item.checked = true;
});
};
const selectIndex = (idx) => {
index.value = idx;
}
const clickMinus = () => {
num.value = num.value > 1 ? num.value - 1 : 1;
}
const clickPlus = () => {
num.value = num.value + 1;
}
// 最大日期默认为6个月
const getFutureDate = () => {
const currentDate = new Date();
const futureDate = new Date(currentDate.getFullYear(), currentDate.getMonth() + 6, currentDate.getDate());
return futureDate;
}
const maxDate = ref(getFutureDate());
const isPositiveInteger = (value) => {
// 判断是否为数字
if (typeof value !== 'number') {
return false;
}
// 判断是否为正整数
return Number.isInteger(value) && value > 0;
}
const isMinValue = (array, value) => { // 判断输入值是否小于数组中剩余名额数
let result = array.filter((item) => item.rest_count < value);
return result.length > 0;
}
const clickNext = () => { // 点击下一步按钮
if (!isPositiveInteger(num.value)) { // 判断是否是非零整数
showFailToast('请检查预约人数输入!');
return false;
}
// 勾选的日期项
const selected_item = shortcut.value.filter(item => {
if (item.checked) {
return item
}
});
if (isMinValue(selected_item, num.value)) {
showFailToast('剩余名额小于预约人数!');
return false;
}
// 选中的日期集合
const selected_dates = selected_item.map(item => item.date);
// 跳转更多确认页
$router.push({
path: '/next',
query: {
dates: JSON.stringify(selected_dates),
count: num.value,
page: 'multiple'
}
});
}
</script>
<style lang="less" scoped>
.preview-page {
padding: 1rem 1rem 0;
.app-time {
padding-bottom: 0.5rem;
border-bottom: 1px solid #D7D7D7;
font-weight: bold;
font-size: 1.05rem;
}
.d-checkbox {
display: flex;
padding: 1rem 0;
border-bottom: 1px solid #D7D7D7;
align-items: center;
color: #93663D;
.icon-box {
width: 1rem;
height: 1rem;
border: 1px solid #93663D;
margin-right: 1rem;
}
.text {
flex: 1;
font-size: 1.15rem;
}
}
.more-box {
display: flex;
align-items: center;
justify-content: flex-end;
.more {
color: #93663D;
margin-top: 1rem;
width: 5rem;
height: 2rem;
background: #FBF3F4;
border: 1px solid #D7D7D7;
border-radius: 10px 10px 0rem 0rem;
line-height: 2rem;
text-align: center;
}
}
.bookingBox {
padding: 2rem 0 1rem;
border-bottom: 1px solid #D7D7D7;
.text {
line-height: 3.1rem;
font-weight: bold;
font-size: 1.05rem;
}
.control {
text-align: right;
}
.minus,
.plus {
width: 2rem;
height: 2rem;
background: #FFFFFF;
border: 1px solid #D7D7D7;
border-radius: 50%;
text-align: center;
line-height: 2rem;
display: inline-block;
}
// .input-box {
// width: 40%;
// display: inline-block;
// }
.input_num {
border: 0;
background-color: #F7F7F7;
height: 2.5rem;
// width: 4em;
width: 70%;
color: #976C46;
text-align: center;
font-weight: bold;
font-size: 1.5rem;
// margin: 0 1rem;
}
}
.next-btn {
background-color: #93663D;
width: 70%;
height: 3rem;
color: white;
line-height: 3rem;
text-align: center;
font-size: 1.25rem;
margin-left: calc(20% - 1rem);
margin-top: 5rem;
}
}
</style>