forked from nsjcy/frontEnd/nsjcy

liuwh
2020-03-31 a0ec2242677dced1e6e098c114460da3d56be157
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
/**
 * 徐祥健<xuxj@hugeinfo.com.cn>
 * 2018年8月29日 16:19
 *
 */
import React from 'react';
 
import { Row, Col, Input, Select, DatePicker, Button, Tag, Tooltip, Modal, Upload, Icon, message, TreeSelect, Breadcrumb } from 'antd';
import HeadView from '../view/HeadView';
import BusDetailView from '../view/BusDetailView';
import AddComView from '../view/AddComView';
// 引入编辑器以及编辑器样式
import BraftEditor from 'braft-editor'
import 'braft-editor/dist/index.css'
import Fetch from '../fetch';
import moment from 'moment';
const Option = Select.Option;
const divStyle = {
  margin: '20px'
};
const domain = Fetch.domain;
const { CheckableTag } = Tag;
export default class ArticleEidt extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      dateSource: [],
      savedate: {},
      formdata: {},
      show: false,
      visible: false,
      closeKey: Date.now(),
      selectedRow: [],
      fileList: [],
      cover: [],
      editorState: BraftEditor.createEditorState(null)
    };
  }
 
  componentDidMount() {
    document.title = '信息发布';
    const { id } = this.props.match.params;
    const { formdata } = this.state;
    Fetch.getArticleDetail(id)
      .then(res => {
        this.setState({
          savedate: res,
          selectedRow: res.companyList || [],
          fileList: res.attachments ? res.attachments.map((item, index) => ({ uid: item.uid, id: item.uid, name: item.name, status: 'done', url: domain + `api/v1/attachment/downloadDisk/${item.uid}?online=false` })) : [],
          cover: res.cover || [],
          editorState: BraftEditor.createEditorState(res.content),
          dateSource: res.channelList || []
        })
        if (res.channelName == '法律宣传') {
          this.setState({
            show: true
          })
        } else {
          this.setState({
            show: false
          })
        }
      })
  }
  saveInputChange = ({ target: { value, name } }) => {
    this.setState(({ savedate }) => ({
      savedate: {
        ...savedate,
        [name]: value
      }
    }))
    const { dateSource } = this.state;
    dateSource.forEach(element => {
      if (element.value == value) {
        if (element.title == '法律宣传') {
          this.setState({
            show: true
          })
        } else {
          this.setState({
            show: false
          })
        }
      }
    });
  }
 
  submit = () => {
    const list = this.state.selectedRow.map(({ id }) => id);
    const { savedate } = this.state;
    savedate.list = list;
    if (!savedate.channelIdd) {
      message.warning("请选择信息分类");
      return;
    }
    Fetch.saveArticle(savedate)
      .then(res => {
        if (res.statuscode === 1) {
          message.success("提交成功!");
          this.props.history.push("/articlePublish")
        } else {
          message.error('保存失败,请联系管理员', 2)
        }
      });
  }
  cancle = () => {
    this.props.history.push("/articlePublish");
  }
  handleClose = delid => {
    const { selectedRow: rows } = this.state;
    this.setState({ selectedRow: rows.filter(({ id }) => id !== delid) });
  };
 
  onChange = (checked) => {
    this.setState({
      visible: true,
    });
  }
 
  handleOk = (selectedRow) => {
    this.setState({
      selectedRow
    })
  }
 
  handleCancel = () => {
    this.setState({
      closeKey: Date.now(),
      visible: false
    });
  }
 
  uploadFn = (param) => {
 
    const serverURL = domain + 'api/v1/attachment/upload'
    const xhr = new XMLHttpRequest
    const fd = new FormData()
    const successFn = (response) => {
      // 假设服务端直接返回文件上传后的地址
      // 上传成功后调用param.success并传入上传后的文件地址
      //Pz
      const res = JSON.parse(xhr.responseText);
      param.success({
        url: domain + 'api/v1/attachment/image/' + res[0].id,
        meta: {
          id: res[0].id,
          title: res[0].name,
          alt: res[0].name,
          loop: true, // 指定音视频是否循环播放
          autoPlay: true, // 指定音视频是否自动播放
          controls: true, // 指定音视频是否显示控制栏
          poster: domain + 'api/v1/attachment/image/' + res[0].id, // 指定视频播放器的封面
        }
      })
    }
 
 
    const progressFn = (event) => {
      // 上传进度发生变化时调用param.progress
      param.progress(event.loaded / event.total * 100)
    }
 
    const errorFn = (response) => {
      // 上传发生错误时调用param.error
      param.error({
        msg: 'unable to upload.'
      })
    }
    xhr.upload.addEventListener("progress", progressFn, false)
    xhr.addEventListener("load", successFn, false)
    xhr.addEventListener("error", errorFn, false)
    xhr.addEventListener("abort", errorFn, false)
    fd.append('file', param.file)
    xhr.open('POST', serverURL, true)
    xhr.send(fd)
  }
 
  submitContent = () => {
    // 在编辑器获得焦点时按下ctrl+s会执行此方法
    // 编辑器内容提交到服务端之前,可直接调用editorState.toHTML()来获取HTML格式的内容
    const htmlContent = this.state.editorState.toHTML()
    this.setState(({ savedate }) => ({
      savedate: {
        ...savedate,
        content: htmlContent
      }
    }))
  }
 
  handleEditorChange = (editorState) => {
    this.setState({ editorState })
  }
 
  render() {
    const { dateSource, show, visible, closeKey, selectedRow, savedate, fileList, editorState } = this.state;
 
    const props = {
      action: domain + 'api/v1/attachment/materialsDisk?entityId=' + savedate.id,
      onChange: ({ file, fileList }) => {
        if (file.status !== 'uploading') {
          console.log(file);
          console.log(fileList);
 
        }
        this.setState({ fileList });
      },
      fileList: fileList,
      onRemove: (file) => {
        Fetch.deleteAttachment(file.uid)
          .then(res => {
            message.success("移除成功!");
          });
        this.setState(({ fileList }) => {
          const index = fileList.indexOf(file);
          const newFileList = fileList.slice();
          newFileList.splice(index, 1);
          return {
            fileList: newFileList,
          };
        });
      }
    };
    return (
      <div className="app-page">
        <HeadView history={this.props.history} />
        <Breadcrumb style={{ padding: '20px' }}>
          <Breadcrumb.Item><a href="index.html#/articlePublish">信息发布</a></Breadcrumb.Item>
          <Breadcrumb.Item>信息公布详情</Breadcrumb.Item>
        </Breadcrumb>
        <BusDetailView type='信息添加' >
          <div style={divStyle}>
            <Row type="flex" align='middle' justify="space-around">
              <Col span={6} pull={0} style={{ textAlign: 'center' }}><span>信息标题</span></Col>
              <Col span={6} push={0} ><Input placeholder="请输入信息标题" style={{ width: '300px' }} name='title' onChange={this.saveInputChange} value={savedate.title || ""} /></Col>
              <Col span={6} pull={0} style={{ textAlign: 'center' }}><span>信息分类</span></Col>
              <Col span={6} push={0} >
                <TreeSelect
                  style={{ width: 300 }}
                  value={savedate.channelIdd || undefined}
                  dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
                  treeData={dateSource}
                  placeholder="请选择类型"
                  treeDefaultExpandAll
                  onChange={(value) => this.saveInputChange({ target: { name: 'channelIdd', value } })}
                />
              </Col>
            </Row>
          </div>
 
          <div style={divStyle}>
            <Row type="flex" align='middle' justify="space-around">
              <Col span={6} pull={0} style={{ textAlign: 'center' }}><span>发布时间</span></Col>
              <Col span={18} push={0} >
                <DatePicker style={{ width: '300px' }} onChange={(_, value) => this.saveInputChange({ target: { name: 'publishTime', value } })} value={moment(savedate.publishTime || Date.now())} /></Col>
            </Row>
          </div>
          <div style={divStyle}>
            <Row type="flex" align='middle' justify="space-around">
              <Col span={6} pull={0} style={{ textAlign: 'center' }}><span>上传附件</span></Col>
              <Col span={18} push={0} >
                <Upload {...props}>
                  <Button>
                    <Icon type="upload" /> 上传附件
                  </Button>
                </Upload>
              </Col>
            </Row>
          </div>
          <div style={divStyle}>
            <Row type="flex" align='top' justify="space-around">
              <Col span={6} pull={0} style={{ textAlign: 'center' }}><span>信息内容</span></Col>
              <Col span={18} push={0} style={{ border: 'solid 1px rgba(0, 0, 0, 0.25)', borderRadius: '5PX' }} >
                <BraftEditor
                  value={editorState}
                  placeholder='请输入文章内容'
                  media={{
                    allowPasteImage: true, // 是否允许直接粘贴剪贴板图片(例如QQ截图等)到编辑器
                    image: true, // 开启图片插入功能
                    video: false, // 开启视频插入功能
                    audio: false, // 开启音频插入功能
                    uploadFn: this.uploadFn
                  }}
                  excludeControls={['emoji']}
                  onChange={this.handleEditorChange}
                  onSave={this.submitContent}
                  onBlur={this.submitContent}
                />
              </Col>
            </Row>
          </div>
          {
            show == true ?
              <div style={divStyle}>
                <Row type="flex" align='top' justify="space-around">
                  <Col span={6} pull={0} style={{ textAlign: 'center' }}><span>推送企业</span></Col>
                  <Col span={18} push={0}>
                    {selectedRow.map(({ companyName: tag, id }, index) => {
                      const isLongTag = tag.length > 20;
                      const tagElem = (
                        <Tag
                          key={tag}
                          closable={true}
                          afterClose={() => this.handleClose(id)}
                          onChange={this.onChange}
                        >
                          {isLongTag ? `${tag.slice(0, 20)}...` : tag}
                        </Tag>
                      );
                      return isLongTag ? (
                        <Tooltip title={tag} key={tag}>
                          {tagElem}
                        </Tooltip>
                      ) : (
                          tagElem
                        );
                    })}
                    <CheckableTag onChange={this.onChange} >
                      添加企业
                    </CheckableTag>
                  </Col>
                </Row>
              </div>
              : null
          }
          <div style={{ display: 'flex', justifyContent: 'center' }}>
            <Button style={{ marginRight: '15px', width: '150px' }} type="primary" onClick={this.submit}>提交</Button>
            <Button style={{ marginLeft: '15px', width: '150px' }} onClick={this.cancle}>取消</Button>
          </div>
        </BusDetailView>
        <Modal
          key={closeKey}
          visible={visible}
          width="62%"
          onCancel={this.handleCancel}
          footer={null}
        >
          <div className="model-detail-view-main">
            <div className='model-detail-view-type'><span>选择企业</span></div>
            <AddComView handleCancel={this.handleCancel} handleOk={this.handleOk} selectedRow={selectedRow} />
          </div>
        </Modal>
      </div>
    );
  }
 
}