forked from huge/frontEnd/hugeOA

Mr Ke
2020-10-20 6bf92751fe8dfa0624285268968a1fd7a8a7e7e5
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
/* eslint-disable */
/**liuwh
 * 4/6/2020, 3:53:15 PM
 * doc comment for the file goes here
 */
 
/** Happy Coding */
import React, { ReactNode, ReactEventHandler, Component } from 'react';
import { Card, Row, Col, Select, Button, Input, Upload, Icon, Spin, message } from 'antd';
import BraftEditor from 'braft-editor'
const Option = Select.Option;
const { TextArea } = Input;
import './index.scss';
import 'braft-editor/dist/index.css'
 
import fetch from '../../../api/request';
import { BASE_URL } from '../../../api/httpurl';
import { emoveTAG } from '../../../utils/utils';
 
const param = [{ value: 'DT00002', name: '通知公告' }, { value: 'DT00001', name: '规章制度' }]
export default class DocumentEditPage extends Component {
  constructor(props) {
    super(props);
    this.config = {
    };
    this.state = {
      loading: false,//页面加载loading
      iconLoading: false,//提交按钮loading
      saveData: {},//表单提交数据
      fileList: [],//附件列表
      disabled: false,
      editorContent: '',//富文本编辑内容
      editorState: BraftEditor.createEditorState(null)
    };
  }
 
  componentWillMount() { }
 
  componentDidMount() {
    this.loadData()
  }
 
  loadData = () => {
    const { id } = this.props.match.params;
    let _this = this;
    _this.setState({
      loading: true,
      disabled: id ? true : false
    })
    fetch({
      url: 'api/document/findDetail',
      params: {
        id: id || 'new'
      }
    }).then(res => {
      if (res) {
        _this.setState({
          saveData: {
            ...res,
            documentType: res.documentType || 'DT00002'
          },
          loading: false,
          fileList: res.attachmentList && res.attachmentList.map((a) => ({ ...a, key: a.id, uid: a.id, name: a.fileName + '.' + a.suffix, })) || []
        });
      }
    });
  }
 
  saveInputChange = ({ target: { value, name } }) => {
    this.setState(({ saveData }) => ({
      saveData: {
        ...saveData,
        [name]: value
      }
    }))
  }
 
  submit = () => {
    let { saveData, editorContent } = this.state;
    // editorContent = emoveTAG(editorContent);
    if (!saveData.documentTitle) {
      return message.warning(this.switchWordByType(saveData.documentType).title + '标题不能为空');
    }
    if (!emoveTAG(editorContent)) {
      return message.warning('通知内容不能为空');
    }
    let _this = this;
    _this.setState({
      iconLoading: true
    })
    fetch({
      url: 'api/document/saveOrUpdateNotice',
      method: 'POST',
      data: {
        ...saveData,
        documentContent: editorContent
      }
    }).then(res => {
      _this.setState({
        iconLoading: false,
      });
      if (res) {
        message.success("提交成功!");
        this.props.history.push(this.switchWordByType(saveData.documentType).href);
      }
    });
  }
 
  cancle = () => {
    const { id } = this.props.match.params;
    if (id) {
      this.props.history.goBack()
    } else {
      this.props.history.push("/index/workbench");
    }
  }
 
  // wen
  switchWordByType = (type) => {
    switch (type) {
      case 'DT00002':
        return {
          title: '通知',
          associateId: 1001,
          href: '/index/workbench/announcement'
        };
      case 'DT00001':
        return {
          title: '规章制度',
          associateId: 1002,
          href: '/index/rules'
        }
    }
  }
 
  handleChange = (editorState) => {
    console.log('htnl', editorState.toHTML())
    this.setState({ editorState, editorContent: editorState.toHTML() })
  }
 
