/** * Copyright (c) OpenSpug Organization. https://github.com/openspug/spug * Copyright (c) * Released under the AGPL-3.0 License. */ import React from 'react'; import { observer } from 'mobx-react'; import { Modal, Form, Input, Select, Col, Button, Upload, message } from 'antd'; import { http, X_TOKEN } from 'libs'; import store from './store'; @observer class ComForm extends React.Component { constructor(props) { super(props); this.state = { loading: false, uploading: false, password: null, addZone: null, fileList: [], editZone: store.record.zone, } } componentDidMount() { if (store.record.pkey) { this.setState({ fileList: [{uid: '0', name: '独立密钥', data: store.record.pkey}] }) } } handleSubmit = () => { this.setState({loading: true}); const formData = this.props.form.getFieldsValue(); formData['id'] = store.record.id; const file = this.state.fileList[0]; if (file && file.data) formData['pkey'] = file.data; http.post('/api/host/', formData) .then(res => { if (res === 'auth fail') { if (formData.pkey) { this.setState({loading: false}); message.error('独立密钥认证失败') } else { this.setState({loading: false}); Modal.confirm({ icon: 'exclamation-circle', title: '首次验证请输入密码', content: this.confirmForm(formData.username), onOk: () => this.handleConfirm(formData), }) } } else { message.success('操作成功'); store.formVisible = false; store.fetchRecords() } }, () => this.setState({loading: false})) }; handleConfirm = (formData) => { if (this.state.password) { formData['password'] = this.state.password; return http.post('/api/host/', formData).then(res => { message.success('验证成功'); store.formVisible = false; store.fetchRecords() }) } message.error('请输入授权密码') }; confirmForm = (username) => { return (
this.setState({password: val.target.value})}/>
) }; handleAddZone = () => { this.setState({zone: ''}, () => { Modal.confirm({ icon: 'exclamation-circle', title: '添加主机类别', content: (
this.setState({addZone: e.target.value})}/>
), onOk: () => { if (this.state.addZone) { store.zones.push(this.state.addZone); this.props.form.setFieldsValue({'zone': this.state.addZone}) } }, }) }); }; handleEditZone = () => { this.setState({zone: store.record.zone}, () => { Modal.confirm({ icon: 'exclamation-circle', title: '编辑主机类别', content: (
this.setState({editZone: e.target.value})}/>
), onOk: () => http.patch('/api/host/', {id: store.record.id, zone: this.state.editZone}) .then(res => { message.success(`成功修改${res}条记录`); store.fetchRecords(); this.props.form.setFieldsValue({'zone': this.state.editZone}) }) }) }); }; handleUploadChange = (v) => { if (v.fileList.length === 0) { this.setState({fileList: []}) } }; handleUpload = (file, fileList) => { this.setState({uploading: true}); const formData = new FormData(); formData.append('file', file); http.post('/api/host/parse/', formData) .then(res => { file.data = res; this.setState({fileList: [file]}) }) .finally(() => this.setState({uploading: false})) return false }; render() { const info = store.record; const {fileList, loading, uploading} = this.state; const {getFieldDecorator} = this.props.form; return ( store.formVisible = false} confirmLoading={loading} onOk={this.handleSubmit}>
{getFieldDecorator('zone', {initialValue: info['zone']})( )} {getFieldDecorator('name', {initialValue: info['name']})( )} {getFieldDecorator('username', {initialValue: info['username']})( )} {getFieldDecorator('hostname', {initialValue: info['hostname']})( )} {getFieldDecorator('port', {initialValue: info['port']})( )} {fileList.length === 0 ? : null} {getFieldDecorator('desc', {initialValue: info['desc']})( )} ⚠️ 首次验证时需要输入登录用户名对应的密码,但不会存储该密码。
) } } export default Form.create()(ComForm)