HotKeywords.jsx
12.1 KB
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import React from 'react'
import { Table, Divider, Modal, Button, Input, Cascader, Upload, Icon, message } from 'antd'
import HotApi from '../../services/seo/hot-keywords'
const { TextArea } = Input;
const titleObj = {
id: '序号',
keyword: '关键词',
msortName: '大品类',
sortName: '小品类',
yoho_goods_num: '商品数',
is_push: '是否推送',
add_time: '添加时间'
};
const columns = [];
Object.keys(titleObj).forEach(key => {
columns.push({
title: titleObj[key],
dataIndex: key,
key: key,
});
});
let globalSort = [];
function ActionButton(props) {
function editClick() {
props.keyword.callbackFn('edit', props.keyword);
}
function deleteClick() {
props.keyword.callbackFn('delete', props.keyword);
}
return (
<span>
<a href="javascript:;" onClick={editClick}>编辑</a>
<Divider type="vertical" />
<a href="javascript:;" onClick={deleteClick}>删除</a>
</span>
);
}
class OptionModal extends React.Component {
constructor(props) {
super(props);
this.props = props;
this.state = {
sorts: globalSort || []
};
this.renderData = Object.assign({}, props.record || {});
this.hotApi = new HotApi();
this.loadHotList = props.loadHotList;
this.hideOptionModal = props.hideOptionModal;
this.handleKeywordChange = this.handleKeywordChange.bind(this);
this.handleDescChange = this.handleDescChange.bind(this);
this.handleSortChange = this.handleSortChange.bind(this);
this.handleImageChange = this.handleImageChange.bind(this);
this.loadSortData = this.loadSortData.bind(this);
switch (this.renderData.type) {
case 'edit':
this.modalTitle = '编辑关键词';
this.handleOk = this.handleSave.bind(this);
break;
case 'delete':
this.modalTitle = '删除关键词';
this.handleOk = this.handleDelete.bind(this);
break;
default:
this.modalTitle = '新建关键词';
this.handleOk = this.handleSave.bind(this);
break;
}
if (!globalSort.length) {
this.loadSortDataAsync().then(result => {
this.setState({sorts: result});
globalSort = result;
});
}
}
handleDelete() {
return this.hotApi.delHotKeywords([this.renderData.id]).then(result => {
if (result.code === 200) {
this.loadHotList && this.loadHotList();
this.hideOptionModal && this.hideOptionModal();
} else {
message.error(result.message);
}
});
}
handleSave() {
return this.hotApi.saveHotKeywords(this.renderData).then(result => {
if (result.code === 200) {
this.loadHotList && this.loadHotList();
this.hideOptionModal && this.hideOptionModal();
} else {
message.error(result.message);
}
});
}
handleKeywordChange(e) {
this.renderData.keyword = e.target.value;
}
handleDescChange(e) {
this.renderData.describe = e.target.value;
}
handleSortChange(select) {
this.renderData.msort = select[0];
this.renderData.misort = select[1];
this.renderData.sort_id = select[2];
}
handleImageChange(info) {
const status = info.file.status;
if (status === 'uploading') {
this.setState({ loading: true });
return;
} else if (status === 'done') {
const result = info.file.response;
let state = { loading: false };
if (result.code === 200) {
this.renderData.goods_img = result.data.images[0];
}
this.setState(state);
}
}
beforeUpload(file) {
const isImage = file.type.indexOf('image') > -1;
const isLt2M = file.size / 1024 / 1024 < 2;
if (!isImage) {
message.error('请上传图片!');
} else if (!isLt2M) {
message.error('图片大小需小于 2MB!');
}
return isImage && isLt2M;
}
loadSortData(selectedOptions) {
const targetOption = selectedOptions[selectedOptions.length - 1];
targetOption.loading = true;
this.loadSortDataAsync(targetOption.value).then(result => {
targetOption.loading = false;
targetOption.children = result;
globalSort = this.state.sorts;
this.setState({sorts: this.state.sorts});
});
}
loadSortDataAsync(id) {
return this.hotApi.getSubSorts(id || 0).then(result => {
result.map(val => {
if (id) {
val.value = val.sort_id;
val.label = val.sort_name;
val.isLeaf = false;
val.children = (val.sub || []).map(sval => {
sval.value = sval.sort_id;
sval.label = sval.sort_name;
sval.isLeaf = true;
return sval;
})
} else {
val.value = val.id;
val.label = val.sortName;
val.isLeaf = false;
}
return val;
});
return result;
});
}
uploadButton(loading) {
return (
<div>
<Icon type={loading ? 'loading' : 'plus'} />
<div className="ant-upload-text">Upload</div>
</div>);
}
render() {
const record = this.renderData;
let title = '';
if (record.sort_id || record.msort || record.misort) {
let arr = [];
record.msortName && arr.push(record.msortName);
record.misortName && arr.push(record.misortName);
record.sortName && arr.push(record.sortName);
record.sortShowName = arr.join('/');
}
if (record && record.type === 'delete') {
return (
<Modal title={this.modalTitle} visible={true} onOk={this.handleOk} onCancel={this.hideOptionModal}>
<p>确认删除该关键词?</p>
</Modal>
)
} else {
const imageUrl = '';
return (
<Modal title={this.modalTitle} visible={true} onOk={this.handleOk} onCancel={this.hideOptionModal}>
<div style={{ paddingBottom: 10 }}>
<span>关键词:</span>
<Input onChange={this.handleKeywordChange} defaultValue={record.keyword} readOnly={record.id > 0}/>
</div>
<div style={{ paddingBottom: 10 }}>
<span>描述:</span>
<TextArea rows={4} onChange={this.handleDescChange} defaultValue={record.describe}/>
</div>
<div style={{ paddingBottom: 10 }}>
<span>封面图:</span>
<Upload
name="avatar"
listType="picture-card"
className="avatar-uploader"
showUploadList={false}
action="/upload/image"
name="file"
beforeUpload={this.beforeUpload}
onChange={this.handleImageChange}>
{record.goods_img ? <img src={record.goods_img} style={{maxWidth: 200}} /> : this.uploadButton(this.state.loading)}
</Upload>
</div>
<div style={{ paddingBottom: 10 }}>
<span>品类:</span>
{record.sortShowName ? <Input defaultValue={record.sortShowName} readOnly={record.id > 0}/> : <Cascader
options={this.state.sorts}
loadData={this.loadSortData}
onChange={this.handleSortChange}
style={{ width: '100%' }}
placeholder="" />}
</div>
</Modal>
)
}
}
}
columns.push({
title: '操作',
key: 'action',
render(keyword) {
return <ActionButton keyword={keyword}/>
}
});
class HotKeywords extends React.Component {
constructor(props) {
super(props);
this.state = {
showModal: false,
dataSource: [],
columns: columns,
selectedRowKeys: [],
pagination: {
current: 1,
total: 1,
pageSize: 10
}
};
this.hotApi = new HotApi();
this.loadHotList = this.loadHotList.bind(this);
this.handleTableChange = this.handleTableChange.bind(this);
this.deleteTableRow = this.deleteTableRow.bind(this);
this.hideOptionModal = this.hideOptionModal.bind(this);
this.addTableRow = this.addTableRow.bind(this);
this.loadHotList();
}
loadHotList(params = {}) {
const { current, pageSize } = this.state.pagination;
params.page = params.page || current || 1;
params.limit = params.limit || pageSize || 10;
this.hotApi.getHotList(params).then(result => {
if (result.code === 200 && result.data) {
const pagination = this.state.pagination;
const { keywords, total } = result.data;
pagination.total = total;
this.setState({
dataSource: keywords,
pagination: pagination
});
}
});
}
onSelectChange(selectedRowKeys) {
this.setState({selectedRowKeys});
}
handleTableChange(pagination) {
this.setState({pagination});
this.loadHotList({page: pagination.current});
}
deleteTableRow() {
this.deleteKeywordsAsync(this.state.selectedRowKeys);
}
addTableRow() {
this.showOptionModal('add');
}
sendToBaidu() {
}
deleteKeywordsAsync(ids) {
this.hotApi.delHotKeywords(ids).then(result => {
result.code === 200 && this.loadHotList();
});
}
showOptionModal(type, record) {
// const modelRecord = {...record};
const modelRecord = record || {};
modelRecord.type = type;
this.modelRecord = modelRecord;
this.setState({showModal: true});
}
hideOptionModal() {
this.modelRecord = null;
this.setState({showModal: false});
}
optionModal(showModal) {
if (showModal) {
return <OptionModal hideOptionModal={this.hideOptionModal} record={this.modelRecord} loadHotList={this.loadHotList}/>
} else {
return <div></div>;
}
}
render() {
const { showModal, dataSource, columns, selectedRowKeys, pagination, loading } = this.state;
const hasSelected = selectedRowKeys.length > 0;
const rowSelection = {
selectedRowKeys,
onChange: this.onSelectChange.bind(this)
};
const rowKey = record => {
record.callbackFn = this.showOptionModal.bind(this);
return record.id;
}
return (
<div>
<div style={{ paddingBottom: 10 }}>
<Button type="primary" onClick={this.deleteTableRow} disabled={!hasSelected} loading={loading}>删除</Button>
<Button type="primary" style={{ marginLeft: 10 }} onClick={this.addTableRow}>添加</Button>
{this.optionModal(showModal)}
</div>
<Table rowSelection={rowSelection} dataSource={dataSource} columns={columns}
rowKey={rowKey} pagination={pagination} onChange={this.handleTableChange}/>
</div>
);
}
}
export default HotKeywords