修复bug
This commit is contained in:
@@ -32,7 +32,7 @@
|
||||
|
||||
<view class="form-item">
|
||||
<view class="label">访客姓名</view>
|
||||
<input class="input" placeholder="请输入访客姓名" v-model="form.name" maxlength="20" />
|
||||
<input class="input" placeholder="请输入访客姓名" v-model="form.name" maxlength="20" @input="onNameInput" />
|
||||
</view>
|
||||
<view class="form-item">
|
||||
<view class="label">
|
||||
@@ -47,7 +47,7 @@
|
||||
<view class="label">
|
||||
<text class="required">*</text>访客联系方式
|
||||
</view>
|
||||
<input class="input" placeholder="请输入联系方式" v-model="form.phone" type="number" maxlength="11" />
|
||||
<input class="input" placeholder="请输入联系方式" v-model="form.phone" type="number" maxlength="11" @input="onPhoneInput" />
|
||||
</view>
|
||||
|
||||
<!-- 车牌号:仅车辆来访显示 -->
|
||||
@@ -68,7 +68,7 @@
|
||||
<view class="label">
|
||||
<text class="required">*</text>随行人数
|
||||
</view>
|
||||
<input class="input" placeholder="请输入随行人数" v-model="form.peopleNum" type="number" maxlength="2" />
|
||||
<input class="input" placeholder="请输入随行人数" v-model="form.peopleNum" type="number" maxlength="2" @input="onPeopleNumInput" />
|
||||
</view>
|
||||
</view>
|
||||
|
||||
@@ -76,21 +76,21 @@
|
||||
<view class="section-title">有效期</view>
|
||||
<view class="form-section">
|
||||
|
||||
<view class="form-item">
|
||||
<view class="label">
|
||||
<view class="form-item" @click="onPickerOpen">
|
||||
<view class="label">
|
||||
<text class="required">*</text>来访时间
|
||||
</view>
|
||||
<view class="value">
|
||||
<uni-datetime-picker type="datetime" v-model="form.visitTime" @change="handleTimeChange"
|
||||
<uni-datetime-picker type="datetime" v-model="form.visitTime" @change="handleTimeChange" @maskClick="pickerVisible = false"
|
||||
:border="false" />
|
||||
</view>
|
||||
</view>
|
||||
<view class="form-item">
|
||||
<view class="label">
|
||||
<view class="form-item" @click="onPickerOpen">
|
||||
<view class="label">
|
||||
<text class="required">*</text>离开时间
|
||||
</view>
|
||||
<view class="value">
|
||||
<uni-datetime-picker type="datetime" v-model="form.leaveTime" @change="handleTimeChange"
|
||||
<uni-datetime-picker type="datetime" v-model="form.leaveTime" @change="handleTimeChange" @maskClick="pickerVisible = false"
|
||||
:border="false" />
|
||||
</view>
|
||||
</view>
|
||||
@@ -140,7 +140,8 @@
|
||||
reactive
|
||||
} from 'vue';
|
||||
import {
|
||||
onLoad
|
||||
onLoad,
|
||||
onBackPress
|
||||
} from "@dcloudio/uni-app"
|
||||
import moment from 'moment';
|
||||
import{getHouseList,addVisitorData} from '../api/api.js'
|
||||
@@ -151,7 +152,7 @@
|
||||
// 表单数据
|
||||
const form = reactive({
|
||||
address: '',
|
||||
villageId: '',
|
||||
villageId: '',
|
||||
houseId: '',
|
||||
name: '',
|
||||
gender: '',
|
||||
@@ -178,6 +179,17 @@ const pageFixed = ref(false);
|
||||
// 车牌号输入框聚焦索引
|
||||
const carFocusIndex = ref(-1);
|
||||
|
||||
// 日期选择器是否打开
|
||||
const pickerVisible = ref(false);
|
||||
|
||||
// 拦截返回事件:若日期选择器打开则关闭选择器而非返回上一页
|
||||
onBackPress(() => {
|
||||
if (pickerVisible.value) {
|
||||
pickerVisible.value = false;
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
// =========方法===========
|
||||
// 获取房屋列表
|
||||
const fetchHouseList = async () => {
|
||||
@@ -194,7 +206,7 @@ const pageFixed = ref(false);
|
||||
title:e.msg || '获取房屋列表失败',
|
||||
icon:'none'
|
||||
})
|
||||
// console.error('获取房屋列表失败', e)
|
||||
// console.error('获取房屋列表失败', e)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -259,36 +271,81 @@ const pageFixed = ref(false);
|
||||
}
|
||||
};
|
||||
|
||||
// 时间校验
|
||||
const handleTimeChange = () => {
|
||||
if (!form.visitTime || !form.leaveTime) return true;
|
||||
|
||||
const visitMoment = moment(form.visitTime);
|
||||
const leaveMoment = moment(form.leaveTime);
|
||||
|
||||
// 离开时间不能早于来访时间
|
||||
if (leaveMoment.isBefore(visitMoment)) {
|
||||
uni.showToast({
|
||||
title: '离开时间不能早于来访时间',
|
||||
icon: 'none'
|
||||
});
|
||||
form.leaveTime = visitMoment.clone().add(1, 'hours').format('YYYY-MM-DD HH:mm');
|
||||
return false;
|
||||
// 姓名输入过滤特殊字符
|
||||
const onNameInput = (e) => {
|
||||
const val = e.detail.value;
|
||||
// 只允许中文、英文字母、数字和中间点(·)
|
||||
const filtered = val.replace(/[^一-龥a-zA-Z0-9·]/g, '')
|
||||
if (filtered !== val) {
|
||||
form.name = filtered;
|
||||
}
|
||||
|
||||
// 不能超过24小时
|
||||
const diffHours = leaveMoment.diff(visitMoment, 'hours', true);
|
||||
if (diffHours > 24) {
|
||||
uni.showToast({
|
||||
title: '时间间隔不能超过24小时',
|
||||
icon: 'none'
|
||||
});
|
||||
form.leaveTime = visitMoment.clone().add(24, 'hours').format('YYYY-MM-DD HH:mm');
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
// 手机号输入过滤非数字字符
|
||||
const onPhoneInput = (e) => {
|
||||
const val = e.detail.value;
|
||||
const filtered = val.replace(/\D/g, '');
|
||||
if (filtered !== val) {
|
||||
form.phone = filtered;
|
||||
}
|
||||
};
|
||||
|
||||
// 随行人数输入过滤小数
|
||||
const onPeopleNumInput = (e) => {
|
||||
const val = e.detail.value;
|
||||
// 只保留整数(去除小数点及之后的内容)
|
||||
const filtered = val.replace(/[^\d]/g, '');
|
||||
if (filtered !== val) {
|
||||
form.peopleNum = filtered;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// 打开选择器时标记状态,供 onBackPress 拦截用
|
||||
const onPickerOpen = () => {
|
||||
pickerVisible.value = true;
|
||||
};
|
||||
|
||||
// 时间校验
|
||||
const handleTimeChange = () => {
|
||||
pickerVisible.value = false;
|
||||
if (!form.visitTime || !form.leaveTime) return true;
|
||||
|
||||
const visitMoment = moment(form.visitTime);
|
||||
const leaveMoment = moment(form.leaveTime);
|
||||
const now = moment();
|
||||
|
||||
// 来访时间不能是过去时间
|
||||
if (visitMoment.isBefore(now)) {
|
||||
uni.showToast({
|
||||
title: '来访时间不能早于当前时间',
|
||||
icon: 'none'
|
||||
});
|
||||
return false;
|
||||
}
|
||||
|
||||
// 离开时间不能早于来访时间
|
||||
if (leaveMoment.isBefore(visitMoment)) {
|
||||
uni.showToast({
|
||||
title: '离开时间不能早于来访时间',
|
||||
icon: 'none'
|
||||
});
|
||||
return false;
|
||||
}
|
||||
|
||||
// 不能超过24小时
|
||||
const diffHours = leaveMoment.diff(visitMoment, 'hours', true);
|
||||
if (diffHours > 24) {
|
||||
uni.showToast({
|
||||
title: '时间间隔不能超过24小时',
|
||||
icon: 'none'
|
||||
});
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
// 随行人数输入过滤小数
|
||||
// 提交表单
|
||||
const submitForm = async () => {
|
||||
// 1. 必填项校验
|
||||
@@ -334,9 +391,9 @@ const pageFixed = ref(false);
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (!form.peopleNum || form.peopleNum <= 0) {
|
||||
if (form.peopleNum === '' || form.peopleNum === null || form.peopleNum === undefined) {
|
||||
uni.showToast({
|
||||
title: '请输入正确的随行人数',
|
||||
title: '请输入随行人数',
|
||||
icon: 'none'
|
||||
});
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user