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
378import React , {Fragment} from "react";
import PageHeaderLayout from '../../layouts/PageHeaderLayout';
import {Row,Col,Card,Form,Input,TreeSelect,Icon,Button,Upload,Switch,InputNumber,DatePicker,Modal,message,
Badge,Divider,Table
} from 'antd';
import httpManager from "../../common/httpManager.js"
import F from "../../common/F.js"
import config from "../../common/config.js"
const FormItem = Form.Item;
class MyForm extends React.Component {
state = {
fileList : []
}
componentDidMount(){
//console.log("form mount")
}
didUploadContent = () => {
//return;
console.log("did ")
var fileList = this.props.form.getFieldValue("img_id_array") ?
this.props.form.getFieldValue("img_id_array").split(" ").filter(function(d){
return d != false;
}).map(function(d){
return {
uid: Math.random(),
name: 'xxx.png',
status: 'done',
url: d,
}
}) : [] ;
this.setState({fileList:fileList})
}
// 表单点击ok
okHandle = () => {
this.props.form.validateFields((err, values) => {
if (err) return;
//console.log(values);return;
this.props.handleFormCreateOrUpdate(values)
this.props.form.resetFields();
});
};
render() {
const { modalVisible, form, formType, handleModalVisible } = this.props;
const upload_url = config.base_host + "/v1/qiniu/upload";
//var fileList = [];
return (
<Modal title="表单" visible={modalVisible} onOk={this.okHandle} onCancel={() => handleModalVisible()}>
<FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="商品名称">
{form.getFieldDecorator('name', {
rules: [{ required: true, message: '请输入名称' }],
//initialValue:props.formData.name
})(<Input placeholder="请输入" />)}
</FormItem>
<FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="商品简介">
{form.getFieldDecorator('introduction', {
rules: [{ required: true, message: '请输入' }],
//initialValue:props.formData.name
})(<Input placeholder="请输入" />)}
</FormItem>
<FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="图片">
{form.getFieldDecorator('picture', {
getValueFromEvent: (e) => {
console.log(e)
if(e.file.response && e.file.response.code ==200 ){
console.log('这里获得image的值')
return e.file.response.data;
}
},
})(
//
<Upload name="upload_file" action={upload_url} showUploadList={false}>
{this.props.form.getFieldValue("picture") ? <img src={this.props.form.getFieldValue("picture")} width="50px" height="50px" /> : <Button><Icon type="upload" /> 上传</Button>}
</Upload>
)}
</FormItem>
<FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="父分类">
{form.getFieldDecorator('category_id', {
rules: [{ required: true, message: '' }],
})(
<TreeSelect
style={{ width: 300 }}
dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
treeData={this.props.category}
placeholder="请选择"
/>
)}
</FormItem>
<FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="商品价格">
{form.getFieldDecorator('price', {
rules: [{ required: true, message: '请输入' }],
//initialValue:props.formData.name
})(<InputNumber min={0} step={0.01} placeholder="请输入" />)}
</FormItem>
<FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="促销价格">
{form.getFieldDecorator('promotion_price', {
rules: [],
//initialValue:props.formData.name
})(<InputNumber min={0} step={0.01} placeholder="请输入" />)}
</FormItem>
<FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="商品库存">
{form.getFieldDecorator('stock', {
rules: [{ required: true, message: '请输入' }],
//initialValue:props.formData.name
})(<InputNumber min={0} placeholder="请输入" />)}
</FormItem>
<FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="商品排序">
{form.getFieldDecorator('sort', {
rules: [{ required: true, message: '请输入' }],
//initialValue:props.formData.name
})(<InputNumber min={0} placeholder="请输入" />)}
</FormItem>
<FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="净含量">
{form.getFieldDecorator('extra_1', {
rules: [{ required: true, message: '请输入' }],
//initialValue:props.formData.name
})(<Input placeholder="请输入" />)}
</FormItem>
<FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="保存方式">
{form.getFieldDecorator('extra_2', {
rules: [{ required: true, message: '请输入' }],
//initialValue:props.formData.name
})(<Input placeholder="请输入" value="1111" />)}
</FormItem>
<FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="保质期">
{form.getFieldDecorator('extra_3', {
rules: [{ required: true, message: '请输入' }],
//initialValue:props.formData.name
})(<Input placeholder="请输入" />)}
</FormItem>
<FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="重量说明">
{form.getFieldDecorator('extra_4', {
rules: [{}],
//initialValue:props.formData.name
})(<Input placeholder="请输入" />)}
</FormItem>
<FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="送到时间说明">
{form.getFieldDecorator('extra_5', {
rules: [{}],
//initialValue:props.formData.name
})(<Input placeholder="请输入" />)}
</FormItem>
<FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="详情图片">
{form.getFieldDecorator('img_id_array', {
getValueFromEvent: (e) => {
var a = "";
e.fileList.map(function(d){
var v = d.response ? d.response.data : d.url;
a += v+" "
});
this.setState({fileList:e.fileList})
return a.trim();
},
})(
//
<Upload name="upload_file" fileList={
this.state.fileList }
listType="picture-card" action={upload_url} >
<Button><Icon type="upload" /> 上传</Button>
</Upload>
)}
</FormItem>
<FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="是否新品">
{form.getFieldDecorator('is_new', {
valuePropName: 'checked',
})(<Switch />)}
</FormItem>
<FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="是否推荐">
{form.getFieldDecorator('is_recommend', {
valuePropName: 'checked',
})(<Switch />)}
</FormItem>
<FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="是否上架">
{form.getFieldDecorator('state', {
valuePropName: 'checked',
})(<Switch />)}
</FormItem>
</Modal>
)
}
}
const CreateForm = Form.create()(MyForm);
class Goods extends React.Component {
state = {
listData:[],
page:1,
pageTotal : 10,
modalVisible: false,
category:[],
formType:1 , //1新增 2修改
updateId:0 //修改id
};
componentDidMount() {
this.loadData()
httpManager.getGoodsCategoryList(1,100).then( (response)=>{
if(!F.checkResponse(response)) return;
console.log(response.data)
var arr = this.changeCategoryJson(response.data.data.data)
this.setState({category:arr})
} )
}
//表单显示或隐藏
handleModalVisible = flag => {
this.setState({modalVisible: !!flag,});
};
//分页变化
onPageChange = (page,pagesize) => {
console.log("分页",page)
this.setState({page:page},function(){
this.loadData()
});
}
changeCategoryJson = (data) =>{
var arr = [];
data.forEach(o => {
var o1 = {};
o1.key = o.id;
o1.value = o.id+"";
o1.title = o.name;
if(o.children && o.children.length>0){
var arr2 = [];
o.children.forEach(j =>{
var j1 = {};
j1.key = j.id;
j1.value = j.id+"";
j1.title = j.name;
arr2.push(j1)
})
o1.children = arr2;
}
arr.push(o1)
});
return arr;
}
//获取数据
loadData = () => {
httpManager.getGoodsList(this.state.page).then((response) => {
if(!F.checkResponse(response)) return;
this.setState({"listData":response.data.data.data,pageTotal:response.data.data.total});
})
}
//点击删除
handleDelete = (record) =>{
if(window.confirm("确认删除?")){
httpManager.deleteGoods(record.id).then( (response)=>{
if(response.data.code != 0){alert(response.data.message);return;}
this.loadData()
} )
}
}
//点击修改
handleUpdate = (record) =>{
this.setState({modalVisible:true,formType:2,updateId:record.id})
console.log(record);
record.category_id = record.category_id+""
this.form.props.form.setFieldsValue(record);
this.form.didUploadContent()
}
//处理修改或新增
handleFormCreateOrUpdate = (data) => {
console.log(data);
if(this.state.formType == 2 ){
httpManager.updateGoods(this.state.updateId,data).then((response)=>{
if(!F.checkResponse(response)) return;
this.handleModalVisible(false);this.loadData();
});
}else{
httpManager.createGoods(data).then((response)=>{
if(!F.checkResponse(response)) return;
this.handleModalVisible(false);this.loadData();
});
}
}
//
render() {
const columns = [
{
title: 'id',
dataIndex: 'id',
},
{
title: '商品名称',
dataIndex: 'name',
},
{
title: '图片',
dataIndex: 'picture',
render(val){
return <img src={val} width="50px" height="50px" />
}
},
{
title: '库存',
dataIndex: 'stock'
},
{
title: '排序',
dataIndex: 'sort',
},
{
title: '操作',
render: (text, record, index) => (
<Fragment>
<a href="javascript:;" onClick={this.handleUpdate.bind(this,record)} >修改</a>
<Divider type="vertical" />
<a href="javascript:;" onClick={this.handleDelete.bind(this,record)} >删除</a>
</Fragment>
),
},
]
const parentMethods = {
handleModalVisible: this.handleModalVisible,
handleFormCreateOrUpdate:this.handleFormCreateOrUpdate
};
return (
<PageHeaderLayout title="商品管理">
<Card bordered={false}>
<Button icon="plus" type="primary" onClick={() => {this.handleModalVisible(true);this.setState({formType:1});this.form.props.form.resetFields(); this.form.props.form.setFieldsValue({stock:"500",sort:"0",extra_1:"500g",extra_2:"冷藏",extra_3:"7天"}); } }>新建</Button>
<Table
dataSource={this.state.listData}
columns={columns}
rowKey="id"
pagination={{total:this.state.pageTotal,onChange:this.onPageChange}}
/>
</Card>
<CreateForm {...parentMethods} category={this.state.category} modalVisible={this.state.modalVisible} wrappedComponentRef={(form) => this.form = form} />
</PageHeaderLayout>
);
}
}
export default Goods;