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

156 lines
4.5 KiB
Vue
Raw Normal View History

2025-11-04 16:53:08 +08:00
<script lang="ts" setup>
// TODO @hw这里 Reply 貌似不存在
2025-11-13 14:44:08 +08:00
import type { Reply } from '#/views/mp/components';
2025-11-04 16:53:08 +08:00
2025-11-12 16:56:18 +08:00
import { computed, nextTick, ref } from 'vue';
2025-11-04 16:53:08 +08:00
import { useVbenModal } from '@vben/common-ui';
import { AutoReplyMsgType, ReplyType } from '@vben/constants';
2025-11-04 16:53:08 +08:00
import { message } from 'ant-design-vue';
2025-11-12 16:56:18 +08:00
import { useVbenForm } from '#/adapter/form';
2025-11-07 19:06:34 +08:00
import { createAutoReply, updateAutoReply } from '#/api/mp/autoReply';
2025-11-04 16:53:08 +08:00
import { $t } from '#/locales';
2025-11-12 16:56:18 +08:00
import { useFormSchema } from '../data';
2025-11-04 16:53:08 +08:00
const emit = defineEmits(['success']);
2025-11-12 16:56:18 +08:00
const formData = ref<{
accountId?: number;
msgType: AutoReplyMsgType;
2025-11-12 16:56:18 +08:00
row?: any;
}>();
2025-11-04 16:53:08 +08:00
const getTitle = computed(() => {
2025-11-12 16:56:18 +08:00
return formData.value?.row?.id
? $t('ui.actionTitle.edit', ['自动回复'])
: $t('ui.actionTitle.create', ['自动回复']);
});
const [Form, formApi] = useVbenForm({
commonConfig: {
componentProps: {
class: 'w-full',
},
formItemClass: 'col-span-2',
labelWidth: 100,
},
layout: 'horizontal',
schema: useFormSchema(AutoReplyMsgType.Keyword),
2025-11-12 16:56:18 +08:00
showDefaultActions: false,
2025-11-04 16:53:08 +08:00
});
const [Modal, modalApi] = useVbenModal({
async onConfirm() {
2025-11-12 16:56:18 +08:00
const { valid } = await formApi.validate();
if (!valid) {
return;
}
2025-11-04 16:53:08 +08:00
// 处理回复消息
2025-11-12 16:56:18 +08:00
const submitForm: any = await formApi.getValues();
// 确保 type 字段使用当前选中的 tab 值
submitForm.type = formData.value?.msgType;
// 确保 accountId 字段存在
submitForm.accountId = formData.value?.accountId;
// 编辑模式下,确保 id 字段存在(从 row 中获取,因为表单 schema 中没有 id 字段)
if (formData.value?.row?.id && !submitForm.id) {
submitForm.id = formData.value.row.id;
}
const reply = submitForm.reply as Reply;
if (reply) {
submitForm.responseMessageType = reply.type;
submitForm.responseContent = reply.content;
submitForm.responseMediaId = reply.mediaId;
submitForm.responseMediaUrl = reply.url;
submitForm.responseTitle = reply.title;
submitForm.responseDescription = reply.description;
submitForm.responseThumbMediaId = reply.thumbMediaId;
submitForm.responseThumbMediaUrl = reply.thumbMediaUrl;
submitForm.responseArticles = reply.articles;
submitForm.responseMusicUrl = reply.musicUrl;
submitForm.responseHqMusicUrl = reply.hqMusicUrl;
}
delete submitForm.reply;
2025-11-04 16:53:08 +08:00
modalApi.lock();
try {
2025-11-12 16:56:18 +08:00
if (submitForm.id === undefined) {
2025-11-07 19:06:34 +08:00
await createAutoReply(submitForm);
2025-11-04 16:53:08 +08:00
message.success('新增成功');
} else {
2025-11-07 19:06:34 +08:00
await updateAutoReply(submitForm);
2025-11-04 16:53:08 +08:00
message.success('修改成功');
}
await modalApi.close();
emit('success');
} finally {
modalApi.unlock();
}
},
async onOpenChange(isOpen: boolean) {
if (!isOpen) {
formData.value = undefined;
return;
}
// 加载数据
const data = modalApi.getData<{
accountId?: number;
msgType: AutoReplyMsgType;
2025-11-04 16:53:08 +08:00
row?: any;
}>();
if (!data) {
return;
}
2025-11-12 16:56:18 +08:00
// 先更新 schema确保表单字段正确
formApi.setState({ schema: useFormSchema(data.msgType) });
// 等待 schema 更新完成
await nextTick();
2025-11-04 16:53:08 +08:00
2025-11-12 16:56:18 +08:00
formData.value = data;
if (data.row?.id) {
2025-11-04 16:53:08 +08:00
// 编辑:加载数据
const rowData = data.row;
2025-11-12 16:56:18 +08:00
const formValues: any = { ...rowData };
formValues.reply = {
2025-11-04 16:53:08 +08:00
type: rowData.responseMessageType,
accountId: data.accountId || -1,
content: rowData.responseContent,
mediaId: rowData.responseMediaId,
url: rowData.responseMediaUrl,
title: rowData.responseTitle,
description: rowData.responseDescription,
thumbMediaId: rowData.responseThumbMediaId,
thumbMediaUrl: rowData.responseThumbMediaUrl,
articles: rowData.responseArticles,
musicUrl: rowData.responseMusicUrl,
hqMusicUrl: rowData.responseHqMusicUrl,
};
2025-11-12 16:56:18 +08:00
await formApi.setValues(formValues);
} else {
// 新建:初始化表单
const initialValues: any = {
id: undefined,
accountId: data.accountId || -1,
type: data.msgType,
requestKeyword: undefined,
requestMatch: data.msgType === AutoReplyMsgType.Keyword ? 1 : undefined,
2025-11-12 16:56:18 +08:00
requestMessageType: undefined,
reply: {
type: ReplyType.Text,
accountId: data.accountId || -1,
},
};
await formApi.setValues(initialValues);
2025-11-04 16:53:08 +08:00
}
},
});
</script>
<template>
<Modal :title="getTitle" class="w-4/5">
2025-11-12 16:56:18 +08:00
<Form class="mx-4" />
2025-11-04 16:53:08 +08:00
</Modal>
</template>