forked from huge/frontEnd/hugeOA

Mr Ke
2020-04-29 c0c820559b46f3a5ede6fbd7f66e77d09981829f
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
/* 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 E from 'wangeditor';
import './index.scss';
const Option = Select.Option;
const { TextArea } = Input;
 
import fetch from '../../../api/request';
import { BASE_URL } from '../../../api/httpurl'
 
const param = [{ value: 'DT00002', name: '通知公告' }, { value: 'DT00001', name: '规章制度' }]
export default class DocumentDetailPage extends Component {
  constructor(props) {
    super(props);
    this.config = {
    };
    this.state = {
      loading: false,//页面加载loading
      iconLoading: false,//提交按钮loading
      saveData: {},//表单提交数据
      fileList: [],//附件列表
      disabled: false,
      editorContent: null//富文本编辑内容
    };
  }
 
  componentWillMount() { }
 
  componentDidMount() {
    this.loadData()
  }
 
  loadData = () => {
    let _this = this;
    const { id } = _this.props.match.params;
    _this.setState({
      loading: true,
      disabled: true
    })
    fetch({
      url: 'api/document/findDetail',
      params: {
        id: id || 'new'
      }
    }).then(res => {
      console.log(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 = () => {
    const { saveData, editorContent } = this.state;
    console.log(saveData)
    if (!saveData.documentTitle) {
      return message.warning(this.switchWordByType(saveData.documentType).title + '标题不能为空');
    }
    if (!editorContent) {
      return message.warning('通知内容不能为空');
    }
    let _this = this;
    _this.setState({
      iconLoading: true
    })
    fetch({
      url: 'api/document/saveOrUpdateNotice',
      method: 'POST',
      data: {
        ...saveData,
        documentContent: editorContent
      }
    }).then(res => {
      console.log(res)
      _this.setState({
        iconLoading: false,
      });
      message.success("提交成功!");
      this.props.history.push("/index/rules")
    });
 
  }
 
  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
        };
      case 'DT00001':
        return {
          title: '规章制度',
          associateId: 1002
        }
    }
  }
 
  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: true, showRemoveIcon: false }
    };
 
    return (
      <div className="document-detail-page-main">
        <Spin spinning={loading}>
 
          <Card style={{ border: 20, margin: 20, padding: 20 }}>
            <Row gutter={16}>
              <Col className="gutter-row marginTop flex-box-row" >
                <div className="document-detail-page-main-label">
                  文档类型:
                </div>
                <div className="document-detail-page-main-value">
                  {
                    saveData.documentType ? param.find(({ value }) => value == saveData.documentType).name : '无'
                  }
                </div>
              </Col>
            </Row>
 
            <Row gutter={16} className="marginTop">
              <Col className="gutter-row marginTop flex-box-row">
                <div className="document-detail-page-main-label">
                  {saveData.documentType && this.switchWordByType(saveData.documentType).title}标题:
                </div>
                <div className="document-detail-page-main-value">
                  {saveData.documentTitle || '无'}
                </div>
              </Col>
            </Row>
 
            <Row gutter={16} className="marginTop">
              <Col className="gutter-row marginTop flex-box-row flex-start">
                <div className="document-detail-page-main-label">
                  通知内容:
                </div>
                <div className="flex-1">
                  {
                    saveData.documentContent ? <div dangerouslySetInnerHTML={{
                      __html: saveData.documentContent
                    }} style={{ width: '70%' }} /> : <div className="document-detail-page-main-value">
                        无
                  </div>
                  }
                </div>
 
              </Col>
            </Row>
 
            {/* 当有富文本内容的时候 -- 显示 */}
            {/* {
              saveData.documentContent &&
              <div dangerouslySetInnerHTML={{
                __html: saveData.documentContent
              }} style={{ width: '70%' }} className="marginTB" />
            } */}
 
            <Row gutter={16} className="marginTop">
              <Col className="gutter-row marginTop flex-box-row flex-start">
                <div className="document-detail-page-main-label">
                  附件:
                </div>
                {
                  saveData.attachmentList && saveData.attachmentList.length > 0 ?
                    <div className="flex-1">
                      <Upload {...props} >
                        <Button disabled={disabled} style={{ display: disabled ? 'none' : 'inline-block' }}>
                          <Icon type="upload" />上传文件</Button>
                      </Upload>
                    </div> : '未上传附件'
                }
              </Col>
            </Row>
 
            <Row type="flex" gutter={20} style={{ marginTop: '20px' }}>
              <Col className="gutter-row" ><Button onClick={this.cancle}>返回</Button></Col>
            </Row>
          </Card>
        </Spin>
      </div>
    )
  }
}