msgTemplateAdd.js
10.4 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
import React,{Component} from 'react';
import './style.less';
import WhiteBar from '../../components/Common/WhiteBar';
import SelecteReciver from './selecteReciver';
import SelecteOrg from './selecteOrg';
import EditorConvertToHTML from './MarkDown';
import {Table,Input,Form,Button,Row,Col,Select,DatePicker,message,Icon,Modal,Tree,Radio, Spin} from 'antd';
import axios from '@src/axios/index';
import Utils from '@src/utils/utils';
import { API_SYS_MSG } from '@src/Api'
import { NavLink } from 'react-router-dom'
const FormItem = Form.Item;
const Option = Select.Option;
const { TextArea } = Input;
class msgTemplateAdd extends React.Component {
state={
textAreaShow: {display: 'block'}, // 多行文本框展示
richAreaShow: {display: 'none'}, // 富文本框展示
msgTemplateInfo:{},
titName:'新增模板',
isDetail: false,
fillData: {},
spinStatus: false,
tempTypeOpts: [],
tempTypeObj: []
}
//页面数据初始化加载
componentWillMount() {
this.getRouterParams()
}
// 模块类型下拉.
getDictSelect = ()=> {
const params = this.props.match.params
let codes = ['msgTempType'];
const options = Utils.getDictOpt(codes);
options.then( (res)=> {
if(params.optTag !== 'addItem') {
this.getSysMsgTemplateById()
}
let resList = res[0].childDicts;
let options = Utils.getOptionHtml(resList, 'id', 'name')
let resObj = {}
resList.map((item) =>{
resObj[item.id] = item.name
})
this.setState({tempTypeOpts: options, tempTypeObj: resObj})
})
}
//获取跳转过来的传参判断是新增、修改模板
getRouterParams = () => {
const params = this.props.match.params
if (!params || !params.optTag) {
message.warn('请重新选择,或者刷新页面')
this.props.history.push({
pathname: '/home/msgPush/msgTemp',
});
return
}
if (params.optTag === 'addItem') {
this.setState({
titName: '新增模板'
})
} else if (params.optTag === 'updateItem') {
this.setState({
titName: '修改模板',
spinStatus: true,
})
} else if (params.optTag === 'detailItem') {
this.setState({
titName: '查看模板',
isDetail: true
})
}
this.getDictSelect()
}
//根据id查询消息模板
getSysMsgTemplateById= ()=>{
const { optTag,id } = this.props.match.params
if (!id) {
message.warn('请重新选择,或者刷新页面')
this.props.history.push({
pathname: '/home/msgPush/msgTemp',
});
return
}else {
axios.ajax({
url: API_SYS_MSG.getSysMsgTemplateById,
data: {
id:id
}
}).then(res => {
this.setState({
spinStatus: false,
})
if (res && Object.keys(res).length > 0) {
let fillData = res
if (this.state.isDetail) {
this.setState({
msgTemplateInfo: res,
templateContent:res.templateContent,
fillData: fillData
}, ()=>{
})
return
}
this.props.form.setFieldsValue({ 'templateName': res.templateName })
this.props.form.setFieldsValue({ 'templateContent': res.templateContent })
this.props.form.setFieldsValue({ 'remark': res.remark })
this.onChangeTemplateType(res.templateType)
this.setState({
msgTemplateInfo: res,
templateContent:res.templateContent
})
}
})
}
}
// 设置模板类型
onChangeTemplateType = (temp) => {
const templateType = this.state.tempTypeObj[temp]
if ( templateType.indexOf('短信') !== -1 ) {
// 短信提醒模板
this.setState({
textAreaShow: {display: 'block'},
richAreaShow: {display: 'none'},
});
} else if (templateType.indexOf('邮件') !== -1 ) {
// 邮箱提醒模板
this.setState({
textAreaShow: {display: 'none'},
richAreaShow: {display: 'block'},
});
}
}
//设置富文本
setRichContent = (content) => {
this.props.form.setFieldsValue({templateContent:content});
}
//消息模板新增或修改
handleOperator = (e) => {
e && e.preventDefault();
let data = Utils.trim(this.props.form.getFieldsValue());
const content = data.templateContent? data.templateContent.replace(/<\/?.+?>/g,"") : ''
if(content.length > 5000) {
message.error('您输入的字符过长')
return
}
const { optTag } = this.props.match.params
const { msgTemplateInfo } = this.state
this.props.form.validateFieldsAndScroll((err, values) => {
if (!err) {
this.setState({
spinStatus: true,
})
axios.optAjax({
url: API_SYS_MSG.insertSysMsgTemplate,
data: {
...data,
id: optTag && optTag === 'updateItem' ? msgTemplateInfo.id:""
}
}).then(res=>{
if(res && res.code && res.code !== 0){
this.setState({
spinStatus: false,
})
return
}
message.success(optTag && optTag === 'updateItem' ? '修改成功' : '新增成功')
this.props.history.push({
pathname: '/home/msgPush/msgTemp',
});
})
}
});
}
// 模块类型下拉
getDictSelect() {
let codes = ['msgTempType'];
const options = Utils.getDictOpt(codes);
options.then( (res)=> {
let resList = res[0].childDicts;
let options = Utils.getOptionHtml(resList, 'id', 'value')
let resObj = {}
resList.map((item) =>{
resObj[item.id] = item.value
})
this.setState({tempTypeOpts: options, tempTypeObj: resObj})
})
}
// 返回
goBack = () => {
this.props.history.push({
pathname: '/home/msgPush/msgTemp'
});
}
render () {
const { getFieldDecorator } = this.props.form;
const { msgTemplateInfo,templateContent } = this.state;
const formItemLayout = {
labelCol: {
md: { span: 12 },
lg: { span: 6 },
},
wrapperCol: {
md: { span: 24 },
lg: { span: 18 },
},
};
const formTextAreaLayout = {
labelCol: {span:2 },
wrapperCol: {span: 16}
}
let messageContent =templateContent?templateContent :'';
return (
<div className="msgTemplateManage">
<Spin tip="加载中..." spinning = {this.state.spinStatus}>
<div className="nav"><a href="#/home/msgPush/msgTemp">消息模板管理</a> > <span>{this.state.titName}</span></div>
<WhiteBar title="基本信息" />
<div className="taskContent">
<Form>
<Row gutter={24}>
<Col span="8">
<FormItem label="模板名称" {...formItemLayout}>
{
this.state.isDetail ?
<span className="ovHidden" title={this.state.fillData.templateName} >{this.state.fillData.templateName}</span> :
getFieldDecorator('templateName', {
rules: [
{ required: true, message: '请输入模板名称'},
{ max: 128, whitespace: true, message: '模板名称最长为128位字符' },
]
})(
<Input placeholder="模板名称" />
)
}
</FormItem>
</Col>
<Col span="8">
<FormItem label="模板类型" {...formItemLayout}>
{
this.state.isDetail ?
<span>{this.state.tempTypeObj[this.state.fillData.templateType]}</span> :
getFieldDecorator('templateType', {
initialValue: msgTemplateInfo.templateType ? this.state.tempTypeObj[msgTemplateInfo.templateType] : undefined,
})(
<Select onChange={this.onChangeTemplateType} placeholder="模板类型">
{this.state.tempTypeOpts}
</Select>
)
}
</FormItem>
</Col>
</Row>
<Row style={this.state.textAreaShow}>
<Col span={24}>
<FormItem {...formTextAreaLayout} label={<span className="multLable"><span style={{color:'#f00'}}> * </span>模板内容 </span>}>
{
this.state.isDetail ?
<span dangerouslySetInnerHTML = {{ __html:this.state.fillData.templateContent }}>
</span> :
getFieldDecorator('templateContent',{
rules: [
{ max: 5000, message: '模板内容最长为5000位字符' },
]
})(
<TextArea style={{minHeight:400}} placeholder="消息内容" />
)
}
</FormItem>
</Col>
</Row>
<Row style={this.state.richAreaShow}>
<Col className="ant-form-item-label" span={2} style = {{textAlign:'center' ,paddingLeft: 10}}>
<label for="templateName" className="ant-form-item-required" title="模板名称">模板内容:</label>
</Col>
<Col span={18}>
<div style={{border: '1px solid #D2D2D2',minHeight:400}}>
<EditorConvertToHTML source = {messageContent} messageContent={messageContent} setRichContent={this.setRichContent} />
</div>
</Col>
</Row>
<Row gutter={24} style={{marginTop: '20px'}}>
<Col span="24">
<FormItem {...formTextAreaLayout} label="备  注:" >
{
this.state.isDetail ?
<span>{this.state.fillData.remark}</span> :
getFieldDecorator('remark',{
rules: [{ max: 300, message: '备注最长为300位字符' },]
})(
<TextArea placeholder="备注" rows={8} columns={10} />
)
}
</FormItem>
</Col>
</Row>
</Form>
</div>
<div className="submitBtn">
{
this.state.isDetail ?
'' : <Button type="primary" disabled = {this.state.spinStatus} onClick={this.handleOperator} >保存</Button>
}
<NavLink to='/home/msgPush/msgTemp'> <Button> 返回 </Button></NavLink>
</div>
</Spin>
</div>
)
}
}
export default Form.create()(msgTemplateAdd);