next.vue
7.18 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
<!--
* @Date: 2023-08-22 14:13:07
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2023-08-25 21:39:23
* @FilePath: /bieyuan/src/views/next.vue
* @Description: 预约用户信息录入页
-->
<template>
<div class="next-page">
<van-form @submit="onSubmit">
<div v-for="(item, index) in userInfo" :key="index" class="input-box">
<p class="title">{{ index + 1 }}. {{ index === 0 ? '我的信息' : '同行人' }}</p>
<van-field :id="'name' + index" v-model="item.name" label="姓名:" type="text" placeholder="请输入姓名" :border="false"
:rules="[{ required: true, message: '请填写姓名' }]" label-width="4em" class="input-text" @focus="onFocus"
@keydown="handleKeyDown" />
<van-field :id="'phone' + index" v-model="item.phone" maxlength="11" label="手机号:" type="digit"
placeholder="请输入手机号" :border="false" :rules="[{ validator: validatorPhone, message: '请输入正确手机号' }]"
label-width="4em" class="input-text" @focus="onFocus" @keydown="handleKeyDown" />
<van-field label="证件类型:" :border="false" label-width="4.5em">
<template #input>
<van-radio-group v-model="item.id_type" direction="horizontal" checked-color="#93663D"
style="height: 2.5rem;">
<van-radio name="id_card">身份证</van-radio>
<van-radio name="passport">护照</van-radio>
</van-radio-group>
</template>
</van-field>
<van-field :id="'id_type' + index" v-if="item.id_type === 'id_card'" v-model="item.id_number" label="身份证:"
type="text" placeholder="请输入身份证号" :border="false" :rules="[{ validator: validatorId, message: '请填写正确身份证号' }]"
label-width="4em" class="input-text" @focus="onFocus" @keydown="handleKeyDown" />
<van-field :id="'id_type' + index" v-else v-model="item.id_number" label="护照:" type="text" placeholder="请输入护照号"
:border="false" :rules="[{ required: true, message: '请填写正确护照号' }]" label-width="4em" class="input-text"
@focus="onFocus" @keydown="handleKeyDown" />
</div>
<div style="padding: 2rem;">
<van-button block type="primary" color="#93663D" native-type="submit">
提交
</van-button>
</div>
</van-form>
</div>
</template>
<script setup>
import { ref } from 'vue'
import { useRoute, useRouter, onBeforeRouteLeave } from 'vue-router'
import { isIdCard, isPhone } from 'xijs';
import { Cookies, $, _, axios, storeToRefs, mainStore, Toast, useTitle } from '@/utils/generatePackage.js'
//import { } from '@/utils/generateModules.js'
//import { } from '@/utils/generateIcons.js'
//import { } from '@/composables'
import { orderDetailMyLastAPI } from '@/api/index'
import { showSuccessToast, showFailToast } from 'vant';
const $route = useRoute();
const $router = useRouter();
useTitle($route.meta.title);
const store = mainStore();
const { appUserInfo } = storeToRefs(store);
const userInfo = ref([]); // 用户预约信息
const num = ref(1);
const keyDownList = ref([]);
const isApply = ref(false);
onMounted(async () => {
num.value = $route.query.count ? +$route.query.count : 1;
if (appUserInfo.value.length) { // 如果有填写缓存需要把数据还原
userInfo.value = appUserInfo.value;
if (num.value > appUserInfo.value.length) { // 新增预约数大于原来的预约数
let new_count = num.value - appUserInfo.value.length;
for (let index = 0; index < new_count; index++) {
userInfo.value.push({
name: '',
phone: '',
id_type: 'id_card',
id_number: '',
})
}
} else { // 新增预约数小于原来的预约数
let new_count = appUserInfo.value.length - num.value;
userInfo.value.splice(-new_count, new_count); // 从后往前删除指定数量
}
} else {
// 根据预约数构造空结构
for (let index = 0; index < num.value; index++) {
userInfo.value.push({
name: '',
phone: '',
id_type: 'id_card',
id_number: '',
})
}
// 查询用户是否填写过申请,如果有把个人信息直接填进去
const { data } = await orderDetailMyLastAPI();
if (data) {
userInfo.value[0] = data;
isApply.value = true;
}
}
//
for (let index = 0; index < userInfo.value.length; index++) {
// const element = keyDownList[index];
const arr = ['name', 'phone', 'id_type']
for (let idx = 0; idx < arr.length; idx++) {
const element = arr[idx] + index;
keyDownList.value.push(element)
}
}
});
// 点击换行功能
const currentIndex = ref('');
const onFocus = (evt) => {
const currentId = $(evt.target).attr('id');
currentIndex.value = keyDownList.value.indexOf(currentId);
}
const handleKeyDown = (evt) => {
//
if (evt.keyCode === 13) {
const id = `input#${keyDownList.value[currentIndex.value + 1]}`;
if (id) {
$(id).focus()
}
}
}
/**
* 手机号码校验
* 函数返回 true 表示校验通过,false 表示不通过
* @param {*} val
*/
const validatorPhone = (val) => {
let flag = false;
// 简单判断手机号位数
if (isPhone(val)) {
flag = true;
} else {
flag = false;
}
return flag
};
const validatorId = (val) => {
let flag = false;
// 简单判断手机号位数
if (isIdCard(val)) {
flag = true;
} else {
flag = false;
}
return flag
};
const onSubmit = async () => {
// 提交预约信息
axios.post('/srv/?a=order_add', {
dates: JSON.parse($route.query.dates),
details: userInfo.value
})
.then(res => {
if (res.data.code === 1) {
// 跳转结果页
$router.push({
path: '/result',
query: {
id: res.data.data.id
}
})
} else { // 预约不足返回上页面缓存信息
console.warn(res);
showFailToast({
message: res.data.msg,
onClose: () => {
// TAG: 缓存用户填写信息
store.changeUserInfo(userInfo.value);
if ($route.query.page === 'single') { // 返回首页
$router.push({
path: '/',
query: {
dates: $route.query.dates,
count: $route.query.count,
page: 'single'
}
});
} else { // 返回日历选择页
$router.push({
path: '/preview',
query: {
dates: $route.query.dates,
count: $route.query.count
}
});
}
}
});
}
})
.catch(err => {
console.error(err);
});
};
</script>
<style lang="less" scoped>
.next-page {
padding: 1rem 1rem 0;
}
:deep(.van-field__label) {
color: #976C46;
font-weight: bold;
line-height: 2.5rem;
}
:deep(.input-text .van-field__body) {
border: 1px solid #D7D7D7;
padding: 0.25rem;
}
.input-box {
&:after {
content: '';
display: block;
border-bottom: 1px solid #D7D7D7;
margin: 1rem 0;
}
.title {
text-align: center;
font-size: 1.25rem;
margin-bottom: 1rem;
}
}
</style>