forked from huge/frontEnd/hugeOA

liuwh
2020-04-30 f9e9df1e43014e67580ad03dc86854a55673403d
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
/* eslint-disable */
/**liuwh
 * 4/30/2020, 2:39:21 PM
 * doc comment for the file goes here
 */
 
/** 爬虫词条管理 */
import React, { ReactNode, ReactEventHandler, Component } from 'react';
import { Badge, Button, Divider, Popconfirm, Modal, Row, Col, Input, message } from 'antd';
import TableView from '../../../common/TableView';
import SearchFormView from '../../../common/SearchFormView';
import './index.scss';
import moment from 'moment';
import './index.scss';
import fetch from '../../../../api/request';
 
export default class Rawler extends Component {
  constructor(props) {
    super(props);
    this.config = {
    };
    this.state = {
      formData: {
        __key: Date.now(),
        page: 1,
        size: 10,
      },
      visible: false,
      btnLoading: false,
      data: {}
    };
  }
 
  componentDidMount() { }
 
  onAdd = () => {
    this.setState({
      visible: true
    })
  }
 
 
  setFormData = data => {
    console.log('form', data);
    this.setState({
      formData: data,
    });
  }
 
  renderColumns = () => {
    return [
      { title: '序号', dataIndex: 'index' },
      { title: '关键词', dataIndex: 'keyword' },
      {
        title: '创建时间', dataIndex: 'createTime', render: (cur, item) => {
          return cur ? moment(cur).format("YYYY-MM-DD HH:mm") : ""
        }
      },
      {
        title: '操作',
        key: 'operation',
        render: (text, record) => {
          return (
            <React.Fragment>
              <a onClick={() => this.linkDetail(record)}>修改</a>
              <Divider type="vertical" />
              <Popconfirm title="确定删除吗?" onConfirm={() => this.handleDelete(record.id, 0)}>
                <a>删除</a>
              </Popconfirm>
              <Divider type="vertical" />
              {
                record.status == '1' ?
                  <Popconfirm title="确定停用吗?" onConfirm={() => this.handleDelete(record.id, '99')}>
                    <a>停用</a>
                  </Popconfirm> :
                  <Popconfirm title="确定启用吗?" onConfirm={() => this.handleDelete(record.id, '1')}>
                    <a style={{ color: 'gray' }}>启用</a>
                  </Popconfirm>
              }
              
            </React.Fragment>
          );
        },
      },
 
    ];
  }
 
  //修改
  linkDetail = record => {
    this.setState({
      data: record,
      visible: true
    })
  }
 
  onCancel = () => {
    this.setState({
      visible: false
    })
  }
 
  onSave = () => {
    let _this = this;
    const { data } = _this.state;
    _this.setState({
      btnLoading: true
    })
    fetch({
      url: `api/param/saveSpiderParam`,
      method: 'POST',
      data: {
        ...data
      }
    }).then(res => {
      console.log(res)
      _this.setState({
        btnLoading: false,
      });
      message.success("更新成功!");
      _this.setState({
        formData: {
          ..._this.state.formData,
          __key: Date.now(),
        },
        visible: false,
        data: {}
      })
    })
  }
 
  //停用或者启用关键词
  handleDelete = (id, status) => {
    const that = this;
    if (status == 0) {
      fetch({
        url: `api/param/deleteParam`,
        params: {
          id
        }
      }).then(res => {
        that.setState({
          formData: {
            ...that.state.formData,
            __key: Date.now(),
          },
        })
      })
 
    } else {
      fetch({
        url: `api/param/deleteOrStartParam`,
        params: {
          id,
          status
        }
      }).then(res => {
        that.setState({
          formData: {
            ...that.state.formData,
            __key: Date.now(),
          },
        })
      })
    }
  }
 
  inputChange = ({ target: { value, name } }) => {
    this.setState(({ data }) => ({
      data: {
        ...data,
        [name]: value
      }
    }))
  }
 
  render() {
    const { formData, visible, data, btnLoading } = this.state;
    let tableParams = {
      url: `api/param/queryParam`,
      formData,
      key: formData.__key,
      columns: this.renderColumns(),
      extraFromData: {
        logType: '2',
      },
      setFormData: this.setFormData
    }
    const formItemLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 18 },
      },
    };
    return (
      <div className="rawler-main">
        <Button type="primary" danger onClick={this.onAdd} >新增关键词</Button>
        <Divider />
        <TableView {...tableParams} />
        <Modal
          destroyOnClose
          title="新增关键词"
          visible={visible}
          footer={null}
          onCancel={this.onCancel}>
          <Row
            type="flex"
            gutter={20}
            style={{ margin: '20px 0 0 20px', alignItems: 'center ' }}>
            <Col>
              关键词:
            </Col>
            <Col style={{ flex: '1' }}>
              <Input value={data.keyword || ''} name='keyword' onChange={this.inputChange} placeholder="请输入关键词" />
            </Col>
          </Row>
          <Row
            type="flex"
            gutter={20}
            justify="end"
            style={{ marginTop: '20px' }}>
            <Col>
              <Button onClick={this.onCancel}>取消</Button>
            </Col>
            <Col>
              <Button type="primary" htmlType="submit" onClick={this.onSave} loading={btnLoading}>
                确定
                </Button>
            </Col>
          </Row>
 
        </Modal>
      </div>
    )
  }
}