useDrawer.vue
3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<template>
<div>
<BasicDrawer
v-bind="$attrs"
@register="registerDrawer"
:title="getTitle"
width="800px"
showFooter
>
<BasicForm @register="registerForm" />
<template #footer>
<a-button @click="handleCancel">取消</a-button>
<a-button @click="handleSaveDraft">保存草稿</a-button>
<a-button type="primary" @click="handleSend">发布通知</a-button>
</template>
</BasicDrawer>
</div>
</template>
<script lang="ts">
import { defineComponent, computed, unref, ref } from 'vue';
import { BasicForm, useForm } from '/@/components/Form';
import { formSchema } from './config.d';
import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
import {
notifyAddDraftApi,
notifyAddLeaseApi,
} from '/@/api/stationnotification/stationnotifyApi';
import { useMessage } from '/@/hooks/web/useMessage';
export default defineComponent({
name: 'ConfigDrawer',
components: { BasicDrawer, BasicForm },
emits: ['success', 'register'],
setup(_, { emit }) {
const { createMessage } = useMessage();
const isUpdate = ref<Boolean>();
const getTitle = computed(() => (!unref(isUpdate) ? '新增通知' : '编辑通知'));
const noticeId = ref('');
const [registerForm, { setFieldsValue, resetFields, validate, getFieldsValue }] = useForm({
labelWidth: 120,
schemas: formSchema,
showActionButtonGroup: false,
});
const [registerDrawer, { closeDrawer }] = useDrawerInner(async (data) => {
await resetFields();
isUpdate.value = data.isUpdate;
//编辑
if (data.isUpdate) {
noticeId.value = data.record.id;
Reflect.set(data.record, 'receiverType', data.record.receiverType === '全部' ? '0' : '1');
if (data.record.receiverType === '1') {
if (!data.record.pointId.length) return;
const organizationId = data.record.pointId.split(',');
Reflect.set(data.record, 'organizationId', organizationId);
}
setFieldsValue(data.record);
}
});
// 发布通知
const handleSend = async () => {
await validate();
const field = getFieldsValue();
const pointId = field.receiverType === '1' ? field.organizationId + '' : null;
// 新增情况
if (!unref(isUpdate)) {
const addNotice = {
...field,
pointId,
};
await notifyAddLeaseApi(addNotice);
} else {
const editNotice = {
...field,
pointId,
id: unref(noticeId),
};
await notifyAddLeaseApi(editNotice);
}
emit('success');
closeDrawer();
createMessage.success('发布成功');
};
// 保存草稿
const handleSaveDraft = async () => {
await validate();
const field = getFieldsValue();
const pointId = field.receiverType === '1' ? field.organizationId + '' : null;
if (!unref(isUpdate)) {
const saveDraft = {
...field,
pointId,
};
await notifyAddDraftApi(saveDraft);
} else {
const editDraft = {
...field,
pointId,
id: unref(noticeId),
};
await notifyAddDraftApi(editDraft);
}
emit('success');
closeDrawer();
createMessage.success('保存草稿成功');
};
const handleCancel = () => {
// resetFields();
closeDrawer();
};
return {
getTitle,
handleSend,
handleCancel,
handleSaveDraft,
registerForm,
registerDrawer,
setFieldsValue,
};
},
});
</script>