表单校验

This commit is contained in:
WindowBird 2025-08-19 11:04:01 +08:00
parent db29e572fe
commit deaa972edb

View File

@ -23,7 +23,11 @@
<!-- 姓名 --> <!-- 姓名 -->
<view class="form-item"> <view class="form-item">
<text class="field-label">姓名</text> <text class="field-label">姓名</text>
<input v-model="formData.name" class="field-input" placeholder="请输入姓名" /> <input
v-model="formData.name"
class="field-input"
placeholder="请输入姓名"
/>
</view> </view>
<!-- 手机号 --> <!-- 手机号 -->
@ -46,6 +50,7 @@
@location-error="onLocationError" @location-error="onLocationError"
@use-location="onUseLocation" @use-location="onUseLocation"
@map-opened="onMapOpened" @map-opened="onMapOpened"
/> />
<!-- 详细位置 --> <!-- 详细位置 -->
@ -82,8 +87,8 @@
<view class="payment-section"> <view class="payment-section">
<button <button
:class="['pay-button', { disabled: !canPay }]" :class="['pay-button', { disabled: !canPay }]"
@click="handlePayment"
:disabled="!canPay" :disabled="!canPay"
@click="handlePayment"
> >
{{ canPay ? `立即支付 ¥${totalAmount}` : '请完善信息' }} {{ canPay ? `立即支付 ¥${totalAmount}` : '请完善信息' }}
</button> </button>
@ -125,25 +130,65 @@
<script> <script>
import commonEnum from '../../enum/commonEnum' import commonEnum from '../../enum/commonEnum'
import MapLocation from '@/components/map-location/map-location.vue' import MapLocation from '@/components/map-location/map-location.vue'
import { getDeviceTypes as fetchDeviceTypes, getPeriodPackages as fetchPeriodPackages } from '@/api/lease/lease.js' import {
getDeviceTypes as fetchDeviceTypes,
getPeriodPackages as fetchPeriodPackages,
} from '@/api/lease/lease.js'
export default { export default {
name: 'LeasePage', name: 'LeasePage',
components: { components: {
MapLocation MapLocation,
}, },
computed: { computed: {
commonEnum() { commonEnum() {
return commonEnum return commonEnum
}, },
// // - formData
canPay() { canPay() {
return this.formData.name.trim() && const nameValid = this.formData.name.trim().length > 0
this.formData.phone.trim() && const phoneValid = this.formData.phone.trim().length > 0
this.formData.address.trim() && const addressValid = this.formData.address.trim().length > 0
this.formData.equipmentId && const equipmentSelected = !!this.formData.equipmentId
this.formData.periodId && const packageSelected = !!this.formData.periodId
parseFloat(this.totalAmount) > 0 const amountValid = parseFloat(this.totalAmount) > 0
const canPayResult = nameValid && phoneValid && addressValid &&
equipmentSelected && packageSelected && amountValid
//
console.log('🔍 canPay 实时检查:', {
nameValid,
phoneValid,
addressValid,
equipmentSelected,
packageSelected,
amountValid,
canPay: canPayResult
})
return canPayResult
},
},
watch: {
//
'formData.name'() {
console.log('🔍 姓名变化:', this.formData.name)
},
'formData.phone'() {
console.log('🔍 手机号变化:', this.formData.phone)
},
'formData.address'() {
console.log('🔍 地址变化:', this.formData.address)
},
'formData.equipmentId'() {
console.log('🔍 设备ID变化:', this.formData.equipmentId)
},
'formData.periodId'() {
console.log('🔍 套餐ID变化:', this.formData.periodId)
},
'totalAmount'() {
console.log('🔍 总金额变化:', this.totalAmount)
} }
}, },
onLoad() { onLoad() {
@ -153,7 +198,7 @@ export default {
data() { data() {
return { return {
formData: { formData: {
name: '张珊珊', name: '',
phone: '', phone: '',
address: '', address: '',
detailAddress: '', detailAddress: '',
@ -171,6 +216,8 @@ export default {
} }
}, },
methods: { methods: {
// //
onLocationSuccess(location) { onLocationSuccess(location) {
console.log('位置获取成功:', location) console.log('位置获取成功:', location)
@ -256,7 +303,7 @@ export default {
// //
this.formData.period = '' this.formData.period = ''
this.formData.periodId = '' this.formData.periodId = ''
this.formData.equipmentSuitId=''
this.selectedPackage = null this.selectedPackage = null
this.totalAmount = '0.00' this.totalAmount = '0.00'
@ -297,8 +344,8 @@ export default {
this.formData.period = selectedPackage.name this.formData.period = selectedPackage.name
this.formData.periodId = selectedPackage.id this.formData.periodId = selectedPackage.id
this.totalAmount = selectedPackage.amount.toFixed(2) this.totalAmount = selectedPackage.amount.toFixed(2)
console.log('选中套餐:', selectedPackage) console.log('选中套餐:', selectedPackage)
console.log('选中时长:', this.formData.periodId)
}, },
}) })
}, },
@ -366,7 +413,7 @@ export default {
...this.formData, ...this.formData,
amount: this.totalAmount, amount: this.totalAmount,
selectedDevice: this.selectedDevice, selectedDevice: this.selectedDevice,
selectedPackage: this.selectedPackage selectedPackage: this.selectedPackage,
}) })
uni.showToast({ uni.showToast({
@ -484,8 +531,6 @@ export default {
} }
} }
// //
.selector { .selector {
flex: 3; flex: 3;
@ -625,4 +670,3 @@ export default {
} }
} }
</style> </style>