Prefacio: ¿Tiene tales problemas, cada vez que construye un proyecto, usará muchos filtros? Este artículo resolverá este problema por ti. Aquí hay muchos filtros o métodos que deben usarse en el desarrollo, todos los cuales se copian y usan.
Filtro de tiempo
// 对Date的扩展,将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,
// 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
// 例子:
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
// (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
Date.prototype.Format = function (fmt) {
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var 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;
}
使用:
//年月日时分秒
new Date(1602506002075).Format('yyyy-MM-dd hh:mm:ss') ==> 2020-10-12
20:33:22
//分隔符自定义
new Date(1602506002075).Format('yyyy/MM/dd hh:mm:ss') ==> 2020/10/12
20:33:22
new Date(1602506002075).Format('yyyy年MM月dd日') ==> 2020年10月12日
//时间格式多样
new Date("2020-10-12").Format('yyyy年MM月dd日') ==> 2020年10月12日
Hace horas y minutos
export function formatTime(time, option) {
time = +time * 1000
const d = new Date(time)
const now = Date.now()
const diff = (now - d) / 1000
if (diff < 30) {
return '刚刚'
} else if (diff < 3600) {
// less 1 hour
return Math.ceil(diff / 60) + '分钟前'
} else if (diff < 3600 * 24) {
return Math.ceil(diff / 3600) + '小时前'
} else if (diff < 3600 * 24 * 2) {
return '1天前'
}
if (option) {
return parseTime(time, option)
} else {
return d.getMonth() + 1 + '月' + d.getDate() + '日' + d.getHours() + '时' + d.getMinutes() + '分'
}
}
URL legal
/* 合法uri*/
export function validURL(url) {
const reg = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{
2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{
2}|[1-9]?[0-9])){
3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{
2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
return reg.test(url)
}
Restricción para ingresar solo números y solo dos lugares decimales
Vue.prototype.$priceLimit = function (value) {
if (!value && value !== 0) {
value = ''
}
value = value === '.' ? '' : value
value = value.toString().replace(/[^\d.]/g, '') // 清除“数字”和“.”以外的字符
value = value.replace(/\.{
2,}/g, '.') // 只保留第一个. 清除多余的
value = value
.replace('.', '$#$')
.replace(/\./g, '')
.replace('$#$', '.')
value = value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3') // 只能输入两个小数
if (value.indexOf('.') < 0 && value != '') {
// 以上已经过滤,此处控制的是如果没有小数点,首位不能为类似于 01、02的金额
value = parseFloat(value)
}
return value
}
使用:
<input type="text" placeholder-class="phcolor" v-model="value" :oninput="value=$priceLimit(value)">
Restringir para ingresar solo números positivos
// 转整数
Vue.prototype.$intNum = function (value) {
if (!value && value !== 0) {
value = ''
}
const price = value.toString().replace(/[^0-9]/g, '', '')
return price
}
acceso a token
import Cookies from 'js-cookie'
const TokenKey = 'userToken';
export function getToken() {
return Cookies.get(TokenKey);
}
export function setToken(token) {
return Cookies.set(TokenKey, token)
}
export function removeToken() {
return Cookies.remove(TokenKey)
}
使用:
//取token
getToken()
//存token
setToken()
//清除token
removeToken()