  render() {
    const { saveData, fileList, loading, disabled } = this.state;
    const props = {
      action: BASE_URL + `api/attachment/materials?associateId=${saveData.documentType == 'DT00002' ? 1001 : 1002}&entityId=` + saveData.id,
      header: {
        Authorization: window.localStorage.getItem('token')
      },
      onChange: ({ file, fileList }) => {
        fileList = fileList.slice(-2);
        fileList = fileList.map(file => {
          if (file.response) {
            // Component will show file.url as link
            file.url = file.response.data[0].url;
          }
          return file;
        });
        this.setState({ fileList });
      },
      fileList: fileList,
      onPreview: (file) => {
        window.open(file.url);
      },
      showUploadList: { showPreviewIcon: true, showDownloadIcon: false, showRemoveIcon: disabled ? false : true }
    };
 
    //指定编辑器工具栏的控件列表
    const controls = ['undo', 'redo', 'separator',
      'headings', 'separator', 'font-size', 'line-height', 'letter-spacing', 'separator',
      'text-color', 'bold', 'italic', 'underline', 'strike-through', 'separator', 'text-indent', 'text-align', 'separator',
      'list-ul', 'list-ol', 'blockquote', 'code', 'separator',
      'link', 'separator', 'hr', 'separator',
      'media', 'separator', 'remove-styles',
      'clear'];
 
    return (
      <div className="document-edit-page-main">
        <Spin spinning={loading}>
 
          <Card style={{ border: 20, margin: 20, padding: 20 }}>
            <Row gutter={16}>
              <Col className="gutter-row marginTop" >
                文档类型:
              </Col>
            </Row>
            <Row gutter={16}>
              <Col className="gutter-row marginTB">
                <Select placeholder="请选择" disabled={disabled} style={{ width: "300px" }} onChange={(value) => this.saveInputChange({ target: { name: 'documentType', value } })} value={saveData.documentType}>
                  {
                    param.map((data, key) => (
                      <Option key={key} value={data.value}>{data.name}</Option>
                    ))
                  }
                </Select>
              </Col>
            </Row>
 
            <Row gutter={16} className="marginTop">
              <Col className="gutter-row marginTop">
                {saveData.documentType && this.switchWordByType(saveData.documentType).title}标题:
                </Col>
            </Row>
 
            <Row gutter={16}>
              <Col className="gutter-row marginTB">
                <Input placeholder="请输入" disabled={disabled} style={{ width: '300px' }} name='documentTitle' onChange={this.saveInputChange} value={saveData.documentTitle || ""} />
              </Col>
            </Row>
 
            <Row gutter={16}>
              <Col className="gutter-row marginTop">
                通知内容:
                </Col>
            </Row>
            {/* 
            <div ref="editorElem" className="marginTB">
            </div> */}
            <BraftEditor value={this.state.editorState} onChange={this.handleChange} controls={controls} className="marginTB BraftEditor-style" />
 
            <Row gutter={16}>
              <Col className="gutter-row marginTop">
                附件:
                </Col>
            </Row>
 
            <Row gutter={16} >
              <Col className="gutter-row marginTB" >
                <Upload {...props} >
                  <Button disabled={disabled} style={{ display: disabled ? 'none' : 'inline-block' }}>
                    <Icon type="upload" />上传文件</Button>
                </Upload>
              </Col>
            </Row>
 
            {/* {
              saveData.documentType == 'DT00002' &&
              <Row gutter={16}>
                <Col className="gutter-row marginTop">
                  通知内容:
                </Col>
              </Row>
            }
            {
              saveData.documentType == 'DT00002' &&
              <Row gutter={16}>
                <Col className="gutter-row" style={{ margin: '12px 0' }} >
                  <TextArea disabled={disabled} style={{ width: '500px' }} name='documentContent' onChange={this.saveInputChange} value={saveData.documentContent || ""} />
                </Col>
              </Row>
            }
            {
              saveData.documentType == 'DT00001' &&
              <Row gutter={16} style={{ marginTop: '12px' }}>
                <Col className="gutter-row" style={{ margin: '12px 0' }} >
                  规章制度源文件:
                </Col>
              </Row>
            }
            {saveData.documentType == 'DT00001' &&
              <Row gutter={16} >
                <Col className="gutter-row" >
                  <Upload {...props} >
                    <Button disabled={disabled} style={{ display: disabled ? 'none' : 'inline-block' }}>
                      <Icon type="upload" />上传文件</Button>
                  </Upload>
                </Col>
              </Row>
            }
            {
              saveData.documentType == 'DT00001' &&
              <Row gutter={16} style={{ marginTop: '12px' }}>
                <Col className="gutter-row" >
                  通知范围:
                </Col>
              </Row>
            }
            {
              saveData.documentType == 'DT00002' &&
              <Row gutter={16} style={{ marginTop: '12px' }}>
                <Col className="gutter-row" style={{ margin: '12px 0' }} >
                  通知范围:
                </Col>
              </Row>
            }
            {saveData.documentType == 'DT00002' &&
              <Row gutter={16} >
                <Col className="gutter-row" >
                  目前仅支持全员通知
                </Col>
              </Row>
            } */}
            <Row type="flex" gutter={20} style={{ marginTop: '12px' }}>
              <Col className="gutter-row" ><Button onClick={this.cancle}>返回</Button></Col>
              <Col className="gutter-row" ><Button disabled={disabled} type="primary" loading={this.state.iconLoading} onClick={this.submit}>发布</Button></Col>
            </Row>
          </Card>
        </Spin>
      </div>
    )
  }
}