import React, { PureComponent as Component } from 'react'; import { Upload, Icon, message, Select, Tooltip, Button, Spin, Switch, Modal, Radio, Input, Checkbox } from 'antd'; import PropTypes from 'prop-types'; import { connect } from 'react-redux'; import './ProjectData.scss'; import axios from 'axios'; import URL from 'url'; const Dragger = Upload.Dragger; import { saveImportData } from '../../../../reducer/modules/interface'; import { fetchUpdateLogData } from '../../../../reducer/modules/news.js'; import { handleSwaggerUrlData } from '../../../../reducer/modules/project'; const Option = Select.Option; const confirm = Modal.confirm; const plugin = require('client/plugin.js'); const RadioGroup = Radio.Group; const importDataModule = {}; const exportDataModule = {}; const HandleImportData = require('common/HandleImportData'); function handleExportRouteParams(url, status, isWiki) { if (!url) { return; } let urlObj = URL.parse(url, true), query = {}; query = Object.assign(query, urlObj.query, { status, isWiki }); return URL.format({ pathname: urlObj.pathname, query }); } // exportDataModule.pdf = { // name: 'Pdf', // route: '/api/interface/download_crx', // desc: '导出项目接口文档为 pdf 文件' // } @connect( state => { return { curCatid: -(-state.inter.curdata.catid), basePath: state.project.currProject.basepath, updateLogList: state.news.updateLogList, swaggerUrlData: state.project.swaggerUrlData }; }, { saveImportData, fetchUpdateLogData, handleSwaggerUrlData } ) class ProjectData extends Component { constructor(props) { super(props); this.state = { selectCatid: '', menuList: [], curImportType: 'swagger', curExportType: null, showLoading: false, dataSync: 'merge', exportContent: 'all', isSwaggerUrl: false, swaggerUrl: '', isWiki: false }; } static propTypes = { match: PropTypes.object, curCatid: PropTypes.number, basePath: PropTypes.string, saveImportData: PropTypes.func, fetchUpdateLogData: PropTypes.func, updateLogList: PropTypes.array, handleSwaggerUrlData: PropTypes.func, swaggerUrlData: PropTypes.string }; UNSAFE_componentWillMount() { axios.get(`/api/interface/getCatMenu?project_id=${this.props.match.params.id}`).then(data => { if (data.data.errcode === 0) { let menuList = data.data.data; this.setState({ menuList: menuList, selectCatid: menuList[0]._id }); } }); plugin.emitHook('import_data', importDataModule); plugin.emitHook('export_data', exportDataModule, this.props.match.params.id); } selectChange(value) { this.setState({ selectCatid: +value }); } uploadChange = info => { const status = info.file.status; if (status !== 'uploading') { console.log(info.file, info.fileList); } if (status === 'done') { message.success(`${info.file.name} 文件上传成功`); } else if (status === 'error') { message.error(`${info.file.name} 文件上传失败`); } }; handleAddInterface = async res => { return await HandleImportData( res, this.props.match.params.id, this.state.selectCatid, this.state.menuList, this.props.basePath, this.state.dataSync, message.error, message.success, () => this.setState({ showLoading: false }) ); }; // 本地文件上传 handleFile = info => { if (!this.state.curImportType) { return message.error('请选择导入数据的方式'); } if (this.state.selectCatid) { this.setState({ showLoading: true }); let reader = new FileReader(); reader.readAsText(info.file); reader.onload = async res => { res = await importDataModule[this.state.curImportType].run(res.target.result); if (this.state.dataSync === 'merge') { // 开启同步 this.showConfirm(res); } else { // 未开启同步 await this.handleAddInterface(res); } }; } else { message.error('请选择上传的默认分类'); } }; showConfirm = async res => { let that = this; let typeid = this.props.match.params.id; let apiCollections = res.apis.map(item => { return { method: item.method, path: item.path }; }); let result = await this.props.fetchUpdateLogData({ type: 'project', typeid, apis: apiCollections }); let domainData = result.payload.data.data; const ref = confirm({ title: '您确认要进行数据同步????', width: 600, okType: 'danger', iconType: 'exclamation-circle', className: 'dataImport-confirm', okText: '确认', cancelText: '取消', content: (
温馨提示: 数据同步后,可能会造成原本的修改数据丢失
不导入已存在的接口
已存在的接口,将合并返回数据的 response,适用于导入了 swagger 数据,保留对数据结构的改动
不保留旧数据,完全使用新数据,适用于接口定义完全交给后端定义
点击或者拖拽文件到上传区域
{ e.stopPropagation(); }} dangerouslySetInnerHTML={{ __html: this.state.curImportType ? importDataModule[this.state.curImportType].desc : null }} />