util.js 2.53 KB
import Vue from 'vue'

export const Util = {
  getExplorer: function () {
    var ua = navigator.userAgent.toLocaleLowerCase()
    var browserType = null
    if (ua.match(/msie/) != null || ua.match(/trident/) != null) {
      browserType = 'IE'
    } else if (ua.match(/firefox/) != null) {
      browserType = '火狐'
    } else if (ua.match(/ubrowser/) != null) {
      browserType = 'UC'
    } else if (ua.match(/opera/) != null) {
      browserType = '欧朋'
    } else if (ua.match(/bidubrowser/) != null) {
      browserType = '百度'
    } else if (ua.match(/metasr/) != null) {
      browserType = '搜狗'
    } else if (
      ua.match(/tencenttraveler/) != null ||
      ua.match(/qqbrowse/) != null
    ) {
      browserType = 'QQ'
    } else if (ua.match(/maxthon/) != null) {
      browserType = '遨游'
    } else {
      browserType = '谷歌'
    }
    return browserType
  },

  exportFn: function (url) {
    console.log(url)
    let browserType = getExplorer()
    if (browserType == 'QQ') {
      window.open(url)
      return
    }
    let iframe = document.createElement('iframe')
    console.log(iframe)
    iframe.style.display = 'none'
    iframe.src = url
    document.body.appendChild(iframe)
    console.log(document.body)
  },

  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.")
  },

  timeTransfer: function (time) {
    const date = new Date(time)
    const y = date.getFullYear()
    let m = date.getMonth() + 1
    m = m < 10 ? '0' + m : m
    let d = date.getDate()
    d = d < 10 ? '0' + d : d
    let h = date.getHours()
    h = h < 10 ? '0' + h : h
    let minute = date.getMinutes()
    minute = minute < 10 ? '0' + minute : minute
    let s = date.getSeconds()
    s = s < 10 ? '0' + s : s
    return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + s
  },
}

export const getExplorer = Util.getExplorer
export const exportFn = Util.exportFn
export const deepClone = Util.deepClone
export const timeTransfer = Util.timeTransfer