hookehuyr

feat(车辆): 添加价格输入校验并优化认证车接口

添加价格输入校验逻辑,限制只能输入数字和小数点
将我的认证车页面接口从getVehicleListAPI更换为getMyListingVehicleAPI
发布成功后跳转至首页而非返回上一页
/*
* @Date: 2025-07-09 14:58:51
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2025-07-11 10:18:35
* @LastEditTime: 2025-07-15 11:55:43
* @FilePath: /jgdl/src/api/car.js
* @Description: 车辆相关API接口
*/
......@@ -93,7 +93,7 @@ export const getRecommendVehicleAPI = (params) => fn(fetch.get(Api.RECOMMEND_VEH
export const getVehicleListAPI = (params) => fn(fetch.get(Api.LIST_VEHICLE, params));
/**
* @description: 获取我卖的车
* @description: 获取我卖的车/我的认证
* @param page 页码
* @param limit 每页数量
* @returns data[{ id, seller_id, school_id, school_name, title, brand, model, manufacture_year, new_level, range_km, total_mileage_km, max_speed_kmh, battery_capacity_ah, brake_wear_level, tire_wear_level, price, market_price, verification_status, rejection_reason, note, photos, is_favorite }]
......
<!--
* @Date: 2022-09-19 14:11:06
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2025-07-11 17:14:08
* @LastEditTime: 2025-07-15 11:56:17
* @FilePath: /jgdl/src/pages/myAuthCar/index.vue
* @Description: 我的认证车页面
-->
......@@ -88,7 +88,7 @@ import { ref, computed, onMounted } from 'vue'
import Taro from '@tarojs/taro'
import './index.less'
// 导入接口
import { getVehicleListAPI } from '@/api/car'
import { getMyListingVehicleAPI } from '@/api/car'
import { DEFAULT_COVER_IMG } from '@/utils/config'
// ==================== API相关 ====================
......@@ -126,7 +126,7 @@ const scrollStyle = computed(() => {
const initData = async () => {
loading.value = true
try {
const response = await getVehicleListAPI({
const response = await getMyListingVehicleAPI({
verification_status: 5, // 已认证状态
page: 0,
limit: pageSize.value
......@@ -157,7 +157,7 @@ const loadMore = async () => {
loading.value = true
try {
const nextPage = currentPage.value + 1
const response = await getVehicleListAPI({
const response = await getMyListingVehicleAPI({
verification_status: 5, // 已认证状态
page: nextPage,
limit: pageSize.value
......
......@@ -219,7 +219,7 @@
</view>
<view class="form-item-right">
<text class="price-symbol">¥</text>
<input v-model="formData.price" placeholder="请输入" type="digit" class="price-input" />
<input v-model="formData.price" placeholder="请输入" type="digit" class="price-input" @onInput="validatePriceInput" />
</view>
</view>
......@@ -231,7 +231,7 @@
<view class="form-item-right">
<text class="market-price-symbol">¥</text>
<input v-model="formData.market_price" placeholder="请输入" type="digit"
class="market-price-input" />
class="market-price-input" @onInput="validateMarketPriceInput" />
</view>
</view>
</view>
......@@ -605,6 +605,42 @@ const showTireWearPicker = () => {
}
/**
* 校验价格输入,只允许数字和小数点
* @param {Object} event - 输入事件对象
*/
const validatePriceInput = (event) => {
const value = event.detail.value
const validValue = value.replace(/[^0-9.]/g, '')
// 防止多个小数点
const parts = validValue.split('.')
if (parts.length > 2) {
const newValue = parts[0] + '.' + parts.slice(1).join('')
formData.price = newValue
} else {
formData.price = validValue
}
}
/**
* 校验市场价格输入,只允许数字和小数点
* @param {Object} event - 输入事件对象
*/
const validateMarketPriceInput = (event) => {
const value = event.detail.value
const validValue = value.replace(/[^0-9.]/g, '')
// 防止多个小数点
const parts = validValue.split('.')
if (parts.length > 2) {
const newValue = parts[0] + '.' + parts.slice(1).join('')
formData.market_price = newValue
} else {
formData.market_price = validValue
}
}
/**
* 学校选择确认
*/
const onSchoolConfirm = ({ selectedValue, selectedOptions }) => {
......@@ -759,11 +795,11 @@ const createCar = async (data) => {
Taro.showToast({
title: '发布成功',
icon: 'success',
duration: 2000,
complete: () => {
Taro.navigateBack()
}
duration: 2000
})
setTimeout(() => {
Taro.reLaunch({ url: '/pages/index/index' })
}, 1000);
}
}
......@@ -799,8 +835,6 @@ const onPublish = async () => {
tire_wear_level: convertTextToScore(formData.tire_wear_level, 'wear')
}
console.warn(submitData);
if (isEditMode.value) {
submitData.id = carId.value
}
......@@ -814,6 +848,18 @@ const onPublish = async () => {
}
/**
* 价格格式校验
* @param {string} price 价格字符串
* @returns {boolean} 是否为有效的价格格式
*/
const isValidPrice = (price) => {
if (!price || price.trim() === '') return false
// 正则表达式:匹配数字和小数点,允许小数
const priceRegex = /^\d+(\.\d+)?$/
return priceRegex.test(price.toString().trim())
}
/**
* 表单验证
* @returns {boolean} 验证结果
*/
......@@ -842,11 +888,23 @@ const validateForm = () => {
return false
}
// 校验出让价格(必填)
if (!formData.price) {
Taro.showToast({ title: '请输入出让价格', icon: 'none' })
return false
}
if (!isValidPrice(formData.price)) {
Taro.showToast({ title: '出让价格格式不正确,请输入有效数字', icon: 'none' })
return false
}
// 校验市场价格(选填,但如果填写了必须格式正确)
if (formData.market_price && !isValidPrice(formData.market_price)) {
Taro.showToast({ title: '市场价格格式不正确,请输入有效数字', icon: 'none' })
return false
}
return true
}
......