You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
4.7 KiB
TypeScript

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import dayjs from 'dayjs';
import _Vue from 'vue';
export const PATTERN_DATETIME = 'YYYY-MM-DD HH:mm:ss';
export const PATTERN_DATE = 'YYYY-MM-DD';
export const PATTERN_TIME = 'HH:mm:ss';
/**
* 对象数组去重
* 传入数组 list 返回去重后的数组对象
* @param list 数据
* @param valueKey 去重的属性 valueKey
*/
export const removeDuplicates = (list: any, valueKey: string) => {
const temp = {};
let arr = [];
arr = list.reduce(function (item, next) {
temp[next[valueKey]] ? '' : (temp[next[valueKey]] = true && item.push(next));
return item;
}, []);
return arr;
};
/**
* 数据NULL处理
* @param object 数据或对象
* @param path 如果object传的是对象path代表字段名称支持 a.b.c 层级查询
* @param defaultValue
*/
export const optional = (object: any, path: any, defaultValue: any) => {
try {
if (defaultValue === undefined) {
defaultValue = path;
} else if (path) {
// this means path='a.b.c', object = object.a.b.c
object = path.split('.').reduce((o: any, i: any) => o[i], object);
}
} catch (e) {
console.warn(e.message);
}
return object || defaultValue;
};
/**
* 数据脱敏 123456 => 12**56
* @param data
* @return {string}
*/
export const masking = (data: string) => {
if (!data) return '****';
let size = data.length / 2 - 1;
if (size < 2) size = 2;
const masking = '********************'.substring(0, size);
const prefix = data.substring(0, (data.length - masking.length) / 2);
const suffix = data.substring(prefix.length + masking.length, data.length);
return prefix + masking + suffix;
};
/**
* 日期格式化
* @param object 日期或对象
* @param path 如果object传的是对象path代表字段名称支持 a.b.c 层级查询
* @return {string}
*/
export const date = (object: any, path: any) => {
return format(object, path, PATTERN_DATE);
};
/**
* 时间格式化
* @param object 时间或对象
* @param path 如果object传的是对象path代表字段名称支持 a.b.c 层级查询
* @return {string}
*/
export const time = (object: any, path: any) => {
return format(object, path, PATTERN_TIME);
};
/**
* 日期时间格式化
* @param object 日期时间或对象
* @param path 如果object传的是对象path代表字段名称支持 a.b.c 层级查询
* @return {string}
*/
export const datetime = (object: any, path: any) => {
return format(object, path, PATTERN_DATETIME);
};
/**
* 日期时间格式化
* @param object 日期时间或对象
* @param path 如果object传的是对象path代表字段名称支持 a.b.c 层级查询
* @param pattern 格式
* @return {string}
*/
export const format = (object: any, path: any, pattern: any) => {
const date = optional(object, path, '');
return date && dayjs(date).format(pattern);
};
/**
* 保留N位小数格式化
* @param object 数字或对象
* @param path 如果object传的是对象path代表字段名称支持 a.b.c 层级查询
* @param fractionDigits 保留N位小数
*/
export const fixed = (object: any, path: any, fractionDigits: any) => {
if (fractionDigits === undefined) {
fractionDigits = path;
path = undefined;
}
return Number(optional(object, path, 0)).toFixed(fractionDigits);
};
/**
* 金额格式化 0.00
* @param object 数字或对象
* @param path 如果object传的是对象path代表字段名称支持 a.b.c 层级查询
*/
export const money = (object: any, path: any) => {
return fixed(object, path, 2);
};
/**
* 距离格式化
* @param object 数字或对象
* @param path 如果object传的是对象path代表字段名称支持 a.b.c 层级查询
*/
export const distance = (object: any, path: any) => {
return fixed(object, path, 2) + 'km';
};
/**
* 返回静态资源路径处理方法
* @param staticPath 静态资源路径前缀
* @return {function(*=, *=): *}
*/
export const assets = (staticPath: any) => {
/**
* 静态资源路径处理
* @param return.object 路径或对象
* @param path 如果object传的是对象path代表字段名称支持 a.b.c 层级查询
* @return {*}
*/
return (object: any, path: any) => {
const suffixPath = optional(object, path, '');
if (!suffixPath || suffixPath.startsWith('http')) {
return suffixPath;
}
return staticPath + suffixPath;
};
};
export default {
install(Vue: typeof _Vue): void {
Vue.filter('optional', optional);
Vue.filter('masking', masking);
Vue.filter('date', date);
Vue.filter('time', time);
Vue.filter('datetime', datetime);
Vue.filter('format', format);
Vue.filter('fixed', fixed);
Vue.filter('money', money);
Vue.filter('distance', distance);
Vue.filter('assets', assets(process.env.VUE_APP_STATIC_PATH));
},
};