diff --git a/client/containers/Project/Interface/InterfaceList/InterfaceEditForm.js b/client/containers/Project/Interface/InterfaceList/InterfaceEditForm.js index 407ce12d..410a224f 100644 --- a/client/containers/Project/Interface/InterfaceList/InterfaceEditForm.js +++ b/client/containers/Project/Interface/InterfaceList/InterfaceEditForm.js @@ -1,17 +1,20 @@ import React, { Component } from 'react' import PropTypes from 'prop-types' +import _ from 'underscore' import { Form, Select, Input, Button, Row, Col, Radio, Icon } from 'antd'; + const FormItem = Form.Item; const Option = Select.Option; const InputGroup = Input.Group; const RadioGroup = Radio.Group; const dataTpl = { req_query: { name: "", required: "1", desc: "" }, - req_headers: { name: "", required: "1", desc: "" } + req_headers: { name: "", required: "1", desc: "" }, + req_params: { name: "", desc: "" } } const mockEditor = require('./mockEditor.js'); @@ -32,12 +35,14 @@ class InterfaceEditForm extends Component { if (curdata.req_query && curdata.req_query.length === 0) delete curdata.req_query; if (curdata.req_headers && curdata.req_headers.length === 0) delete curdata.req_headers; if (curdata.req_body_form && curdata.req_body_form.length === 0) delete curdata.req_body_form; + if (curdata.req_params && curdata.req_params.length === 0) delete curdata.req_params; this.state = Object.assign({ title: '', path: '', status: 'undone', method: 'get', + req_params: [], req_query: [{ name: '', desc: '', @@ -66,23 +71,31 @@ class InterfaceEditForm extends Component { this.props.form.validateFields((err, values) => { if (!err) { if (values.res_body_type === 'json') values.res_body = this.state.res_body; + values.req_params = this.state.req_params; values.req_body_json = this.state.res_body; - let isfile = false; - if(values.req_body_type === 'form'){ - values.req_body_form.forEach((item)=>{ - if(item.type === 'file'){ + values.method = this.state.method; + let isfile = false, isHavaContentType = false; + if (values.req_body_type === 'form') { + values.req_body_form.forEach((item) => { + if (item.type === 'file') { isfile = true; } }) - values.req_headers.filter( (item)=>{ - item.name !== 'Content-Type' + values.req_headers.map((item) => { + if (item.name === 'Content-Type') { + item.value = isfile ? 'multipart/form-data' : 'application/x-www-form-urlencoded' + isHavaContentType = true; + } }) - values.req_headers.unshift({ - name: 'Content-Type', - value: isfile? 'multipart/form-data': 'application/x-www-form-urlencoded' - }) - + if (isHavaContentType === false) { + values.req_headers.unshift({ + name: 'Content-Type', + value: isfile ? 'multipart/form-data' : 'application/x-www-form-urlencoded' + }) + } + + } this.props.onSubmit(values) @@ -101,7 +114,7 @@ class InterfaceEditForm extends Component { }) } }) - + resBodyEditor = mockEditor({ container: 'res_body_json', data: that.state.res_body, @@ -121,9 +134,10 @@ class InterfaceEditForm extends Component { }) } - addParams = (name) => { + addParams = (name, data) => { let newValue = {} - newValue[name] = [].concat(this.state[name], dataTpl[name]) + data = data || dataTpl[name] + newValue[name] = [].concat(this.state[name], data) this.setState(newValue) } @@ -138,6 +152,22 @@ class InterfaceEditForm extends Component { this.setState(newValue) } + handlePath = (e) => { + let val = e.target.value; + if (val && val.indexOf(":") !== -1) { + let paths = val.split("/"), name, i; + for(i=1; i< paths.length; i++){ + if(paths[i][0] === ':'){ + name = paths[i].substr(1); + if(!_.find(this.state.req_params, {name: name})){ + this.addParams('req_params', { name: name }) + } + } + } + + } + } + render() { const { getFieldDecorator } = this.props.form; const formItemLayout = { @@ -241,6 +271,33 @@ class InterfaceEditForm extends Component { } + const paramsTpl = (data, index) => { + return + + {getFieldDecorator('req_params[' + index + '].name', { + initialValue: data.name + })( + + )} + + + {getFieldDecorator('req_params[' + index + '].desc', { + initialValue: data.desc + })( + + )} + + + this.delParams(index, 'req_params')} /> + + + + } + + const paramsList = this.state.req_params.map((item, index) => { + return paramsTpl(item, index) + }) + const QueryList = this.state.req_query.map((item, index) => { return queryTpl(item, index) }) @@ -275,32 +332,34 @@ class InterfaceEditForm extends Component { {...formItemLayout} label="接口路径" > - {getFieldDecorator('path', { - initialValue: this.state.path, - rules: [{ - required: true, message: '清输入接口路径!' - }] - })( - - {getFieldDecorator('method', { - initialValue: 'GET' - })( - - )} - { }} style={{ width: '25%'}} /> - {getFieldDecorator('path', { - initialValue: this.state.path - })( - - )} - + + + + + { }} style={{ width: '25%' }} /> + + {getFieldDecorator('path', { + initialValue: this.state.path, + rules: [{ + required: true, message: '清输入接口路径!' + }] + })( + + )} + + + + {paramsList} + + + + - )}
- +
@@ -470,7 +529,7 @@ class InterfaceEditForm extends Component { {getFieldDecorator('res_body', { initialValue: this.state.res_body })( - + )} diff --git a/client/containers/Project/Interface/InterfaceList/Run.js b/client/containers/Project/Interface/InterfaceList/Run.js index b51614a0..a21f0ed1 100644 --- a/client/containers/Project/Interface/InterfaceList/Run.js +++ b/client/containers/Project/Interface/InterfaceList/Run.js @@ -1,7 +1,7 @@ import React, { Component } from 'react' import PropTypes from 'prop-types' import { connect } from 'react-redux' -import { Button, Input, Select, Card, Alert, Spin, Icon, message, Collapse } from 'antd' +import { Button, Input, Select, Card, Alert, Spin, Icon, message, Collapse, Radio } from 'antd' import { autobind } from 'core-decorators'; import crossRequest from 'cross-request'; import { withRouter } from 'react-router'; @@ -17,6 +17,8 @@ const { TextArea } = Input; const InputGroup = Input.Group; const Option = Select.Option; const Panel = Collapse.Panel; +const RadioButton = Radio.Button; +const RadioGroup = Radio.Group; @connect( state => ({ @@ -376,8 +378,6 @@ export default class Run extends Component { } - {/*
{interfaceName}
*/} -
@@ -421,7 +421,8 @@ export default class Run extends Component { headers.map((item, index) => { return (
- this.changeHeader(e, index, true)} className="key" />{' = '} + this.changeHeader(e, index, true)} className="key" /> + = this.changeHeader(e, index)} className="value" /> this.deleteHeader(index)} />
@@ -447,49 +448,55 @@ export default class Run extends Component { > { method === 'POST' && paramsType !== 'form' && paramsType !== 'file' &&
+ + JSON + TEXT + XML + HTML + -
{paramsType}
} { - method === 'POST' && paramsType === 'form' && ( -
- { - params.map((item, index) => { - return ( -
- this.changeParams(e, index, 'key')} style={{display: 'inline-block', width: 200, margin: 10}} /> - []{' = '} - {item.type === 'file' ? - this.changeParams(e, index, 'value')} multiple style={{display: 'inline-block', width: 200, margin: 10}} /> : - this.changeParams(e, index, 'value')} style={{display: 'inline-block', width: 200, margin: 10}} /> - } -
- ) - }) - } - -
- ) + method === 'POST' && paramsType === 'form' && +
+ { + params.map((item, index) => { + return ( +
+ this.changeParams(e, index, 'key')} className="key" /> + [ + + ] + = + { + item.type === 'file' ? this.changeParams(e, index, 'value')} multiple className="value" /> : + this.changeParams(e, index, 'value')} className="value" /> + } + this.deleteParams(index)} /> +
+ ) + }) + } + +
} { - method === 'POST' && paramsType === 'file' && ( -
- -
- ) + method === 'POST' && paramsType === 'file' && +
+ +
} { - method !== 'POST' && ( -
GET 请求没有 Body。
- ) + method !== 'POST' && +
GET 请求没有 BODY。
} diff --git a/client/containers/Project/Interface/InterfaceList/View.js b/client/containers/Project/Interface/InterfaceList/View.js index b7901f57..bec943fc 100644 --- a/client/containers/Project/Interface/InterfaceList/View.js +++ b/client/containers/Project/Interface/InterfaceList/View.js @@ -30,17 +30,17 @@ class View extends Component { componentDidMount() { let that = this; mockEditor({ - container: 'req_body_json', + container: 'vreq_body_json', data: that.props.req_body_form, onChange: function () {} }) mockEditor({ - container: 'res_body_json', + container: 'vres_body_json', data: that.props.res_body, onChange: function () {} }) mockEditor({ - container: 'req_query_json', + container: 'vreq_query_json', data: that.props.req_query, onChange: function () {} }) @@ -106,15 +106,15 @@ class View extends Component {
Query: -
+
请求Body: -
+
响应Body: -
+
diff --git a/exampleCode/api/project/list.json b/exampleCode/api/project/list.json index 665950ec..37a3dc06 100644 --- a/exampleCode/api/project/list.json +++ b/exampleCode/api/project/list.json @@ -2,7 +2,6 @@ "errcode": 0, "errmsg": "success", "data": { - "total": 2, "list": [ { "_id": 529, @@ -148,16 +147,6 @@ "107" ] } - ], - "userinfo": { - "107": { - "_id": 107, - "username": "admin", - "email": "admin@admin.com", - "role": "admin", - "add_time": 1500280333, - "up_time": 1500373530 - } - } + ] } } \ No newline at end of file