mock平台

InterfaceList.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. import React, { PureComponent as Component } from 'react';
  2. import { connect } from 'react-redux';
  3. import PropTypes from 'prop-types';
  4. import axios from 'axios';
  5. import { Table, Button, Modal, message, Tooltip, Select, Icon } from 'antd';
  6. import AddInterfaceForm from './AddInterfaceForm';
  7. import {
  8. fetchInterfaceListMenu,
  9. fetchInterfaceList,
  10. fetchInterfaceCatList
  11. } from '../../../../reducer/modules/interface.js';
  12. import { getProject } from '../../../../reducer/modules/project.js';
  13. import { Link } from 'react-router-dom';
  14. import variable from '../../../../constants/variable';
  15. import './Edit.scss';
  16. import Label from '../../../../components/Label/Label.js';
  17. const Option = Select.Option;
  18. const limit = 20;
  19. @connect(
  20. state => {
  21. return {
  22. curData: state.inter.curdata,
  23. curProject: state.project.currProject,
  24. catList: state.inter.list,
  25. totalTableList: state.inter.totalTableList,
  26. catTableList: state.inter.catTableList,
  27. totalCount: state.inter.totalCount,
  28. count: state.inter.count
  29. };
  30. },
  31. {
  32. fetchInterfaceListMenu,
  33. fetchInterfaceList,
  34. fetchInterfaceCatList,
  35. getProject
  36. }
  37. )
  38. class InterfaceList extends Component {
  39. constructor(props) {
  40. super(props);
  41. this.state = {
  42. visible: false,
  43. data: [],
  44. filteredInfo: {},
  45. catid: null,
  46. total: null,
  47. current: 1
  48. };
  49. }
  50. static propTypes = {
  51. curData: PropTypes.object,
  52. catList: PropTypes.array,
  53. match: PropTypes.object,
  54. curProject: PropTypes.object,
  55. history: PropTypes.object,
  56. fetchInterfaceListMenu: PropTypes.func,
  57. fetchInterfaceList: PropTypes.func,
  58. fetchInterfaceCatList: PropTypes.func,
  59. totalTableList: PropTypes.array,
  60. catTableList: PropTypes.array,
  61. totalCount: PropTypes.number,
  62. count: PropTypes.number,
  63. getProject: PropTypes.func
  64. };
  65. handleRequest = async props => {
  66. const { params } = props.match;
  67. if (!params.actionId) {
  68. let projectId = params.id;
  69. this.setState({
  70. catid: null
  71. });
  72. let option = {
  73. page: this.state.current,
  74. limit,
  75. project_id: projectId,
  76. status: this.state.filteredInfo.status,
  77. tag: this.state.filteredInfo.tag
  78. };
  79. await this.props.fetchInterfaceList(option);
  80. } else if (isNaN(params.actionId)) {
  81. let catid = params.actionId.substr(4);
  82. this.setState({catid: +catid});
  83. let option = {
  84. page: this.state.current,
  85. limit,
  86. catid,
  87. status: this.state.filteredInfo.status,
  88. tag: this.state.filteredInfo.tag
  89. };
  90. await this.props.fetchInterfaceCatList(option);
  91. }
  92. };
  93. // 更新分类简介
  94. handleChangeInterfaceCat = (desc, name) => {
  95. let params = {
  96. catid: this.state.catid,
  97. name: name,
  98. desc: desc
  99. };
  100. axios.post('/api/interface/up_cat', params).then(async res => {
  101. if (res.data.errcode !== 0) {
  102. return message.error(res.data.errmsg);
  103. }
  104. let project_id = this.props.match.params.id;
  105. await this.props.getProject(project_id);
  106. await this.props.fetchInterfaceListMenu(project_id);
  107. message.success('接口集合简介更新成功');
  108. });
  109. };
  110. handleChange = (pagination, filters, sorter) => {
  111. this.setState({
  112. current: pagination.current || 1,
  113. sortedInfo: sorter,
  114. filteredInfo: filters
  115. }, () => this.handleRequest(this.props));
  116. };
  117. componentWillMount() {
  118. this.actionId = this.props.match.params.actionId;
  119. this.handleRequest(this.props);
  120. }
  121. componentWillReceiveProps(nextProps) {
  122. let _actionId = nextProps.match.params.actionId;
  123. if (this.actionId !== _actionId) {
  124. this.actionId = _actionId;
  125. this.setState(
  126. {
  127. current: 1
  128. },
  129. () => this.handleRequest(nextProps)
  130. );
  131. }
  132. }
  133. handleAddInterface = data => {
  134. data.project_id = this.props.curProject._id;
  135. axios.post('/api/interface/add', data).then(res => {
  136. if (res.data.errcode !== 0) {
  137. return message.error(`${res.data.errmsg}, 你可以在左侧的接口列表中对接口进行删改`);
  138. }
  139. message.success('接口添加成功');
  140. let interfaceId = res.data.data._id;
  141. this.props.history.push('/project/' + data.project_id + '/interface/api/' + interfaceId);
  142. this.props.fetchInterfaceListMenu(data.project_id);
  143. });
  144. };
  145. changeInterfaceCat = async (id, catid) => {
  146. const params = {
  147. id: id,
  148. catid
  149. };
  150. let result = await axios.post('/api/interface/up', params);
  151. if (result.data.errcode === 0) {
  152. message.success('修改成功');
  153. this.handleRequest(this.props);
  154. this.props.fetchInterfaceListMenu(this.props.curProject._id);
  155. } else {
  156. message.error(result.data.errmsg);
  157. }
  158. };
  159. changeInterfaceStatus = async value => {
  160. const params = {
  161. id: value.split('-')[0],
  162. status: value.split('-')[1]
  163. };
  164. let result = await axios.post('/api/interface/up', params);
  165. if (result.data.errcode === 0) {
  166. message.success('修改成功');
  167. this.handleRequest(this.props);
  168. } else {
  169. message.error(result.data.errmsg);
  170. }
  171. };
  172. //page change will be processed in handleChange by pagination
  173. // changePage = current => {
  174. // if (this.state.current !== current) {
  175. // this.setState(
  176. // {
  177. // current: current
  178. // },
  179. // () => this.handleRequest(this.props)
  180. // );
  181. // }
  182. // };
  183. render() {
  184. let tag = this.props.curProject.tag;
  185. let tagFilter = tag.map(item => {
  186. return {text: item.name, value: item.name};
  187. });
  188. const columns = [
  189. {
  190. title: '接口名称',
  191. dataIndex: 'title',
  192. key: 'title',
  193. width: 30,
  194. render: (text, item) => {
  195. return (
  196. <Link to={'/project/' + item.project_id + '/interface/api/' + item._id}>
  197. <span className="path">{text}</span>
  198. </Link>
  199. );
  200. }
  201. },
  202. {
  203. title: '接口路径',
  204. dataIndex: 'path',
  205. key: 'path',
  206. width: 50,
  207. render: (item, record) => {
  208. const path = this.props.curProject.basepath + item;
  209. let methodColor =
  210. variable.METHOD_COLOR[record.method ? record.method.toLowerCase() : 'get'] ||
  211. variable.METHOD_COLOR['get'];
  212. return (
  213. <div>
  214. <span
  215. style={{ color: methodColor.color, backgroundColor: methodColor.bac }}
  216. className="colValue"
  217. >
  218. {record.method}
  219. </span>
  220. <Tooltip title="开放接口" placement="topLeft">
  221. <span>{record.api_opened && <Icon className="opened" type="eye-o" />}</span>
  222. </Tooltip>
  223. <Tooltip title={path} placement="topLeft" overlayClassName="toolTip">
  224. <span className="path">{path}</span>
  225. </Tooltip>
  226. </div>
  227. );
  228. }
  229. },
  230. {
  231. title: '接口分类',
  232. dataIndex: 'catid',
  233. key: 'catid',
  234. width: 28,
  235. render: (item, record) => {
  236. return (
  237. <Select
  238. value={item + ''}
  239. className="select path"
  240. onChange={catid => this.changeInterfaceCat(record._id, catid)}
  241. >
  242. {this.props.catList.map(cat => {
  243. return (
  244. <Option key={cat.id + ''} value={cat._id + ''}>
  245. <span>{cat.name}</span>
  246. </Option>
  247. );
  248. })}
  249. </Select>
  250. );
  251. }
  252. },
  253. {
  254. title: '状态',
  255. dataIndex: 'status',
  256. key: 'status',
  257. width: 24,
  258. render: (text, record) => {
  259. const key = record.key;
  260. return (
  261. <Select
  262. value={key + '-' + text}
  263. className="select"
  264. onChange={this.changeInterfaceStatus}
  265. >
  266. <Option value={key + '-done'}>
  267. <span className="tag-status done">已完成</span>
  268. </Option>
  269. <Option value={key + '-undone'}>
  270. <span className="tag-status undone">未完成</span>
  271. </Option>
  272. </Select>
  273. );
  274. },
  275. filters: [
  276. {
  277. text: '已完成',
  278. value: 'done'
  279. },
  280. {
  281. text: '未完成',
  282. value: 'undone'
  283. }
  284. ],
  285. onFilter: (value, record) => record.status.indexOf(value) === 0
  286. },
  287. {
  288. title: 'tag',
  289. dataIndex: 'tag',
  290. key: 'tag',
  291. width: 14,
  292. render: text => {
  293. let textMsg = text.length > 0 ? text.join('\n') : '未设置';
  294. return <div className="table-desc">{textMsg}</div>;
  295. },
  296. filters: tagFilter,
  297. onFilter: (value, record) => {
  298. return record.tag.indexOf(value) >= 0;
  299. }
  300. }
  301. ];
  302. let intername = '',
  303. desc = '';
  304. let cat = this.props.curProject ? this.props.curProject.cat : [];
  305. if (cat) {
  306. for (let i = 0; i < cat.length; i++) {
  307. if (cat[i]._id === this.state.catid) {
  308. intername = cat[i].name;
  309. desc = cat[i].desc;
  310. break;
  311. }
  312. }
  313. }
  314. // const data = this.state.data ? this.state.data.map(item => {
  315. // item.key = item._id;
  316. // return item;
  317. // }) : [];
  318. let data = [];
  319. let total = 0;
  320. const { params } = this.props.match;
  321. if (!params.actionId) {
  322. data = this.props.totalTableList;
  323. total = this.props.totalCount;
  324. } else if (isNaN(params.actionId)) {
  325. data = this.props.catTableList;
  326. total = this.props.count;
  327. }
  328. data = data.map(item => {
  329. item.key = item._id;
  330. return item;
  331. });
  332. const pageConfig = {
  333. total: total,
  334. pageSize: limit,
  335. current: this.state.current
  336. // onChange: this.changePage
  337. };
  338. const isDisabled = this.props.catList.length === 0;
  339. // console.log(this.props.curProject.tag)
  340. return (
  341. <div style={{ padding: '24px' }}>
  342. <h2 className="interface-title" style={{ display: 'inline-block', margin: 0 }}>
  343. {intername ? intername : '全部接口'}共 ({total}) 个
  344. </h2>
  345. <Button
  346. style={{ float: 'right' }}
  347. disabled={isDisabled}
  348. type="primary"
  349. onClick={() => this.setState({ visible: true })}
  350. >
  351. 添加接口
  352. </Button>
  353. <div style={{ marginTop: '10px' }}>
  354. <Label onChange={value => this.handleChangeInterfaceCat(value, intername)} desc={desc} />
  355. </div>
  356. <Table
  357. className="table-interfacelist"
  358. pagination={pageConfig}
  359. columns={columns}
  360. onChange={this.handleChange}
  361. dataSource={data}
  362. />
  363. {this.state.visible && (
  364. <Modal
  365. title="添加接口"
  366. visible={this.state.visible}
  367. onCancel={() => this.setState({ visible: false })}
  368. footer={null}
  369. className="addcatmodal"
  370. >
  371. <AddInterfaceForm
  372. catid={this.state.catid}
  373. catdata={cat}
  374. onCancel={() => this.setState({ visible: false })}
  375. onSubmit={this.handleAddInterface}
  376. />
  377. </Modal>
  378. )}
  379. </div>
  380. );
  381. }
  382. }
  383. export default InterfaceList;