SysDataSourceModal.vue
10.3 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
<template>
<a-modal
:title="title"
:width="800"
:visible="visible"
:confirmLoading="confirmLoading"
@ok="handleOk"
@cancel="handleCancel"
cancelText="close">
<a-spin :spinning="confirmLoading">
<a-form :form="form">
<a-form-item
:labelCol="labelCol"
:wrapperCol="wrapperCol"
label="数据源编码">
<a-input placeholder="请输入数据源编码" :disabled="!!model.id" v-decorator="['code', validatorRules.code]"/>
</a-form-item>
<a-form-item
:labelCol="labelCol"
:wrapperCol="wrapperCol"
label="数据源名称">
<a-input placeholder="请输入数据源名称" v-decorator="['name', validatorRules.name]"/>
</a-form-item>
<a-form-item
:labelCol="labelCol"
:wrapperCol="wrapperCol"
label="数据库类型">
<j-dict-select-tag placeholder="请选择数据库类型" dict-code="database_type" triggerChange
v-decorator="['dbType', validatorRules.dbType]" @change="handleDbTypeChange"/>
</a-form-item>
<a-form-item
:labelCol="labelCol"
:wrapperCol="wrapperCol"
label="驱动类">
<a-input placeholder="请输入驱动类" v-decorator="['dbDriver', validatorRules.dbDriver]"/>
</a-form-item>
<a-form-item
:labelCol="labelCol"
:wrapperCol="wrapperCol"
label="数据源地址">
<a-input placeholder="请输入数据源地址" v-decorator="['dbUrl', validatorRules.dbUrl]"/>
</a-form-item>
<!-- <a-form-item
:labelCol="labelCol"
:wrapperCol="wrapperCol"
label="数据库名称">
<a-input placeholder="请输入数据库名称" v-decorator="['dbName', validatorRules.dbName]"/>
</a-form-item>-->
<a-form-item
:labelCol="labelCol"
:wrapperCol="wrapperCol"
label="用户名">
<a-input placeholder="请输入用户名" v-decorator="['dbUsername', validatorRules.dbUsername]"/>
</a-form-item>
<a-form-item
:labelCol="labelCol"
:wrapperCol="wrapperCol"
label="密码">
<a-row :gutter="8">
<a-col :span="21">
<a-input-password placeholder="请输入密码" v-decorator="['dbPassword', validatorRules.dbPassword]"/>
</a-col>
<a-col :span="3">
<a-button type="primary" size="small" style="width: 100%" @click="handleTest">测试</a-button>
</a-col>
</a-row>
</a-form-item>
<a-form-item
:labelCol="labelCol"
:wrapperCol="wrapperCol"
label="备注">
<a-textarea placeholder="请输入备注" v-decorator="['remark', {}]"/>
</a-form-item>
</a-form>
</a-spin>
</a-modal>
</template>
<script>
import pick from 'lodash.pick'
import {httpAction, postAction} from '@/api/manage'
import {validateDuplicateValue} from '@/utils/util'
export default {
name: 'SysDataSourceModal',
components: {},
data() {
return {
title: '操作',
visible: false,
model: {},
labelCol: {
xs: {span: 24},
sm: {span: 5},
},
wrapperCol: {
xs: {span: 24},
sm: {span: 16},
},
confirmLoading: false,
form: this.$form.createForm(this),
validatorRules: {
code: {
validateFirst: true,
rules: [
{required: true, message: '请输入数据源编码!'},
{
validator: (rule, value, callback) => {
let pattern = /^[a-z|A-Z][a-z|A-Z\d_-]{0,}$/
if (!pattern.test(value)) {
callback('编码必须以字母开头,可包含数字、下划线、横杠')
} else {
validateDuplicateValue('sys_data_source', 'code', value, this.model.id, callback)
}
}
}
]
},
name: {rules: [{required: true, message: '请输入数据源名称!'}]},
dbType: {rules: [{required: true, message: '请选择数据库类型!'}]},
dbDriver: {rules: [{required: true, message: '请输入驱动类!'}]},
dbUrl: {rules: [{required: true, message: '请输入数据源地址!'}]},
dbName: {rules: [{required: true, message: '请输入数据库名称!'}]},
dbUsername: {rules: [{required: true, message: '请输入用户名!'}]},
dbPassword: {rules: [{required: false, message: '请输入密码!'}]}
},
url: {
add: '/sys/dataSource/add',
edit: '/sys/dataSource/edit',
},
dbDriverMap: {
// MySQL 数据库
'1': {dbDriver: 'com.mysql.jdbc.Driver'},
//MySQL5.7+ 数据库
'4': {dbDriver: 'com.mysql.cj.jdbc.Driver'},
// Oracle
'2': {dbDriver: 'oracle.jdbc.OracleDriver'},
// SQLServer 数据库
'3': {dbDriver: 'com.microsoft.sqlserver.jdbc.SQLServerDriver'},
// marialDB 数据库
'5': {dbDriver: 'org.mariadb.jdbc.Driver'},
// postgresql 数据库
'6': {dbDriver: 'org.postgresql.Driver'},
// 达梦 数据库
'7': {dbDriver: 'dm.jdbc.driver.DmDriver'},
// 人大金仓 数据库
'8': {dbDriver: 'com.kingbase8.Driver'},
// 神通 数据库
'9': {dbDriver: 'com.oscar.Driver'},
// SQLite 数据库
'10': {dbDriver: 'org.sqlite.JDBC'},
// DB2 数据库
'11': {dbDriver: 'com.ibm.db2.jcc.DB2Driver'},
// Hsqldb 数据库
'12': {dbDriver: 'org.hsqldb.jdbc.JDBCDriver'},
// Derby 数据库
'13': {dbDriver: 'org.apache.derby.jdbc.ClientDriver'},
// H2 数据库
'14': {dbDriver: 'org.h2.Driver'},
// 其他数据库
'15': {dbDriver: ''}
},
dbUrlMap: {
// MySQL 数据库
'1': {dbUrl: 'jdbc:mysql://127.0.0.1:3306/jeecg-boot?characterEncoding=UTF-8&useUnicode=true&useSSL=false'},
//MySQL5.7+ 数据库
'4': {dbUrl: 'jdbc:mysql://127.0.0.1:3306/jeecg-boot?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai'},
// Oracle
'2': {dbUrl: 'jdbc:oracle:thin:@127.0.0.1:1521:ORCL'},
// SQLServer 数据库
'3': {dbUrl: 'jdbc:sqlserver://127.0.0.1:1433;SelectMethod=cursor;DatabaseName=jeecgboot'},
// Mariadb 数据库
'5': {dbUrl: 'jdbc:mariadb://127.0.0.1:3306/jeecg-boot?characterEncoding=UTF-8&useSSL=false'},
// Postgresql 数据库
'6': {dbUrl: 'jdbc:postgresql://127.0.0.1:5432/jeecg-boot'},
// 达梦 数据库
'7': {dbUrl: 'jdbc:dm://127.0.0.1:5236/?jeecg-boot&zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8'},
// 人大金仓 数据库
'8': {dbUrl: 'jdbc:kingbase8://127.0.0.1:54321/jeecg-boot'},
// 神通 数据库
'9': {dbUrl: 'jdbc:oscar://192.168.1.125:2003/jeecg-boot'},
// SQLite 数据库
'10': {dbUrl: 'jdbc:sqlite://opt/test.db'},
// DB2 数据库
'11': {dbUrl: 'jdbc:db2://127.0.0.1:50000/jeecg-boot'},
// Hsqldb 数据库
'12': {dbUrl: 'jdbc:hsqldb:hsql://127.0.0.1/jeecg-boot'},
// Derby 数据库
'13': {dbUrl: 'jdbc:derby://127.0.0.1:1527/jeecg-boot'},
// H2 数据库
'14': {dbUrl: 'jdbc:h2:tcp://127.0.0.1:8082/jeecg-boot'},
// 其他数据库
'15': {dbUrl: ''}
}
}
},
created() {
},
methods: {
add() {
this.edit({})
},
edit(record) {
this.form.resetFields()
this.model = Object.assign({}, record)
this.visible = true
this.$nextTick(() => {
this.form.setFieldsValue(pick(this.model, 'code', 'name', 'remark', 'dbType', 'dbDriver', 'dbUrl', 'dbName', 'dbUsername', 'dbPassword'))
})
},
close() {
this.$emit('close')
this.visible = false
},
handleOk() {
// 触发表单验证
this.form.validateFields((err, values) => {
if (!err) {
this.confirmLoading = true
let formData = Object.assign(this.model, values)
let httpUrl = this.url.add, method = 'post'
if (this.model.id) {
httpUrl = this.url.edit
method = 'put'
// 由于编码的特殊性,所以不能更改
formData['code'] = undefined
}
httpAction(httpUrl, formData, method).then((res) => {
if (res.success) {
this.$message.success(res.message)
this.$emit('ok')
this.close()
} else {
this.$message.warning(res.message)
}
}).finally(() => {
this.confirmLoading = false
})
}
})
},
handleCancel() {
this.close()
},
// 测试数据源配置是否可以正常连接
handleTest() {
let keys = ['dbType', 'dbDriver', 'dbUrl', 'dbName', 'dbUsername', 'dbPassword']
// 获取以上字段的值,并清除校验状态
let fieldsValues = this.form.getFieldsValue(keys)
let setFields = {}
keys.forEach(key => setFields[key] = {value: fieldsValues[key], errors: null})
// 清除校验状态,目的是可以让错误文字闪烁
this.form.setFields(setFields)
// 重新校验
this.$nextTick(() => {
this.form.validateFields(keys, (errors, values) => {
if (!errors) {
let loading = this.$message.loading('连接中……', 0)
postAction('/online/cgreport/api/testConnection', fieldsValues).then(res => {
if (res.success) {
this.$message.success('连接成功')
} else throw new Error(res.message)
}).catch(error => {
this.$warning({title: '连接失败', content: error.message || error})
}).finally(() => loading())
}
})
})
},
// 数据库类型更改时,联动更改数据库驱动
handleDbTypeChange(val) {
let dbDriver = this.dbDriverMap[val]
let dbUrl = this.dbUrlMap[val]
if (dbDriver) {
this.form.setFieldsValue(dbDriver)
}
if (dbUrl) {
this.form.setFieldsValue(dbUrl)
}
},
}
}
</script>
<style lang="less" scoped></style>