董国庆
2026-02-24 4a180c0ef43733522a0d39aec6289f4a7bdf23bd
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
import { sendRequest } from '@/utils/antdUtils';
import { CloseOutlined, LoadingOutlined, PlusOutlined } from '@ant-design/icons';
import { PageContainer } from '@ant-design/pro-components';
import { history, useLocation } from '@umijs/max';
import { Button, Card, Col, Form, Image, Input, message, Row, Select, Upload } from 'antd';
import { useEffect, useState } from 'react';
import { add, edit, subjectInfo, tCategoryList, tCategorylistById } from './service';
 
const SystemSettings = () => {
  const formItemLayout = {
    labelCol: { span: 2 },
    wrapperCol: { span: 8 },
  };
  const { search } = useLocation();
  const searchParams = new URLSearchParams(search);
  const [title, setTitle] = useState('');
  const [form] = Form.useForm();
  const [deptList, setDeptList] = useState([
    { deptName: '自主学习一', deptId: '自主学习一' },
    { deptName: '自主学习二', deptId: '自主学习二' },
    { deptName: '自主学习三', deptId: '自主学习三' },
    { deptName: '自主学习四', deptId: '自主学习四' },
    { deptName: '自主学习五', deptId: '自主学习五' },
  ]);
  const [loading, setLoading] = useState(false);
  const [imgUrl, setImgUrl] = useState(''); // 分享图片
  const [correctMp3Url, setCorrectMp3UrlUrl] = useState(''); // 正确音效
  const [errMp3Url, setErrMp3UrlUrl] = useState(''); // 错误音效
  const [errMp3Url1, setErrMp3UrlUrl1] = useState(''); // 错误音效
 
  const [firstTypeList, setFirstTypeList] = useState([]);
  const [secondTypeList, setSecondTypeList] = useState([]);
 
  useEffect(() => {
    tCategoryList({ state: 1 }).then((res) => {
      setFirstTypeList(res.data);
    });
 
    if (searchParams.get('id')) {
      subjectInfo(searchParams.get('id')).then((res) => {
        res.data.type = res.data.type.split('、');
        setImgUrl(res.data.img);
        setCorrectMp3UrlUrl(res.data.correct);
        form.setFieldsValue(res.data);
        if (res.data.type == '') {
          console.log('---------------------------');
          form.setFieldsValue({ type: [] });
        }
        if (res.data.firstCategory) {
          getTypeList(res.data.firstCategory);
        }
        if (res.data.error) {
          let list = res.data.error.split(',');
          setErrMp3UrlUrl(list[0]);
          setErrMp3UrlUrl1(list[1]);
          form.setFieldValue({ error1: list[0] });
          form.setFieldValue({ error2: list[1] });
        }
      });
    }
    let title = searchParams.get('detail')
      ? '题目详情'
      : searchParams.get('type') === 'copy'
      ? '复制题目'
      : searchParams.get('type') === 'edit'
      ? '编辑题目'
      : '新增题目';
    setTitle(title);
  }, []);
  const getTypeList = (id) => {
    tCategorylistById({ id: id }).then((res) => {
      setSecondTypeList(res.data);
    });
  };
 
  // 分享图片
  const handleChange = (info) => {
    if (info.file.status === 'done') {
      setImgUrl(info.file.response);
      setLoading(false);
    }
  };
  // 正确音效
  const handleChangCorrectMp3 = (info) => {
    setCorrectMp3UrlUrl(info);
    setLoading(false);
  };
  // 错误音效
  const handleChangeErrMp3 = (info, type) => {
    if (type == 2) {
      console.log('error2');
      setErrMp3UrlUrl1(info);
    } else {
      console.log('error1 error1 error1');
      setErrMp3UrlUrl(info);
    }
  };
 
  // 上传前校验
  const beforeUpload = (file) => {
    return new Promise(async (resolve, reject) => {
      // setLoading(true)
      resolve(file);
    });
  };
 
  const uploadButton = () => {
    return (
      <button
        style={{
          border: 0,
          background: 'rgb(233, 233, 233)',
          width: '200px',
          height: '132px',
        }}
        type="button"
      >
        {loading ? <LoadingOutlined /> : <PlusOutlined />}
        <div
          style={{
            marginTop: 24,
          }}
        ></div>
      </button>
    );
  };
  const close = () => {
    history.back();
  };
 
  const save = () => {
    form.validateFields().then(async (values) => {
      console.log('value values values values', values);
      if ((errMp3Url && !errMp3Url1) || (!errMp3Url && errMp3Url1)) {
        message.error('请上传两个错误语音');
        return;
      }
      let params = {
        english: values.english,
        name: values.name,
        img: imgUrl,
        correct: correctMp3Url,
        firstCategory: values.firstCategory,
        secondCategory: values.secondCategory,
        type: (values.type || []).join('、'),
        error: errMp3Url ? errMp3Url + ',' + errMp3Url1 : '',
      };
      let state = false;
      if (searchParams.get('id')) {
        if (searchParams.get('type') === 'copy') {
          state = await sendRequest(add, params);
        } else {
          params = {
            id: searchParams.get('id'),
            ...params,
          };
          state = await sendRequest(edit, params);
        }
      } else {
        state = await sendRequest(add, params);
      }
      if (state) {
        history.back();
      }
    });
  };
 
  return (
    <PageContainer loading={loading} title={title}>
      <Card>
        <Form scrollToFirstError form={form} {...formItemLayout}>
          <Row>
            <Col span={24}>
              <Form.Item
                name="firstCategory"
                label="所属一级分类"
                rules={[
                  {
                    required: true,
                    message: '请选择所属一级分类',
                  },
                ]}
              >
                <Select
                  style={{ width: '200px' }}
                  // mode="multiple"
                  options={firstTypeList}
                  onChange={(id) => getTypeList(id)}
                  fieldNames={{ label: 'name', value: 'id' }}
                  placeholder="请选择"
                />
              </Form.Item>
            </Col>
            <Col span={24}>
              <Form.Item
                name="secondCategory"
                label="所属二级分类"
                rules={[
                  {
                    required: true,
                    message: '请选择所属二级分类',
                  },
                ]}
              >
                <Select
                  style={{ width: '200px' }}
                  // mode="multiple"
                  options={secondTypeList}
                  fieldNames={{ label: 'name', value: 'id' }}
                  placeholder="请选择"
                />
              </Form.Item>
            </Col>
            <Col span={24}>
              <Form.Item
                name="name"
                label="题目名称"
                required
                rules={[
                  {
                    required: true,
                    message: '请输入题目名称',
                  },
                ]}
              >
                <Input style={{ width: '200px' }} placeholder="请输入" />
              </Form.Item>
            </Col>
            <Col span={24}>
              <Form.Item
                name="english"
                label="英文释义"
                required
                rules={[
                  {
                    required: true,
                    message: '请输入英文释义',
                  },
                ]}
              >
                <Input style={{ width: '200px' }} placeholder="请输入" />
              </Form.Item>
            </Col>
            <Col span={24}>
              <Form.Item name="type" label="预期题目类型">
                <Select
                  style={{ width: '200px' }}
                  mode="multiple"
                  options={deptList}
                  fieldNames={{ label: 'deptName', value: 'deptId' }}
                  placeholder="请选择"
                />
              </Form.Item>
            </Col>
            <Col span={24}>
              <Form.Item
                name="img"
                label="上传图片"
                required
                extra={
                  <div>
                    <div style={{ color: 'red' }}>推荐尺寸:684px x 456px </div>
                    <div style={{ color: 'red' }}>推荐大小:300kb以内</div>
                  </div>
                }
                rules={[
                  {
                    required: true,
                    message: '请上传图片',
                  },
                ]}
              >
                <Upload
                  name="file"
                  className="avatar-uploader"
                  showUploadList={false}
                  onChange={handleChange}
                  action={BASE_URL + '/management/base/oss/upload'}
                  headers={{ Authorization: localStorage.getItem('token') }}
                  accept="image/png, image/jpeg, image/jpg, image/webp"
                >
                  {imgUrl ? <Image width={200} src={imgUrl} /> : uploadButton()}
                </Upload>
              </Form.Item>
            </Col>
            <Col span={24}>
              <Form.Item
                name="correct"
                label="上传正确语音"
                required
                rules={[
                  {
                    required: true,
                    message: '请上传正确语音',
                  },
                ]}
              >
                <div style={{ display: 'flex' }}>
                  <Upload
                    maxCount={1}
                    name="file"
                    className="avatar-uploader"
                    showUploadList={false}
                    onSuccess={handleChangCorrectMp3}
                    beforeUpload={beforeUpload}
                    action={BASE_URL + '/management/base/oss/upload'}
                    headers={{ Authorization: localStorage.getItem('token') }}
                    // fileList={correctMp3Url}
                    accept=".mp3,.wav"
                  >
                    {correctMp3Url ? (
                      <div style={{ display: 'flex' }}>
                        <audio controls src={correctMp3Url}></audio>
                      </div>
                    ) : (
                      <Button type="primary" style={{ marginLeft: '20px' }}>
                        添加
                      </Button>
                    )}
                  </Upload>
                  {correctMp3Url && (
                    <CloseOutlined
                      onClick={() => setCorrectMp3UrlUrl()}
                      style={{ marginLeft: '20px' }}
                    />
                  )}
                </div>
              </Form.Item>
            </Col>
            <Col span={24}>
              <Form.Item name="error" label="上传错误语音">
                <div style={{ display: 'flex' }}>
                  <div style={{ marginRight: '10px', lineHeight: '32px' }}>1、</div>
                  <Upload
                    maxCount={1}
                    name="file"
                    className="avatar-uploader"
                    showUploadList={false}
                    onSuccess={(e) => handleChangeErrMp3(e, 1)}
                    beforeUpload={beforeUpload}
                    action={BASE_URL + '/management/base/oss/upload'}
                    headers={{ Authorization: localStorage.getItem('token') }}
                    // fileList={errMp3Url}
                    accept=".mp3,.wav"
                  >
                    {errMp3Url ? (
                      <audio controls src={errMp3Url}></audio>
                    ) : (
                      <Button type="primary">添加</Button>
                    )}
                  </Upload>
                  {errMp3Url && (
                    <CloseOutlined
                      onClick={() => setErrMp3UrlUrl()}
                      style={{ marginLeft: '20px' }}
                    />
                  )}
                </div>
              </Form.Item>
            </Col>
 
            <Col span={24}>
              <Form.Item name="error2" label=" " colon={false}>
                <div style={{ display: 'flex' }}>
                  <div style={{ marginRight: '10px', lineHeight: '32px' }}>2、</div>
                  <Upload
                    maxCount={1}
                    name="file"
                    className="avatar-uploader"
                    showUploadList={false}
                    onSuccess={(e) => handleChangeErrMp3(e, 2)}
                    beforeUpload={beforeUpload}
                    action={BASE_URL + '/management/base/oss/upload'}
                    headers={{ Authorization: localStorage.getItem('token') }}
                    // fileList={errMp3Url}
                    accept=".mp3,.wav"
                  >
                    {errMp3Url1 ? (
                      <audio controls src={errMp3Url1}></audio>
                    ) : (
                      <Button type="primary">添加</Button>
                    )}
                  </Upload>
                  {errMp3Url1 && (
                    <CloseOutlined
                      onClick={() => setErrMp3UrlUrl1()}
                      style={{ marginLeft: '20px' }}
                    />
                  )}
                </div>
              </Form.Item>
            </Col>
          </Row>
 
          <div style={{ display: 'flex', justifyContent: 'center', marginTop: 20 }}>
            <Button onClick={close} style={{ marginRight: '20px' }}>
              关闭
            </Button>
            {!searchParams.get('detail') && (
              <Button type="primary" onClick={save}>
                确认
              </Button>
            )}
          </div>
        </Form>
      </Card>
    </PageContainer>
  );
};
 
export default SystemSettings;