fix(ecom-dmb): 修复月度目标模态框问题:月份选择器验证异常、第二次无法打开,以及商品卡片宽度从180调整为240

This commit is contained in:
小蟑螂
2026-04-18 10:25:23 +08:00
parent d5c9e87833
commit c11106ba1e
2 changed files with 47 additions and 44 deletions
@@ -284,7 +284,7 @@ onUnmounted(() => {
v-else-if="productList.length > 0"
:list="productList"
row-key="id"
:width="180"
:width="240"
:breakpoints="{
2000: { rowPerView: 5 },
1700: { rowPerView: 4 },
@@ -5,7 +5,7 @@ import type {
} from '#/api/ecommerce/shop-monthly-target';
import type { ShopVO } from '#/api/ecommerce/shop/types';
import { onMounted, ref, watch } from 'vue';
import { computed, nextTick, onMounted, ref, watch } from 'vue';
import { useVbenModal } from '@vben/common-ui';
@@ -44,16 +44,23 @@ interface Emits {
(e: 'cancel'): void;
}
// 创建 vben modal
// 创建 vben modal(与父组件 v-model:open 同步 isOpen,避免仅点遮罩关闭时父级仍为 true 导致无法二次打开)
const [Modal, modalApi] = useVbenModal({
draggable: true,
class: 'w-[700px]',
destroyOnClose: false,
onCancel() {
handleCancel();
},
onConfirm() {
handleOk();
},
onOpenChange(isOpen: boolean) {
if (!isOpen) {
emit('update:open', false);
resetFormData();
}
},
});
// 响应式数据
@@ -69,8 +76,21 @@ const formData = ref<ShopMonthlyTargetDTO.Create>({
remark: '',
});
// 目标月份选择器的值(dayjs对象)
const targetMonthValue = ref<dayjs.Dayjs | null>(null);
// 目标月份展示值(只读 computed + @update:value 写回,避免与 Form 校验同一 tick 不同步)
const targetMonthForPicker = computed(() => {
if (!formData.value.targetMonth) return undefined;
return dayjs(formData.value.targetMonth);
});
function onTargetMonthChange(val: dayjs.Dayjs | string | undefined) {
if (val === undefined || val === null || val === '') {
formData.value.targetMonth = '';
} else {
const d = dayjs.isDayjs(val) ? val : dayjs(val);
formData.value.targetMonth = d.isValid() ? d.format('YYYY-MM-01') : '';
}
nextTick(() => formRef.value?.clearValidate?.(['targetMonth']));
}
// 表单验证规则
const rules = {
@@ -93,42 +113,29 @@ const shopListLoading = ref(false);
// 监听props变化
watch(
() => props.open,
(newVal) => {
if (newVal) {
modalApi.open();
isEdit.value = !!props.shopMonthlyTargetData;
if (props.shopMonthlyTargetData) {
// 编辑模式,填充表单数据
formData.value = {
shopId: props.shopMonthlyTargetData.shopId,
targetMonth: props.shopMonthlyTargetData.targetMonth,
targetAmount: props.shopMonthlyTargetData.targetAmount,
targetOrderCount: props.shopMonthlyTargetData.targetOrderCount,
remark: props.shopMonthlyTargetData.remark || '',
};
// 设置目标月份选择器的值
targetMonthValue.value = dayjs(props.shopMonthlyTargetData.targetMonth);
} else {
// 新增模式,重置表单数据
resetFormData();
}
} else {
async (newVal) => {
if (!newVal) {
modalApi.close();
return;
}
},
{ immediate: true },
);
// 监听目标月份选择器变化
watch(
() => targetMonthValue.value,
(newVal) => {
if (newVal) {
// 格式化为 YYYY-MM-01
formData.value.targetMonth = newVal.format('YYYY-MM-01');
isEdit.value = !!props.shopMonthlyTargetData;
if (props.shopMonthlyTargetData) {
// 编辑模式,填充表单数据
formData.value = {
shopId: props.shopMonthlyTargetData.shopId,
targetMonth: props.shopMonthlyTargetData.targetMonth,
targetAmount: props.shopMonthlyTargetData.targetAmount,
targetOrderCount: props.shopMonthlyTargetData.targetOrderCount,
remark: props.shopMonthlyTargetData.remark || '',
};
} else {
formData.value.targetMonth = '';
// 新增模式,重置表单数据
resetFormData();
}
await nextTick();
modalApi.open();
await nextTick();
formRef.value?.clearValidate?.();
},
);
@@ -158,7 +165,6 @@ const resetFormData = () => {
targetOrderCount: undefined as any,
remark: '',
};
targetMonthValue.value = null;
};
// 确认操作
@@ -187,19 +193,15 @@ const handleOk = async () => {
emit('success');
modalApi.close();
emit('update:open', false);
} catch (error) {
console.error('操作失败:', error);
message.error(isEdit.value ? '更新失败' : '创建失败');
}
};
// 取消操作
// 取消操作(关闭与表单重置由 onOpenChange 统一处理,避免重复)
const handleCancel = () => {
emit('cancel');
modalApi.close();
emit('update:open', false);
resetFormData();
};
// 组件挂载时加载数据
@@ -233,11 +235,12 @@ onMounted(() => {
<Form.Item label="目标月份" name="targetMonth" required>
<DatePicker
v-model:value="targetMonthValue"
:value="targetMonthForPicker"
placeholder="请选择目标月份"
picker="month"
format="YYYY-MM"
style="width: 100%"
@update:value="onTargetMonthChange"
/>
</Form.Item>