董国庆
10 小时以前 63ecd83a0f17442cb1ce3b497f4c7932fda4177f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { sendRequest } from '@/utils/antdUtils';
import { LoadingOutlined, PlusOutlined } from '@ant-design/icons';
import { PageContainer, } from '@ant-design/pro-components';
import { Button, Select, Row, Col, Input, Card, Space, Form, Upload, Spin, message, InputNumber } from 'antd';
import { useState, useEffect } from 'react';
import { addAndEdit, getDetail } from '../service';
import { history, useLocation } from 'umi';
import { customRequest } from '@/utils/utils';
const AddOrEditOrDetail = () => {
 
  const [form] = Form.useForm();
  const [fileList, setFileList] = useState([])//banner图片
  const [loading, setLoading] = useState(false);
  const { search } = useLocation();
  const searchParams = new URLSearchParams(search);
 
  const config = {
    name: 'file',
    action: BASE_URL + '/file/obs/upload',
    headers: {
      authorization: localStorage.getItem('token'),
    },
  };
  const formItemLayout = {
    labelCol: { span: 6 },
    wrapperCol: { span: 20 },
  };
  const uploadButton = (text) => {
    return <div>
      <PlusOutlined />
      <div
        style={{
          marginTop: 8,
        }}
      >
      </div>
    </div>
  };
  useEffect(() => {
    if (searchParams.get('id')) {
      getDetail( searchParams.get('id') ).then(res => {
          if (res.data.picUrl) {
              let obj = [{
                  uid: 1,
                  name: 'banner',
                  url: res.data.picUrl
              }]
              setFileList(obj)
          }
          form.setFieldsValue(res.data)
      })
    }
  }, [])
 
  // 上传banner前
  const beforeUpload = (file) => {
    return new Promise(async (resolve, reject) => {
      if (file.name.includes(',')) {
        message.warning('上传文件不能包含英文逗号(,)')
        return Upload.LIST_IGNORE
      }
      setLoading(true)
      resolve(file)
    });
  };
  // 上传banner
  const handleChange = ({ file: file, fileList: newFileList }) => {
    if (file.status == 'error') {
      setLoading(false)
      setFileList([])
      message.error('上传失败')
      return
    }
    if (file.status == 'done') {
      setLoading(false)
      message.success('上传成功')
    }
    newFileList.map((item) => {
      if (!item.url && item.status == 'done') {
        item.url = item.response.data;
      }
    });
    setFileList(newFileList);
  };
 
  // 提交表单
  const submit = () => {
    form.validateFields().then(async (values) => {
      
      values.picUrl = fileList[0].url
      delete values.image
      if (searchParams.get('id')) {
          values.id = searchParams.get('id')
          let state = await sendRequest(addAndEdit, values)
          if (state) {
              history.back()
          }
          return
      }
      let state = await sendRequest(addAndEdit, values)
      if (state) {
          history.back()
      }
    })
  }
  return (
    <PageContainer title={searchParams.get('detail') ? '查看详情' : searchParams.get('id') ? '编辑banner' : '添加banner'}>
      <Spin spinning={loading}>
        <Form scrollToFirstError layout="horizontal" {...formItemLayout} form={form}>
          <Card style={{ background: '#fff', paddingTop: '15px' }}>
            <Row>
              <Col span={12}>
                <Form.Item
                  name="bannerName"
                  label='banner名称' rules={[
                    {
                      required: true,
                      message: '请输入banner名称',
                    },
                  ]}
                >
                  <Input disabled={searchParams.get('detail')} placeholder='请输入banner名称'></Input>
                </Form.Item>
 
                <Form.Item
                  name="image"
                  label="banner图片"
                  extra={
                    <div>
                      <div>推荐尺寸732px * 320px</div>
                    </div>
                  }
                  rules={[
                    {
                      required: fileList.length == 0 ? true : false,
                      message: '请上传banner图片',
                    },
                  ]}
                >
                  <Upload
                    {...config}
                    listType="picture-card"
                    maxCount={1}
                    beforeUpload={beforeUpload}
                    onChange={handleChange}
                    showUploadList={{
                      showPreviewIcon: false,
                    }}
                    customRequest={customRequest}
                    // accept="image/png, image/jpeg, image/jpg"
                    fileList={fileList}
                    disabled={searchParams.get('detail')}
                  >
                    {fileList?.length == 1 || searchParams.get('detail') ? null : uploadButton()}
                  </Upload>
                </Form.Item>
              </Col>
            </Row>
            <div style={{ display: 'flex', justifyContent: 'center' }}>
              <Space size='large'>
                <Button onClick={() => history.back()}>关闭</Button>
                {
                  !searchParams.get('detail') && <Button type='primary' onClick={submit}>保存</Button>
                }
              </Space>
            </div>
          </Card>
        </Form>
      </Spin>
    </PageContainer >
  );
}
 
export default AddOrEditOrDetail