123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- import { oss_baseUrl } from '@/config'; // oss 图片地址
- import dayjs from 'dayjs';
- /**
- * 时间戳转时间-过滤器
- * 时间格式:2018-01-01 14:16:13
- */
- function dateFilter(val) {
- let time = parseInt(val) * 1000;
- return dayjs(time).format('YYYY-MM-DD HH:mm:ss');
- }
- /**
- * 标准时间转时间时间-过滤器
- * 时间格式:2018-01-01 14:16:13
- */
- function timeFilter(val) {
- return dayjs(val).format('YYYY-MM-DD HH:mm:ss');
- // let d = new Date(val);
- // let datetime = d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate();
- // return datetime;
- }
- /**
- * 标准时间转时间时间-过滤器
- * 时间格式:2018-01-01
- */
- function dateOnlyFilter(val) {
- return dayjs(val).format('YYYY-MM-DD');
- }
- /**
- * 时间戳转标准时间-过滤器
- * 时间格式:2018-01-01 14:16:13
- */
- function ConvertTime(timestamp) {
- let date = new Date(timestamp);
- let Y = date.getFullYear() + '-';
- let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
- let D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
- let h = (date.getHours() < 10 ? '0' + (date.getHours()) : date.getHours()) + ':';
- let m = (date.getMinutes() < 10 ? '0' + (date.getMinutes()) : date.getMinutes()) + ':';
- let s = (date.getSeconds() < 10 ? '0' + (date.getSeconds()) : date.getSeconds());
- return Y + M + D + h + m + s;
- }
- /**
- * 审核状态过滤器
- * @return {string}
- */
- function Examine(val) {
- let str = '';
- if (val === '1') {
- str = '待审核';
- } else if (val === '2') {
- str = '审核失败';
- } else if (val === '3') {
- str = '审核通过';
- }
- return str;
- }
- /**
- * 时间转标准时间-过滤器
- * 时间格式:2018-01-01 14:16:13 =》标准时间
- */
- function parserDate(date) {
- let t = Date.parse(date);
- if (!isNaN(t)) {
- return new Date(Date.parse(date.replace(/-/g, '/')));
- } else {
- return new Date();
- }
- }
- /**
- * 货币格式化-过滤器
- * (1234,2) => ¥1234.00
- * (1234,2,$) => $1234.00
- * (1234,2,'') => 1234.00
- */
- function moneyFmt(value, decimals = 2, symbol = '¥') {
- const val = parseFloat(value);
- if (isNaN(val)) {
- return value;
- }
- const money = val.toFixed(decimals);
- return `${symbol}${money}`;
- }
- /**
- * 日期格式化-过滤器
- * 根据format格式化日期str
- * 2018-01-01 14:16:13 (yyyy/MM/dd hh-mm-ss) => 2018/01/01 14-16-13
- * 2018-01-01 14:16:13 (yy/M/d h-m) => 18/01/01 14-16
- * 2018-01-01 14:16:13 (yyMMdd) => 180101
- * 2018-01-01 (yyyyMM) => 201801
- * 14:16:13 (hh:mm) => 14:16
- */
- function dateFmt(value, format = 'YYYY-MM-dd hh:mm:ss') {
- if (!value) {
- return;
- }
- const dateObj = parse(value);
- const year = dateObj.year;
- const month = dateObj.month;
- const day = dateObj.day;
- const hour = dateObj.hour;
- const minute = dateObj.minute;
- const second = dateObj.second;
- let str = format;
- str = str.replace(/yyyy|YYYY/, year);
- str = str.replace(/yy|YY/, year % 100 > 9 ? (year % 100).toString() : '0' + (year % 100));
- str = str.replace(/MM|M/g, month);
- str = str.replace(/dd|DD|d|D/g, day);
- str = str.replace(/hh|HH|h|H/g, hour);
- str = str.replace(/mm|m/g, minute);
- str = str.replace(/ss|SS|s|S/g, second);
- return str;
- // 解析日期string->{year,month...},参数格式可以是datetime/date/time
- function parse(dateStr) {
- const dateObj = {
- year: '',
- month: '',
- day: '',
- hour: '',
- minute: '',
- second: ''
- };
- if (typeof dateStr !== 'string') {
- return dateObj;
- }
- const arr = dateStr.split(' ');
- if (arr.length > 1) {
- // datetime
- const _date = arr[0].split('-');
- dateObj.year = _date[0];
- dateObj.month = _date[1];
- dateObj.day = _date[2];
- const _time = arr[1].split(':');
- dateObj.hour = _time[0];
- dateObj.minute = _time[1];
- dateObj.second = _time[2];
- } else if (arr.length === 1) {
- // date or time
- if (arr[0].indexOf('-') > -1) {
- // date
- const _date = arr[0].split('-');
- dateObj.year = _date[0];
- dateObj.month = _date[1];
- dateObj.day = _date[2];
- } else if (arr[0].indexOf(':') > -1) {
- // time
- const _time = arr[0].split(':');
- dateObj.hour = _time[0];
- dateObj.minute = _time[1];
- dateObj.second = _time[2];
- }
- }
- return dateObj;
- }
- }
- /**
- * 字符串长度溢出格式化-过滤器
- */
- function ellipsis(value, len) {
- if (typeof value !== 'string') {
- value = value.toString();
- }
- return value.length > len ? value.substr(0, len) + '...' : value;
- }
- /**
- * 中间变星号
- * phone 手机号
- * bank 银行卡
- * IDcode 身份证
- */
- function star(value, type) {
- if (!value) {
- return;
- }
- if (type === 'phone') {
- return value.substr(0, 3) + '****' + value.substr(7);
- } else if (type === 'bank') {
- return '************' + value.substr(12);
- } else if (type === 'IDcode') {
- return value.substr(0, 1) + '****************' + value.substr(17);
- }
- }
- /**
- * 距离值格式化-过滤器
- * 854 => '<854km'
- * 1234 => '<1.2km'
- * 5678 => '<5.7km'
- */
- function distanceFmt(value) {
- value = parseFloat(value);
- let str = '';
- if (value < 1000) {
- str = `${value.toFixed(0)}m`;
- } else {
- str = `${(value / 1000).toFixed(1)}km`;
- }
- return str;
- }
- /**
- * 静态图片组装格式化-过滤器
- * name 图片名称
- * path 图片路径 默认static
- * type 图片格式 (jpg,png)
- */
- function imageFmt(name, type = 'png', path = 'static') {
- return `${oss_baseUrl}/${path}/${name}.${type}`;
- }
- // 时间截取转换
- function timeClipFmt(value) {
- var time = value.replace("T", " ");
- return time.slice(0, 19);
- };
- // 时间转换
- function timeChange(value) {
- var time = value.replace("T", " ");
- return time.slice(0, 19);
- };
- export default {
- moneyFmt,
- dateFmt,
- Examine,
- timeFilter,
- dateFilter,
- dateOnlyFilter,
- parserDate,
- ellipsis,
- ConvertTime,
- star,
- distanceFmt,
- imageFmt,
- timeClipFmt
- };
|