在main.js 引入
import * as filters from '@/filters/index';
Object.keys(filters).forEach(key => Vue.filter(key, filters[key])); // 循环注册为过滤器
/** Create by QIXIN771
* Create date 2020/7/8
**/
/* 时间戳转换 fmt: 'yyyy-MM-dd hh:mm:ss' */
import { isNullOrUndefined } from 'util';
import { CHINA_ADDRESS } from '../model/data-model/china-address';
export const formatDate = (value, fmt) => {
if (isNullOrUndefined(value) || value === '') {
return;
}
const date = new Date(parseInt(value));
if (typeof fmt === 'undefined') {
fmt = 'yyyy-MM-dd HH:mm:ss';
}
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
}
let o = {
'M+': date.getMonth() + 1,
'd+': date.getDate(),
'H+': date.getHours(),
'm+': date.getMinutes(),
's+': date.getSeconds()
};
for (let k in o) {
if (new RegExp(`(${k})`).test(fmt)) {
let str = o[k] + '';
fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : ('00' + str).substr(str.length));
}
}
return fmt;
};
/* 格式化价格 */
export const moneyFormat = (value, cutSize: number) => {
if (typeof cutSize === 'undefined') {
cutSize = 3;
}
if (!value) return;
if (typeof value == 'number') {
value = '' + value;
}
if (value == null || value == '' || value == undefined || value == 'null' || value == 'undefined') {
return;
}
let str_array = value.split('');
let size = str_array.length;
if (size <= 3) {
return value;
}
str_array = str_array.reverse();
let index = str_array.findIndex(r => r == '.');
let temp_array = [];
for (let i = 0; i < size; i++) {
let s = str_array[i];
temp_array.push(s);
if (s != '.' && i > index) {
if ((i - index) % cutSize == 0) {
temp_array.push(',');
}
}
}
let temp_res = temp_array.reverse();
if (temp_res[0] == '-' && temp_res[1] == ',') {
temp_res.splice(1, 1);
}
let res = temp_res.join('');
res = res.replace(/^,/, '');
return res;
};
/* 格式化账号 */
export const accountFormat = (data: any, key) => {
// 设置银行卡号,每四位添加一个空格
data[key] = data[key]
.replace(/\s/g, '')
.replace(/[^\d]/g, '')
.replace(/(\d{4})(?=\d)/g, '$1 ');
data[key] = data[key];
};
/* 格式化金额小数点位数 */
export const doubleFormat = (value: string, cutSize: number) => {
if (typeof cutSize === 'undefined') {
cutSize = 2;
}
if (value == null) {
return value;
}
if (typeof value !== 'string') {
return value;
}
const tmp = parseFloat(value);
if (isNaN(tmp)) {
return 'not a number';
} else {
return tmp.toFixed(cutSize);
}
};
/* 状态类型 */
export const searchOption = (value, option: any) => {
let label = '';
option.forEach(res => {
if (res.value === value) {
label = res.label;
}
});
return label;
};
// 省市区地址
export const cityFormat = (value: string) => {
if (!isNullOrUndefined(value)) {
if (value.indexOf('undefined') >= 0) {
return '';
}
let arr = [];
let cityStr: string = '';
let data = CHINA_ADDRESS;
if (value.indexOf('/') >= 0) {
arr = value.split('/');
arr.forEach(element => {
let res = data.find(item => item.value === element || item.name === element);
cityStr += res.name;
});
} else if (!isNaN(Number(value))) {
if (value.substr(2, 2) == '00') {
const province = data.find(item => item.value === value);
cityStr = province.name;
} else if (value.substr(4, 2) == '00') {
const city = data.find(item => item.value === value);
const province = data.find(item => item.value === city.parent);
cityStr = province.name + city.name;
} else {
const area = data.find(item => item.value === value);
const city = data.find(item => item.value === area.parent);
const province = data.find(item => item.value === city.parent);
cityStr = province.name + city.name + area.name;
}
} else {
return '';
}
return cityStr;
}
}