Showing
1 changed file
with
161 additions
and
19 deletions
| ... | @@ -138,7 +138,7 @@ | ... | @@ -138,7 +138,7 @@ |
| 138 | <!-- 车辆出厂年份 --> | 138 | <!-- 车辆出厂年份 --> |
| 139 | <nut-form-item label-position="top" label="车辆出厂年份" prop="manufacture_year"> | 139 | <nut-form-item label-position="top" label="车辆出厂年份" prop="manufacture_year"> |
| 140 | <view class="form-item-content" @click="showYearPicker"> | 140 | <view class="form-item-content" @click="showYearPicker"> |
| 141 | - <text class="form-value" :class="{ 'form-value-selected': formData.manufacture_year, 'form-value-placeholder': !formData.manufacture_year }">{{ formData.manufacture_year || '请选择' }}</text> | 141 | + <text class="form-value" :class="{ 'form-value-selected': formData.manufacture_year, 'form-value-placeholder': !formData.manufacture_year }">{{ formData.manufacture_year ? formData.manufacture_year + '年' : '请选择' }}</text> |
| 142 | <Right class="arrow-icon" /> | 142 | <Right class="arrow-icon" /> |
| 143 | </view> | 143 | </view> |
| 144 | </nut-form-item> | 144 | </nut-form-item> |
| ... | @@ -285,14 +285,8 @@ | ... | @@ -285,14 +285,8 @@ |
| 285 | 285 | ||
| 286 | <!-- 年份选择 --> | 286 | <!-- 年份选择 --> |
| 287 | <nut-popup v-model:visible="yearPickerVisible" position="bottom"> | 287 | <nut-popup v-model:visible="yearPickerVisible" position="bottom"> |
| 288 | - <nut-date-picker | 288 | + <nut-picker v-model="yearValue" :columns="yearOptions" title="选择出厂年份" |
| 289 | - v-model="yearValue" | 289 | + @confirm="onYearConfirm" @cancel="yearPickerVisible = false" /> |
| 290 | - title="选择出厂年份" | ||
| 291 | - type="year-month" | ||
| 292 | - :max-date="maxYearDate" | ||
| 293 | - @confirm="onYearConfirm" | ||
| 294 | - @cancel="yearPickerVisible = false" | ||
| 295 | - /> | ||
| 296 | </nut-popup> | 290 | </nut-popup> |
| 297 | 291 | ||
| 298 | <!-- 新旧程度选择 --> | 292 | <!-- 新旧程度选择 --> |
| ... | @@ -330,7 +324,7 @@ | ... | @@ -330,7 +324,7 @@ |
| 330 | </template> | 324 | </template> |
| 331 | 325 | ||
| 332 | <script setup> | 326 | <script setup> |
| 333 | -import { ref, reactive, onMounted, computed } from 'vue' | 327 | +import { ref, reactive, onMounted, computed, nextTick } from 'vue' |
| 334 | import { Plus, Right, Location, Close } from '@nutui/icons-vue-taro' | 328 | import { Plus, Right, Location, Close } from '@nutui/icons-vue-taro' |
| 335 | import Taro, { useDidShow } from '@tarojs/taro' | 329 | import Taro, { useDidShow } from '@tarojs/taro' |
| 336 | import BASE_URL from '@/utils/config'; | 330 | import BASE_URL from '@/utils/config'; |
| ... | @@ -359,9 +353,9 @@ const goBack = () => { | ... | @@ -359,9 +353,9 @@ const goBack = () => { |
| 359 | // 获取页面参数 | 353 | // 获取页面参数 |
| 360 | const instance = Taro.getCurrentInstance() | 354 | const instance = Taro.getCurrentInstance() |
| 361 | const { id, mode, type, from } = instance.router?.params || {} | 355 | const { id, mode, type, from } = instance.router?.params || {} |
| 362 | -const isEditMode = ref(mode === 'edit' && id) | 356 | +const isEditMode = ref(mode === 'edit' && !!id) |
| 363 | -const isAuthMode = ref(type === 'auth' && id) | 357 | +const isAuthMode = ref(type === 'auth' && !!id) |
| 364 | -const isMyCarMode = ref(type === 'myCar' && id) | 358 | +const isMyCarMode = ref(type === 'myCar' && !!id) |
| 365 | const carId = ref(id || '') | 359 | const carId = ref(id || '') |
| 366 | 360 | ||
| 367 | // 已上传图片的URL | 361 | // 已上传图片的URL |
| ... | @@ -430,14 +424,14 @@ const brandModelPickerRef = ref(null) | ... | @@ -430,14 +424,14 @@ const brandModelPickerRef = ref(null) |
| 430 | const schoolValue = ref([]) | 424 | const schoolValue = ref([]) |
| 431 | const brandValue = ref([]) | 425 | const brandValue = ref([]) |
| 432 | const modelValue = ref([]) | 426 | const modelValue = ref([]) |
| 433 | -const yearValue = ref(new Date()) | 427 | +const yearValue = ref([]) |
| 434 | const conditionValue = ref([]) | 428 | const conditionValue = ref([]) |
| 435 | // const batteryWearValue = ref([]) | 429 | // const batteryWearValue = ref([]) |
| 436 | const brakeWearValue = ref([]) | 430 | const brakeWearValue = ref([]) |
| 437 | const tireWearValue = ref([]) | 431 | const tireWearValue = ref([]) |
| 438 | 432 | ||
| 439 | -// 年份选择器最大日期限制(当前年份的12月31日) | 433 | +// 年份选择器选项数据(当前年份往前10年) |
| 440 | -const maxYearDate = ref(new Date(new Date().getFullYear(), 11, 31)) | 434 | +const yearOptions = ref([]) |
| 441 | 435 | ||
| 442 | // 计算属性:判断是否为已认证状态 | 436 | // 计算属性:判断是否为已认证状态 |
| 443 | const isVerified = computed(() => { | 437 | const isVerified = computed(() => { |
| ... | @@ -769,7 +763,8 @@ const onModelConfirm = ({ selectedValue }) => { | ... | @@ -769,7 +763,8 @@ const onModelConfirm = ({ selectedValue }) => { |
| 769 | * 年份选择确认 | 763 | * 年份选择确认 |
| 770 | */ | 764 | */ |
| 771 | const onYearConfirm = ({ selectedValue }) => { | 765 | const onYearConfirm = ({ selectedValue }) => { |
| 772 | - formData.manufacture_year = `${selectedValue[0]}年` | 766 | + // selectedValue[0] 直接就是选中的年份值 |
| 767 | + formData.manufacture_year = selectedValue[0] | ||
| 773 | yearPickerVisible.value = false | 768 | yearPickerVisible.value = false |
| 774 | } | 769 | } |
| 775 | 770 | ||
| ... | @@ -1091,6 +1086,13 @@ const loadCarData = async () => { | ... | @@ -1091,6 +1086,13 @@ const loadCarData = async () => { |
| 1091 | formData.right_photo = carData.right_photo || '' | 1086 | formData.right_photo = carData.right_photo || '' |
| 1092 | formData.other_photo = carData.other_photo || '' | 1087 | formData.other_photo = carData.other_photo || '' |
| 1093 | 1088 | ||
| 1089 | + // 数据填充完成后设置各选择器默认值 | ||
| 1090 | + setYearDefaultValue() | ||
| 1091 | + setConditionDefaultValue() | ||
| 1092 | + setBrakeWearDefaultValue() | ||
| 1093 | + setTireWearDefaultValue() | ||
| 1094 | + setSchoolDefaultValue() | ||
| 1095 | + | ||
| 1094 | Taro.hideLoading() | 1096 | Taro.hideLoading() |
| 1095 | } | 1097 | } |
| 1096 | } catch (error) { | 1098 | } catch (error) { |
| ... | @@ -1106,7 +1108,10 @@ const loadCarData = async () => { | ... | @@ -1106,7 +1108,10 @@ const loadCarData = async () => { |
| 1106 | /** | 1108 | /** |
| 1107 | * 加载学校列表 | 1109 | * 加载学校列表 |
| 1108 | */ | 1110 | */ |
| 1109 | -const loadSchools = async () => { | 1111 | +/** |
| 1112 | + * 初始化学校选项(仅加载选项,不设置默认值) | ||
| 1113 | + */ | ||
| 1114 | +const initSchoolOptions = async () => { | ||
| 1110 | try { | 1115 | try { |
| 1111 | const { code, data } = await getSchoolsAPI() | 1116 | const { code, data } = await getSchoolsAPI() |
| 1112 | if (code && data) { | 1117 | if (code && data) { |
| ... | @@ -1114,6 +1119,7 @@ const loadSchools = async () => { | ... | @@ -1114,6 +1119,7 @@ const loadSchools = async () => { |
| 1114 | text: school.name, | 1119 | text: school.name, |
| 1115 | value: school.id | 1120 | value: school.id |
| 1116 | })) | 1121 | })) |
| 1122 | + console.log('学校选项初始化完成,共', schoolOptions.value.length, '个选项') | ||
| 1117 | } | 1123 | } |
| 1118 | } catch (error) { | 1124 | } catch (error) { |
| 1119 | console.error('加载学校列表失败:', error) | 1125 | console.error('加载学校列表失败:', error) |
| ... | @@ -1181,16 +1187,152 @@ const checkUserPermission = async () => { | ... | @@ -1181,16 +1187,152 @@ const checkUserPermission = async () => { |
| 1181 | return true | 1187 | return true |
| 1182 | } | 1188 | } |
| 1183 | 1189 | ||
| 1190 | +/** | ||
| 1191 | + * 初始化年份选项数据(仅初始化选项,不设置默认值) | ||
| 1192 | + */ | ||
| 1193 | +const initYearOptions = () => { | ||
| 1194 | + const currentYear = new Date().getFullYear() | ||
| 1195 | + const years = [] | ||
| 1196 | + | ||
| 1197 | + // 生成当前年份往前10年的年份选项 | ||
| 1198 | + for (let i = 0; i < 10; i++) { | ||
| 1199 | + const year = currentYear - i | ||
| 1200 | + years.push({ | ||
| 1201 | + text: `${year}年`, | ||
| 1202 | + value: year | ||
| 1203 | + }) | ||
| 1204 | + } | ||
| 1205 | + | ||
| 1206 | + yearOptions.value = years | ||
| 1207 | + console.log('年份选项初始化完成:', years.length, '个选项') | ||
| 1208 | +} | ||
| 1209 | + | ||
| 1210 | +/** | ||
| 1211 | + * 设置年份默认值(在数据加载后调用) | ||
| 1212 | + */ | ||
| 1213 | +const setYearDefaultValue = () => { | ||
| 1214 | + const currentYear = new Date().getFullYear() | ||
| 1215 | + | ||
| 1216 | + // 如果是编辑模式且有原值,设置对应的选中项 | ||
| 1217 | + if (isEditMode.value && formData.manufacture_year) { | ||
| 1218 | + // 原值已经是纯数字,直接使用 | ||
| 1219 | + const originalYear = parseInt(formData.manufacture_year) | ||
| 1220 | + const yearExists = yearOptions.value.some(item => item.value === originalYear) | ||
| 1221 | + if (yearExists) { | ||
| 1222 | + // v-model应该是包含value值的数组 | ||
| 1223 | + yearValue.value = [originalYear] | ||
| 1224 | + } else { | ||
| 1225 | + yearValue.value = [] | ||
| 1226 | + } | ||
| 1227 | + console.log('编辑模式年份默认值设置:', yearValue.value, '原始年份:', originalYear) | ||
| 1228 | + } else { | ||
| 1229 | + // 发布模式设置默认为当前年份 | ||
| 1230 | + yearValue.value = [currentYear] | ||
| 1231 | + console.log('发布模式年份默认值设置:', yearValue.value) | ||
| 1232 | + } | ||
| 1233 | +} | ||
| 1234 | + | ||
| 1235 | +/** | ||
| 1236 | + * 设置新旧程度默认值(在数据加载后调用) | ||
| 1237 | + */ | ||
| 1238 | +const setConditionDefaultValue = () => { | ||
| 1239 | + // 如果是编辑模式且有原值,设置对应的选中项 | ||
| 1240 | + if (isEditMode.value && formData.new_level) { | ||
| 1241 | + const originalCondition = formData.new_level | ||
| 1242 | + const conditionExists = conditionOptions.value.some(item => item.value === originalCondition) | ||
| 1243 | + if (conditionExists) { | ||
| 1244 | + conditionValue.value = [originalCondition] | ||
| 1245 | + } else { | ||
| 1246 | + conditionValue.value = [] | ||
| 1247 | + } | ||
| 1248 | + console.log('编辑模式新旧程度默认值设置:', conditionValue.value, '原始值:', originalCondition) | ||
| 1249 | + } else { | ||
| 1250 | + // 发布模式设置默认为9成新 | ||
| 1251 | + conditionValue.value = ['9成新'] | ||
| 1252 | + console.log('发布模式新旧程度默认值设置:', conditionValue.value) | ||
| 1253 | + } | ||
| 1254 | +} | ||
| 1255 | + | ||
| 1256 | +/** | ||
| 1257 | + * 设置刹车磨损度默认值(在数据加载后调用) | ||
| 1258 | + */ | ||
| 1259 | +const setBrakeWearDefaultValue = () => { | ||
| 1260 | + // 如果是编辑模式且有原值,设置对应的选中项 | ||
| 1261 | + if (isEditMode.value && formData.brake_wear_level) { | ||
| 1262 | + const originalBrakeWear = formData.brake_wear_level | ||
| 1263 | + const brakeWearExists = wearLevelOptions.value.some(item => item.value === originalBrakeWear) | ||
| 1264 | + if (brakeWearExists) { | ||
| 1265 | + brakeWearValue.value = [originalBrakeWear] | ||
| 1266 | + } else { | ||
| 1267 | + brakeWearValue.value = [] | ||
| 1268 | + } | ||
| 1269 | + console.log('编辑模式刹车磨损度默认值设置:', brakeWearValue.value, '原始值:', originalBrakeWear) | ||
| 1270 | + } else { | ||
| 1271 | + // 发布模式设置默认为全新 | ||
| 1272 | + brakeWearValue.value = ['全新'] | ||
| 1273 | + console.log('发布模式刹车磨损度默认值设置:', brakeWearValue.value) | ||
| 1274 | + } | ||
| 1275 | +} | ||
| 1276 | + | ||
| 1277 | +/** | ||
| 1278 | + * 设置轮胎磨损度默认值(在数据加载后调用) | ||
| 1279 | + */ | ||
| 1280 | +const setTireWearDefaultValue = () => { | ||
| 1281 | + // 如果是编辑模式且有原值,设置对应的选中项 | ||
| 1282 | + if (isEditMode.value && formData.tire_wear_level) { | ||
| 1283 | + const originalTireWear = formData.tire_wear_level | ||
| 1284 | + const tireWearExists = wearLevelOptions.value.some(item => item.value === originalTireWear) | ||
| 1285 | + if (tireWearExists) { | ||
| 1286 | + tireWearValue.value = [originalTireWear] | ||
| 1287 | + } else { | ||
| 1288 | + tireWearValue.value = [] | ||
| 1289 | + } | ||
| 1290 | + console.log('编辑模式轮胎磨损度默认值设置:', tireWearValue.value, '原始值:', originalTireWear) | ||
| 1291 | + } else { | ||
| 1292 | + // 发布模式设置默认为全新 | ||
| 1293 | + tireWearValue.value = ['全新'] | ||
| 1294 | + console.log('发布模式轮胎磨损度默认值设置:', tireWearValue.value) | ||
| 1295 | + } | ||
| 1296 | +} | ||
| 1297 | + | ||
| 1298 | +/** | ||
| 1299 | + * 设置学校选择器默认值 | ||
| 1300 | + */ | ||
| 1301 | +const setSchoolDefaultValue = () => { | ||
| 1302 | + if (isEditMode.value) { | ||
| 1303 | + // 编辑模式:基于表单数据设置选择器值 | ||
| 1304 | + if (formData.school_id && formData.school_name) { | ||
| 1305 | + const matchingOption = schoolOptions.value.find(option => option.value === formData.school_id) | ||
| 1306 | + if (matchingOption) { | ||
| 1307 | + schoolValue.value = [matchingOption.value] | ||
| 1308 | + console.log('学校选择器默认值设置为:', matchingOption.text) | ||
| 1309 | + } | ||
| 1310 | + } | ||
| 1311 | + } else { | ||
| 1312 | + // 发布模式:不设置默认值,保持用户手动选择 | ||
| 1313 | + console.log('发布模式下学校选择器不设置默认值') | ||
| 1314 | + } | ||
| 1315 | +} | ||
| 1316 | + | ||
| 1184 | // 页面加载时执行 | 1317 | // 页面加载时执行 |
| 1185 | onMounted(async () => { | 1318 | onMounted(async () => { |
| 1319 | + // 立即初始化年份选项(不设置默认值) | ||
| 1320 | + initYearOptions() | ||
| 1321 | + | ||
| 1186 | // 加载基础数据 | 1322 | // 加载基础数据 |
| 1187 | await Promise.all([ | 1323 | await Promise.all([ |
| 1188 | - loadSchools(), | 1324 | + initSchoolOptions(), |
| 1189 | loadBrandsModels() | 1325 | loadBrandsModels() |
| 1190 | ]) | 1326 | ]) |
| 1191 | 1327 | ||
| 1192 | if (isEditMode.value) { | 1328 | if (isEditMode.value) { |
| 1193 | loadCarData() | 1329 | loadCarData() |
| 1330 | + } else { | ||
| 1331 | + // 发布模式下直接设置各选择器默认值 | ||
| 1332 | + setYearDefaultValue() | ||
| 1333 | + setConditionDefaultValue() | ||
| 1334 | + setBrakeWearDefaultValue() | ||
| 1335 | + setTireWearDefaultValue() | ||
| 1194 | } | 1336 | } |
| 1195 | 1337 | ||
| 1196 | // 默认权限检查逻辑 | 1338 | // 默认权限检查逻辑 | ... | ... |
-
Please register or login to post a comment