import React, { Component } from 'react' import { Modal, Button, Input, message, Upload } from 'antd' import { FormItem } from 'wptpc-design' import { yc } from '@/conf/config' const { TextArea } = Input class Index extends Component { state = { data: null, style: null, params: null }; constructor (props) { super(props) let params = null if (props.params) { params = props.params params.header = props.params.header || [] } this.state = { params: { detail: [], header: [], needNew: 0, name: '', getTokenUrl: '', ...params } } if (typeof props.getValue === 'function') { props.getValue(this.onOk) } } // 设置 header 属性字段 setHeader (index, value, key) { const { params } = this.state if (params.header[index]) { params.header[index][key] = value } this.setState({ params: params }) } // 新增属性字段 addHeader = () => { const { params } = this.state this.setState({ params: { ...params, header: [ ...params.header, { key: '', value: '' } ] } }) }; // 删除属性字段 delHeader= (index) => this.setState((prevState) => ({ params: { ...prevState.params, header: prevState.params.header.filter((_, i) => i !== index) } })); // 设置属性字段 setDetail (i, v, k) { const { params } = this.state if (params.detail[i]) { params.detail[i][k] = v } this.setState({ params: params }) } // 新增属性字段 addDetail = () => { const { params } = this.state this.setState({ params: { ...params, detail: [ ...params.detail, { url: '' } ] } }) }; // 删除属性字段 delDetail = (index) => this.setState((prevState) => ({ params: { ...prevState.params, detail: prevState.params.detail.filter((_, i) => i !== index) } })); // 统一change onParamsChange = (k, v) => { const { params } = this.state const newParams = { ...params } this.setState( { params: { ...newParams, [k]: v } } ) }; onOk = (cb) => { if (!this.getCheck()) { return } const { params = {} } = this.state const { detail = [] } = params // 接口内容空值判断 if (!detail.length) { message.warning('压测 接口内容 不能为空!') return } // 接口 url 空值判断 if (!detail.every((value) => value.url)) { message.warning('压测 接口 url 不能为空!') return } if (typeof this.props.onOk === 'function') { this.props.onOk({ ...params }) } if (typeof cb === 'function') { // eslint-disable-next-line standard/no-callback-literal cb({ ...params }) } }; render () { const { params = {} } = this.state const { onCancel, showModal, update } = this.props const detail = params.detail || [] const header = params.header || [] const settingProps = [] const uploadProps = { showUploadList: false, name: 'file', action: `${yc}/schedule/task/import`, onChange: (info) => { if (info.file.status === 'done') { if (info.file.response.code !== 0) { return message.error(info.file.response.msg) } if (info.file.response.data.length > 0) { const { params } = this.state params.detail = info.file.response.data.map((url) => ({ url })) this.setState({ params: params }) message.success(`${info.file.name} 文件上传成功`) } } else if (info.file.status === 'error') { message.error(`${info.file.name} 文件上传失败`) } } } if (update) { settingProps.push({ label: '需要新压测数据', extra: '如果你不明白这个选项是什么,请不要修改~', extraInline: true, key: 'needNew', type: 'switch', checkedChildren: '是', unCheckedChildren: '否' }) } this.formSetting = [ ...settingProps, { label: '任务名', key: 'name', placeholder: '请输入压测任务名', isRequired: true, type: 'input' }, { label: '登录 URL', key: 'getTokenUrl', placeholder: '请输入用户登录的 URL', isRequired: true, type: 'input' }, { label: '自定义 header', key: 'header', isRequired: false, render: () => { return (
{header.map((d, i) => (
this.setHeader(i, e.target.value, 'key') } /> this.setHeader(i, e.target.value, 'value') } />
))} {!header.length && (
) } }, { label: '压测接口', key: 'detail', isRequired: true, render: () => { return (
{detail.map((d, i) => (