import React, { Component } from "react";
import ReactDOM from "react-dom";
import { Row, Col, Button, Modal, Input, Card, Table, message } from "antd";
import { stringify } from "qs";
import mock from "../../.roadhogrc.mock";
import config from "./config";
import utils from "./utils";
import request from "./request";
import styles from "./api.less";
const { TextArea } = Input;
/* eslint no-underscore-dangle:0 */
const mockData = mock.__mockData || mock;
const { port, isStatic, docPort } = config;
const { isObject, parseKey, handleRequest } = utils;
class ApiItem extends Component {
state = {
urlValue: "",
theMockData: {},
postParams: undefined
};
handleChange = e => {
this.setState({
urlValue: e.target.value
});
};
handlePostParams = e => {
let postParams = e.target.value;
this.setState({
postParams
});
};
handleShowData = data => {
if (this.props.onPostClick) {
this.props.onPostClick(data);
}
};
handlePostRequest = (u, url, postParams, method) => {
let params;
if (Object.prototype.toString.call(postParams) === "[object String]") {
try {
params = JSON.parse(postParams);
} catch (e) {
message.error("parse params error: ", JSON.stringify(e, null, 2));
}
}
if (params) {
if (method != "GET" && port) {
request(`http://localhost:${docPort}${u}`, {
method: "POST",
body: params
}).then(data => {
this.handleShowData(data);
});
} else {
handleRequest(u, url, params, this.handleShowData);
}
}
};
render() {
const { req, data } = this.props;
const { method, url: u } = parseKey(req);
const url = `http://localhost${port ? `:${port}` : ":8000"}${u}`;
let { urlValue, postParams } = this.state;
const params = data.$params || {};
const desc = data.$desc || "";
const columns = [
{
key: "p",
dataIndex: "p",
title: "参数"
},
{
key: "desc",
dataIndex: "desc",
title: "说明"
},
{
key: "exp",
dataIndex: "exp",
title: "样例"
}
];
const dataSource = [];
const getParams = {};
Object.keys(params).forEach(p => {
const pd = params[p];
if (isObject(pd)) {
getParams[p] = params[p].exp;
dataSource.push({
p,
desc: params[p].desc,
exp: params[p].exp
});
} else {
getParams[p] = params[p];
dataSource.push({
p,
desc: "",
exp: params[p]
});
}
});
if (method === "GET") {
if (!urlValue && dataSource.length > 0) {
urlValue = `${url}?${stringify(getParams)}`;
}
}
if (!urlValue) {
urlValue = url;
}
if (!postParams) {
postParams = JSON.stringify(getParams, null, 2);
}
return (
{desc}
} {dataSource.length > 0 && (