董国庆
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
import { PageContainer } from '@ant-design/pro-components';
import { Button, Input, Form, Card, Row, Col, Space, message } from 'antd';
import { useState, useEffect } from 'react';
import BraftEditor from '@/components/BraftEditor';
import { history, useLocation } from "@umijs/max"
import { edit } from './service'
import { sendRequest } from '@/utils/antdUtils';
 
const AddAndEditAndDetail = () => {
 
    const [form] = Form.useForm();
    const [info, setInfo] = useState({ content: '' });
    const searchParams = new URLSearchParams(useLocation().search);
 
    useEffect(() => {
        if (searchParams.get('id')) {
            edit({ id: searchParams.get('id'), type: 3 }).then((res) => {
                if (res.code == 200) {
                    setInfo({ content: res.data.answer })
                    form.setFieldsValue(res.data)
                }
            })
        }
    }, []);
 
    // 监听富文本的值
    const braftCallBack = (value) => {
        let params = info;
        params.content = value;
        setInfo(params);
    };
    const removeHtmlTags = (str) => {
        if (str) {
            return str.replace(/<(?!img\b)[^>]*>/gi, '');
        }
    }
 
    const save = () => {
        form.validateFields().then(async (values) => {
            if (!removeHtmlTags(info.content)) {
                message.warning('请填写资讯内容')
                return
            }
            values.answer = info.content
            if (searchParams.get('id')) {
                values.id = searchParams.get('id')
                values.type = 2
                let state = await sendRequest(edit, values)
                if (state) {
                    history.back()
                }
            } else {
                values.type = 1
                let state = await sendRequest(edit, values)
                if (state) {
                    history.back()
                }
 
            }
        })
    }
 
    return (
        <PageContainer title={searchParams.get('detail') ? '使用指南详情' : searchParams.get('id') ? '编辑使用指南' : '添加使用指南'}>
            <Card>
                <Form scrollToFirstError form={form}>
                    <Row style={{ marginTop: 20 }}>
                        <Col span={8}>
                            <Form.Item
                                name="title"
                                label="问题标题"
                                rules={[{ required: true, message: '请输入问题标题' }]}
                            >
                                <Input disabled={searchParams.get('detail')} placeholder="请输入"></Input>
                            </Form.Item>
                        </Col>
                    </Row>
                </Form>
                <Row style={{ marginTop: 20 }}>
                    <Col>
                        <div style={{ display: 'flex' }}>
                            <span style={{ width: 90, flexShrink: 0 }}><span style={{ color: 'red', marginRight: 4 }}>*</span>问题答案:</span>
                            <BraftEditor readOnly={searchParams.get('detail')} data={info.content} resultCallback={braftCallBack} />
                        </div>
                    </Col>
                </Row>
                <div style={{ display: 'flex', justifyContent: 'center', marginTop: 20 }}>
                    <Space size='large'>
                        <Button onClick={() => history.back()}>关闭</Button>
                        {
                            !searchParams.get('detail') && <Button type='primary' onClick={save}>保存</Button>
                        }
                    </Space>
                </div>
            </Card>
        </PageContainer>
    )
}
 
export default AddAndEditAndDetail