|
@@ -1,5 +1,9 @@
|
|
|
import Vue from 'vue'
|
|
|
-import moment from 'moment'
|
|
|
+import {
|
|
|
+ dateFormatter,
|
|
|
+ dateFromNow,
|
|
|
+ addSpaceForTel
|
|
|
+} from './globalFunctions'
|
|
|
|
|
|
// 注册全局过滤器
|
|
|
|
|
@@ -7,6 +11,29 @@ Vue.filter('fen2Yuan', function (v) {
|
|
|
return v / 100
|
|
|
})
|
|
|
|
|
|
+// 时间格式化(同时间格式化函数)
|
|
|
+Vue.filter('dateFormatter', dateFormatter)
|
|
|
+// 时间戳转换 多少秒、多少分、多少小时前、多少天前 超出10天显示年月日
|
|
|
+Vue.filter('dateFromNow', dateFromNow)
|
|
|
+// 手机号加空格
|
|
|
+Vue.filter('addSpaceForTel', addSpaceForTel)
|
|
|
+
|
|
|
+// Vue.filter('addSpaceForBank', function (v) {
|
|
|
+// // 纯数字银行卡号字符串加空格
|
|
|
+// // return v.replace(/\s/g, '').replace(/(\d{4})(?=\d)/g, '$1 ')
|
|
|
+// // 带有*的银行卡号字符串加空格
|
|
|
+// return v.replace(/\s/g, '').replace(/(.{4})/g, '$1 ')
|
|
|
+// })
|
|
|
+
|
|
|
+// Vue.filter('addConfusionForBank', function (v) {
|
|
|
+// // 带有*的银行卡号字符串加空格
|
|
|
+// if (String(v).length < 12) {
|
|
|
+// return v.replace(/\s/g, '').replace(/^(\d{2})\d+(\d{2})$/, '$1 **** **** $2')
|
|
|
+// } else {
|
|
|
+// return v.replace(/\s/g, '').replace(/^(\d{4})\d+(\d{4})$/, '$1 **** **** $2')
|
|
|
+// }
|
|
|
+// })
|
|
|
+
|
|
|
// 金额大写,链接:https://juejin.im/post/5a2a7a5051882535cd4abfce
|
|
|
// upDigit(1682)
|
|
|
// result:"人民币壹仟陆佰捌拾贰元整"
|
|
@@ -40,61 +67,6 @@ Vue.filter('upPrice', function (n) {
|
|
|
return head + s.replace(/(零.)*零元/, '元').replace(/(零.)+/g, '零').replace(/^整$/, '零元整')
|
|
|
})
|
|
|
|
|
|
-// 时间格式化(同时间格式化函数)
|
|
|
-/*
|
|
|
-* 时间格式化函数(将时间格式化为,2019年08月12日,2019-08-12,2019/08/12的形式)
|
|
|
-* pattern参数(想要什么格式的数据就传入什么格式的数据)
|
|
|
-* · 'yyyy-MM-dd' ---> 输出如2019-09-20
|
|
|
-* · 'yyyy-MM-dd HH:mm' ---> 输出如2019-09-20 08:20
|
|
|
-* · 'yyyy-MM-dd HH:mm:ss' ---> 输出如2019-09-20 08:20:23
|
|
|
-* · 'yyyy/MM/dd' ---> 输出如2019/09/20
|
|
|
-* · 'yyyy年MM月dd日' ---> 输出如2019年09月20日
|
|
|
-* · 'yyyy年MM月dd日 HH时mm分' ---> 输出如2019年09月20日 08时20分
|
|
|
-* · 'yyyy年MM月dd日 HH时mm分ss秒' ---> 输出如2019年09月20日 08时20分23秒
|
|
|
-* · 'yyyy年MM月dd日 HH时mm分ss秒 EE' ---> 输出如2019年09月20日 08时20分23秒 周二
|
|
|
-* · 'yyyy年MM月dd日 HH时mm分ss秒 EEE' ---> 输出如2019年09月20日 08时20分23秒 星期二
|
|
|
-* 参考: https://www.cnblogs.com/mr-wuxiansheng/p/6296646.html
|
|
|
-*/
|
|
|
-Vue.filter('dateFormatter', function (date, fmt = 'yyyy-MM-dd HH:mm:ss') {
|
|
|
- if (!date) return
|
|
|
- date = new Date(moment(date).valueOf())
|
|
|
- const o = {
|
|
|
- 'y+': date.getFullYear(),
|
|
|
- 'M+': date.getMonth() + 1, // 月份
|
|
|
- 'd+': date.getDate(), // 日
|
|
|
- // 12小时制
|
|
|
- 'h+': date.getHours() % 12 === 0 ? 12 : date.getHours() % 12, // 小时
|
|
|
- // 24小时制
|
|
|
- 'H+': date.getHours(), // 小时
|
|
|
- 'm+': date.getMinutes(), // 分
|
|
|
- 's+': date.getSeconds(), // 秒
|
|
|
- 'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
|
|
|
- S: date.getMilliseconds(), // 毫秒
|
|
|
- 'E+': date.getDay() // 周
|
|
|
- }
|
|
|
- const week = {
|
|
|
- 0: '日',
|
|
|
- 1: '一',
|
|
|
- 2: '二',
|
|
|
- 3: '三',
|
|
|
- 4: '四',
|
|
|
- 5: '五',
|
|
|
- 6: '六'
|
|
|
- }
|
|
|
- if (/(y+)/.test(fmt)) {
|
|
|
- fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
|
|
|
- }
|
|
|
- if (/(E+)/.test(fmt)) {
|
|
|
- fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? '星期' : '周') : '') + week[date.getDay() + ''])
|
|
|
- }
|
|
|
- for (const k in o) {
|
|
|
- if (new RegExp('(' + k + ')').test(fmt)) {
|
|
|
- fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
|
|
|
- }
|
|
|
- }
|
|
|
- return fmt
|
|
|
-})
|
|
|
-
|
|
|
// 金额3位逗号分隔
|
|
|
Vue.filter('formatNumber', function (s, n = -1, comma = false) {
|
|
|
if (n !== -1) n = n > 0 && n <= 20 ? n : 2
|
|
@@ -132,61 +104,3 @@ Vue.filter('formatNumber', function (s, n = -1, comma = false) {
|
|
|
|
|
|
return left.reverse().join('') + point + right
|
|
|
})
|
|
|
-
|
|
|
-Vue.filter('dateFromNow', function (time: string | number) {
|
|
|
- const date1 = new Date(time) // 开始时间
|
|
|
- const date2 = new Date() // 结束时间
|
|
|
- const date3 = date2.getTime() - date1.getTime() // 时间差的毫秒数
|
|
|
- // 计算出相差天数
|
|
|
- const days = Math.floor(date3 / (24 * 3600 * 1000))
|
|
|
- // 计算出小时数
|
|
|
- const leave1 = date3 % (24 * 3600 * 1000) // 计算天数后剩余的毫秒数
|
|
|
- const hours = Math.floor(leave1 / (3600 * 1000))
|
|
|
- // 计算相差分钟数
|
|
|
- const leave2 = leave1 % (3600 * 1000) // 计算小时数后剩余的毫秒数
|
|
|
- const minutes = Math.floor(leave2 / (60 * 1000))
|
|
|
- // 计算相差秒数
|
|
|
- let td = '30秒前'
|
|
|
- if (days > 0) {
|
|
|
- if (days > 10) {
|
|
|
- const date1year = date1.getFullYear()
|
|
|
- const date2year = date2.getFullYear()
|
|
|
- let date1month: any = date1.getMonth() + 1
|
|
|
- let date1day: any = date1.getDate()
|
|
|
- if (date1month < 10) {
|
|
|
- date1month = '0' + date1month
|
|
|
- }
|
|
|
- if (date1day < 10) {
|
|
|
- date1day = '0' + date1day
|
|
|
- }
|
|
|
- if (date1year < date2year) {
|
|
|
- td = date1.getFullYear() + '-' + date1month + '-' + date1day
|
|
|
- } else {
|
|
|
- td = date1month + '-' + date1day
|
|
|
- }
|
|
|
- } else {
|
|
|
- td = days + '天前'
|
|
|
- }
|
|
|
- } else if (hours > 0) {
|
|
|
- td = hours + '小时前'
|
|
|
- } else if (minutes > 0) {
|
|
|
- td = minutes + '分钟前'
|
|
|
- }
|
|
|
- return td
|
|
|
-})
|
|
|
-
|
|
|
-// Vue.filter('addSpaceForBank', function (v) {
|
|
|
-// // 纯数字银行卡号字符串加空格
|
|
|
-// // return v.replace(/\s/g, '').replace(/(\d{4})(?=\d)/g, '$1 ')
|
|
|
-// // 带有*的银行卡号字符串加空格
|
|
|
-// return v.replace(/\s/g, '').replace(/(.{4})/g, '$1 ')
|
|
|
-// })
|
|
|
-
|
|
|
-// Vue.filter('addConfusionForBank', function (v) {
|
|
|
-// // 带有*的银行卡号字符串加空格
|
|
|
-// if (String(v).length < 12) {
|
|
|
-// return v.replace(/\s/g, '').replace(/^(\d{2})\d+(\d{2})$/, '$1 **** **** $2')
|
|
|
-// } else {
|
|
|
-// return v.replace(/\s/g, '').replace(/^(\d{4})\d+(\d{4})$/, '$1 **** **** $2')
|
|
|
-// }
|
|
|
-// })
|