utils.js
7.73 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
import React from 'react';
import { Select,Tooltip } from 'antd'
import axios from '@src/axios/index';
import { API_DICT_MANAGE } from '@src/Api'
import moment from 'moment';
const Option = Select.Option;
export default {
formateDate(time) {
if (!time) return '';
let date = new Date(time);
return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();
},
formateDateToYMDSFM(time) {
if (!time) return '';
let date = new Date(time * 1000);
return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();
},
formateDateToYMD(time) {
if (!time) return '';
let date = new Date(time * 1000);
return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' '
},
formateDateToSFM(time) {
if (!time) return '';
let date = new Date(time * 1000);
return date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();
},
formateDateToDetail(time,type) {
if (!time) return '';
let date;
switch (type) {
case 'seconds':
date = new Date(time * 1000);
break;
case 'standard':
date = new Date(time);
break;
}
let year = date.getFullYear();
let month = date.getMonth() + 1;
let day = date.getDate();
let hour = date.getHours() >= 10 ? date.getHours() : '0' + date.getHours();
let minutes = date.getMinutes() >= 10 ? date.getMinutes() : '0' + date.getMinutes();
let seconds = date.getSeconds() >= 10 ? date.getSeconds() : '0' + date.getSeconds();
return year + '-' + month + '-' + day + ' ' + hour + ':' + minutes + ':' + seconds;
},
//日期获取时间戳
formatTimeStamp(date) {
return parseInt(moment(date).valueOf() / 1000)
},
//时间戳转日期
formatMomentDate(stamp) {
if (typeof (stamp) === "string") {
stamp = Number(stamp)
}
return moment(stamp).format('YYYY-MM-DD')
},
formatTableColumn(field){
if(!field) return ''
if (field.length > 80) {
return <Tooltip placement="topLeft" title={field}>
<span className="tdWidthStyle">{field}</span>
</Tooltip>
} else if (7<field.length <80 ) {
return <Tooltip placement="bottomLeft" title={field}>
<span className="tdWidthStyle">{field}</span>
</Tooltip>
}else {
return field
}
},
//过滤空格
filterSpace (e, field,that) {
let userInfo = that.props.form.getFieldsValue();
if (e.keyCode === 32 && userInfo[field]) {
that.props.form.setFieldsValue({ [field]: userInfo[field].replace(/^\s+|\s+$/g, '') })
}
},
// 去掉左右两边的空格
trim (value){
if (value === null) {
return;
}
if (typeof (value) === 'object') {
var newObje = {};
for( let items in value) {
if (typeof (value[items]) === 'string'){
newObje[items] = value[items].replace(/(^\s*)|(\s*$)/g, '');
} else {
newObje[items] = value[items];
}
}
return newObje;
}
if (typeof (value) === 'string') {
return value.replace(/(^\s*)|(\s*$)/g, '')
} else {
return ;
}
},
// 将数据转成table tree 结构
formatTreeData(data, name = 'childOrgs') {
if(!data){
return
}
data.map(item => {
if (item[name]) {
if (item[name].length > 0) {
item[name] = this.formatTreeData(item[name], name);
} else {
delete item[name];
}
}
})
return data;
// let ss = JSON.stringify(data).replace(/,"childOrgs":\[\]/g, '').replace(/"childOrgs":\[\],/g,'')
// return JSON.parse(ss);
},
pagination(data, callback) {
return {
onChange: (current,e) => {
console.log('jump:' + current);
console.log('jump:' + e);
callback(current)
},
pageNum:3,
current: data.current,
pageSize: data.size,
total: data.total,
showTotal: () => {
return `当前共${data.total}条`
},
showQuickJumper: true
}
},
// 格式化金额,单位:分(eg:430分=4.30元)
formatFee(fee, suffix = '') {
if (!fee) {
return 0;
}
return Number(fee).toFixed(2) + suffix;
},
// 格式化公里(eg:3000 = 3公里)
formatMileage(mileage, text) {
if (!mileage) {
return 0;
}
if (mileage >= 1000) {
text = text || " km";
return Math.floor(mileage / 100) / 10 + text;
} else {
text = text || " m";
return mileage + text;
}
},
// 隐藏手机号中间4位
formatPhone(phone) {
phone += '';
return phone.replace(/(\d{3})\d*(\d{4})/g, '$1***$2')
},
// 隐藏身份证号中11位
formatIdentity(number) {
number += '';
return number.replace(/(\d{3})\d*(\d{4})/g, '$1***********$2')
},
getOptionList(data, value, label) {
var value = value || 'id';
var label = label || 'name';
if (!data) {
return [];
}
let options = [
<Option value={0} key="all_key">其它</Option>
] //[<Option value="0" key="all_key">全部</Option>];
data.map((item) => {
options.push(<Option value={item[value]} key={item[value]}>{item[label]}</Option>)
})
return options;
},
getOptionHtml(data, value, label) {
var value = value || 'id';
var label = label || 'name';
if (!data) {
return [];
}
let options = []
data.map((item) => {
options.push(<Option value={item[value]} key={item[value]}>{item[label]}</Option>)
})
return options;
},
/**
* ETable 行点击通用函数
* @param {*选中行的索引} selectedRowKeys
* @param {*选中行对象} selectedItem
*/
updateSelectedItem(selectedRowKeys, selectedRows, selectedIds) {
if (selectedIds) {
this.setState({
selectedRowKeys,
selectedIds: selectedIds,
selectedItem: selectedRows
})
} else {
this.setState({
selectedRowKeys,
selectedItem: selectedRows
})
}
},
/**
* getDictOpt 获取字典里面的下拉 因为后台的code 字段极可能会改,所以在前端我做了一个对应的对象来做控制
* @param {*传过来的数组} list
*/
getDictOpt( list ) {
// 前端需要定义一套code值,以为后台这个值会变动
const data = {
nation: 'nation', // 国籍
marriage: 'marriage', // 婚姻状态
cert: 'cert', //证件类型
occupation: 'occupation',
userGrade: 'userGrade',
creditGrade: 'creditGrade',
insurance: 'insurance',
smoke: 'smoke',
msgTempType: 'msgTempType'
}
let codes = []
list.map((item) =>{
codes.push(data[item])
})
return new Promise((resolve, reject) =>{
axios.ajax({
url: API_DICT_MANAGE.getDictsByCode,
data: {codes}
}).then(res => {
if (res && res.length > 0) {
resolve(res)
}
})
})
},
// 表单正则表达式以及提示
getFormRules (name) {
const list = {
'numLetters': {regular:/^([a-zA-Z0-9])*$/},
'version': {regular: /^\w*(\.[0-9]+)*$/},
'chartLetters': {regular: /^\s*[\u4E00-\u9FA5-a-zA-Z]{0,100}(?:·[\u4E00-\u9FA5-a-zA-Z]{0,100})*\s*$/},
'IDCode':{regular:/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/},
'phone': {regular: /^(\s*||\s*(\d{5})||\s*(\d{3,4})?(-)?\d{5,8}\s*)$/},
'mobile': {regular: /^(0|86|17951)?(13[0-9]|15[012356789]|17[0-9]|18[0-9]|14[57])[0-9]{8}$/},
'email': {regular : /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/},
'number': {regular: /^[0-9]*$/},
'letters': {regular: /^([a-zA-Z])+$/}
}
return list[name]
}
}