工作小记录-小方法

import moment from 'moment'
// 返回当前时间(格式:2018-01-09)
const currDate = () => moment().format('YYYY-MM-DD HH:mm:ss')

// ...
export {
    isToday,
    ellipsisText,
    currDate
    // ...
}
// 处理千分位的方法,1,000
export function formatAmount(num) {
    if (num) {
        let reg = /\d{1,3}(?=(\d{3})+$)/g
        let arr = num.toString().split('.')
        let integer = arr[0].replace(reg, '$&,')
        return arr[1] ? `${integer}.${arr[1]}` : integer
    } else {
        return 0
    }
}
// table行颜色变化 斑马线
function getRowClassName(record, index) {
    let str = '';
    if (record.isDeleted) {
        str = 'cl_c'
    };
    str += index % 2 === 1 ? ' dark_column' : '';
    return str;
}
// 格式化留言的日期
const isToday = val => {
    if (!val) return ''
    const today = moment().format('YYYY MM DD')
    const yesterday = moment()
        .subtract(1, 'd')
        .format('YYYY MM DD')
    const temp = moment(val).format('YYYY MM DD')
    if (today === temp) {
        return '今天 ' + moment(val).format('H:mm')
    } else if (yesterday === temp) {
        return '昨天 ' + moment(val).format('H:mm')
    } else {
        return moment(val).format('YYYY-MM-DD H:mm')
    }
}
// 处理文字过长的问题
const ellipsisText = (val, num) => {
    if (!val) return ''
    const len = val.toString().length
    return len > num ? val.substring(0, num) + '...' : val
}
/* eslint-disable */
const regexs = {
    numericRegex: /^[0-9]+$/,
    email: /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/,
    fax: /^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/,
    phone: /^((\+?[0-9]{1,4})|(\(\+86\)))?(13[0-9]|14[57]|15[012356789]|17[0678]|18[0-9])\d{8}$/,
    url: /[a-zA-z]+:\/\/[^\s]/,
    ip: /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])((\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])){3}|(\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])){5})$/,
    fBlank: /^\s+/,
    lBlank: /\s+$/
}

const backVal = field => (typeof field === 'string' ? field : field.value)

// 验证合法邮箱
export const isEmail = field => regexs.email.test(backVal(field))

// 验证合法 ip 地址
export const isIp = field => regexs.ip.test(backVal(field))

// 验证传真
export const isFax = field => regexs.fax.test(backVal(field))

// 验证座机
export const isTel = field => regexs.fax.test(backVal(field))

// 验证手机
export const isPhone = field => regexs.phone.test(backVal(field))

// 验证URL
export const isUrl = field => regexs.url.test(backVal(field))

// 前空格
export const fBlank = field => regexs.fBlank.test(backVal(field))

// 后空格
export const lBlank = field => regexs.lBlank.test(backVal(field))

// 空格校验
export const spaceValidFunction = (rule, value, callback) => {
    if (fBlank(value)) {
        callback('开头不能有空格') // 校验未通过
        return
    }
    if (lBlank(value)) {
        callback('结尾不能有空格') // 校验未通过
        return
    }

    callback() // 校验通过
}

// UM账号校验 英文大小写、数字和-校验规则
export const accountValidFn = (str) => {
    let regex=/^[0-9a-zA-Z_\-]*$/g;
    return regex.test(str);
}

 

 

posted @ 2021-07-01 11:17  前端出发者  阅读(41)  评论(0)    收藏  举报