diff --git a/client/components/Breadcrumb/Breadcrumb.scss b/client/components/Breadcrumb/Breadcrumb.scss index c0b73ff6..08ec4d76 100644 --- a/client/components/Breadcrumb/Breadcrumb.scss +++ b/client/components/Breadcrumb/Breadcrumb.scss @@ -2,5 +2,5 @@ .breadcrumb-container { @include row-width-limit; - margin: 16px auto -8px; + margin: 16px auto; } diff --git a/client/containers/AddInterface/AddInterface.js b/client/containers/AddInterface/AddInterface.js index b9e3e6d8..1e0e333b 100644 --- a/client/containers/AddInterface/AddInterface.js +++ b/client/containers/AddInterface/AddInterface.js @@ -78,7 +78,7 @@ class AddInterface extends Component { mockJson: '', mockURL: '', projectData: {}, - tagName: '创建接口', + tagName: '添加接口', showMock: '' } } diff --git a/client/containers/AddInterface/InterfaceTest/InterfaceTest.js b/client/containers/AddInterface/InterfaceTest/InterfaceTest.js index 1c0f1cae..43664fe7 100644 --- a/client/containers/AddInterface/InterfaceTest/InterfaceTest.js +++ b/client/containers/AddInterface/InterfaceTest/InterfaceTest.js @@ -1,10 +1,11 @@ import React, { Component } from 'react' import PropTypes from 'prop-types' import { connect } from 'react-redux' -import { Button, Input, Select, Card, Alert, Spin } from 'antd' +import { Button, Input, Select, Card, Alert, Spin, Icon, message } from 'antd' import { autobind } from 'core-decorators'; import crossRequest from 'cross-request'; import { withRouter } from 'react-router'; +import axios from 'axios'; import URL from 'url'; import { @@ -48,8 +49,8 @@ export default class InterfaceTest extends Component { params: {}, paramsNotJson: false, headers: {}, - search: '', - currDomain: '' + currDomain: '', + paramsType: 'from' } constructor(props) { @@ -57,15 +58,15 @@ export default class InterfaceTest extends Component { } componentWillMount() { - this.interfacePropsToState() + this.getInterfaceState() } componentWillReceiveProps(nextProps) { - this.interfacePropsToState(nextProps) + this.getInterfaceState(nextProps) } @autobind - interfacePropsToState(nextProps) { + getInterfaceState(nextProps) { const props = nextProps || this.props; const { method, url, seqGroup, interfaceProject } = props; const { prd_host, basepath, protocol, env } = interfaceProject; @@ -76,33 +77,40 @@ export default class InterfaceTest extends Component { domains[item.name] = item.domain; }) - const query = {}; - let params = {}; + const query = []; + let params = []; let reqParams = this.props.reqParams ? this.props.reqParams : '{}'; let paramsNotJson = false; try { - reqParams = JSON.parse(reqParams) - paramsNotJson = false; + reqParams = JSON.parse(reqParams); + // paramsNotJson = false; } catch (e) { - paramsNotJson = true; + // paramsNotJson = true; + reqParams = {}; + message.error('请求参数不是 JSON 格式'); } if (method === 'GET') { Object.keys(reqParams).forEach(key => { const value = typeof reqParams[key] === 'object' ? JSON.stringify(reqParams[key]) : reqParams[key].toString(); - query[key] = value; + query.push({key, value}) }) } else if (method === 'POST') { - params = reqParams; + // params = reqParams; + Object.keys(reqParams).forEach(key => { + const value = typeof reqParams[key] === 'object' ? JSON.stringify(reqParams[key]) : reqParams[key].toString(); + query.push({key, value, type: 'text'}) + }) } - const headers = {} + const headers = [] seqGroup.forEach((headerItem) => { if (headerItem.name) { - headers[headerItem.name] = headerItem.value; + headers.push({name: headerItem.name, value: headerItem.value}); } }) this.setState({ + method, domains, pathname, query, @@ -110,21 +118,21 @@ export default class InterfaceTest extends Component { paramsNotJson, headers, currDomain: domains.prd, - loading: false + loading: false, + paramsType: 'form' }); } @autobind - testInterface() { - const { method } = this.props; - const { pathname, query, headers, params, currDomain } = this.state; + requestInterface() { + const { headers, params, currDomain, method, pathname, query } = this.state; const urlObj = URL.parse(currDomain); const href = URL.format({ protocol: urlObj.protocol || 'http', host: urlObj.host, pathname, - query + query: this.getQueryObj(query) }); this.setState({ loading: true }) @@ -132,8 +140,8 @@ export default class InterfaceTest extends Component { crossRequest({ url: href, method, - headers, - data: params, + headers: this.getHeadersObj(headers), + data: this.arrToObj(params), success: (res) => { try { res = JSON.parse(res) @@ -152,44 +160,149 @@ export default class InterfaceTest extends Component { @autobind changeDomain(value) { - const domain = this.state.domains[value]; - this.setState({ currDomain: domain }); + this.setState({ currDomain: value }); } @autobind - changeHeader(e, key) { + selectDomain(value) { + this.setState({ currDomain: value }); + } + + @autobind + changeHeader(e, index, isName) { const headers = JSON.parse(JSON.stringify(this.state.headers)); - headers[key] = e.target.value; + const v = e.target.value; + if (isName) { + headers[index].name = v; + } else { + headers[index].value = v; + } this.setState({ headers }); } - @autobind - changeQuery(e, key) { - const query = JSON.parse(JSON.stringify(this.state.query)); - query[key] = e.target.value; - this.setState({ query }); + addHeader() { + const { headers } = this.state; + this.setState({headers: headers.concat([{name: '', value: ''}])}) + } + @autobind + deleteHeader(index) { + const { headers } = this.state; + this.setState({headers: headers.filter((item, i) => +index !== +i)}); } @autobind - changeParams(e, key) { + changeQuery(e, index, isKey) { + const query = JSON.parse(JSON.stringify(this.state.query)); + const v = e.target.value; + if (isKey) { + query[index].key = v; + } else { + query[index].value = v; + } + this.setState({ query }); + } + @autobind + addQuery() { + const { query } = this.state; + this.setState({query: query.concat([{key: '', value: ''}])}) + } + @autobind + deleteQuery(index) { + const { query } = this.state; + this.setState({query: query.filter((item, i) => +index !== +i)}); + } + + @autobind + changeParams(e, index, type) { const params = JSON.parse(JSON.stringify(this.state.params)); - params[key] = e.target.value; + switch (type) { + case 'key': + params[index].key = e.target.value + break; + case 'type': + params[index].type = e + break; + case 'value': + params[index].value = e.target.value + break; + default: + break; + } this.setState({ params }); } + @autobind + addParams() { + const { params } = this.state; + this.setState({params: params.concat([{key: '', value: '', type: 'text'}])}) + } + @autobind + deleteParams(index) { + const { params } = this.state; + this.setState({params: params.filter((item, i) => +index !== +i)}); + } + + @autobind + changeMethod(value) { + this.setState({ method: value }); + } + + @autobind + changePath(e) { + const path = e.target.value; + const urlObj = URL.parse(path, true); + this.setState({ + query: urlObj.query, + pathname: urlObj.pathname + }) + } + + @autobind + changeParamsType(value) { + this.setState({paramsType: value}) + } hasCrossRequestPlugin() { const dom = document.getElementById('y-request'); return dom.getAttribute('key') === 'yapi'; } + arrToObj(arr) { + const obj = {}; + arr.forEach(item => { + if (item.key) { + obj[item.key] = item.value || ''; + } + }) + return obj; + } + getQueryObj(query) { + const queryObj = {}; + query.forEach(item => { + if (item.key) { + queryObj[item.key] = item.value || ''; + } + }) + return queryObj; + } + getHeadersObj(headers) { + const headersObj = {}; + headers.forEach(item => { + if (item.name && item.value) { + headersObj[item.name] = item.value; + } + }) + return headersObj; + } + render () { - const { interfaceName, method } = this.props; - const { domains, pathname, query, headers, params, paramsNotJson } = this.state; - const search = URL.format({ - query - }); + const { interfaceName } = this.props; + const { method, domains, pathname, query, headers, params, currDomain, paramsType } = this.state; const hasPlugin = this.hasCrossRequestPlugin(); + const search = decodeURIComponent(URL.format({query: this.getQueryObj(query)})); + + console.log(axios) + window.axios = axios return ( @@ -213,6 +326,8 @@ export default class InterfaceTest extends Component { }