!282 feat:【antd】【mall】满减送组件优化
Merge pull request !282 from puhui999/dev-mall
This commit is contained in:
@@ -25,7 +25,6 @@ export function useFormSchema(): VbenFormSchema[] {
|
||||
},
|
||||
rules: 'required',
|
||||
},
|
||||
// TODO @puhui999:商品的选择:上面 spuId 可以选择了,下面的 skuId 打开后,没商品。
|
||||
{
|
||||
fieldName: 'skuId',
|
||||
label: '商品规格',
|
||||
|
||||
@@ -101,23 +101,13 @@ const [Modal, modalApi] = useVbenModal({
|
||||
async onOpenChange(isOpen: boolean) {
|
||||
if (!isOpen) {
|
||||
// 重置表单数据
|
||||
// TODO @puhui999:105 到 108 的代码,不需要的呀?(可以测试下)
|
||||
formData.value = {
|
||||
descriptionScores: 5,
|
||||
benefitScores: 5,
|
||||
} as Partial<MallCommentApi.Comment>;
|
||||
selectedSku.value = undefined;
|
||||
return;
|
||||
}
|
||||
// 加载数据
|
||||
const data = modalApi.getData<MallCommentApi.Comment>();
|
||||
if (!data || !data.id) {
|
||||
// TODO @puhui999:115 到 121 的代码,不需要的呀?(可以测试下)
|
||||
// 新建模式:重置表单
|
||||
formData.value = {
|
||||
descriptionScores: 5,
|
||||
benefitScores: 5,
|
||||
} as Partial<MallCommentApi.Comment>;
|
||||
selectedSku.value = undefined;
|
||||
await formApi.setValues({ spuId: undefined, skuId: undefined });
|
||||
return;
|
||||
|
||||
@@ -1,17 +1,16 @@
|
||||
<!-- SKU 选择弹窗组件 -->
|
||||
<script lang="ts" setup>
|
||||
import type { VxeGridProps } from '#/adapter/vxe-table';
|
||||
import type { MallSpuApi } from '#/api/mall/product/spu';
|
||||
|
||||
import { ref } from 'vue';
|
||||
|
||||
import { fenToYuan } from '@vben/utils';
|
||||
|
||||
import { Modal } from 'ant-design-vue';
|
||||
|
||||
import { useVbenVxeGrid } from '#/adapter/vxe-table';
|
||||
import { getSpu } from '#/api/mall/product/spu';
|
||||
|
||||
import { useSkuGridColumns } from './spu-select-data';
|
||||
|
||||
interface SpuData {
|
||||
spuId: number;
|
||||
}
|
||||
@@ -23,52 +22,14 @@ const emit = defineEmits<{
|
||||
const visible = ref(false);
|
||||
const spuId = ref<number>();
|
||||
|
||||
/** 表格列配置 */
|
||||
const gridColumns: VxeGridProps['columns'] = [
|
||||
{
|
||||
type: 'radio',
|
||||
width: 55,
|
||||
},
|
||||
{
|
||||
field: 'picUrl',
|
||||
title: '图片',
|
||||
width: 100,
|
||||
align: 'center',
|
||||
cellRender: {
|
||||
name: 'CellImage',
|
||||
},
|
||||
},
|
||||
{
|
||||
field: 'properties',
|
||||
title: '规格',
|
||||
minWidth: 120,
|
||||
align: 'center',
|
||||
formatter: ({ cellValue }) => {
|
||||
return (
|
||||
cellValue?.map((p: MallSpuApi.Property) => p.valueName)?.join(' ') ||
|
||||
'-'
|
||||
);
|
||||
},
|
||||
},
|
||||
{
|
||||
field: 'price',
|
||||
title: '销售价(元)',
|
||||
width: 120,
|
||||
align: 'center',
|
||||
formatter: ({ cellValue }) => {
|
||||
return fenToYuan(cellValue);
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
const [Grid, gridApi] = useVbenVxeGrid({
|
||||
gridOptions: {
|
||||
columns: gridColumns,
|
||||
columns: useSkuGridColumns(),
|
||||
height: 400,
|
||||
border: true,
|
||||
showOverflow: true,
|
||||
radioConfig: {
|
||||
reserve: true,
|
||||
highlight: true,
|
||||
},
|
||||
rowConfig: {
|
||||
keyField: 'id',
|
||||
@@ -77,22 +38,6 @@ const [Grid, gridApi] = useVbenVxeGrid({
|
||||
pagerConfig: {
|
||||
enabled: false,
|
||||
},
|
||||
proxyConfig: {
|
||||
// TODO @puhui999:看看注释的部分,后续要不要删除
|
||||
// autoLoad: false, // 禁用自动加载,手动触发查询
|
||||
ajax: {
|
||||
query: async () => {
|
||||
if (!spuId.value) {
|
||||
return { list: [], total: 0 };
|
||||
}
|
||||
const spu = await getSpu(spuId.value);
|
||||
return {
|
||||
list: spu.skus || [],
|
||||
total: spu.skus?.length || 0,
|
||||
};
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
gridEvents: {
|
||||
radioChange: () => {
|
||||
@@ -111,7 +56,6 @@ const [Grid, gridApi] = useVbenVxeGrid({
|
||||
/** 关闭弹窗 */
|
||||
function closeModal() {
|
||||
visible.value = false;
|
||||
gridApi.grid.clearRadioRow();
|
||||
spuId.value = undefined;
|
||||
}
|
||||
|
||||
@@ -122,12 +66,14 @@ async function openModal(data?: SpuData) {
|
||||
}
|
||||
spuId.value = data.spuId;
|
||||
visible.value = true;
|
||||
// TODO @puhui999:看看注释的部分,后续要不要删除
|
||||
// // 等待弹窗和 Grid 组件完全渲染后再查询数据
|
||||
// await nextTick();
|
||||
// if (gridApi.grid) {
|
||||
// await gridApi.query();
|
||||
// }
|
||||
// 注意:useVbenVxeGrid 关闭分页(pagerConfig.enabled=false)后,proxyConfig.ajax.query 的结果不会传递到 vxe-table
|
||||
// 需要手动调用 reloadData 设置表格数据
|
||||
if (!spuId.value) {
|
||||
gridApi.grid?.reloadData([]);
|
||||
return;
|
||||
}
|
||||
const spu = await getSpu(spuId.value);
|
||||
gridApi.grid?.reloadData(spu.skus || []);
|
||||
}
|
||||
|
||||
/** 对外暴露的方法 */
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
import type { Ref } from 'vue';
|
||||
|
||||
import type { VbenFormSchema } from '#/adapter/form';
|
||||
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
|
||||
import type { VxeGridProps, VxeTableGridOptions } from '#/adapter/vxe-table';
|
||||
import type { MallSpuApi } from '#/api/mall/product/spu';
|
||||
import type { MallCategoryApi } from '#/api/mall/product/category';
|
||||
|
||||
import { computed } from 'vue';
|
||||
|
||||
import { fenToYuan } from '@vben/utils';
|
||||
|
||||
import { getRangePickerDefaultProps } from '#/utils';
|
||||
|
||||
/** 列表的搜索表单 */
|
||||
@@ -118,3 +121,49 @@ export function useGridColumns(
|
||||
},
|
||||
] as VxeTableGridOptions['columns'];
|
||||
}
|
||||
|
||||
/** SKU 列表的字段 */
|
||||
export function useSkuGridColumns(): VxeGridProps['columns'] {
|
||||
return [
|
||||
{
|
||||
type: 'radio',
|
||||
width: 55,
|
||||
},
|
||||
{
|
||||
field: 'id',
|
||||
title: '商品编号',
|
||||
minWidth: 100,
|
||||
align: 'center',
|
||||
},
|
||||
{
|
||||
field: 'picUrl',
|
||||
title: '图片',
|
||||
width: 100,
|
||||
align: 'center',
|
||||
cellRender: {
|
||||
name: 'CellImage',
|
||||
},
|
||||
},
|
||||
{
|
||||
field: 'properties',
|
||||
title: '规格',
|
||||
minWidth: 120,
|
||||
align: 'center',
|
||||
formatter: ({ cellValue }) => {
|
||||
return (
|
||||
cellValue?.map((p: MallSpuApi.Property) => p.valueName)?.join(' ') ||
|
||||
'-'
|
||||
);
|
||||
},
|
||||
},
|
||||
{
|
||||
field: 'price',
|
||||
title: '销售价(元)',
|
||||
width: 120,
|
||||
align: 'center',
|
||||
formatter: ({ cellValue }) => {
|
||||
return fenToYuan(cellValue);
|
||||
},
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
@@ -190,13 +190,10 @@ const [Grid, gridApi] = useVbenVxeGrid({
|
||||
keyField: 'id',
|
||||
isHover: true,
|
||||
},
|
||||
// TODO @puhui999:貌似直接 { trigger: 'row', reserve: true } 就可以了?不会影响 radio 的哈。(可以测试下。)
|
||||
expandConfig: props.isSelectSku
|
||||
? {
|
||||
trigger: 'row',
|
||||
reserve: true,
|
||||
}
|
||||
: undefined,
|
||||
expandConfig: {
|
||||
trigger: 'row',
|
||||
reserve: true,
|
||||
},
|
||||
proxyConfig: {
|
||||
ajax: {
|
||||
async query({ page }: any, formValues: any) {
|
||||
|
||||
@@ -2,7 +2,9 @@
|
||||
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
|
||||
import type { MallCouponTemplateApi } from '#/api/mall/promotion/coupon/couponTemplate';
|
||||
|
||||
import { useVbenModal } from '@vben/common-ui';
|
||||
import { ref } from 'vue';
|
||||
|
||||
import { Modal } from 'ant-design-vue';
|
||||
|
||||
import { useVbenVxeGrid } from '#/adapter/vxe-table';
|
||||
import { getCouponTemplatePage } from '#/api/mall/promotion/coupon/couponTemplate';
|
||||
@@ -15,7 +17,11 @@ const props = defineProps<{
|
||||
takeType?: number; // 领取方式
|
||||
}>();
|
||||
|
||||
const emit = defineEmits(['success']);
|
||||
const emit = defineEmits<{
|
||||
(e: 'change', v: MallCouponTemplateApi.CouponTemplate[]): void;
|
||||
}>();
|
||||
|
||||
const visible = ref(false); // 弹窗显示状态
|
||||
|
||||
const [Grid, gridApi] = useVbenVxeGrid({
|
||||
formOptions: {
|
||||
@@ -48,19 +54,42 @@ const [Grid, gridApi] = useVbenVxeGrid({
|
||||
} as VxeTableGridOptions<MallCouponTemplateApi.CouponTemplate>,
|
||||
});
|
||||
|
||||
const [Modal, modalApi] = useVbenModal({
|
||||
async onConfirm() {
|
||||
// 从 gridApi 获取选中的记录
|
||||
const selectedRecords = (gridApi.grid?.getCheckboxRecords() ||
|
||||
[]) as MallCouponTemplateApi.CouponTemplate[];
|
||||
await modalApi.close();
|
||||
emit('success', selectedRecords);
|
||||
},
|
||||
/** 打开弹窗 */
|
||||
async function open() {
|
||||
visible.value = true;
|
||||
// 重置查询条件并重新加载数据,与老组件行为一致
|
||||
await gridApi.query();
|
||||
}
|
||||
|
||||
/** 关闭弹窗 */
|
||||
function closeModal() {
|
||||
visible.value = false;
|
||||
}
|
||||
|
||||
/** 确认选择 */
|
||||
function handleConfirm() {
|
||||
// 从 gridApi 获取选中的记录
|
||||
const selectedRecords = (gridApi.grid?.getCheckboxRecords() ||
|
||||
[]) as MallCouponTemplateApi.CouponTemplate[];
|
||||
emit('change', selectedRecords);
|
||||
closeModal();
|
||||
}
|
||||
|
||||
/** 对外暴露的方法 */
|
||||
defineExpose({
|
||||
open,
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Modal title="选择优惠券" class="w-2/3">
|
||||
<Modal
|
||||
v-model:open="visible"
|
||||
title="选择优惠券"
|
||||
width="65%"
|
||||
:destroy-on-close="true"
|
||||
@ok="handleConfirm"
|
||||
@cancel="closeModal"
|
||||
>
|
||||
<Grid />
|
||||
</Modal>
|
||||
</template>
|
||||
|
||||
@@ -180,26 +180,16 @@ const [Modal, modalApi] = useVbenModal({
|
||||
},
|
||||
async onOpenChange(isOpen: boolean) {
|
||||
if (!isOpen) {
|
||||
// 重置表单数据(新增和编辑模式都需要)
|
||||
formData.value = undefined;
|
||||
spuList.value = [];
|
||||
spuPropertyList.value = [];
|
||||
return;
|
||||
}
|
||||
// 重置表单数据(新增和编辑模式都需要)
|
||||
// TODO @puhui999:这里的重置,是不是在 183 到 185 已经处理了呀。
|
||||
formData.value = undefined;
|
||||
spuList.value = [];
|
||||
spuPropertyList.value = [];
|
||||
|
||||
// 加载数据
|
||||
const data = modalApi.getData<MallPointActivityApi.PointActivity>();
|
||||
if (!data || !data.id) {
|
||||
// 新增模式:重置表单字段
|
||||
// TODO @puhui999:197 到 201 这块的 setValues 的设置,是不是必要哈。可以看看。
|
||||
await formApi.setValues({
|
||||
sort: 0,
|
||||
remark: '',
|
||||
spuId: undefined,
|
||||
});
|
||||
return;
|
||||
}
|
||||
// 加载数据
|
||||
|
||||
@@ -166,26 +166,87 @@ export function useFormSchema(): VbenFormSchema[] {
|
||||
},
|
||||
rules: z.number().default(PromotionProductScopeEnum.ALL.scope),
|
||||
},
|
||||
// TODO @puhui999:选择完删除后,自动就退出了 modal;
|
||||
{
|
||||
fieldName: 'productSpuIds',
|
||||
label: '选择商品',
|
||||
component: 'Input',
|
||||
dependencies: {
|
||||
triggerFields: ['productScope'],
|
||||
triggerFields: ['productScope', 'productScopeValues'],
|
||||
show: (values) => {
|
||||
return values.productScope === PromotionProductScopeEnum.SPU.scope;
|
||||
},
|
||||
trigger(values, form) {
|
||||
// 当加载已有数据时,根据 productScopeValues 设置 productSpuIds
|
||||
if (
|
||||
values.productScope === PromotionProductScopeEnum.SPU.scope &&
|
||||
values.productScopeValues
|
||||
) {
|
||||
form.setFieldValue('productSpuIds', values.productScopeValues);
|
||||
}
|
||||
},
|
||||
},
|
||||
rules: 'required',
|
||||
},
|
||||
{
|
||||
fieldName: 'productCategoryIds',
|
||||
label: '选择分类',
|
||||
component: 'Input',
|
||||
dependencies: {
|
||||
triggerFields: ['productScope', 'productScopeValues'],
|
||||
show: (values) => {
|
||||
return (
|
||||
values.productScope === PromotionProductScopeEnum.CATEGORY.scope
|
||||
);
|
||||
},
|
||||
trigger(values, form) {
|
||||
// 当加载已有数据时,根据 productScopeValues 设置 productCategoryIds
|
||||
if (
|
||||
values.productScope === PromotionProductScopeEnum.CATEGORY.scope &&
|
||||
values.productScopeValues
|
||||
) {
|
||||
const categoryIds = values.productScopeValues;
|
||||
// 单选时使用数组不能反显,取第一个元素
|
||||
form.setFieldValue(
|
||||
'productCategoryIds',
|
||||
Array.isArray(categoryIds) && categoryIds.length > 0
|
||||
? categoryIds[0]
|
||||
: categoryIds,
|
||||
);
|
||||
}
|
||||
},
|
||||
},
|
||||
rules: 'required',
|
||||
},
|
||||
// TODO @puhui999:这里还有个分类;
|
||||
{
|
||||
fieldName: 'rules',
|
||||
label: '优惠设置',
|
||||
component: 'Input',
|
||||
formItemClass: 'items-start',
|
||||
// TODO @puhui999:这里可能要加个 rules: 'required',
|
||||
rules: 'required',
|
||||
},
|
||||
// 隐藏字段:用于自动同步 productScopeValues
|
||||
{
|
||||
fieldName: 'productScopeValues',
|
||||
component: 'Input',
|
||||
dependencies: {
|
||||
triggerFields: ['productScope', 'productSpuIds', 'productCategoryIds'],
|
||||
show: () => false,
|
||||
trigger(values, form) {
|
||||
switch (values.productScope) {
|
||||
case PromotionProductScopeEnum.CATEGORY.scope: {
|
||||
const categoryIds = Array.isArray(values.productCategoryIds)
|
||||
? values.productCategoryIds
|
||||
: [values.productCategoryIds];
|
||||
form.setFieldValue('productScopeValues', categoryIds);
|
||||
break;
|
||||
}
|
||||
case PromotionProductScopeEnum.SPU.scope: {
|
||||
form.setFieldValue('productScopeValues', values.productSpuIds);
|
||||
break;
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<script lang="ts" setup>
|
||||
import type { MallRewardActivityApi } from '#/api/mall/promotion/reward/rewardActivity';
|
||||
|
||||
import { computed, nextTick, ref } from 'vue';
|
||||
import { computed, ref } from 'vue';
|
||||
|
||||
import { useVbenModal } from '@vben/common-ui';
|
||||
import {
|
||||
@@ -19,6 +19,7 @@ import {
|
||||
updateRewardActivity,
|
||||
} from '#/api/mall/promotion/reward/rewardActivity';
|
||||
import { $t } from '#/locales';
|
||||
import { ProductCategorySelect } from '#/views/mall/product/category/components';
|
||||
import { SpuShowcase } from '#/views/mall/product/spu/components';
|
||||
|
||||
import { useFormSchema } from '../data';
|
||||
@@ -26,10 +27,8 @@ import RewardRule from './reward-rule.vue';
|
||||
|
||||
const emit = defineEmits(['success']);
|
||||
|
||||
const formData = ref<MallRewardActivityApi.RewardActivity>({
|
||||
// TODO @puhui999:这里的 conditionType、productScope 是不是可以删除呀。因为 data.ts 已经搞了 defaultValue;
|
||||
const formData = ref<Partial<MallRewardActivityApi.RewardActivity>>({
|
||||
conditionType: PromotionConditionTypeEnum.PRICE.type,
|
||||
productScope: PromotionProductScopeEnum.ALL.scope,
|
||||
rules: [],
|
||||
});
|
||||
|
||||
@@ -39,8 +38,6 @@ const getTitle = computed(() => {
|
||||
: $t('ui.actionTitle.create', ['满减送']);
|
||||
});
|
||||
|
||||
const rewardRuleRef = ref<InstanceType<typeof RewardRule>>();
|
||||
|
||||
const [Form, formApi] = useVbenForm({
|
||||
commonConfig: {
|
||||
componentProps: {
|
||||
@@ -53,7 +50,6 @@ const [Form, formApi] = useVbenForm({
|
||||
showDefaultActions: false,
|
||||
});
|
||||
|
||||
// TODO @芋艿:这里需要在简化下;
|
||||
const [Modal, modalApi] = useVbenModal({
|
||||
async onConfirm() {
|
||||
const { valid } = await formApi.validate();
|
||||
@@ -63,8 +59,9 @@ const [Modal, modalApi] = useVbenModal({
|
||||
modalApi.lock();
|
||||
// 提交表单
|
||||
try {
|
||||
const data = await formApi.getValues();
|
||||
rewardRuleRef.value?.setRuleCoupon();
|
||||
const values = await formApi.getValues();
|
||||
// 合并表单值和 formData(含 id、productSpuIds、productCategoryIds 等)
|
||||
const data = { ...formData.value, ...values };
|
||||
if (data.startAndEndTime && Array.isArray(data.startAndEndTime)) {
|
||||
data.startTime = data.startAndEndTime[0];
|
||||
data.endTime = data.startAndEndTime[1];
|
||||
@@ -76,9 +73,24 @@ const [Modal, modalApi] = useVbenModal({
|
||||
item.limit = convertToInteger(item.limit || 0);
|
||||
}
|
||||
});
|
||||
setProductScopeValues(data);
|
||||
// 设置 productScopeValues
|
||||
switch (data.productScope) {
|
||||
case PromotionProductScopeEnum.CATEGORY.scope: {
|
||||
const categoryIds = data.productCategoryIds;
|
||||
data.productScopeValues = Array.isArray(categoryIds)
|
||||
? categoryIds
|
||||
: categoryIds
|
||||
? [categoryIds]
|
||||
: [];
|
||||
break;
|
||||
}
|
||||
case PromotionProductScopeEnum.SPU.scope: {
|
||||
data.productScopeValues = data.productSpuIds;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
await (formData.value?.id
|
||||
await (data.id
|
||||
? updateRewardActivity(data as MallRewardActivityApi.RewardActivity)
|
||||
: createRewardActivity(data as MallRewardActivityApi.RewardActivity));
|
||||
// 关闭并提示
|
||||
@@ -91,11 +103,7 @@ const [Modal, modalApi] = useVbenModal({
|
||||
},
|
||||
async onOpenChange(isOpen: boolean) {
|
||||
if (!isOpen) {
|
||||
formData.value = {
|
||||
conditionType: PromotionConditionTypeEnum.PRICE.type,
|
||||
productScope: PromotionProductScopeEnum.ALL.scope,
|
||||
rules: [],
|
||||
};
|
||||
formData.value = {};
|
||||
return;
|
||||
}
|
||||
// 加载数据
|
||||
@@ -116,56 +124,11 @@ const [Modal, modalApi] = useVbenModal({
|
||||
formData.value = result;
|
||||
// 设置到 values
|
||||
await formApi.setValues(result);
|
||||
|
||||
await getProductScope();
|
||||
} finally {
|
||||
modalApi.unlock();
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
async function getProductScope() {
|
||||
switch (formData.value.productScope) {
|
||||
case PromotionProductScopeEnum.CATEGORY.scope: {
|
||||
await nextTick();
|
||||
let productCategoryIds = formData.value.productScopeValues as any;
|
||||
if (
|
||||
Array.isArray(productCategoryIds) &&
|
||||
productCategoryIds.length === 1
|
||||
) {
|
||||
productCategoryIds = productCategoryIds[0];
|
||||
}
|
||||
formData.value.productCategoryIds = productCategoryIds;
|
||||
break;
|
||||
}
|
||||
case PromotionProductScopeEnum.SPU.scope: {
|
||||
formData.value.productSpuIds = formData.value.productScopeValues;
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO @puhui999:/Users/yunai/Java/yudao-ui-admin-vben-v5/apps/web-antd/src/views/mall/promotion/coupon/template/data.ts 可以类似 /Users/yunai/Java/yudao-ui-admin-vben-v5/apps/web-antd/src/views/mall/promotion/coupon/template/data.ts 的 productScopeValues(微信交流)
|
||||
function setProductScopeValues(data: any) {
|
||||
switch (formData.value.productScope) {
|
||||
case PromotionProductScopeEnum.CATEGORY.scope: {
|
||||
data.productScopeValues = Array.isArray(formData.value.productCategoryIds)
|
||||
? formData.value.productCategoryIds
|
||||
: [formData.value.productCategoryIds];
|
||||
break;
|
||||
}
|
||||
case PromotionProductScopeEnum.SPU.scope: {
|
||||
data.productScopeValues = formData.value.productSpuIds;
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -173,13 +136,18 @@ function setProductScopeValues(data: any) {
|
||||
<Form class="mx-6">
|
||||
<!-- 自定义插槽:优惠规则 -->
|
||||
<template #rules>
|
||||
<RewardRule ref="rewardRuleRef" v-model="formData" />
|
||||
<RewardRule v-model="formData" />
|
||||
</template>
|
||||
|
||||
<!-- 自定义插槽:商品选择 -->
|
||||
<template #productSpuIds>
|
||||
<SpuShowcase v-model="formData.productSpuIds" />
|
||||
</template>
|
||||
|
||||
<!-- 自定义插槽:分类选择 -->
|
||||
<template #productCategoryIds>
|
||||
<ProductCategorySelect v-model="formData.productCategoryIds" multiple />
|
||||
</template>
|
||||
</Form>
|
||||
</Modal>
|
||||
</template>
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
import type { MallCouponTemplateApi } from '#/api/mall/promotion/coupon/couponTemplate';
|
||||
import type { MallRewardActivityApi } from '#/api/mall/promotion/reward/rewardActivity';
|
||||
|
||||
import { nextTick, onMounted, ref } from 'vue';
|
||||
import { nextTick, onMounted, ref, watch } from 'vue';
|
||||
|
||||
import { CouponTemplateTakeTypeEnum, DICT_TYPE } from '@vben/constants';
|
||||
|
||||
@@ -32,15 +32,14 @@ interface GiveCoupon extends MallCouponTemplateApi.CouponTemplate {
|
||||
const rewardRule = useVModel(props, 'modelValue', emits);
|
||||
const list = ref<GiveCoupon[]>([]); // 选择的优惠劵列表
|
||||
|
||||
// TODO @puhui999:1)命名上,可以弱化 coupon;例如说 selectRef;原因是,本身就是 coupon-select.vue;2)相关的处理的方法,最好都带 handle,如果是处理事件;例如说 deleteCoupon 改成 handleDelete;
|
||||
/** 选择优惠券 */
|
||||
const couponSelectRef = ref<InstanceType<typeof CouponSelect>>();
|
||||
function selectCoupon() {
|
||||
couponSelectRef.value?.open();
|
||||
const selectRef = ref<InstanceType<typeof CouponSelect>>();
|
||||
function handleSelect() {
|
||||
selectRef.value?.open();
|
||||
}
|
||||
|
||||
/** 选择优惠券后的回调 */
|
||||
function handleCouponChange(val: any[]) {
|
||||
function handleChange(val: any[]) {
|
||||
for (const item of val) {
|
||||
if (list.value.some((v) => v.id === item.id)) {
|
||||
continue;
|
||||
@@ -50,13 +49,18 @@ function handleCouponChange(val: any[]) {
|
||||
}
|
||||
|
||||
/** 删除优惠券 */
|
||||
function deleteCoupon(index: number) {
|
||||
function handleDelete(index: number) {
|
||||
list.value.splice(index, 1);
|
||||
}
|
||||
|
||||
/** 初始化赠送的优惠券列表 */
|
||||
async function initGiveCouponList() {
|
||||
if (!rewardRule.value || !rewardRule.value.giveCouponTemplateCounts) {
|
||||
// 校验优惠券存在
|
||||
if (
|
||||
!rewardRule.value ||
|
||||
!rewardRule.value.giveCouponTemplateCounts ||
|
||||
Object.keys(rewardRule.value.giveCouponTemplateCounts).length === 0
|
||||
) {
|
||||
return;
|
||||
}
|
||||
const tempLateIds = Object.keys(
|
||||
@@ -74,19 +78,22 @@ async function initGiveCouponList() {
|
||||
});
|
||||
}
|
||||
|
||||
/** 设置赠送的优惠券 */
|
||||
// TODO @puhui999:这个有办法不提供,就是不用 form.vue 去调用,更加透明~
|
||||
function setGiveCouponList() {
|
||||
if (!rewardRule.value) {
|
||||
return;
|
||||
}
|
||||
rewardRule.value.giveCouponTemplateCounts = {};
|
||||
list.value.forEach((rule) => {
|
||||
rewardRule.value.giveCouponTemplateCounts![rule.id] = rule.giveCount!;
|
||||
});
|
||||
}
|
||||
|
||||
defineExpose({ setGiveCouponList });
|
||||
/** 监听 list 变化,自动同步到 rewardRule */
|
||||
watch(
|
||||
list,
|
||||
(val) => {
|
||||
if (!rewardRule.value) {
|
||||
return;
|
||||
}
|
||||
// 核心:清空 giveCouponTemplateCounts,解决删除不生效的问题
|
||||
rewardRule.value.giveCouponTemplateCounts = {};
|
||||
// 设置优惠券和其数量的对应
|
||||
val.forEach((item) => {
|
||||
rewardRule.value.giveCouponTemplateCounts![item.id] = item.giveCount!;
|
||||
});
|
||||
},
|
||||
{ deep: true },
|
||||
);
|
||||
|
||||
onMounted(async () => {
|
||||
await nextTick();
|
||||
@@ -96,7 +103,7 @@ onMounted(async () => {
|
||||
|
||||
<template>
|
||||
<div class="w-full">
|
||||
<Button type="link" class="ml-2" @click="selectCoupon">添加优惠劵</Button>
|
||||
<Button type="link" class="pl-0" @click="handleSelect">添加优惠券</Button>
|
||||
|
||||
<div
|
||||
v-for="(item, index) in list"
|
||||
@@ -130,15 +137,15 @@ onMounted(async () => {
|
||||
type="number"
|
||||
/>
|
||||
<span>张</span>
|
||||
<Button type="link" danger @click="deleteCoupon(index)">删除</Button>
|
||||
<Button type="link" danger @click="handleDelete(index)">删除</Button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 优惠券选择 -->
|
||||
<CouponSelect
|
||||
ref="couponSelectRef"
|
||||
ref="selectRef"
|
||||
:take-type="CouponTemplateTakeTypeEnum.ADMIN.type"
|
||||
@change="handleCouponChange"
|
||||
@change="handleChange"
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<script lang="ts" setup>
|
||||
import type { MallRewardActivityApi } from '#/api/mall/promotion/reward/rewardActivity';
|
||||
|
||||
import { computed, ref } from 'vue';
|
||||
import { computed } from 'vue';
|
||||
|
||||
import { PromotionConditionTypeEnum } from '@vben/constants';
|
||||
|
||||
@@ -23,7 +23,7 @@ import RewardRuleCouponSelect from './reward-rule-coupon-select.vue';
|
||||
defineOptions({ name: 'RewardRule' });
|
||||
|
||||
const props = defineProps<{
|
||||
modelValue: MallRewardActivityApi.RewardActivity;
|
||||
modelValue: Partial<MallRewardActivityApi.RewardActivity>;
|
||||
}>();
|
||||
|
||||
const emits = defineEmits<{
|
||||
@@ -31,8 +31,6 @@ const emits = defineEmits<{
|
||||
}>();
|
||||
|
||||
const formData = useVModel(props, 'modelValue', emits);
|
||||
const rewardRuleCouponSelectRef =
|
||||
ref<InstanceType<typeof RewardRuleCouponSelect>[]>();
|
||||
|
||||
const isPriceCondition = computed(() => {
|
||||
return (
|
||||
@@ -55,19 +53,8 @@ function handleAdd() {
|
||||
|
||||
/** 处理删除 */
|
||||
function handleDelete(ruleIndex: number) {
|
||||
formData.value.rules.splice(ruleIndex, 1);
|
||||
formData.value.rules?.splice(ruleIndex, 1);
|
||||
}
|
||||
|
||||
function setRuleCoupon() {
|
||||
if (!rewardRuleCouponSelectRef.value) {
|
||||
return;
|
||||
}
|
||||
rewardRuleCouponSelectRef.value.forEach((item: any) =>
|
||||
item.setGiveCouponList(),
|
||||
);
|
||||
}
|
||||
|
||||
defineExpose({ setRuleCoupon });
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -99,7 +86,6 @@ defineExpose({ setRuleCoupon });
|
||||
:min="0"
|
||||
:precision="2"
|
||||
:step="0.1"
|
||||
:controls="false"
|
||||
class="!w-40"
|
||||
placeholder="请输入金额"
|
||||
/>
|
||||
@@ -116,57 +102,51 @@ defineExpose({ setRuleCoupon });
|
||||
</FormItem>
|
||||
|
||||
<!-- 优惠内容 -->
|
||||
<!-- TODO @puhui999:这里样式,让 AI 调整下;1)类似优惠劵折行啦;2)整体要左移点; -->
|
||||
<FormItem label="优惠内容" :label-col="{ span: 4 }">
|
||||
<div class="flex flex-col gap-4">
|
||||
<!-- 订单金额优惠 -->
|
||||
<div class="flex flex-col gap-2">
|
||||
<div class="font-medium">订单金额优惠</div>
|
||||
<div class="ml-4 flex items-center gap-2">
|
||||
<span>减</span>
|
||||
<InputNumber
|
||||
v-model:value="rule.discountPrice"
|
||||
:min="0"
|
||||
:precision="2"
|
||||
:step="0.1"
|
||||
:controls="false"
|
||||
class="!w-40"
|
||||
placeholder="请输入金额"
|
||||
/>
|
||||
<span>元</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 包邮 -->
|
||||
<FormItem
|
||||
label="优惠内容"
|
||||
:label-col="{ span: 4 }"
|
||||
:wrapper-col="{ span: 20 }"
|
||||
>
|
||||
<div class="flex flex-col gap-2">
|
||||
<span>订单金额优惠</span>
|
||||
<div class="flex items-center gap-2">
|
||||
<span class="font-medium">包邮:</span>
|
||||
<span>减</span>
|
||||
<InputNumber
|
||||
v-model:value="rule.discountPrice"
|
||||
:min="0"
|
||||
:precision="2"
|
||||
:step="0.1"
|
||||
class="!w-32"
|
||||
placeholder="请输入金额"
|
||||
/>
|
||||
<span>元</span>
|
||||
</div>
|
||||
<div class="flex items-center gap-2">
|
||||
<span>包邮:</span>
|
||||
<Switch
|
||||
v-model:checked="rule.freeDelivery"
|
||||
checked-children="是"
|
||||
un-checked-children="否"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<!-- 送积分 -->
|
||||
<div class="flex items-center gap-2">
|
||||
<span class="font-medium">送积分:</span>
|
||||
<span>送</span>
|
||||
<InputNumber
|
||||
v-model:value="rule.point"
|
||||
:min="0"
|
||||
:controls="false"
|
||||
class="!w-40"
|
||||
placeholder="请输入积分"
|
||||
/>
|
||||
<span>积分</span>
|
||||
<div>
|
||||
<div>送积分:</div>
|
||||
<div class="mt-2 flex items-center gap-2">
|
||||
<span>送</span>
|
||||
<InputNumber
|
||||
v-model:value="rule.point"
|
||||
:min="0"
|
||||
class="!w-32"
|
||||
placeholder="请输入积分"
|
||||
/>
|
||||
<span>积分</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 送优惠券 -->
|
||||
<div class="flex items-start gap-2">
|
||||
<span class="font-medium">送优惠券:</span>
|
||||
<div class="flex items-center gap-2">
|
||||
<span class="w-20">送优惠券:</span>
|
||||
<RewardRuleCouponSelect
|
||||
ref="rewardRuleCouponSelectRef"
|
||||
:model-value="rule"
|
||||
@update:model-value="(val) => (formData.rules![index] = val)"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user