From 2cfc9b4f76b05ad43f390deeb3d1c1395957e4bd Mon Sep 17 00:00:00 2001 From: zwjamnsss Date: Wed, 26 Jul 2017 22:01:30 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=AF=B7=E6=B1=82=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../InterfaceTest/InterfaceTest.js | 166 ++++++++++++------ .../InterfaceTable/InterfaceTable.js | 2 - 2 files changed, 116 insertions(+), 52 deletions(-) diff --git a/client/containers/AddInterface/InterfaceTest/InterfaceTest.js b/client/containers/AddInterface/InterfaceTest/InterfaceTest.js index b79fb77a..8a6f8df1 100644 --- a/client/containers/AddInterface/InterfaceTest/InterfaceTest.js +++ b/client/containers/AddInterface/InterfaceTest/InterfaceTest.js @@ -43,7 +43,15 @@ export default class InterfaceTest extends Component { state = { res: '', - header: {} + method: 'GET', + domains: [], + pathname: '', + query: {}, + params: {}, + paramsNotJson: false, + headers: {}, + search: '', + currDomain: '' } constructor(props) { @@ -51,21 +59,42 @@ export default class InterfaceTest extends Component { } componentWillMount() { + this.interfacePropsToState() + } + componentWillReceiveProps(nextProps) { + this.interfacePropsToState(nextProps) } @autobind - testInterface() { - const { method, url, seqGroup, interfaceProject } = this.props; - const { prd_host, basepath, protocol } = interfaceProject; - const reqParams = JSON.parse(this.props.reqParams); + interfacePropsToState(nextProps) { + const props = nextProps || this.props; + const { method, url, seqGroup, interfaceProject } = props; + const { prd_host, basepath, protocol, env } = interfaceProject; + const pathname = (basepath + url).replace(/\/+/g, '/'); + + const domains = {prd: protocol + '://' + prd_host}; + env.forEach(item => { + domains[item.name] = item.domain; + }) const query = {}; + let params = {}; + let reqParams = this.props.reqParams ? this.props.reqParams : '{}'; + let paramsNotJson = false; + try { + reqParams = JSON.parse(reqParams) + paramsNotJson = false; + } catch (e) { + paramsNotJson = true; + } if (method === 'GET') { Object.keys(reqParams).forEach(key => { - const value = typeof reqParams[key] === 'object' ? JSON.stringify(reqParams) : reqParams.toString(); + const value = typeof reqParams[key] === 'object' ? JSON.stringify(reqParams[key]) : reqParams[key].toString(); query[key] = value; }) + } else if (method === 'POST') { + params = reqParams; } const headers = {} @@ -75,10 +104,27 @@ export default class InterfaceTest extends Component { } }) + this.setState({ + domains, + pathname, + query, + params, + paramsNotJson, + headers, + currDomain: domains.prd + }); + } + + @autobind + testInterface() { + const { method } = this.props; + const { pathname, query, headers, params, currDomain } = this.state; + const urlObj = URL.parse(currDomain); + const href = URL.format({ - protocol: protocol || 'http', - host: prd_host, - pathname: (basepath + url).replace(/\/+/g, '/'), + protocol: urlObj.protocol || 'http', + host: urlObj.host, + pathname, query }); @@ -86,9 +132,7 @@ export default class InterfaceTest extends Component { url: href, method, headers, - data: { - a:1 - }, + data: params, success: (res, header) => { console.log(header) this.setState({res}) @@ -96,33 +140,38 @@ export default class InterfaceTest extends Component { }) } + @autobind + changeDomain(value) { + const domain = this.state.domains[value]; + this.setState({ currDomain: domain }); + } + + @autobind + changeHeader(e, key) { + const headers = JSON.parse(JSON.stringify(this.state.headers)); + headers[key] = e.target.value; + this.setState({ headers }); + } + + @autobind + changeQuery(e, key) { + const query = JSON.parse(JSON.stringify(this.state.query)); + query[key] = e.target.value; + this.setState({ query }); + } + + @autobind + changeParams(e, key) { + const params = JSON.parse(JSON.stringify(this.state.params)); + params[key] = e.target.value; + this.setState({ params }); + } + render () { - const { method, url, seqGroup, interfaceName, interfaceProject } = this.props; - const { prd_host, basepath, protocol, env } = interfaceProject; - const reqParams = this.props.reqParams ? JSON.parse(this.props.reqParams) : []; - const pathname = (basepath + url).replace(/\/+/g, '/'); - - const domains = [{name: 'prd', domain: protocol + '://' + prd_host}]; - env.forEach(item => { - domains.push({name: item.name, domain: item.domain}); - }) - - const query = {}; - if (method === 'GET') { - Object.keys(reqParams).forEach(key => { - const value = typeof reqParams[key] === 'object' ? JSON.stringify(reqParams[key]) : reqParams[key].toString(); - query[key] = value; - }) - } - - const headers = {} - seqGroup.forEach((headerItem) => { - if (headerItem.name) { - headers[headerItem.name] = headerItem.value; - } - }) + const { interfaceName, method } = this.props; + const { domains, pathname, query, headers, params, paramsNotJson } = this.state; const search = URL.format({ query }); @@ -135,40 +184,58 @@ export default class InterfaceTest extends Component {
- { - domains.map((item, index) => ()) + Object.keys(domains).map((key, index) => ()) } - +
- +
- HEADERS: { Object.keys(headers).map((key, index) => { return (
{' = '} - + this.changeHeader(e, key)} style={{display: 'inline-block', width: 200, margin: 10}} />
) }) }
- -
- 请求参数: + +
{ - Object.keys(reqParams).map((key, index) => { - const value = typeof reqParams[key] === 'object' ? JSON.stringify(reqParams[key]) : reqParams[key].toString(); + Object.keys(query).map((key, index) => { + const value = typeof query[key] === 'object' ? JSON.stringify(query[key]) : query[key].toString(); return (
{' = '} - + this.changeQuery(e, key)} style={{display: 'inline-block', width: 200, margin: 10}} /> +
+ ) + }) + } +
+
+ +
+ { paramsNotJson ? + : + Object.keys(params).map((key, index) => { + const value = typeof params[key] === 'object' ? JSON.stringify(params[key]) : params[key].toString(); + return ( +
+ {' = '} + this.changeParams(e, key)} style={{display: 'inline-block', width: 200, margin: 10}} />
) }) @@ -176,9 +243,8 @@ export default class InterfaceTest extends Component {
- +
- 返回结果: