mock平台

ProjectEnvContent.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. import React, { Component } from 'react';
  2. import PropTypes from 'prop-types';
  3. import './index.scss';
  4. import { Icon, Row, Col, Form, Input, Select, Button, AutoComplete, Tooltip } from 'antd';
  5. const FormItem = Form.Item;
  6. const Option = Select.Option;
  7. import constants from 'client/constants/variable.js';
  8. const initMap = {
  9. header: [
  10. {
  11. name: '',
  12. value: ''
  13. }
  14. ],
  15. cookie: [
  16. {
  17. name: '',
  18. value: ''
  19. }
  20. ],
  21. global: [
  22. {
  23. name: '',
  24. value: ''
  25. }
  26. ]
  27. };
  28. class ProjectEnvContent extends Component {
  29. static propTypes = {
  30. projectMsg: PropTypes.object,
  31. form: PropTypes.object,
  32. onSubmit: PropTypes.func,
  33. handleEnvInput: PropTypes.func
  34. };
  35. initState(curdata) {
  36. let header = [
  37. {
  38. name: '',
  39. value: ''
  40. }
  41. ];
  42. let cookie = [
  43. {
  44. name: '',
  45. value: ''
  46. }
  47. ];
  48. let global = [
  49. {
  50. name: '',
  51. value: ''
  52. }
  53. ];
  54. const curheader = curdata.header;
  55. const curGlobal = curdata.global;
  56. if (curheader && curheader.length !== 0) {
  57. curheader.forEach(item => {
  58. if (item.name === 'Cookie') {
  59. let cookieStr = item.value;
  60. if (cookieStr) {
  61. cookieStr = cookieStr.split(';').forEach(c => {
  62. if (c) {
  63. c = c.split('=');
  64. cookie.unshift({
  65. name: c[0] ? c[0].trim() : '',
  66. value: c[1] ? c[1].trim() : ''
  67. });
  68. }
  69. });
  70. }
  71. } else {
  72. header.unshift(item);
  73. }
  74. });
  75. }
  76. if (curGlobal && curGlobal.length !== 0) {
  77. curGlobal.forEach(item => {
  78. global.unshift(item);
  79. });
  80. }
  81. return { header, cookie, global };
  82. }
  83. constructor(props) {
  84. super(props);
  85. this.state = Object.assign({}, initMap);
  86. }
  87. addHeader = (value, index, name) => {
  88. let nextHeader = this.state[name][index + 1];
  89. if (nextHeader && typeof nextHeader === 'object') {
  90. return;
  91. }
  92. let newValue = {};
  93. let data = { name: '', value: '' };
  94. newValue[name] = [].concat(this.state[name], data);
  95. this.setState(newValue);
  96. };
  97. delHeader = (key, name) => {
  98. let curValue = this.props.form.getFieldValue(name);
  99. let newValue = {};
  100. newValue[name] = curValue.filter((val, index) => {
  101. return index !== key;
  102. });
  103. this.props.form.setFieldsValue(newValue);
  104. this.setState(newValue);
  105. };
  106. handleInit(data) {
  107. this.props.form.resetFields();
  108. let newValue = this.initState(data);
  109. this.setState({ ...newValue });
  110. }
  111. componentWillReceiveProps(nextProps) {
  112. let curEnvName = this.props.projectMsg.name;
  113. let nextEnvName = nextProps.projectMsg.name;
  114. if (curEnvName !== nextEnvName) {
  115. this.handleInit(nextProps.projectMsg);
  116. }
  117. }
  118. handleOk = e => {
  119. e.preventDefault();
  120. const { form, onSubmit, projectMsg } = this.props;
  121. form.validateFields((err, values) => {
  122. if (!err) {
  123. let header = values.header.filter(val => {
  124. return val.name !== '';
  125. });
  126. let cookie = values.cookie.filter(val => {
  127. return val.name !== '';
  128. });
  129. let global = values.global.filter(val => {
  130. return val.name !== '';
  131. });
  132. if (cookie.length > 0) {
  133. header.push({
  134. name: 'Cookie',
  135. value: cookie.map(item => item.name + '=' + item.value).join(';')
  136. });
  137. }
  138. let assignValue = {};
  139. assignValue.env = Object.assign(
  140. { _id: projectMsg._id },
  141. {
  142. name: values.env.name,
  143. domain: values.env.protocol + values.env.domain,
  144. header: header,
  145. global
  146. }
  147. );
  148. onSubmit(assignValue);
  149. }
  150. });
  151. };
  152. render() {
  153. const { projectMsg } = this.props;
  154. const { getFieldDecorator } = this.props.form;
  155. const headerTpl = (item, index) => {
  156. const headerLength = this.state.header.length - 1;
  157. return (
  158. <Row gutter={2} key={index}>
  159. <Col span={10}>
  160. <FormItem>
  161. {getFieldDecorator('header[' + index + '].name', {
  162. validateTrigger: ['onChange', 'onBlur'],
  163. initialValue: item.name || ''
  164. })(
  165. <AutoComplete
  166. style={{ width: '200px' }}
  167. allowClear={true}
  168. dataSource={constants.HTTP_REQUEST_HEADER}
  169. placeholder="请输入header名称"
  170. onChange={() => this.addHeader(item, index, 'header')}
  171. filterOption={(inputValue, option) =>
  172. option.props.children.toUpperCase().indexOf(inputValue.toUpperCase()) !== -1
  173. }
  174. />
  175. )}
  176. </FormItem>
  177. </Col>
  178. <Col span={12}>
  179. <FormItem>
  180. {getFieldDecorator('header[' + index + '].value', {
  181. validateTrigger: ['onChange', 'onBlur'],
  182. initialValue: item.value || ''
  183. })(<Input placeholder="请输入参数内容" style={{ width: '90%', marginRight: 8 }} />)}
  184. </FormItem>
  185. </Col>
  186. <Col span={2} className={index === headerLength ? ' env-last-row' : null}>
  187. {/* 新增的项中,只有最后一项没有有删除按钮 */}
  188. <Icon
  189. className="dynamic-delete-button delete"
  190. type="delete"
  191. onClick={e => {
  192. e.stopPropagation();
  193. this.delHeader(index, 'header');
  194. }}
  195. />
  196. </Col>
  197. </Row>
  198. );
  199. };
  200. const commonTpl = (item, index, name) => {
  201. const length = this.state[name].length - 1;
  202. return (
  203. <Row gutter={2} key={index}>
  204. <Col span={10}>
  205. <FormItem>
  206. {getFieldDecorator(`${name}[${index}].name`, {
  207. validateTrigger: ['onChange', 'onBlur'],
  208. initialValue: item.name || ''
  209. })(
  210. <Input
  211. placeholder={`请输入 ${name} Name`}
  212. style={{ width: '200px' }}
  213. onChange={() => this.addHeader(item, index, name)}
  214. />
  215. )}
  216. </FormItem>
  217. </Col>
  218. <Col span={12}>
  219. <FormItem>
  220. {getFieldDecorator(`${name}[${index}].value`, {
  221. validateTrigger: ['onChange', 'onBlur'],
  222. initialValue: item.value || ''
  223. })(<Input placeholder="请输入参数内容" style={{ width: '90%', marginRight: 8 }} />)}
  224. </FormItem>
  225. </Col>
  226. <Col span={2} className={index === length ? ' env-last-row' : null}>
  227. {/* 新增的项中,只有最后一项没有有删除按钮 */}
  228. <Icon
  229. className="dynamic-delete-button delete"
  230. type="delete"
  231. onClick={e => {
  232. e.stopPropagation();
  233. this.delHeader(index, name);
  234. }}
  235. />
  236. </Col>
  237. </Row>
  238. );
  239. };
  240. const envTpl = data => {
  241. return (
  242. <div>
  243. <h3 className="env-label">环境名称</h3>
  244. <FormItem required={false}>
  245. {getFieldDecorator('env.name', {
  246. validateTrigger: ['onChange', 'onBlur'],
  247. initialValue: data.name === '新环境' ? '' : data.name || '',
  248. rules: [
  249. {
  250. required: false,
  251. whitespace: true,
  252. validator(rule, value, callback) {
  253. if (value) {
  254. if (value.length === 0) {
  255. callback('请输入环境名称');
  256. } else if (!/\S/.test(value)) {
  257. callback('请输入环境名称');
  258. } else {
  259. return callback();
  260. }
  261. } else {
  262. callback('请输入环境名称');
  263. }
  264. }
  265. }
  266. ]
  267. })(
  268. <Input
  269. onChange={e => this.props.handleEnvInput(e.target.value)}
  270. placeholder="请输入环境名称"
  271. style={{ width: '90%', marginRight: 8 }}
  272. />
  273. )}
  274. </FormItem>
  275. <h3 className="env-label">环境域名</h3>
  276. <FormItem required={false}>
  277. {getFieldDecorator('env.domain', {
  278. validateTrigger: ['onChange', 'onBlur'],
  279. initialValue: data.domain ? data.domain.split('//')[1] : '',
  280. rules: [
  281. {
  282. required: false,
  283. whitespace: true,
  284. validator(rule, value, callback) {
  285. if (value) {
  286. if (value.length === 0) {
  287. callback('请输入环境域名!');
  288. } else if (/\s/.test(value)) {
  289. callback('环境域名不允许出现空格!');
  290. } else {
  291. return callback();
  292. }
  293. } else {
  294. callback('请输入环境域名!');
  295. }
  296. }
  297. }
  298. ]
  299. })(
  300. <Input
  301. placeholder="请输入环境域名"
  302. style={{ width: '90%', marginRight: 8 }}
  303. addonBefore={getFieldDecorator('env.protocol', {
  304. initialValue: data.domain ? data.domain.split('//')[0] + '//' : 'http://',
  305. rules: [
  306. {
  307. required: true
  308. }
  309. ]
  310. })(
  311. <Select>
  312. <Option value="http://">{'http://'}</Option>
  313. <Option value="https://">{'https://'}</Option>
  314. </Select>
  315. )}
  316. />
  317. )}
  318. </FormItem>
  319. <h3 className="env-label">Header</h3>
  320. {this.state.header.map((item, index) => {
  321. return headerTpl(item, index);
  322. })}
  323. <h3 className="env-label">Cookie</h3>
  324. {this.state.cookie.map((item, index) => {
  325. return commonTpl(item, index, 'cookie');
  326. })}
  327. <h3 className="env-label">
  328. global
  329. <a
  330. target="_blank"
  331. rel="noopener noreferrer"
  332. href="https://hellosean1025.github.io/yapi/documents/project.html#%E9%85%8D%E7%BD%AE%E7%8E%AF%E5%A2%83"
  333. style={{ marginLeft: 8 }}
  334. >
  335. <Tooltip title="点击查看文档">
  336. <Icon type="question-circle-o" style={{fontSize: '13px'}}/>
  337. </Tooltip>
  338. </a>
  339. </h3>
  340. {this.state.global.map((item, index) => {
  341. return commonTpl(item, index, 'global');
  342. })}
  343. </div>
  344. );
  345. };
  346. return (
  347. <div>
  348. {envTpl(projectMsg)}
  349. <div className="btnwrap-changeproject">
  350. <Button
  351. className="m-btn btn-save"
  352. icon="save"
  353. type="primary"
  354. size="large"
  355. onClick={this.handleOk}
  356. >
  357. 保 存
  358. </Button>
  359. </div>
  360. </div>
  361. );
  362. }
  363. }
  364. export default Form.create()(ProjectEnvContent);