mock平台

index.js 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. import React, { Component } from 'react';
  2. import PropTypes from 'prop-types';
  3. import './index.scss';
  4. import { Alert, Modal, Row, Col, Icon, Collapse, Input, Tooltip } from 'antd';
  5. import MockList from './MockList.js';
  6. import MethodsList from './MethodsList.js';
  7. import VariablesSelect from './VariablesSelect.js';
  8. import { trim } from '../../common.js';
  9. const { handleParamsValue } = require('common/utils.js');
  10. const Panel = Collapse.Panel;
  11. // 深拷贝
  12. function deepEqual(state) {
  13. return JSON.parse(JSON.stringify(state));
  14. }
  15. function closeRightTabsAndAddNewTab(arr, index, name, params) {
  16. let newParamsList = [].concat(arr);
  17. newParamsList.splice(index + 1, newParamsList.length - index);
  18. newParamsList.push({
  19. name: '',
  20. params: []
  21. });
  22. let curParams = params || [];
  23. let curname = name || '';
  24. newParamsList[index] = {
  25. ...newParamsList[index],
  26. name: curname,
  27. params: curParams
  28. };
  29. return newParamsList;
  30. }
  31. class ModalPostman extends Component {
  32. static propTypes = {
  33. visible: PropTypes.bool,
  34. handleCancel: PropTypes.func,
  35. handleOk: PropTypes.func,
  36. inputValue: PropTypes.any,
  37. envType: PropTypes.string,
  38. id: PropTypes.number
  39. };
  40. constructor(props) {
  41. super(props);
  42. this.state = {
  43. methodsShow: false,
  44. methodsShowMore: false,
  45. methodsList: [],
  46. constantInput: '',
  47. activeKey: '1',
  48. methodsParamsList: [
  49. {
  50. name: '',
  51. params: [],
  52. type: 'dataSource'
  53. }
  54. ]
  55. };
  56. }
  57. componentWillMount() {
  58. let { inputValue } = this.props;
  59. this.setState({
  60. constantInput: inputValue
  61. });
  62. // this.props.inputValue && this.handleConstantsInput(this.props.inputValue, 0);
  63. inputValue && this.handleInitList(inputValue);
  64. }
  65. handleInitList(val) {
  66. val = val.replace(/^\{\{(.+)\}\}$/g, '$1');
  67. let valArr = val.split('|');
  68. if (valArr[0].indexOf('@') >= 0) {
  69. this.setState({
  70. activeKey: '2'
  71. });
  72. } else if (valArr[0].indexOf('$') >= 0) {
  73. this.setState({
  74. activeKey: '3'
  75. });
  76. }
  77. let paramsList = [
  78. {
  79. name: trim(valArr[0]),
  80. params: [],
  81. type: 'dataSource'
  82. }
  83. ];
  84. for (let i = 1; i < valArr.length; i++) {
  85. let nameArr = valArr[i].split(':');
  86. let paramArr = nameArr[1] && nameArr[1].split(',');
  87. paramArr =
  88. paramArr &&
  89. paramArr.map(item => {
  90. return trim(item);
  91. });
  92. let item = {
  93. name: trim(nameArr[0]),
  94. params: paramArr || []
  95. };
  96. paramsList.push(item);
  97. }
  98. this.setState(
  99. {
  100. methodsParamsList: paramsList
  101. },
  102. () => {
  103. this.mockClick(valArr.length)();
  104. }
  105. );
  106. }
  107. mockClick(index) {
  108. return (curname, params) => {
  109. let newParamsList = closeRightTabsAndAddNewTab(
  110. this.state.methodsParamsList,
  111. index,
  112. curname,
  113. params
  114. );
  115. this.setState({
  116. methodsParamsList: newParamsList
  117. });
  118. };
  119. }
  120. // 处理常量输入
  121. handleConstantsInput = val => {
  122. val = val.replace(/^\{\{(.+)\}\}$/g, '$1');
  123. this.setState({
  124. constantInput: val
  125. });
  126. this.mockClick(0)(val);
  127. };
  128. handleParamsInput = (e, clickIndex, paramsIndex) => {
  129. let newParamsList = deepEqual(this.state.methodsParamsList);
  130. newParamsList[clickIndex].params[paramsIndex] = e;
  131. this.setState({
  132. methodsParamsList: newParamsList
  133. });
  134. };
  135. // 方法
  136. MethodsListSource = props => {
  137. return (
  138. <MethodsList
  139. click={this.mockClick(props.index)}
  140. clickValue={props.value}
  141. params={props.params}
  142. paramsInput={this.handleParamsInput}
  143. clickIndex={props.index}
  144. />
  145. );
  146. };
  147. // 处理表达式
  148. handleValue(val) {
  149. return handleParamsValue(val, {});
  150. }
  151. // 处理错误
  152. handleError() {
  153. return (
  154. <Alert
  155. message="请求“变量集”尚未运行,所以我们无法从其响应中提取的值。您可以在测试集合中测试这些变量。"
  156. type="warning"
  157. />
  158. );
  159. }
  160. // 初始化
  161. setInit() {
  162. let initParamsList = [
  163. {
  164. name: '',
  165. params: [],
  166. type: 'dataSource'
  167. }
  168. ];
  169. this.setState({
  170. methodsParamsList: initParamsList
  171. });
  172. }
  173. // 处理取消插入
  174. handleCancel = () => {
  175. this.setInit();
  176. this.props.handleCancel();
  177. };
  178. // 处理插入
  179. handleOk = installValue => {
  180. this.props.handleOk(installValue);
  181. this.setInit();
  182. };
  183. // 处理面板切换
  184. handleCollapse = key => {
  185. this.setState({
  186. activeKey: key
  187. });
  188. };
  189. render() {
  190. const { visible, envType } = this.props;
  191. const { methodsParamsList, constantInput } = this.state;
  192. const outputParams = () => {
  193. let str = '';
  194. let length = methodsParamsList.length;
  195. methodsParamsList.forEach((item, index) => {
  196. let isShow = item.name && length - 2 !== index;
  197. str += item.name;
  198. item.params.forEach((item, index) => {
  199. let isParams = index > 0;
  200. str += isParams ? ' , ' : ' : ';
  201. str += item;
  202. });
  203. str += isShow ? ' | ' : '';
  204. });
  205. return '{{ ' + str + ' }}';
  206. };
  207. return (
  208. <Modal
  209. title={
  210. <p>
  211. <Icon type="edit" /> 高级参数设置
  212. </p>
  213. }
  214. visible={visible}
  215. onOk={() => this.handleOk(outputParams())}
  216. onCancel={this.handleCancel}
  217. wrapClassName="modal-postman"
  218. width={1024}
  219. maskClosable={false}
  220. okText="插入"
  221. >
  222. <Row className="modal-postman-form" type="flex">
  223. {methodsParamsList.map((item, index) => {
  224. return item.type === 'dataSource' ? (
  225. <Col span={8} className="modal-postman-col" key={index}>
  226. <Collapse
  227. className="modal-postman-collapse"
  228. activeKey={this.state.activeKey}
  229. onChange={this.handleCollapse}
  230. bordered={false}
  231. accordion
  232. >
  233. <Panel header={<h3 className="mock-title">常量</h3>} key="1">
  234. <Input
  235. placeholder="基础参数值"
  236. value={constantInput}
  237. onChange={e => this.handleConstantsInput(e.target.value, index)}
  238. />
  239. </Panel>
  240. <Panel header={<h3 className="mock-title">mock数据</h3>} key="2">
  241. <MockList click={this.mockClick(index)} clickValue={item.name} />
  242. </Panel>
  243. {envType === 'case' && (
  244. <Panel
  245. header={
  246. <h3 className="mock-title">
  247. 变量&nbsp;<Tooltip
  248. placement="top"
  249. title="YApi 提供了强大的变量参数功能,你可以在测试的时候使用前面接口的 参数 或 返回值 作为 后面接口的参数,即使接口之间存在依赖,也可以轻松 一键测试~"
  250. >
  251. <Icon type="question-circle-o" />
  252. </Tooltip>
  253. </h3>
  254. }
  255. key="3"
  256. >
  257. <VariablesSelect
  258. id={this.props.id}
  259. click={this.mockClick(index)}
  260. clickValue={item.name}
  261. />
  262. </Panel>
  263. )}
  264. </Collapse>
  265. </Col>
  266. ) : (
  267. <Col span={8} className="modal-postman-col" key={index}>
  268. <this.MethodsListSource index={index} value={item.name} params={item.params} />
  269. </Col>
  270. );
  271. })}
  272. </Row>
  273. <Row className="modal-postman-expression">
  274. <Col span={6}>
  275. <h3 className="title">表达式</h3>
  276. </Col>
  277. <Col span={18}>
  278. <span className="expression-item">{outputParams()}</span>
  279. </Col>
  280. </Row>
  281. <Row className="modal-postman-preview">
  282. <Col span={6}>
  283. <h3 className="title">预览</h3>
  284. </Col>
  285. <Col span={18}>
  286. <h3>{this.handleValue(outputParams()) || (outputParams() && this.handleError())}</h3>
  287. </Col>
  288. </Row>
  289. </Modal>
  290. );
  291. }
  292. }
  293. export default ModalPostman;