La méthode toujours utilisée dans le projet, prête à l'emploi (mise à jour continue)

Préface: Avez-vous de tels problèmes, à chaque fois que vous construisez un projet, vous utiliserez beaucoup de filtres? Cet article résoudra ce problème pour vous. Voici de nombreux filtres ou méthodes qui doivent être utilisés dans le développement, qui sont tous copiés et utilisés.

Filtre horaire

// 对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日

Il y a des heures et des minutes

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 légale

/* 合法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)
}

Restriction pour saisir uniquement des nombres et seulement deux décimales

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)">

Restreindre à saisir uniquement des nombres positifs

// 转整数
Vue.prototype.$intNum = function (value) {
    
    
  if (!value && value !== 0) {
    
    
    value = ''
  }
  const price = value.toString().replace(/[^0-9]/g, '', '')
  return price
}

accès au jeton

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()

Je suppose que tu aimes

Origine blog.csdn.net/Smell_rookie/article/details/109036210
conseillé
Classement