yapi/client/containers/AddInterface/InterfaceTest/InterfaceTest.js

164 lines
4.6 KiB
JavaScript
Raw Normal View History

2017-07-25 13:34:48 +08:00
import React, { Component } from 'react'
2017-07-25 21:24:12 +08:00
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import { Button, Input } from 'antd'
2017-07-25 13:34:48 +08:00
import { autobind } from 'core-decorators';
import crossRequest from 'cross-request';
2017-07-25 21:24:12 +08:00
import { withRouter } from 'react-router';
import URL from 'url';
2017-07-25 13:34:48 +08:00
import {
} from '../../../actions/group.js'
import './InterfaceTest.scss'
2017-07-26 14:54:14 +08:00
const { TextArea } = Input;
2017-07-25 21:24:12 +08:00
@connect(
state => ({
reqParams: state.addInterface.reqParams,
method: state.addInterface.method,
url: state.addInterface.url,
seqGroup: state.addInterface.seqGroup,
interfaceName: state.addInterface.interfaceName,
interfaceProject: state.addInterface.project
}),
{
}
)
@withRouter
2017-07-25 13:34:48 +08:00
export default class InterfaceTest extends Component {
static propTypes = {
2017-07-25 21:24:12 +08:00
reqParams: PropTypes.string,
method: PropTypes.string,
url: PropTypes.string,
interfaceName: PropTypes.string,
seqGroup: PropTypes.array,
match: PropTypes.object,
interfaceProject: PropTypes.object
2017-07-25 13:34:48 +08:00
}
state = {
2017-07-25 21:24:12 +08:00
res: {},
header: {}
2017-07-25 13:34:48 +08:00
}
constructor(props) {
super(props)
}
2017-07-25 21:24:12 +08:00
componentWillMount() {
}
2017-07-25 13:34:48 +08:00
@autobind
testInterface() {
2017-07-26 14:54:14 +08:00
const { method, url, seqGroup, interfaceProject } = this.props;
const { prd_host, basepath, protocol } = interfaceProject;
const reqParams = JSON.parse(this.props.reqParams);
const headers = {}
let query = {};
if (method === 'GET') {
Object.keys(reqParams).forEach(key => {
const value = typeof reqParams[key] === 'object' ? JSON.stringify(reqParams) : reqParams.toString();
query[key] = value;
})
}
seqGroup.forEach((headerItem) => {
headers[headerItem.name] = headerItem.value;
})
const href = URL.format({
protocol: protocol || 'http',
host: prd_host,
pathname: (basepath + url).replace(/\/+/g, '/'),
query
});
2017-07-25 13:34:48 +08:00
crossRequest({
2017-07-26 14:54:14 +08:00
url: href,
method,
headers,
2017-07-25 13:34:48 +08:00
data: {
a:1
},
success: (res, header) => {
this.setState({res})
console.log(header)
}
})
}
render () {
2017-07-25 21:24:12 +08:00
const { method, url, seqGroup, interfaceName, interfaceProject } = this.props;
const { prd_host, basepath, protocol } = interfaceProject;
const reqParams = JSON.parse(this.props.reqParams);
let query = {};
if (method === 'GET') {
Object.keys(reqParams).forEach(key => {
2017-07-26 14:54:14 +08:00
const value = typeof reqParams[key] === 'object' ? JSON.stringify(reqParams[key]) : reqParams[key].toString();
2017-07-25 21:24:12 +08:00
query[key] = value;
})
}
const href = URL.format({
protocol: protocol || 'http',
host: prd_host,
2017-07-26 14:54:14 +08:00
pathname: (basepath + url).replace(/\/+/g, '/'),
2017-07-25 21:24:12 +08:00
query
});
2017-07-25 13:34:48 +08:00
return (
2017-07-26 14:54:14 +08:00
<div className="interface-test">
<div className="interface-name">{interfaceName}</div>
<div className="req-part">
<div className="req-row method">
METHOD<Input value={method} disabled style={{display: 'inline-block', width: 200}} />
</div>
<div className="req-row url">
URL<Input value={href} style={{display: 'inline-block', width: 800, margin: 10}} />
<Button onClick={this.testInterface} type="primary">发送</Button>
</div>
<div className="req-row headers">
HEADERS
{
seqGroup.map((headerItem, index) => {
return (
<div key={index}>
<Input disabled value={headerItem.name} style={{display: 'inline-block', width: 200, margin: 10}} />{' = '}
<Input value={headerItem.value} style={{display: 'inline-block', width: 200, margin: 10}} />
</div>
)
})
}
</div>
<div className="req-row params">
请求参数
2017-07-25 21:24:12 +08:00
{
Object.keys(reqParams).map((key, index) => {
2017-07-26 14:54:14 +08:00
const value = typeof reqParams[key] === 'object' ? JSON.stringify(reqParams[key]) : reqParams[key].toString();
2017-07-25 21:24:12 +08:00
return (
<div key={index}>
2017-07-26 14:54:14 +08:00
<Input disabled value={key} style={{display: 'inline-block', width: 200, margin: 10}} />{' = '}
<Input value={value} style={{display: 'inline-block', width: 200, margin: 10}} />
2017-07-25 21:24:12 +08:00
</div>
)
})
}
</div>
</div>
2017-07-26 14:54:14 +08:00
<div className="res-part">
2017-07-26 10:26:15 +08:00
返回结果
2017-07-26 14:54:14 +08:00
<TextArea value={JSON.stringify(this.state.res, 2)}></TextArea>
2017-07-25 13:34:48 +08:00
</div>
</div>
)
}
}