SendMailModal.jsx
9.85 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import React, { useState, useEffect } from 'react'
import { Modal, Form, Input, Button, Space, message, Tag, Alert, Radio, Tooltip } from 'antd'
import { SendOutlined, MailOutlined, PlusOutlined, UserAddOutlined, TeamOutlined, UserOutlined, CloseOutlined, ImportOutlined } from '@ant-design/icons'
import request from '../utils/request'
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
export default function SendMailModal({ open, onClose, script, onSent }) {
const [form] = Form.useForm()
const [sending, setSending] = useState(false)
// 收件人列表
const [recipients, setRecipients] = useState([])
// 单个添加输入框
const [singleInput, setSingleInput] = useState('')
// 批量导入输入框
const [batchInput, setBatchInput] = useState('')
const [showBatchInput, setShowBatchInput] = useState(false)
// 发送模式: batch=群发(一封邮件所有人), individual=逐个发送(每人一封)
const [sendMode, setSendMode] = useState('batch')
useEffect(() => {
if (open && script) {
form.resetFields()
// 从脚本预设的收件人初始化列表
const savedEmails = (script.recipientEmails || '').split(',').map(e => e.trim()).filter(e => e && emailRegex.test(e))
setRecipients(savedEmails)
setSingleInput('')
setBatchInput('')
setShowBatchInput(false)
setSendMode('batch')
setTimeout(() => {
form.setFieldsValue({
emailSubject: script.emailSubject || '',
})
}, 0)
}
}, [open, script])
// 添加单个收件人
const handleAddSingle = () => {
const email = singleInput.trim()
if (!email) return
if (!emailRegex.test(email)) {
message.warning(`邮箱格式不正确: ${email}`)
return
}
if (recipients.includes(email)) {
message.info('该收件人已在列表中')
return
}
setRecipients([...recipients, email])
setSingleInput('')
}
// 批量导入收件人
const handleBatchImport = () => {
if (!batchInput.trim()) return
const emails = batchInput.split(/[,;\n\s]+/).map(e => e.trim()).filter(e => e)
const invalidEmails = emails.filter(e => !emailRegex.test(e))
if (invalidEmails.length > 0) {
message.warning(`以下邮箱格式不正确: ${invalidEmails.join(', ')}`)
}
const validEmails = emails.filter(e => emailRegex.test(e))
// 去重
const newEmails = validEmails.filter(e => !recipients.includes(e))
if (newEmails.length > 0) {
setRecipients([...recipients, ...newEmails])
message.success(`成功添加 ${newEmails.length} 个收件人`)
} else {
message.info('没有新的收件人可添加')
}
setBatchInput('')
setShowBatchInput(false)
}
// 删除收件人
const handleRemoveRecipient = (email) => {
setRecipients(recipients.filter(r => r !== email))
}
// 清空所有收件人
const handleClearAll = () => {
setRecipients([])
}
// 从脚本预设导入
const handleImportFromScript = () => {
const savedEmails = (script.recipientEmails || '').split(',').map(e => e.trim()).filter(e => e && emailRegex.test(e))
const newEmails = savedEmails.filter(e => !recipients.includes(e))
if (newEmails.length > 0) {
setRecipients([...recipients, ...newEmails])
message.success(`从脚本配置导入 ${newEmails.length} 个收件人`)
} else {
message.info('脚本配置中的收件人已全部在列表中')
}
}
const handleSend = async () => {
if (recipients.length === 0) {
message.warning('请至少添加一个收件人')
return
}
try {
const values = await form.validateFields()
setSending(true)
const res = await request.post(`/sql-manage/${script.id}/send-mail`, {
recipientEmails: recipients.join(','),
emailSubject: values.emailSubject,
sendMode,
})
if (res.success) {
message.success(res.message)
if (onSent) onSent()
onClose()
} else {
message.error(res.message)
}
} catch (e) {
if (e.errorFields) return
message.error(e.message || '发送失败')
} finally {
setSending(false)
}
}
if (!script) return null
return (
<Modal
title={
<Space>
<SendOutlined />
<span>手动发送邮件</span>
<Tag color="blue">{script.name}</Tag>
</Space>
}
open={open}
onCancel={onClose}
width={620}
footer={
<Space>
<Button onClick={onClose}>取消</Button>
<Button
type="primary"
icon={<SendOutlined />}
loading={sending}
onClick={handleSend}
disabled={recipients.length === 0}
>
{sendMode === 'batch'
? `群发邮件(${recipients.length}人)`
: `逐个发送(${recipients.length}封)`
}
</Button>
</Space>
}
>
<Alert
type="info"
showIcon
style={{ marginBottom: 16 }}
message="手动发送将执行脚本SQL,生成Excel附件并发送邮件到指定收件人。收件人可与脚本配置的不同。"
/>
{/* 发送模式选择 */}
<div style={{ marginBottom: 16 }}>
<div style={{ marginBottom: 8, fontWeight: 500 }}>发送模式</div>
<Radio.Group value={sendMode} onChange={e => setSendMode(e.target.value)}>
<Radio value="batch">
<Space size={4}>
<TeamOutlined />
<span>群发</span>
</Space>
<span style={{ fontSize: 12, color: '#999', marginLeft: 4 }}>— 一封邮件发送给所有人</span>
</Radio>
<Radio value="individual">
<Space size={4}>
<UserOutlined />
<span>逐个发送</span>
</Space>
<span style={{ fontSize: 12, color: '#999', marginLeft: 4 }}>— 每人单独收到一封邮件</span>
</Radio>
</Radio.Group>
</div>
{/* 收件人管理 */}
<div style={{ marginBottom: 16 }}>
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: 8 }}>
<span style={{ fontWeight: 500 }}>
收件人 {recipients.length > 0 && <Tag color="blue" style={{ marginLeft: 4 }}>{recipients.length}人</Tag>}
</span>
<Space size={4}>
{script.recipientEmails && (
<Tooltip title="从脚本配置的收件人导入">
<Button size="small" type="dashed" icon={<ImportOutlined />} onClick={handleImportFromScript}>
导入预设
</Button>
</Tooltip>
)}
{recipients.length > 0 && (
<Button size="small" type="dashed" danger onClick={handleClearAll}>
清空
</Button>
)}
</Space>
</div>
{/* 逐个添加 */}
<Space.Compact style={{ width: '100%', marginBottom: 8 }}>
<Input
placeholder="输入邮箱地址,回车添加"
value={singleInput}
onChange={e => setSingleInput(e.target.value)}
onPressEnter={handleAddSingle}
prefix={<UserAddOutlined style={{ color: '#bbb' }} />}
/>
<Button type="primary" icon={<PlusOutlined />} onClick={handleAddSingle}>
添加
</Button>
</Space.Compact>
{/* 批量导入切换 */}
{!showBatchInput ? (
<Button
size="small"
type="dashed"
icon={<TeamOutlined />}
onClick={() => setShowBatchInput(true)}
style={{ marginBottom: 8 }}
>
批量导入
</Button>
) : (
<div style={{ marginBottom: 8 }}>
<Input.TextArea
placeholder="输入多个邮箱,用逗号、分号或换行分隔"
value={batchInput}
onChange={e => setBatchInput(e.target.value)}
autoSize={{ minRows: 2, maxRows: 4 }}
/>
<Space style={{ marginTop: 4 }}>
<Button size="small" type="primary" onClick={handleBatchImport}>确认导入</Button>
<Button size="small" onClick={() => { setShowBatchInput(false); setBatchInput('') }}>取消</Button>
</Space>
</div>
)}
{/* 收件人标签列表 */}
{recipients.length > 0 && (
<div style={{
padding: '8px 12px',
background: '#fafafa',
border: '1px solid #f0f0f0',
borderRadius: 6,
maxHeight: 180,
overflowY: 'auto',
}}>
<Space size={[8, 8]} wrap>
{recipients.map((email) => (
<Tag
key={email}
color="blue"
icon={<MailOutlined />}
closable
onClose={() => handleRemoveRecipient(email)}
closeIcon={<CloseOutlined style={{ fontSize: 10 }} />}
>
{email}
</Tag>
))}
</Space>
</div>
)}
</div>
<Form form={form} layout="vertical">
<Form.Item
name="emailSubject"
label="邮件主题"
tooltip="为空则使用脚本配置的主题或自动生成"
>
<Input placeholder="例如:月度终端支付明细报表(自定义)" />
</Form.Item>
<div style={{ padding: '8px 12px', background: '#f6f6f6', borderRadius: 6, fontSize: 12, color: '#888' }}>
<div>邮件将自动附带Excel附件,包含脚本查询结果数据。</div>
{script.purpose && (
<div style={{ marginTop: 4 }}>
<span style={{ color: '#666' }}>脚本用途:</span>{script.purpose}
</div>
)}
</div>
</Form>
</Modal>
)
}