ConnectionModal.jsx
5.23 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
import React, { useState, useEffect } from 'react'
import { Modal, Form, Input, Select, Button, message, Space, List, Tag, Popconfirm } from 'antd'
import { PlusOutlined, DeleteOutlined, LinkOutlined, DisconnectOutlined } from '@ant-design/icons'
import request from '../utils/request'
export default function ConnectionModal({ open, onClose, onConnect, activeConnId }) {
const [form] = Form.useForm()
const [connections, setConnections] = useState([])
const [loading, setLoading] = useState(false)
const [testLoading, setTestLoading] = useState(false)
const [showForm, setShowForm] = useState(false)
useEffect(() => {
if (open) loadConnections()
}, [open])
const loadConnections = async () => {
const res = await request.get('/connection')
if (res.success) setConnections(res.data)
}
const handleTest = async () => {
try {
const values = await form.validateFields()
setTestLoading(true)
const res = await request.post('/connection/test', values)
if (res.success) {
message.success('连接测试成功')
} else {
message.error(`连接测试失败: ${res.message}`)
}
} catch {
// 表单验证失败
} finally {
setTestLoading(false)
}
}
const handleSave = async () => {
try {
const values = await form.validateFields()
setLoading(true)
const res = await request.post('/connection', values)
if (res.success) {
message.success('连接保存成功')
form.resetFields()
setShowForm(false)
loadConnections()
} else {
message.error(res.message)
}
} catch {
// 表单验证失败
} finally {
setLoading(false)
}
}
const handleDelete = async (id) => {
await request.delete(`/connection/${id}`)
message.success('已删除')
loadConnections()
}
const handleConnect = (conn) => {
onConnect(conn)
onClose()
}
return (
<Modal
title="数据库连接管理"
open={open}
onCancel={onClose}
footer={null}
width={640}
>
<div style={{ marginBottom: 16 }}>
<Button
type="primary"
icon={<PlusOutlined />}
onClick={() => setShowForm(!showForm)}
>
{showForm ? '收起表单' : '新增连接'}
</Button>
</div>
{showForm && (
<Form form={form} layout="vertical" style={{ marginBottom: 24, padding: 16, background: '#fafafa', borderRadius: 8 }}>
<Form.Item name="name" label="连接名称" rules={[{ required: true, message: '请输入连接名称' }]}>
<Input placeholder="例如:生产环境" />
</Form.Item>
<div style={{ display: 'flex', gap: 12 }}>
<Form.Item name="host" label="主机地址" rules={[{ required: true }]} style={{ flex: 2 }}>
<Input placeholder="192.168.1.100" />
</Form.Item>
<Form.Item name="port" label="端口" initialValue="3306" style={{ flex: 1 }}>
<Input placeholder="3306" />
</Form.Item>
</div>
<div style={{ display: 'flex', gap: 12 }}>
<Form.Item name="user" label="用户名" rules={[{ required: true }]} style={{ flex: 1 }}>
<Input placeholder="root" />
</Form.Item>
<Form.Item name="password" label="密码" style={{ flex: 1 }}>
<Input.Password placeholder="密码" />
</Form.Item>
</div>
<Form.Item name="database" label="数据库名" rules={[{ required: true }]}>
<Input placeholder="saas_project_v2" />
</Form.Item>
<Form.Item>
<Space>
<Button onClick={handleTest} loading={testLoading} icon={<LinkOutlined />}>
测试连接
</Button>
<Button type="primary" onClick={handleSave} loading={loading}>
保存连接
</Button>
</Space>
</Form.Item>
</Form>
)}
<List
dataSource={connections}
locale={{ emptyText: '暂无连接配置,请点击上方按钮新增' }}
renderItem={item => (
<List.Item
actions={[
<Button
type="link"
size="small"
icon={<LinkOutlined />}
onClick={() => handleConnect(item)}
disabled={activeConnId === item.id}
>
{activeConnId === item.id ? '已连接' : '连接'}
</Button>,
<Popconfirm title="确定删除该连接?" onConfirm={() => handleDelete(item.id)}>
<Button type="link" size="small" danger icon={<DeleteOutlined />}>
删除
</Button>
</Popconfirm>,
]}
>
<List.Item.Meta
title={
<span>
{item.name}
{activeConnId === item.id && (
<Tag color="green" style={{ marginLeft: 8 }}>当前连接</Tag>
)}
</span>
}
description={`${item.host}:${item.port} / ${item.database} (${item.user})`}
/>
</List.Item>
)}
/>
</Modal>
)
}