feat:【antd】【mall】diy 主页面的迁移

This commit is contained in:
YunaiV
2025-10-25 21:27:37 +08:00
parent d8f4979a47
commit 5e259eb685
15 changed files with 552 additions and 55 deletions

View File

@@ -1,6 +1,8 @@
import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import { getRangePickerDefaultProps } from '#/utils';
/** 表单配置 */
export function useFormSchema(): VbenFormSchema[] {
return [
@@ -51,7 +53,7 @@ export function useGridFormSchema(): VbenFormSchema[] {
component: 'Input',
componentProps: {
placeholder: '请输入页面名称',
allowClear: true,
clearable: true,
},
},
{
@@ -59,9 +61,8 @@ export function useGridFormSchema(): VbenFormSchema[] {
label: '创建时间',
component: 'RangePicker',
componentProps: {
placeholder: ['开始时间', '结束时间'],
...getRangePickerDefaultProps(),
allowClear: true,
valueFormat: 'YYYY-MM-DD HH:mm:ss',
},
},
];

View File

@@ -0,0 +1,64 @@
<script setup lang="ts">
import type { MallDiyPageApi } from '#/api/mall/promotion/diy/page';
import { onMounted, ref, unref } from 'vue';
import { useRoute } from 'vue-router';
import { message } from 'ant-design-vue';
import * as DiyPageApi from '#/api/mall/promotion/diy/page';
import { DiyEditor, PAGE_LIBS } from '#/views/mall/promotion/components';
/** 装修页面表单 */
defineOptions({ name: 'DiyPageDecorate' });
const route = useRoute();
const formData = ref<MallDiyPageApi.DiyPage>();
/** 获取详情 */
async function getPageDetail(id: any) {
const hideLoading = message.loading({
content: '加载中...',
duration: 0,
});
try {
formData.value = await DiyPageApi.getDiyPageProperty(id);
} finally {
hideLoading();
}
}
/** 提交表单 */
async function submitForm() {
const hideLoading = message.loading({
content: '保存中...',
duration: 0,
});
try {
await DiyPageApi.updateDiyPageProperty(unref(formData)!);
message.success('保存成功');
} finally {
hideLoading();
}
}
/** 初始化 */
onMounted(() => {
if (!route.params.id) {
message.warning('参数错误,页面编号不能为空!');
return;
}
formData.value = {} as MallDiyPageApi.DiyPage;
getPageDetail(route.params.id);
});
</script>
<template>
<DiyEditor
v-if="formData?.id"
v-model="formData!.property"
:title="formData!.name"
:libs="PAGE_LIBS"
@save="submitForm"
/>
</template>

View File

@@ -6,6 +6,8 @@ import { useRouter } from 'vue-router';
import { DocAlert, Page, useVbenModal } from '@vben/common-ui';
import { message } from 'ant-design-vue';
import { ACTION_ICON, TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
import { deleteDiyPage, getDiyPagePage } from '#/api/mall/promotion/diy/page';
import { $t } from '#/locales';
@@ -37,7 +39,6 @@ function handleEdit(row: MallDiyPageApi.DiyPage) {
formModalApi.setData(row).open();
}
// TODO @xingyu装修未实现
/** 装修页面 */
function handleDecorate(row: MallDiyPageApi.DiyPage) {
push({ name: 'DiyPageDecorate', params: { id: row.id } });
@@ -45,8 +46,17 @@ function handleDecorate(row: MallDiyPageApi.DiyPage) {
/** 删除 DIY 页面 */
async function handleDelete(row: MallDiyPageApi.DiyPage) {
await deleteDiyPage(row.id as number);
handleRefresh();
const hideLoading = message.loading({
content: $t('ui.actionMessage.deleting', [row.name]),
duration: 0,
});
try {
await deleteDiyPage(row.id as number);
message.success($t('ui.actionMessage.deleteSuccess', [row.name]));
handleRefresh();
} finally {
hideLoading();
}
}
const [Grid, gridApi] = useVbenVxeGrid({

View File

@@ -45,12 +45,6 @@ const [Modal, modalApi] = useVbenModal({
modalApi.lock();
// 提交表单
const data = (await formApi.getValues()) as MallDiyPageApi.DiyPage;
// 确保必要的默认值
if (!data.previewPicUrls) {
data.previewPicUrls = [];
}
try {
await (formData.value?.id ? updateDiyPage(data) : createDiyPage(data));
// 关闭并提示
@@ -84,7 +78,7 @@ const [Modal, modalApi] = useVbenModal({
</script>
<template>
<Modal class="w-2/5" :title="getTitle">
<Modal :title="getTitle" class="w-2/5">
<Form />
</Modal>
</template>

View File

@@ -3,6 +3,8 @@ import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import { DICT_TYPE } from '@vben/constants';
import { getRangePickerDefaultProps } from '#/utils';
/** 表单配置 */
export function useFormSchema(): VbenFormSchema[] {
return [
@@ -53,7 +55,7 @@ export function useGridFormSchema(): VbenFormSchema[] {
component: 'Input',
componentProps: {
placeholder: '请输入模板名称',
allowClear: true,
clearable: true,
},
},
{
@@ -61,9 +63,8 @@ export function useGridFormSchema(): VbenFormSchema[] {
label: '创建时间',
component: 'RangePicker',
componentProps: {
placeholder: ['开始时间', '结束时间'],
...getRangePickerDefaultProps(),
allowClear: true,
valueFormat: 'YYYY-MM-DD HH:mm:ss',
},
},
];

View File

@@ -0,0 +1,208 @@
<script lang="ts" setup>
import type { MallDiyPageApi } from '#/api/mall/promotion/diy/page';
import type { MallDiyTemplateApi } from '#/api/mall/promotion/diy/template';
import type { DiyComponentLibrary } from '#/views/mall/promotion/components'; // 商城的 DIY 组件,在 DiyEditor 目录下
import { onMounted, reactive, ref } from 'vue';
import { useRoute } from 'vue-router';
import { useTabs } from '@vben/hooks';
import { IconifyIcon } from '@vben/icons';
import { useAccessStore } from '@vben/stores';
import { isEmpty } from '@vben/utils';
import { message, Radio, RadioGroup, Tooltip } from 'ant-design-vue';
import * as DiyPageApi from '#/api/mall/promotion/diy/page';
import * as DiyTemplateApi from '#/api/mall/promotion/diy/template';
import { DiyEditor, PAGE_LIBS } from '#/views/mall/promotion/components';
/** 装修模板表单 */
defineOptions({ name: 'DiyTemplateDecorate' });
const route = useRoute();
const { refreshTab } = useTabs();
const DIY_PAGE_INDEX_KEY = 'diy_page_index'; // 特殊:存储 reset 重置时,当前 selectedTemplateItem 值,从而进行恢复
const selectedTemplateItem = ref(0);
const templateItems = reactive([
{ name: '基础设置', icon: 'ep:iphone' },
{ name: '首页', icon: 'ep:home-filled' },
{ name: '我的', icon: 'ep:user-filled' },
]); // 左上角工具栏操作按钮
const formData = ref<MallDiyTemplateApi.DiyTemplateProperty>();
const currentFormData = ref<
MallDiyPageApi.DiyPage | MallDiyTemplateApi.DiyTemplateProperty
>({
property: '',
} as MallDiyPageApi.DiyPage); // 当前编辑的属性
const currentFormDataMap = ref<
Map<string, MallDiyPageApi.DiyPage | MallDiyTemplateApi.DiyTemplateProperty>
>(new Map()); // templateItem 对应的缓存
const previewUrl = ref(''); // 商城 H5 预览地址
const templateLibs = [] as DiyComponentLibrary[]; // 模板组件库
const libs = ref<DiyComponentLibrary[]>(templateLibs); // 当前组件库
/** 获取详情 */
async function getPageDetail(id: any) {
const hideLoading = message.loading({
content: '加载中...',
duration: 0,
});
try {
formData.value = await DiyTemplateApi.getDiyTemplateProperty(id);
// 拼接手机预览链接
const domain = import.meta.env.VITE_MALL_H5_DOMAIN;
const accessStore = useAccessStore();
previewUrl.value = `${domain}?templateId=${formData.value.id}&${accessStore.tenantId}`;
} finally {
hideLoading();
}
}
/** 模板选项切换 */
function handleTemplateItemChange(val: any) {
// 缓存模版编辑数据
currentFormDataMap.value.set(
templateItems[selectedTemplateItem.value]?.name || '',
currentFormData.value!,
);
// 读取模版缓存
const data = currentFormDataMap.value.get(templateItems[val]?.name || '');
// 切换模版
selectedTemplateItem.value = val;
// 情况一:编辑模板
if (val === 0) {
libs.value = templateLibs;
currentFormData.value = (isEmpty(data) ? formData.value : data) as
| MallDiyPageApi.DiyPage
| MallDiyTemplateApi.DiyTemplateProperty;
return;
}
// 情况二:编辑页面
libs.value = PAGE_LIBS;
currentFormData.value = (
isEmpty(data)
? formData.value!.pages.find(
(page: MallDiyPageApi.DiyPage) =>
page.name === templateItems[val]?.name,
)
: data
) as MallDiyPageApi.DiyPage | MallDiyTemplateApi.DiyTemplateProperty;
}
/** 提交表单 */
async function submitForm() {
const hideLoading = message.loading({
content: '保存中...',
duration: 0,
});
try {
// 对所有的 templateItems 都进行保存,有缓存则保存缓存,解决都有修改时只保存了当前所编辑的 templateItem导致装修效果存在差异
for (const [i, templateItem] of templateItems.entries()) {
const data = currentFormDataMap.value.get(templateItem.name) as any;
// 情况一:基础设置
if (i === 0) {
// 提交模板属性
await DiyTemplateApi.updateDiyTemplateProperty(
isEmpty(data) ? formData.value! : data,
);
continue;
}
// 提交页面属性
// 情况二:提交当前正在编辑的页面
if (currentFormData.value?.name.includes(templateItem.name)) {
await DiyPageApi.updateDiyPageProperty(currentFormData.value!);
continue;
}
// 情况三:提交页面编辑缓存
if (!isEmpty(data)) {
await DiyPageApi.updateDiyPageProperty(data!);
}
}
message.success('保存成功');
} finally {
hideLoading();
}
}
/** 刷新当前 Tab */
function handleEditorReset() {
sessionStorage.setItem(DIY_PAGE_INDEX_KEY, `${selectedTemplateItem.value}`);
refreshTab();
}
/** 恢复当前 Tab 之前的 indexselectedTemplateItem */
function recoverPageIndex() {
// 恢复重置前的页面,默认是第一个页面
const pageIndex = Number(sessionStorage.getItem(DIY_PAGE_INDEX_KEY)) || 0;
// 移除标记
sessionStorage.removeItem(DIY_PAGE_INDEX_KEY);
// 重新初始化数据
currentFormData.value = formData.value as
| MallDiyPageApi.DiyPage
| MallDiyTemplateApi.DiyTemplateProperty;
currentFormDataMap.value = new Map<
string,
MallDiyPageApi.DiyPage | MallDiyTemplateApi.DiyTemplateProperty
>();
// 切换页面
if (pageIndex !== selectedTemplateItem.value) {
handleTemplateItemChange(pageIndex);
}
}
/** 初始化 */
onMounted(async () => {
if (!route.params.id) {
message.warning('参数错误,页面编号不能为空!');
return;
}
// 查询详情
formData.value = {} as MallDiyTemplateApi.DiyTemplateProperty;
await getPageDetail(route.params.id);
// 恢复重置前的页面
recoverPageIndex();
});
</script>
<template>
<DiyEditor
v-if="formData?.id"
v-model="currentFormData!.property"
:libs="libs"
:preview-url="previewUrl"
:show-navigation-bar="selectedTemplateItem !== 0"
:show-page-config="selectedTemplateItem !== 0"
:show-tab-bar="selectedTemplateItem === 0"
:title="templateItems[selectedTemplateItem]?.name || ''"
@reset="handleEditorReset"
@save="submitForm"
>
<template #toolBarLeft>
<RadioGroup
:value="selectedTemplateItem"
class="h-full!"
@change="handleTemplateItemChange"
>
<Tooltip
v-for="(item, index) in templateItems"
:key="index"
:title="item.name"
>
<Radio.Button :value="index">
<IconifyIcon :icon="item.icon" :size="24" />
</Radio.Button>
</Tooltip>
</RadioGroup>
</template>
</DiyEditor>
</template>

View File

@@ -43,7 +43,6 @@ function handleEdit(row: MallDiyTemplateApi.DiyTemplate) {
formModalApi.setData(row).open();
}
// TODO @xingyu装修未实现
/** 装修模板 */
function handleDecorate(row: MallDiyTemplateApi.DiyTemplate) {
router.push({ name: 'DiyTemplateDecorate', params: { id: row.id } });
@@ -51,20 +50,33 @@ function handleDecorate(row: MallDiyTemplateApi.DiyTemplate) {
/** 使用模板 */
async function handleUse(row: MallDiyTemplateApi.DiyTemplate) {
confirm({
content: `是否使用模板"${row.name}"?`,
}).then(async () => {
// 发起删除
await confirm(`是否使用模板"${row.name}"?`);
const hideLoading = message.loading({
content: `正在使用模板"${row.name}"...`,
duration: 0,
});
try {
await useDiyTemplate(row.id as number);
message.success('使用成功');
handleRefresh();
});
} finally {
hideLoading();
}
}
/** 删除DIY模板 */
/** 删除 DIY 模板 */
async function handleDelete(row: MallDiyTemplateApi.DiyTemplate) {
await deleteDiyTemplate(row.id as number);
handleRefresh();
const hideLoading = message.loading({
content: $t('ui.actionMessage.deleting', [row.name]),
duration: 0,
});
try {
await deleteDiyTemplate(row.id as number);
message.success($t('ui.actionMessage.deleteSuccess', [row.name]));
handleRefresh();
} finally {
hideLoading();
}
}
const [Grid, gridApi] = useVbenVxeGrid({
@@ -142,14 +154,14 @@ const [Grid, gridApi] = useVbenVxeGrid({
},
{
label: '使用',
type: 'link' as const,
type: 'link',
auth: ['promotion:diy-template:use'],
ifShow: !row.used,
onClick: handleUse.bind(null, row),
},
{
label: $t('common.delete'),
type: 'link' as const,
type: 'link',
danger: true,
icon: ACTION_ICON.DELETE,
auth: ['promotion:diy-template:delete'],

View File

@@ -16,9 +16,7 @@ import { $t } from '#/locales';
import { useFormSchema } from '../data';
/** 提交表单 */
const emit = defineEmits(['success']);
const formData = ref<MallDiyTemplateApi.DiyTemplate>();
const getTitle = computed(() => {
return formData.value?.id
@@ -47,15 +45,6 @@ const [Modal, modalApi] = useVbenModal({
modalApi.lock();
// 提交表单
const data = (await formApi.getValues()) as MallDiyTemplateApi.DiyTemplate;
// 确保必要的默认值
if (!data.previewPicUrls) {
data.previewPicUrls = [];
}
if (data.used === undefined) {
data.used = false;
}
try {
await (formData.value?.id
? updateDiyTemplate(data)
@@ -91,7 +80,7 @@ const [Modal, modalApi] = useVbenModal({
</script>
<template>
<Modal class="w-2/5" :title="getTitle">
<Modal :title="getTitle" class="w-2/5">
<Form />
</Modal>
</template>