Files
iot-device-management-frontend/apps/web-antd/src/views/mp/draft/modules/form.vue

99 lines
2.3 KiB
Vue
Raw Normal View History

2025-11-05 17:01:42 +08:00
<script lang="ts" setup>
2025-11-12 16:56:18 +08:00
import type { NewsItem } from './types';
2025-11-05 17:01:42 +08:00
2025-11-12 16:56:18 +08:00
import { computed, provide, ref } from 'vue';
2025-11-05 17:01:42 +08:00
2025-11-12 16:56:18 +08:00
import { useVbenModal } from '@vben/common-ui';
2025-11-05 17:01:42 +08:00
import { message, Spin } from 'ant-design-vue';
2025-11-07 19:06:34 +08:00
import { createDraft, updateDraft } from '#/api/mp/draft';
2025-11-05 17:01:42 +08:00
2025-11-12 16:56:18 +08:00
import NewsForm from './news-form.vue';
2025-11-05 17:01:42 +08:00
const emit = defineEmits(['success']);
2025-11-12 16:56:18 +08:00
// DONE @hw是不是通过 id 字段判断是否为新增?类似 /Users/yunai/Java/yudao-ui-admin-vben-v5/apps/web-antd/src/views/system/user/modules/form.vue
2025-11-05 17:01:42 +08:00
const formData = ref<{
accountId: number;
mediaId?: string;
newsList?: NewsItem[];
}>();
const newsList = ref<NewsItem[]>([]);
2025-11-12 16:56:18 +08:00
// DONE @hw不需要 isSave通过 modal 去 lock 就好啦。
2025-11-05 17:01:42 +08:00
const isSubmitting = ref(false);
const getTitle = computed(() => {
2025-11-12 16:56:18 +08:00
return formData.value?.mediaId ? '修改图文' : '新建图文';
2025-11-05 17:01:42 +08:00
});
2025-11-12 16:56:18 +08:00
// 提供 accountId 给子组件
provide(
'accountId',
computed(() => formData.value?.accountId),
);
2025-11-05 17:01:42 +08:00
const [Modal, modalApi] = useVbenModal({
async onConfirm() {
if (!formData.value) {
return;
}
isSubmitting.value = true;
modalApi.lock();
try {
2025-11-12 16:56:18 +08:00
if (formData.value.mediaId) {
2025-11-07 19:06:34 +08:00
await updateDraft(
2025-11-05 17:01:42 +08:00
formData.value.accountId,
formData.value.mediaId,
newsList.value,
);
message.success('更新成功');
2025-11-12 16:56:18 +08:00
} else {
await createDraft(formData.value.accountId, newsList.value);
message.success('新增成功');
2025-11-05 17:01:42 +08:00
}
await modalApi.close();
emit('success');
} finally {
isSubmitting.value = false;
modalApi.unlock();
}
},
async onOpenChange(isOpen: boolean) {
if (!isOpen) {
formData.value = undefined;
newsList.value = [];
return;
}
const data = modalApi.getData<{
accountId: number;
isCreating: boolean;
mediaId?: string;
newsList?: NewsItem[];
}>();
if (!data) {
return;
}
2025-11-12 16:56:18 +08:00
formData.value = {
accountId: data.accountId,
mediaId: data.mediaId,
newsList: data.newsList,
};
2025-11-05 17:01:42 +08:00
newsList.value = data.newsList || [];
},
});
</script>
<template>
<Modal :title="getTitle" class="w-4/5" destroy-on-close>
<Spin :spinning="isSubmitting">
<NewsForm
v-if="formData"
v-model="newsList"
2025-11-12 16:56:18 +08:00
:is-creating="!formData.mediaId"
2025-11-05 17:01:42 +08:00
/>
</Spin>
</Modal>
</template>