fix: 修复月度目标筛选及重置问题

- DateTimeConfig: 新增LocalDate反序列化器,支持yyyy-MM等多种格式
- monthly-target: 修复搜索表单重置后筛选条件不重置的问题
This commit is contained in:
小蟑螂
2026-04-20 19:18:37 +08:00
parent 06224ddd95
commit dd26732237
2 changed files with 108 additions and 13 deletions
@@ -14,6 +14,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
@@ -53,7 +54,9 @@ public class DateTimeConfig {
// 点格式:yyyy.MM.dd HH:mm:ss
DateTimeFormatter.ofPattern("yyyy.MM.dd HH:mm:ss"),
// 点格式:yyyy.MM.dd
DateTimeFormatter.ofPattern("yyyy.MM.dd")
DateTimeFormatter.ofPattern("yyyy.MM.dd"),
// 年月格式:yyyy-MM
DateTimeFormatter.ofPattern("yyyy-MM")
);
/**
@@ -74,6 +77,64 @@ public class DateTimeConfig {
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ")
);
/**
* 自定义LocalDate反序列化器
*/
public static class FlexibleLocalDateDeserializer extends JsonDeserializer<LocalDate> {
@Override
public LocalDate deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
String dateString = p.getText().trim();
if (dateString == null || dateString.isEmpty()) {
return null;
}
// 年月格式特殊处理(yyyy-MM 长度为7),返回该月第一天
if (dateString.length() == 7) {
int year = Integer.parseInt(dateString.substring(0, 4));
int month = Integer.parseInt(dateString.substring(5, 7));
return LocalDate.of(year, month, 1);
}
for (DateTimeFormatter formatter : DATE_TIME_FORMATTERS) {
try {
return LocalDate.parse(dateString, formatter);
} catch (DateTimeParseException e) {
// 继续尝试下一个格式
}
}
// 如果所有格式都失败,尝试从 ISO ZonedDateTime 解析再转 LocalDate
for (DateTimeFormatter formatter : ZONED_DATE_TIME_FORMATTERS) {
try {
ZonedDateTime zdt = ZonedDateTime.parse(dateString, formatter);
return zdt.toLocalDate();
} catch (DateTimeParseException e) {
// 继续尝试下一个格式
}
}
throw new IOException("无法解析日期格式: " + dateString);
}
}
/**
* 自定义LocalDate序列化器
* 统一输出格式为 yyyy-MM-dd
*/
public static class StandardLocalDateSerializer extends JsonSerializer<LocalDate> {
private static final DateTimeFormatter OUTPUT_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
@Override
public void serialize(LocalDate value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
if (value == null) {
gen.writeNull();
} else {
gen.writeString(value.format(OUTPUT_FORMATTER));
}
}
}
/**
* 自定义LocalDateTime反序列化器
*/
@@ -148,7 +209,13 @@ public class DateTimeConfig {
// 注册自定义模块
mapper.registerModule(dateTimeModule);
// 单独为 LocalDate 注册反序列化器和序列化器(覆盖 JavaTimeModule 的默认实现)
SimpleModule localDateModule = new SimpleModule();
localDateModule.addDeserializer(LocalDate.class, new FlexibleLocalDateDeserializer());
localDateModule.addSerializer(LocalDate.class, new StandardLocalDateSerializer());
mapper.registerModule(localDateModule);
return mapper;
}
}
@@ -7,7 +7,7 @@ import type {
import type { ShopVO } from '#/api/ecommerce/shop/types';
import type { PageQueryDTO } from '#/types/common';
import { computed, onMounted, ref } from 'vue';
import { computed, ref } from 'vue';
import { Page } from '@vben/common-ui';
import { createIconifyIcon } from '@vben/icons';
@@ -40,8 +40,10 @@ const searchForm = ref({
endTargetMonth: undefined as string | undefined,
});
// 店铺选项
// 店铺选项(懒加载)
const shopOptions = ref<{ label: string; value: number }[]>([]);
const shopLoading = ref(false);
const shopFetched = ref(false);
// VbenVxeGrid 表格配置
const gridOptions: VxeGridProps<any> = {
@@ -171,7 +173,14 @@ const formOptions = computed(() => ({
placeholder: '请选择店铺',
options: shopOptions,
allowClear: true,
onChange: () => gridReloadRef.value?.(),
showSearch: true,
filterOption: false,
loading: shopLoading,
onFocus: fetchShopOptions,
onChange: (val: any) => {
searchForm.value.shopId = val;
gridReloadRef.value?.();
},
},
fieldName: 'shopId',
label: '店铺',
@@ -182,7 +191,10 @@ const formOptions = computed(() => ({
placeholder: '开始月份',
picker: 'month',
style: { width: '100%' },
onChange: () => gridReloadRef.value?.(),
onChange: (val: any) => {
searchForm.value.startTargetMonth = val ? val.format('YYYY-MM') : undefined;
gridReloadRef.value?.();
},
},
fieldName: 'startTargetMonth',
label: '目标月份',
@@ -193,7 +205,10 @@ const formOptions = computed(() => ({
placeholder: '结束月份',
picker: 'month',
style: { width: '100%' },
onChange: () => gridReloadRef.value?.(),
onChange: (val: any) => {
searchForm.value.endTargetMonth = val ? val.format('YYYY-MM') : undefined;
gridReloadRef.value?.();
},
},
fieldName: 'endTargetMonth',
label: '至',
@@ -211,12 +226,23 @@ function refreshTable() {
}
}
// 重置搜索表单
function handleReset() {
searchForm.value = {
shopId: undefined,
startTargetMonth: undefined,
endTargetMonth: undefined,
};
gridReloadRef.value?.();
}
// 创建 VbenVxeGrid 实例
const [Grid, gridApi] = useVbenVxeGrid({
gridOptions,
formOptions: formOptions.value,
showSearchForm: true,
tableTitle: '店铺月销量目标管理',
handleReset,
});
gridReloadRef.value = () => gridApi.reload();
@@ -253,8 +279,10 @@ const formatAmount = (amount: number) => {
return `¥${amount.toFixed(2)}`;
};
// 获取店铺选项
const fetchShopOptions = async () => {
// 获取店铺选项(首次触发时加载)
async function fetchShopOptions() {
if (shopFetched.value || shopLoading.value) return;
shopLoading.value = true;
try {
const response = await getShopPageApi({
page: 1,
@@ -265,15 +293,15 @@ const fetchShopOptions = async () => {
label: shop.name,
value: shop.id,
}));
shopFetched.value = true;
} catch (error) {
console.error('获取店铺列表失败:', error);
} finally {
shopLoading.value = false;
}
};
}
// 初始化
onMounted(() => {
fetchShopOptions();
});
</script>
<template>