bannerAdd.js
7.54 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
import React,{Component} from 'react';
import WhiteBar from '@src/components/Common/WhiteBar';
import {Table,Input,Form,Button,Row,Col,Message,Radio,Upload,Icon} from 'antd';
import { API_MENU_MANAGE, API_FILEUPLOAD_MANAGE,API_SYS_CMS } from '@src/Api';
import SYS_CONFIG from '@src/config/constant.js';
import {Link} from 'react-router-dom';
import axios from '@src/axios/index';
class BannerAdd extends Component {
state = {
cover: '', // 图片上传的地址
id: '',
}
upDateCount = {count: 0}
componentDidMount () {
let id = this.props.match.params.id;
if (id) {
this.setState({id});
this.getBannerDetail(id);
}
}
getBannerDetail = (id) => {
axios.ajax({
url: API_SYS_CMS.getAppBannerById,
data: {
id: id
}
}).then((res) => {
this.props.form.setFieldsValue({
state: res.state,
title: res.title
});
this.setState({
cover: res.cover,
conent: res.content
});
})
}
imgUpLoadChange = (info,fileList,e) => {
let _this = this;
if (info.file.status === 'done') {
this.getBase64(info.file.originFileObj,(imgUrl) => {
// imgUrl: base64位编码
_this.imgUpLoad(imgUrl);
});
}
}
// 图片上传
imgUpLoad = (imageUrl) => {
axios.ajax({
url: API_FILEUPLOAD_MANAGE.base64,
data: {
"imageDatas": [{
"className": "bannerImg",
"data": imageUrl
}]
}
}).then((res) => {
console.log(res);
let cover = SYS_CONFIG.fileApi + '/img/' + res[0];
this.setState({cover},() => {
console.log(this.state.cover);
});
});
}
//图片转base64
getBase64 = (img, callback)=> {
const reader = new FileReader();
reader.addEventListener('load', () => callback(reader.result));
reader.readAsDataURL(img)
}
// 移除
removeImg = (file) => {
this.setState({cover: ''});
this.upDateCount.count = 0;
}
// 上传前
beforeUpload = (file) => {
let _this = this;
return new Promise ((resolve,reject) => {
// 对图片上传图片进行验证
const imgArr = ['image/jpeg', 'image/png', 'image/jpg'];
const isJPG = imgArr.indexOf(file.type) === -1;
if (isJPG) {
Message.warn('图片只支持jpeg,png格式');
reject();
}
const isLt2M = file.size / 1024 / 1024 < 2;
if (!isLt2M) {
Message.warn('图片不能大于2M');
reject();
}
if (_this.state.id) {
if (_this.upDateCount.count == 0) {
// 更改图片
_this.setState({cover: ''});
_this.upDateCount.count++;
resolve();
} else {
Message.warn('每次只能上传一张图片');
reject();
}
} else {
// 添加图片
if (_this.state.cover !== '') {
Message.warn('每次只能上传一张图片');
reject();
} else {
resolve();
}
}
})
}
// 保存图片
save = () => {
let _this = this;
// 进行验证
this.props.form.validateFields((err,data) => {
if (!err) {
if (!this.state.cover) {
Message.warn('请先上传图片');
return;
}
if (_this.state.id) {
// 修改图片
_this.updateBanner(data);
} else {
// 新增图片
_this.addBanner(data);
}
}
})
}
// 添加图片
addBanner = (data) => {
axios.ajax({
url: API_SYS_CMS.addBanner,
data: {
...data,
cover: this.state.cover
}
}).then((res) => {
Message.success('保存成功');
this.props.history.push({pathname: '/home/permission/bannerManage'});
});
}
// 修改图片
updateBanner = (data) => {
axios.ajax({
url: API_SYS_CMS.editBanner,
data: {
...data,
cover: this.state.cover,
id: this.state.id
}
}).then((res) => {
Message.success('修改成功');
this.props.history.push({pathname: '/home/permission/bannerManage'});
});
}
render () {
let _this = this;
const FormItem = Form.Item;
const {getFieldDecorator} = this.props.form;
const RadioGroup = Radio.Group;
const formItemLayout = {
labelCol: {
md: { span: 12 },
lg: { span: 8 },
},
wrapperCol: {
md: { span: 24 },
lg: { span: 16 },
},
}
return (
<div className="com-BannerAdd">
<WhiteBar title="添加图片" />
<div style={{paddingLeft: 20}}>
<Button>
<Link to="/home/permission/bannerManage">返回</Link>
</Button>
</div>
<WhiteBar title="基本信息" />
<div style={{background: '#fff',padding: 20}}>
<Form>
<Row gutter={10}>
<Col span={6}>
<FormItem {...formItemLayout} label="标题名称:">
{
getFieldDecorator('title',{
rules: [
{required: true,message: '标题不能为空'},
{max:20,message: '标题名称不能超过20个字符'},
{pattern: /^(?!_)(?!.*?_$)[a-zA-Z0-9_\u4e00-\u9fa5]+$/, message: '标题名称名称不能有特殊字符'}
]
})(
<Input type="text" placeholder="标题名称" />
)
}
</FormItem>
</Col>
<Col span={6}>
<FormItem {...formItemLayout} label="状态:">
{
getFieldDecorator('state',{
rules: [
{required: true,message: '状态不能为空'}
]
})(
<RadioGroup onChange={this.chgIconSelect}>
<Radio value={1}>有效</Radio>
<Radio value={2}>无效</Radio>
</RadioGroup>
)
}
</FormItem>
</Col>
<Col span={6}>
<FormItem {...formItemLayout} label="图片链接:">
{
getFieldDecorator('content',{
rules: [
]
})(
<Input type="text" placeholder="图片链接的地址" />
)
}
</FormItem>
</Col>
</Row>
<Row>
<Col span={18} style={{paddingTop: 5,textAlign:'center'}}>
<Upload
accept={"image/*"}
action={SYS_CONFIG.baseApi + API_FILEUPLOAD_MANAGE.base64}
onChange={this.imgUpLoadChange}
onRemove={this.removeImg}
beforeUpload={this.beforeUpload}
className='uploadBtn'>
<Button>
<Icon type="upload" /> 点击上传
</Button>
</Upload>
</Col>
</Row>
<Row>
<Col span={18}>
<div style={{margin:'auto',width: 400,height: 250,textAlign: 'center',border:'1px solid #ddd'}}>
{
this.state.cover == '' ?
<p style={{marginTop: 50}}>图片预览</p> : <img style={{width: 300 }} src={this.state.cover} />
}
</div>
</Col>
</Row>
</Form>
</div>
<div style={{marginTop:20,textAlign: 'center'}}>
<Button onClick={this.save}>保存</Button>
</div>
</div>
)
}
}
export default Form.create()(BannerAdd);