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

195 lines
5.8 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'
2017-07-26 18:24:06 +08:00
import { Button, Input, Select, Card } 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-26 17:53:36 +08:00
const InputGroup = Input.Group;
const Option = Select.Option;
2017-07-26 14:54:14 +08:00
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-26 17:53:36 +08:00
res: '',
2017-07-25 21:24:12 +08:00
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);
2017-07-26 17:53:36 +08:00
const query = {};
2017-07-26 14:54:14 +08:00
if (method === 'GET') {
Object.keys(reqParams).forEach(key => {
const value = typeof reqParams[key] === 'object' ? JSON.stringify(reqParams) : reqParams.toString();
query[key] = value;
})
}
2017-07-26 17:53:36 +08:00
const headers = {}
2017-07-26 14:54:14 +08:00
seqGroup.forEach((headerItem) => {
2017-07-26 17:53:36 +08:00
if (headerItem.name) {
headers[headerItem.name] = headerItem.value;
}
2017-07-26 14:54:14 +08:00
})
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) => {
console.log(header)
2017-07-26 17:22:12 +08:00
this.setState({res})
2017-07-25 13:34:48 +08:00
}
})
}
render () {
2017-07-25 21:24:12 +08:00
const { method, url, seqGroup, interfaceName, interfaceProject } = this.props;
2017-07-26 17:53:36 +08:00
const { prd_host, basepath, protocol, env } = interfaceProject;
2017-07-25 21:24:12 +08:00
const reqParams = JSON.parse(this.props.reqParams);
2017-07-26 17:53:36 +08:00
const pathname = (basepath + url).replace(/\/+/g, '/');
2017-07-25 21:24:12 +08:00
2017-07-26 17:53:36 +08:00
const domains = [{name: 'prd', domain: protocol + '://' + prd_host}];
env.forEach(item => {
domains.push({name: item.name, domain: item.domain});
})
const query = {};
2017-07-25 21:24:12 +08:00
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;
})
}
2017-07-26 18:24:06 +08:00
const headers = {}
seqGroup.forEach((headerItem) => {
if (headerItem.name) {
headers[headerItem.name] = headerItem.value;
}
})
2017-07-26 17:53:36 +08:00
const search = URL.format({
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">
2017-07-26 17:53:36 +08:00
<div className="req-row href">
<InputGroup compact style={{display: 'inline-block', width: 680}}>
<Input value={method} disabled style={{display: 'inline-block', width: 80}} />
<Select defaultValue="prd" style={{display: 'inline-block', width: 300}}>
{
domains.map((item, index) => (<Option value={item.name} key={index}>{item.domain}</Option>))
}
</Select>
<Input value={pathname+search} style={{display: 'inline-block', width: 300}} />
</InputGroup>
<Button onClick={this.testInterface} type="primary" style={{marginLeft: 10}}>发送</Button>
2017-07-26 14:54:14 +08:00
</div>
2017-07-26 18:24:06 +08:00
<Card noHovering style={{marginTop: 10}} className={Object.keys(headers).length ? '' : 'hidden'}>
<div className="req-row headers">
HEADERS
{
Object.keys(headers).map((key, index) => {
return (
<div key={index}>
<Input disabled value={key} style={{display: 'inline-block', width: 200, margin: 10}} />{' = '}
<Input value={headers[key]} style={{display: 'inline-block', width: 200, margin: 10}} />
</div>
)
})
}
</div>
</Card>
<Card noHovering style={{marginTop: 10}} className={Object.keys(reqParams).length ? '' : 'hidden'}>
<div className="req-row params">
请求参数
{
Object.keys(reqParams).map((key, index) => {
const value = typeof reqParams[key] === 'object' ? JSON.stringify(reqParams[key]) : reqParams[key].toString();
return (
<div key={index}>
<Input disabled value={key} style={{display: 'inline-block', width: 200, margin: 10}} />{' = '}
<Input value={value} style={{display: 'inline-block', width: 200, margin: 10}} />
</div>
)
})
}
</div>
</Card>
2017-07-25 21:24:12 +08:00
</div>
2017-07-26 18:24:06 +08:00
<Card noHovering style={{marginTop: 10}}>
<div className="res-part">
返回结果
<div>
<TextArea
value={this.state.res ? JSON.stringify(this.state.res, 2) : ''}
style={{margin: 10}}
autosize={{ minRows: 2, maxRows: 6 }}
></TextArea>
</div>
2017-07-26 17:53:36 +08:00
</div>
2017-07-26 18:24:06 +08:00
</Card>
2017-07-25 13:34:48 +08:00
</div>
)
}
}