TemplateDrawer.vue
4.92 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<template>
<BasicDrawer
v-bind="$attrs"
@register="registerDrawer"
showFooter
:title="getTitle"
:helpMessage="['用户登录、忘记密码模板平台只提供{code}参数', '初始密码设置平台提供{name}参数']"
width="500px"
@ok="handleSubmit"
>
<BasicForm @register="registerForm" />
</BasicDrawer>
</template>
<script lang="ts">
import { defineComponent, ref, computed, unref } from 'vue';
import { BasicForm, useForm } from '/@/components/Form';
import { formSchema } from './template.data';
import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
import { saveOrEditMessageTemplate } from '/@/api/message/template';
import { useMessage } from '/@/hooks/web/useMessage';
import { findMessageConfig } from '/@/api/message/config';
export default defineComponent({
name: 'TemplateDrawer',
components: { BasicDrawer, BasicForm },
emits: ['success', 'register'],
setup(_, { emit }) {
const isUpdate = ref(true);
const [registerForm, { validate, setFieldsValue, resetFields, updateSchema }] = useForm({
labelWidth: 120,
schemas: formSchema,
showActionButtonGroup: false,
});
const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
await resetFields();
setDrawerProps({ confirmLoading: false });
isUpdate.value = !!data?.isUpdate;
if (unref(isUpdate)) {
const config = data.record.config;
for (const key in config) {
Reflect.set(data.record, key + '', config[key]);
}
await setFieldsValue(data.record);
const res = await findMessageConfig({});
const options = res.map((item) => {
return {
disabled: item.messageType !== data.record.messageType,
label: item.configName,
value: item.id,
};
});
await updateSchema({
field: 'messageConfigId',
componentProps: {
options,
},
});
if (data.record.templatePurpose === 'FOR_LOGIN') {
updateSchema({
field: 'templatePurpose',
helpMessage: `登录模板平台提供如下参数:
{
"code":"验证码"
}`,
});
} else if (data.record.templatePurpose === 'FOR_FORGET_PASSWORD') {
updateSchema({
field: 'templatePurpose',
helpMessage: `忘记密码模板平台提供如下参数:
{
"code":"验证码"
}`,
});
} else if (data.record.templatePurpose === 'FOR_ALARM_NOTICE') {
updateSchema({
field: 'templatePurpose',
helpMessage: `告警通知模板平台提供如下参数:
{
"type":"告警类型",
"device_name":"设备名称",
"severity":"告警等级",
"organization":"设备所属组织",
"createTime":告警时间
}`,
});
} else if (data.record.templatePurpose === 'FOR_SET_PASSWORD') {
updateSchema({
field: 'templatePurpose',
helpMessage: `清除密码模板平台提供如下参数:
{
"code":"验证码"
}`,
});
}
} else {
updateSchema({
field: 'templatePurpose',
helpMessage: [],
});
}
});
const getTitle = computed(() => (!unref(isUpdate) ? '新增模板配置' : '编辑模板配置'));
async function handleSubmit() {
setDrawerProps({ confirmLoading: true });
try {
const values = await validate();
const { createMessage } = useMessage();
let config = {};
if (values.messageType === 'PHONE_MESSAGE') {
config = {
accessKeyId: values.accessKeyId,
accessKeySecret: values.accessKeySecret,
};
} else if (values.messageType === 'EMAIL_MESSAGE') {
config = {
host: values.host,
port: values.port,
username: values.username,
password: values.password,
};
}
Reflect.set(values, 'config', config);
let saveMessage = '添加成功';
let updateMessage = '修改成功';
await saveOrEditMessageTemplate(values, unref(isUpdate));
closeDrawer();
emit('success');
createMessage.success(unref(isUpdate) ? updateMessage : saveMessage);
} finally {
setTimeout(() => {
setDrawerProps({ confirmLoading: false });
}, 300);
}
}
return {
resetFields,
registerDrawer,
registerForm,
getTitle,
handleSubmit,
};
},
});
</script>