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