util.js 1.68 KB
import { Message } from 'element-ui'
let util = {}
util.cloneText = function(message) {
    let input = document.createElement('input')
    input.value = message
    document.body.appendChild(input)
    input.select()
    input.setSelectionRange(0, input.value.length), document.execCommand('Copy')
    document.body.removeChild(input)
    Message.success('复制成功')
}
util.getBeforeDate = function(sign = '-') {
        let day = new Date();
        day.setTime(day.getTime() - 24 * 60 * 60 * 1000);
        var s = day.getFullYear() + sign + (day.getMonth() + 1) + sign + day.getDate();
        return s;
    }
    /**
     * 校验手机号码格式
     */
util.checkPhone = function(mobile) {
        let myreg = /^(13[0-9]|15[0-9]|16[0-9]|17[0-9]|18[0-9]|19[0-9]|14[0-9])[0-9]{8}$/;
        return myreg.test(mobile);
    }
    /**
     * 校验手机号码格式
     */
util.checkisCard = function(card) {
    let myreg = /^(^\d{15}$)|(^\d{17}([0-9]|X)$)/;
    return myreg.test(card);
}
util.deepClone = function(obj) {
    // Handle the 3 simple types, and null or undefined or function
    if (obj == null || typeof obj !== 'object') return obj
        // Handle Date
    if (obj instanceof Date) {
        const copy = new Date()
        copy.setTime(obj.getTime())
        return copy
    }
    // Handle Array or Object
    if ((obj instanceof Array) | (obj instanceof Object)) {
        const copy = obj instanceof Array ? [] : {}
        for (var attr in obj) {
            if (obj.hasOwnProperty(attr)) {
                copy[attr] = util.deepClone(obj[attr])
            }
        }
        return copy
    }
    throw new Error("Unable to clone obj! Its type isn't supported.")
}
export default util