import router from '../../router/index'
import Vue from 'vue'
const util = {
    getParams() { 
        const params = {}
        const { id = '', idNum = '' } = this.$route.query
        if (id) {
            params.deviceName = id
        } else {
            params.id = idNum
        }
        return params
    },
    
    // baseApi:'http://47.97.64.216:8001',
    // baseApi: 'http://172.27.1.29:8001',
    baseApi: 'http://172.27.1.181:8001',
    getFileUrl(ids) { //获取回显的url
        const arr = []
        if (ids) {
            ids = ids.split(',')
        } else {
            ids = []
        }
        ids.forEach(item => {
            arr.push(`${util.baseApi}${item.slice(9)}`)
        })
        return arr
    },
    getFileName(name) {
        const arr = []
        if (name) {
            name = name.split(',')
        } else {
            name = []
        }
        name.forEach(item => {
          arr.push(item)
        })
        return arr
    },
    /**
     * @param {*} arr 
     * @return []
     * @desc 处理后台数据为[]
     */
    isArr(arr) {
        return Array.isArray(arr) ? arr : [arr]
    },

    /**
     * @param {*} phone 
     * @return boolean
     * @desc 验证手机号格式
     */
    isMobile(phone) {
        return /^1[3456789]\d{9}$/.test(phone)
    },

    /**
     * @param {*} email
     * @return boolean
     * @desc 验证邮箱格式
     */
    isEmail(email) {
        return /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/.test(email)
    },
    leavePageToHome(time = 1,route) {
        let timer = null
        const timeFn = function () {
            //console.log('执行一次----')
           
            timer = setTimeout(() => {
                console.log('leave', route,'r')
                document.body.removeEventListener('click', time2Fn)

                router.push(route)
               
            },time * 60 * 1000)
        }

        timeFn()
        const time2Fn = function () {
            timer && clearTimeout(timer)
            //console.log('add click')
            timeFn()
        }
      
        setTimeout(() => {
            document.body.addEventListener('click', time2Fn)
        }, 800)

        const backCancel = function () {
            console.log('取消定时器')
            timer && clearTimeout(timer)
            document.body.removeEventListener('click', time2Fn)
        }

        return backCancel
      
    }
}


export default util


   /**
     * @param {Function} func
     * @param {number} wait
     * @param {boolean} immediate
     * @return {*}
     */
export function debounce(func, wait, immediate) {
    let timeout, args, context, timestamp, result

    const later = function () {
        // 据上一次触发时间间隔
        const last = +new Date() - timestamp

        // 上次被包装函数被调用时间间隔 last 小于设定时间间隔 wait
        if (last < wait && last > 0) {
            timeout = setTimeout(later, wait - last)
        } else {
            timeout = null
            // 如果设定为immediate===true，因为开始边界已经调用过了此处无需调用
            if (!immediate) {
                result = func.apply(context, args)
                if (!timeout) context = args = null
            }
        }
    }

    return function (...args) {
        context = this
        timestamp = +new Date()
        const callNow = immediate && !timeout
        // 如果延时不存在，重新设定延时
        if (!timeout) timeout = setTimeout(later, wait)
        if (callNow) {
            result = func.apply(context, args)
            context = args = null
        }

        return result
    }
}