/** * KRA - Database Init Page * Database initialization wizard */ import React, { useState } from 'react'; import { Form, Input, Select, Button, message, Modal, Spin } from 'antd'; import { history, Helmet } from '@umijs/max'; import { initDB } from '@/services/kratos/initdb'; import { createStyles } from 'antd-style'; type DBType = 'mysql' | 'pgsql' | 'oracle' | 'mssql' | 'sqlite'; interface InitFormData { adminPassword: string; dbType: DBType; host: string; port: string; userName: string; password: string; dbName: string; dbPath?: string; template?: string; } const dbDefaults: Record> = { mysql: { host: '127.0.0.1', port: '3306', userName: 'root', dbName: 'kra', }, pgsql: { host: '127.0.0.1', port: '5432', userName: 'postgres', dbName: 'kra', template: 'template0', }, oracle: { host: '127.0.0.1', port: '1521', userName: 'oracle', dbName: 'kra', }, mssql: { host: '127.0.0.1', port: '1433', userName: 'mssql', dbName: 'kra', }, sqlite: { host: '', port: '', userName: '', dbName: 'kra', dbPath: '', }, }; const useStyles = createStyles(({ token }) => ({ container: { width: '100%', height: '100vh', display: 'flex', position: 'relative', overflow: 'hidden', }, leftSection: { flex: 1, display: 'flex', alignItems: 'center', justifyContent: 'center', background: '#fff', position: 'relative', zIndex: 10, }, rightSection: { width: '50%', height: '100%', background: '#194bfb', display: 'none', '@media (min-width: 768px)': { display: 'block', }, }, rightBanner: { width: '100%', height: '100%', objectFit: 'cover', }, oblique: { position: 'absolute', height: '130%', width: '60%', background: '#fff', transform: 'rotate(-12deg)', marginLeft: '-320px', zIndex: 5, }, content: { zIndex: 999, maxWidth: '500px', padding: '24px', }, readmeSection: { animation: 'slideInFwdTop 0.4s cubic-bezier(0.25, 0.46, 0.45, 0.94) both', }, formSection: { width: '384px', animation: 'slideInLeft 0.5s cubic-bezier(0.25, 0.46, 0.45, 0.94) both', }, title: { fontSize: '32px', fontWeight: 700, textAlign: 'center' as const, marginBottom: '16px', color: token.colorText, }, subtitle: { fontSize: '16px', color: token.colorTextSecondary, marginBottom: '8px', }, notice: { fontSize: '14px', color: token.colorTextSecondary, marginBottom: '8px', lineHeight: 1.8, }, link: { color: '#1890ff', fontWeight: 600, }, highlight: { color: '#ff4d4f', fontWeight: 700, fontSize: '24px', marginLeft: '8px', }, buttonGroup: { display: 'flex', justifyContent: 'space-between', marginTop: '32px', }, formItem: { marginBottom: '16px', }, })); const InitPage: React.FC = () => { const [form] = Form.useForm(); const [showForm, setShowForm] = useState(false); const [loading, setLoading] = useState(false); const { styles } = useStyles(); const handleShowForm = () => { setShowForm(true); }; const handleGoDoc = () => { window.open('https://go-kratos.dev/docs/getting-started/start', '_blank'); }; const handleDBTypeChange = (value: DBType) => { const defaults = dbDefaults[value]; form.setFieldsValue({ ...defaults, password: '', adminPassword: form.getFieldValue('adminPassword'), }); }; const handleSubmit = async (values: InitFormData) => { if (values.adminPassword.length < 6) { message.error('密码长度不能小于6位'); return; } setLoading(true); try { const res = await initDB(values); if (res.code === 0) { message.success(res.msg || '初始化成功'); Modal.confirm({ title: '配置完成', content: '已经完成基础数据库初始化!建议先阅读官方文档,以获得更好的开发体验。', okText: '查看文档', cancelText: '稍后查看', centered: true, onOk: () => { window.open('https://go-kratos.dev/docs/', '_blank'); history.push('/user/login'); }, onCancel: () => { history.push('/user/login'); }, }); } else { message.error(res.msg || '初始化失败'); } } catch (error: any) { message.error(error?.message || '初始化失败'); } finally { setLoading(false); } }; return (
初始化 - Kratos Admin
{!showForm ? (

KRATOS-ADMIN

初始化须知

1. 您需有一定的 React 和 Golang 基础

2. 请您确认是否已经阅读过 官方文档

3. 请您确认是否了解后续的配置流程

4. 如果您使用 MySQL 数据库,请确认数据库引擎为 InnoDB

注:开发组不为文档中书写过的内容提供无偿服务

) : (
prev.dbType !== cur.dbType}> {({ getFieldValue }) => { const dbType = getFieldValue('dbType'); if (dbType === 'sqlite') return null; return ( <> ); }} prev.dbType !== cur.dbType}> {({ getFieldValue }) => { const dbType = getFieldValue('dbType'); if (dbType === 'sqlite') { return ( ); } if (dbType === 'pgsql') { return ( ); } return null; }}
)}
banner
); }; export default InitPage;