import './View.scss'
import React, { PureComponent as Component } from 'react'
import { connect } from 'react-redux'
import PropTypes from 'prop-types'
import { Table, Icon, Row, Col, Tooltip, message } from 'antd'
import { Link } from 'react-router-dom'
import AceEditor from 'client/components/AceEditor/AceEditor'
import { formatTime, safeArray } from '../../../../common.js'
import ErrMsg from '../../../../components/ErrMsg/ErrMsg.js'
import variable from '../../../../constants/variable'
import constants from '../../../../constants/variable.js'
import copy from 'copy-to-clipboard'
import SchemaTable from '../../../../components/SchemaTable/SchemaTable.js'
const HTTP_METHOD = constants.HTTP_METHOD
@connect(state => {
return {
curData: state.inter.curdata,
custom_field: state.group.field,
currProject: state.project.currProject
}
})
class View extends Component {
constructor(props) {
super(props)
this.state = {
init: true,
enter: false
}
}
static propTypes = {
curData: PropTypes.object,
currProject: PropTypes.object,
custom_field: PropTypes.object
}
req_body_form(req_body_type, req_body_form) {
if (req_body_type === 'form') {
const columns = [
{
title: '参数名称',
dataIndex: 'name',
key: 'name',
width: 140
},
{
title: '参数类型',
dataIndex: 'type',
key: 'type',
width: 100,
render: text => {
text = text || ''
return text.toLowerCase() === 'text' ? (
T文本
) : (
文件
)
}
},
{
title: '是否必须',
dataIndex: 'required',
key: 'required',
width: 100
},
{
title: '示例',
dataIndex: 'example',
key: 'example',
width: 80,
render(_, item) {
return
{item.example}
}
},
{
title: '备注',
dataIndex: 'value',
key: 'value',
render(_, item) {
return {item.value}
}
}
]
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,
example: item.example,
required: item.required == 0 ? '否' : '是',
type: item.type
})
})
}
return (
)
}
}
res_body(res_body_type, res_body, res_body_is_json_schema) {
if (res_body_type === 'json') {
if (res_body_is_json_schema) {
return
} else {
return (
)
}
} else if (res_body_type === 'raw') {
return (
)
}
}
req_body(req_body_type, req_body_other, req_body_is_json_schema) {
if (req_body_other) {
if (req_body_is_json_schema && req_body_type === 'json') {
return
} else {
return (
)
}
}
}
req_query(query) {
const columns = [
{
title: '参数名称',
dataIndex: 'name',
width: 140,
key: 'name'
},
{
title: '类型',
dataIndex: 'type',
width: 140,
key: 'type'
},
{
title: '是否必须',
width: 100,
dataIndex: 'required',
key: 'required'
},
{
title: '示例',
dataIndex: 'example',
key: 'example',
width: 80,
render(_, item) {
return {item.example}
}
},
{
title: '备注',
dataIndex: 'value',
key: 'value',
render(_, item) {
return {item.value}
}
}
]
const dataSource = []
if (query && query.length) {
query.map((item, i) => {
dataSource.push({
key: i,
name: item.name,
value: item.desc,
example: item.example,
required: item.required == 0 ? '否' : '是',
type: item.type
})
})
}
return (
)
}
countEnter(str) {
let i = 0
let c = 0
if (!str || !str.indexOf) {
return 0
}
while (str.indexOf('\n', i) > -1) {
i = str.indexOf('\n', i) + 2
c++
}
return c
}
componentDidMount() {
if (!this.props.curData.title && this.state.init) {
this.setState({ init: false })
}
}
enterItem = () => {
this.setState({
enter: true
})
}
leaveItem = () => {
this.setState({
enter: false
})
}
copyUrl = url => {
copy(url)
message.success('已经成功复制到剪切板')
}
flagMsg = (mock, strice) => {
if (mock && strice) {
return ( 全局mock & 严格模式 )
} else if (!mock && strice) {
return ( 严格模式 )
} else if (mock && !strice) {
return ( 全局mock )
} else {
return
}
}
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 == 0 ? '否' : '是',
value: item.value,
example: item.example,
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,
desc: item.desc,
example: item.example,
type: item.type
})
})
}
const req_params_columns = [
{
title: '参数名称',
dataIndex: 'name',
key: 'name',
width: 140
},
{
title: '类型',
dataIndex: 'type',
key: 'type',
width: 140
},
{
title: '示例',
dataIndex: 'example',
key: 'example',
width: 80,
render(_, item) {
return {item.example}
}
},
{
title: '备注',
dataIndex: 'desc',
key: 'desc',
render(_, item) {
return {item.desc}
}
}
]
const columns = [
{
title: '参数名称',
dataIndex: 'name',
key: 'name',
width: '200px'
},
{
title: '参数值',
dataIndex: 'value',
key: 'value',
width: '300px'
},
{
title: '是否必须',
dataIndex: 'required',
key: 'required',
width: '100px'
},
{
title: '示例',
dataIndex: 'example',
key: 'example',
width: '80px',
render(_, item) {
return {item.example}
}
},
{
title: '备注',
dataIndex: 'desc',
key: 'desc',
render(_, item) {
return {item.desc}
}
}
]
let status = {
designing: '设计中',
coding: '开发中',
done: '已完成'
}
let bodyShow =
this.props.curData.req_body_other ||
(this.props.curData.req_body_type === 'form' &&
this.props.curData.req_body_form &&
this.props.curData.req_body_form.length)
let requestShow =
(dataSource && dataSource.length) ||
(req_dataSource && req_dataSource.length) ||
(this.props.curData.req_query && this.props.curData.req_query.length) ||
bodyShow
let methodColor =
variable.METHOD_COLOR[
this.props.curData.method
? this.props.curData.method.toLowerCase()
: 'get'
]
// statusColor = statusColor[this.props.curData.status?this.props.curData.status.toLowerCase():"undone"];
// const aceEditor =
if (!methodColor) {
methodColor = 'get'
}
const { tag, up_time, title, uid, username } = this.props.curData
let res = (
基本信息
接口名称:
{title}
创 建 人:
{username}
状 态:
{status[this.props.curData.status]}
更新时间:
{formatTime(up_time)}
{safeArray(tag) && safeArray(tag).length > 0 && (
Tag :
{tag.join(' , ')}
)}
接口路径:
{this.props.curData.method}
{this.props.currProject.basepath}
{this.props.curData.path}
this.copyUrl(
this.props.currProject.basepath + this.props.curData.path,
)
}
style={{
display: this.state.enter ? 'inline-block' : 'none'
}}
/>
Mock地址:
{this.flagMsg(
this.props.currProject.is_mock_open,
this.props.currProject.strice,
)}
window.open(
location.protocol +
'//' +
location.hostname +
(location.port !== '' ? ':' + location.port : '') +
`/mock/${this.props.currProject._id}${this.props.currProject.basepath}${this.props.curData.path}`,
'_blank',
)
}>
{location.protocol +
'//' +
location.hostname +
(location.port !== '' ? ':' + location.port : '') +
`/mock/${this.props.currProject._id}${this.props.currProject.basepath}${this.props.curData.path}`}
{this.props.curData.custom_field_value &&
this.props.custom_field.enable && (
{this.props.custom_field.name}:
{this.props.curData.custom_field_value}
)}
{this.props.curData.desc &&
备注
}
{this.props.curData.desc && (
)}
请求参数
{req_dataSource.length ? (
) : (
''
)}
{dataSource.length ? (
) : (
''
)}
{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 === 'form'
? this.req_body_form(
this.props.curData.req_body_type,
this.props.curData.req_body_form,
)
: this.req_body(
this.props.curData.req_body_type,
this.props.curData.req_body_other,
this.props.curData.req_body_is_json_schema,
)}
返回数据
{this.res_body(
this.props.curData.res_body_type,
this.props.curData.res_body,
this.props.curData.res_body_is_json_schema,
)}
)
if (!this.props.curData.title) {
if (this.state.init) {
res =
} else {
res =
}
}
return res
}
}
export default View