import './View.scss' import React, { Component } from 'react' import { connect } from 'react-redux' import PropTypes from 'prop-types' import { Table } from 'antd' const mockEditor = require('./mockEditor.js') import { formatTime } from '../../../../common.js'; import ErrMsg from '../../../../components/ErrMsg/ErrMsg.js'; // import { Card } from 'antd' // import { getMockUrl } from '../../reducer/modules/news.js' @connect(state=>{ // console.log(state); return { curData: state.inter.curdata, currProject: state.project.currProject } }) class View extends Component { constructor(props) { super(props); this.state = { init: true } } static propTypes = { curData: PropTypes.object, currProject: PropTypes.object } req_body_form(req_body_type,req_body_form){ if(req_body_type === 'json'){ return
请求Body:
}else if(req_body_type === 'form'){ const columns = [{ title: '参数名称', dataIndex: 'name', key: 'name' }, { title: '是否必须', dataIndex: 'required', key: 'required' }, { title: '备注', dataIndex: 'value', key: 'value', width: '45%' }]; const dataSource = []; if(req_body_form&&req_body_form.length){ req_body_form.map((item,i)=>{ dataSource.push({ key: i, name: item.name, value: item.desc, required: item.required?"必须":"非必须" }) }) } return
请求Body: }else if(req_body_type === 'file'){ return
请求Body:
{this.props.curData.req_body_other}
}else if(req_body_type === 'raw'){ return
请求Body:
{this.props.curData.req_body_other}
} } res_body(res_body_type,res_body){ if(res_body_type === 'json'){ return
返回Body:
}else if(res_body_type === 'raw'){ return
返回Body:
{res_body}
} } req_query(query){ const columns = [{ title: '参数名称', dataIndex: 'name', key: 'name' }, { title: '是否必须', dataIndex: 'required', key: 'required' }, { title: '备注', dataIndex: 'value', key: 'value', width: '45%' }]; const dataSource = []; if(query&&query.length){ query.map((item,i)=>{ dataSource.push({ key: i, name: item.name, value: item.desc, required: item.required?"必须":"非必须" }) }) } return
; } bindAceEditor(){ if(this.props.curData.req_body_type === "json"&&this.props.curData.title){ mockEditor({ container: 'vreq_body_json', data: this.props.curData.req_body_other, readOnly:true, onChange: function () {} }) } if(this.props.curData.title&&this.props.curData.res_body_type === "json"){ mockEditor({ container: 'vres_body_json', data: this.props.curData.res_body, readOnly:true, onChange: function () {} }) } } componentDidMount(){ if(this.props.curData.title){ this.bindAceEditor.bind(this)(); } if(!this.props.curData.title&&this.state.init){ this.setState({init: false}); } } componentDidUpdate(){ this.bindAceEditor.bind(this)(); } componentWillUpdate(){ if(!this.props.curData.title&&this.state.init){ this.setState({init: false}); } } render () { const dataSource = []; if(this.props.curData.req_headers&&this.props.curData.req_headers.length){ this.props.curData.req_headers.map((item,i)=>{ dataSource.push({ key: i, name: item.name, required: item.required?"必须":"非必须", value: item.value, desc: item.desc }) }) } const req_dataSource = []; if(this.props.curData.req_params&&this.props.curData.req_params.length){ this.props.curData.req_params.map((item,i)=>{ req_dataSource.push({ key: i, name: item.name, value: item.desc }) }) } const req_params_columns = [{ title: '参数名称', dataIndex: 'name', key: 'name' },{ title: '备注', dataIndex: 'value', key: 'value' }]; const columns = [{ title: '参数名称', dataIndex: 'name', key: 'name', width: 2 }, { title: '参数值', dataIndex: 'value', key: 'value', width: 2 }, { title: '是否必须', dataIndex: 'required', key: 'required', width: 1 },{ title: '备注', dataIndex: 'desc', key: 'desc', width: 4 }]; let status = { undone: "未完成", done: "完成" } let statusColor = { undone: "rgb(255, 85, 0)", done: "#cfefdf" } let methodColor = { post: { bac: "#d2eafb", color: "#108ee9" }, get: { bac: "#cfefdf", color: "#00a854" }, input: { bac: "#57cf27", color: "#9c82a2" }, delete: { bac: "#57cf27", color: "#57cf27" }, head: { bac: "#d4d5f7", color: "#0b47ef" }, patch: { bac: "#f9f4e1", color: "#fac200" }, options: { bac: "#f7cdf5", color: "#fa00f2" } } methodColor = methodColor[this.props.curData.method?this.props.curData.method.toLowerCase():"get"]; if(!methodColor) methodColor = "get"; let res =
接口名: {this.props.curData.title}
请求方法: {this.props.curData.method}
接口路径: {this.props.currProject.basepath}{this.props.curData.path}
状态: {status[this.props.curData.status]}
创建时间: {formatTime(this.props.curData.add_time)}
更新时间: {formatTime(this.props.curData.up_time)}
Mock地址: {location.protocol + '//' + location.hostname + (location.port !== "" ? ":" + location.port : "") + `/mock/${this.props.currProject._id}${this.props.currProject.basepath}${this.props.curData.path}`}
{this.props.curData.desc?
接口描述: {this.props.curData.desc}
:""} {req_dataSource.length?
路径参数:
:""} {dataSource.length?
请求Headers:
:""} {this.props.curData.req_query&&this.props.curData.req_query.length?
Query: {this.req_query(this.props.curData.req_query)}
:""} {/*
请求Body类型: {this.props.curData.req_body_type}
*/} {this.req_body_form(this.props.curData.req_body_type,this.props.curData.req_body_form)} {/*
返回Body类型: {this.props.curData.res_body_type}
*/} {this.res_body(this.props.curData.res_body_type,this.props.curData.res_body)} ; if(!this.props.curData.title){ if(this.state.init){ res =
; }else{ res = ; } } return res; } } export default View