提交 | 用户 | 时间
|
820397
|
1 |
<template> |
H |
2 |
<Dialog v-model="dialogVisible" :title="dialogTitle" width="800"> |
|
3 |
<el-form |
|
4 |
ref="formRef" |
|
5 |
v-loading="formLoading" |
|
6 |
:model="formData" |
|
7 |
:rules="formRules" |
|
8 |
label-width="80px" |
|
9 |
> |
|
10 |
<el-form-item label="公告标题" prop="title"> |
|
11 |
<el-input v-model="formData.title" placeholder="请输入公告标题" /> |
|
12 |
</el-form-item> |
|
13 |
<el-form-item label="公告内容" prop="content"> |
|
14 |
<Editor v-model="formData.content" height="150px" /> |
|
15 |
</el-form-item> |
|
16 |
<el-form-item label="公告类型" prop="type"> |
|
17 |
<el-select v-model="formData.type" clearable placeholder="请选择公告类型"> |
|
18 |
<el-option |
|
19 |
v-for="dict in getIntDictOptions(DICT_TYPE.SYSTEM_NOTICE_TYPE)" |
|
20 |
:key="parseInt(dict.value as any)" |
|
21 |
:label="dict.label" |
|
22 |
:value="parseInt(dict.value as any)" |
|
23 |
/> |
|
24 |
</el-select> |
|
25 |
</el-form-item> |
|
26 |
<el-form-item label="状态" prop="status"> |
|
27 |
<el-select v-model="formData.status" clearable placeholder="请选择状态"> |
|
28 |
<el-option |
|
29 |
v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)" |
|
30 |
:key="parseInt(dict.value as any)" |
|
31 |
:label="dict.label" |
|
32 |
:value="parseInt(dict.value as any)" |
|
33 |
/> |
|
34 |
</el-select> |
|
35 |
</el-form-item> |
|
36 |
<el-form-item label="备注" prop="remark"> |
|
37 |
<el-input v-model="formData.remark" placeholder="请输备注" type="textarea" /> |
|
38 |
</el-form-item> |
|
39 |
</el-form> |
|
40 |
<template #footer> |
|
41 |
<el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button> |
|
42 |
<el-button @click="dialogVisible = false">取 消</el-button> |
|
43 |
</template> |
|
44 |
</Dialog> |
|
45 |
</template> |
|
46 |
<script lang="ts" setup> |
|
47 |
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict' |
|
48 |
import { CommonStatusEnum } from '@/utils/constants' |
|
49 |
import * as NoticeApi from '@/api/system/notice' |
|
50 |
|
|
51 |
defineOptions({ name: 'SystemNoticeForm' }) |
|
52 |
|
|
53 |
const { t } = useI18n() // 国际化 |
|
54 |
const message = useMessage() // 消息弹窗 |
|
55 |
|
|
56 |
const dialogVisible = ref(false) // 弹窗的是否展示 |
|
57 |
const dialogTitle = ref('') // 弹窗的标题 |
|
58 |
const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用 |
|
59 |
const formType = ref('') // 表单的类型:create - 新增;update - 修改 |
|
60 |
const formData = ref({ |
|
61 |
id: undefined, |
|
62 |
title: '', |
|
63 |
type: undefined, |
|
64 |
content: '', |
|
65 |
status: CommonStatusEnum.ENABLE, |
|
66 |
remark: '' |
|
67 |
}) |
|
68 |
const formRules = reactive({ |
|
69 |
title: [{ required: true, message: '公告标题不能为空', trigger: 'blur' }], |
|
70 |
type: [{ required: true, message: '公告类型不能为空', trigger: 'change' }], |
|
71 |
status: [{ required: true, message: '状态不能为空', trigger: 'change' }], |
|
72 |
content: [{ required: true, message: '公告内容不能为空', trigger: 'blur' }] |
|
73 |
}) |
|
74 |
const formRef = ref() // 表单 Ref |
|
75 |
|
|
76 |
/** 打开弹窗 */ |
|
77 |
const open = async (type: string, id?: number) => { |
|
78 |
dialogVisible.value = true |
|
79 |
dialogTitle.value = t('action.' + type) |
|
80 |
formType.value = type |
|
81 |
resetForm() |
|
82 |
// 修改时,设置数据 |
|
83 |
if (id) { |
|
84 |
formLoading.value = true |
|
85 |
try { |
|
86 |
formData.value = await NoticeApi.getNotice(id) |
|
87 |
} finally { |
|
88 |
formLoading.value = false |
|
89 |
} |
|
90 |
} |
|
91 |
} |
|
92 |
defineExpose({ open }) // 提供 open 方法,用于打开弹窗 |
|
93 |
|
|
94 |
/** 提交表单 */ |
|
95 |
const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调 |
|
96 |
const submitForm = async () => { |
|
97 |
// 校验表单 |
|
98 |
if (!formRef) return |
|
99 |
const valid = await formRef.value.validate() |
|
100 |
if (!valid) return |
|
101 |
// 提交请求 |
|
102 |
formLoading.value = true |
|
103 |
try { |
|
104 |
const data = formData.value as unknown as NoticeApi.NoticeVO |
|
105 |
if (formType.value === 'create') { |
|
106 |
await NoticeApi.createNotice(data) |
|
107 |
message.success(t('common.createSuccess')) |
|
108 |
} else { |
|
109 |
await NoticeApi.updateNotice(data) |
|
110 |
message.success(t('common.updateSuccess')) |
|
111 |
} |
|
112 |
dialogVisible.value = false |
|
113 |
// 发送操作成功的事件 |
|
114 |
emit('success') |
|
115 |
} finally { |
|
116 |
formLoading.value = false |
|
117 |
} |
|
118 |
} |
|
119 |
|
|
120 |
/** 重置表单 */ |
|
121 |
const resetForm = () => { |
|
122 |
formData.value = { |
|
123 |
id: undefined, |
|
124 |
title: '', |
|
125 |
type: undefined, |
|
126 |
content: '', |
|
127 |
status: CommonStatusEnum.ENABLE, |
|
128 |
remark: '' |
|
129 |
} |
|
130 |
formRef.value?.resetFields() |
|
131 |
} |
|
132 |
</script> |