| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383 |
- import React, { Component } from 'react';
- import PropTypes from 'prop-types';
- import './index.scss';
- import { Icon, Row, Col, Form, Input, Select, Button, AutoComplete, Tooltip } from 'antd';
- const FormItem = Form.Item;
- const Option = Select.Option;
- import constants from 'client/constants/variable.js';
- const initMap = {
- header: [
- {
- name: '',
- value: ''
- }
- ],
- cookie: [
- {
- name: '',
- value: ''
- }
- ],
- global: [
- {
- name: '',
- value: ''
- }
- ]
- };
- class ProjectEnvContent extends Component {
- static propTypes = {
- projectMsg: PropTypes.object,
- form: PropTypes.object,
- onSubmit: PropTypes.func,
- handleEnvInput: PropTypes.func
- };
- initState(curdata) {
- let header = [
- {
- name: '',
- value: ''
- }
- ];
- let cookie = [
- {
- name: '',
- value: ''
- }
- ];
- let global = [
- {
- name: '',
- value: ''
- }
- ];
- const curheader = curdata.header;
- const curGlobal = curdata.global;
- if (curheader && curheader.length !== 0) {
- curheader.forEach(item => {
- if (item.name === 'Cookie') {
- let cookieStr = item.value;
- if (cookieStr) {
- cookieStr = cookieStr.split(';').forEach(c => {
- if (c) {
- c = c.split('=');
- cookie.unshift({
- name: c[0] ? c[0].trim() : '',
- value: c[1] ? c[1].trim() : ''
- });
- }
- });
- }
- } else {
- header.unshift(item);
- }
- });
- }
- if (curGlobal && curGlobal.length !== 0) {
- curGlobal.forEach(item => {
- global.unshift(item);
- });
- }
- return { header, cookie, global };
- }
- constructor(props) {
- super(props);
- this.state = Object.assign({}, initMap);
- }
- addHeader = (value, index, name) => {
- let nextHeader = this.state[name][index + 1];
- if (nextHeader && typeof nextHeader === 'object') {
- return;
- }
- let newValue = {};
- let data = { name: '', value: '' };
- newValue[name] = [].concat(this.state[name], data);
- this.setState(newValue);
- };
- delHeader = (key, name) => {
- let curValue = this.props.form.getFieldValue(name);
- let newValue = {};
- newValue[name] = curValue.filter((val, index) => {
- return index !== key;
- });
- this.props.form.setFieldsValue(newValue);
- this.setState(newValue);
- };
- handleInit(data) {
- this.props.form.resetFields();
- let newValue = this.initState(data);
- this.setState({ ...newValue });
- }
- componentWillReceiveProps(nextProps) {
- let curEnvName = this.props.projectMsg.name;
- let nextEnvName = nextProps.projectMsg.name;
- if (curEnvName !== nextEnvName) {
- this.handleInit(nextProps.projectMsg);
- }
- }
- handleOk = e => {
- e.preventDefault();
- const { form, onSubmit, projectMsg } = this.props;
- form.validateFields((err, values) => {
- if (!err) {
- let header = values.header.filter(val => {
- return val.name !== '';
- });
- let cookie = values.cookie.filter(val => {
- return val.name !== '';
- });
- let global = values.global.filter(val => {
- return val.name !== '';
- });
- if (cookie.length > 0) {
- header.push({
- name: 'Cookie',
- value: cookie.map(item => item.name + '=' + item.value).join(';')
- });
- }
- let assignValue = {};
- assignValue.env = Object.assign(
- { _id: projectMsg._id },
- {
- name: values.env.name,
- domain: values.env.protocol + values.env.domain,
- header: header,
- global
- }
- );
- onSubmit(assignValue);
- }
- });
- };
- render() {
- const { projectMsg } = this.props;
- const { getFieldDecorator } = this.props.form;
- const headerTpl = (item, index) => {
- const headerLength = this.state.header.length - 1;
- return (
- <Row gutter={2} key={index}>
- <Col span={10}>
- <FormItem>
- {getFieldDecorator('header[' + index + '].name', {
- validateTrigger: ['onChange', 'onBlur'],
- initialValue: item.name || ''
- })(
- <AutoComplete
- style={{ width: '200px' }}
- allowClear={true}
- dataSource={constants.HTTP_REQUEST_HEADER}
- placeholder="请输入header名称"
- onChange={() => this.addHeader(item, index, 'header')}
- filterOption={(inputValue, option) =>
- option.props.children.toUpperCase().indexOf(inputValue.toUpperCase()) !== -1
- }
- />
- )}
- </FormItem>
- </Col>
- <Col span={12}>
- <FormItem>
- {getFieldDecorator('header[' + index + '].value', {
- validateTrigger: ['onChange', 'onBlur'],
- initialValue: item.value || ''
- })(<Input placeholder="请输入参数内容" style={{ width: '90%', marginRight: 8 }} />)}
- </FormItem>
- </Col>
- <Col span={2} className={index === headerLength ? ' env-last-row' : null}>
- {/* 新增的项中,只有最后一项没有有删除按钮 */}
- <Icon
- className="dynamic-delete-button delete"
- type="delete"
- onClick={e => {
- e.stopPropagation();
- this.delHeader(index, 'header');
- }}
- />
- </Col>
- </Row>
- );
- };
- const commonTpl = (item, index, name) => {
- const length = this.state[name].length - 1;
- return (
- <Row gutter={2} key={index}>
- <Col span={10}>
- <FormItem>
- {getFieldDecorator(`${name}[${index}].name`, {
- validateTrigger: ['onChange', 'onBlur'],
- initialValue: item.name || ''
- })(
- <Input
- placeholder={`请输入 ${name} Name`}
- style={{ width: '200px' }}
- onChange={() => this.addHeader(item, index, name)}
- />
- )}
- </FormItem>
- </Col>
- <Col span={12}>
- <FormItem>
- {getFieldDecorator(`${name}[${index}].value`, {
- validateTrigger: ['onChange', 'onBlur'],
- initialValue: item.value || ''
- })(<Input placeholder="请输入参数内容" style={{ width: '90%', marginRight: 8 }} />)}
- </FormItem>
- </Col>
- <Col span={2} className={index === length ? ' env-last-row' : null}>
- {/* 新增的项中,只有最后一项没有有删除按钮 */}
- <Icon
- className="dynamic-delete-button delete"
- type="delete"
- onClick={e => {
- e.stopPropagation();
- this.delHeader(index, name);
- }}
- />
- </Col>
- </Row>
- );
- };
- const envTpl = data => {
- return (
- <div>
- <h3 className="env-label">环境名称</h3>
- <FormItem required={false}>
- {getFieldDecorator('env.name', {
- validateTrigger: ['onChange', 'onBlur'],
- initialValue: data.name === '新环境' ? '' : data.name || '',
- rules: [
- {
- required: false,
- whitespace: true,
- validator(rule, value, callback) {
- if (value) {
- if (value.length === 0) {
- callback('请输入环境名称');
- } else if (!/\S/.test(value)) {
- callback('请输入环境名称');
- } else {
- return callback();
- }
- } else {
- callback('请输入环境名称');
- }
- }
- }
- ]
- })(
- <Input
- onChange={e => this.props.handleEnvInput(e.target.value)}
- placeholder="请输入环境名称"
- style={{ width: '90%', marginRight: 8 }}
- />
- )}
- </FormItem>
- <h3 className="env-label">环境域名</h3>
- <FormItem required={false}>
- {getFieldDecorator('env.domain', {
- validateTrigger: ['onChange', 'onBlur'],
- initialValue: data.domain ? data.domain.split('//')[1] : '',
- rules: [
- {
- required: false,
- whitespace: true,
- validator(rule, value, callback) {
- if (value) {
- if (value.length === 0) {
- callback('请输入环境域名!');
- } else if (/\s/.test(value)) {
- callback('环境域名不允许出现空格!');
- } else {
- return callback();
- }
- } else {
- callback('请输入环境域名!');
- }
- }
- }
- ]
- })(
- <Input
- placeholder="请输入环境域名"
- style={{ width: '90%', marginRight: 8 }}
- addonBefore={getFieldDecorator('env.protocol', {
- initialValue: data.domain ? data.domain.split('//')[0] + '//' : 'http://',
- rules: [
- {
- required: true
- }
- ]
- })(
- <Select>
- <Option value="http://">{'http://'}</Option>
- <Option value="https://">{'https://'}</Option>
- </Select>
- )}
- />
- )}
- </FormItem>
- <h3 className="env-label">Header</h3>
- {this.state.header.map((item, index) => {
- return headerTpl(item, index);
- })}
- <h3 className="env-label">Cookie</h3>
- {this.state.cookie.map((item, index) => {
- return commonTpl(item, index, 'cookie');
- })}
- <h3 className="env-label">
- global
- <a
- target="_blank"
- rel="noopener noreferrer"
- href="https://hellosean1025.github.io/yapi/documents/project.html#%E9%85%8D%E7%BD%AE%E7%8E%AF%E5%A2%83"
- style={{ marginLeft: 8 }}
- >
- <Tooltip title="点击查看文档">
- <Icon type="question-circle-o" style={{fontSize: '13px'}}/>
- </Tooltip>
- </a>
- </h3>
- {this.state.global.map((item, index) => {
- return commonTpl(item, index, 'global');
- })}
- </div>
- );
- };
- return (
- <div>
- {envTpl(projectMsg)}
- <div className="btnwrap-changeproject">
- <Button
- className="m-btn btn-save"
- icon="save"
- type="primary"
- size="large"
- onClick={this.handleOk}
- >
- 保 存
- </Button>
- </div>
- </div>
- );
- }
- }
- export default Form.create()(ProjectEnvContent);
|