/* eslint-disable */
const toolClass = {
  dateFormat: (timestamp, fmt) => { // 时间格式转换
    if (timestamp) {
      if (!fmt) {
        fmt = 'yyyy-MM-dd hh:mm'
      }
      let date = new Date(timestamp)
      var o = {
        'M+': date.getMonth() + 1,
        'd+': date.getDate(),
        'h+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds(),
        'q+': Math.floor((date.getMonth() + 3) / 3),
        'S': date.getMilliseconds()
      }
      if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.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
    } else {
      return ''
    }
  },
  JsBridge: { // IOS/Android原生交互
    init: function (callback) { // 初始化
      var u = navigator.userAgent
      var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)
      if (!isiOS) {
        if (window.WebViewJavascriptBridge) {
          callback(WebViewJavascriptBridge)
        } else {
          document.addEventListener(
            'WebViewJavascriptBridgeReady',
            function () {
              callback(WebViewJavascriptBridge)
            },
            false
          )
        }
      } else {
        if (window.WebViewJavascriptBridge) return callback(WebViewJavascriptBridge)
        if (window.WVJBCallbacks) return window.WVJBCallbacks.push(callback)
        window.WVJBCallbacks = [callback]
        var WVJBIframe = document.createElement('iframe')
        WVJBIframe.style.display = 'none'
        WVJBIframe.src = 'https://__bridge_loaded__'
        document.documentElement.appendChild(WVJBIframe)
        setTimeout(function () { document.documentElement.removeChild(WVJBIframe) }, 0)
      }
    },
    first: function () { // 建立第一次链接
      var u = navigator.userAgent
      var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)
      if (!isiOS) {
        var _this = this
        _this.init(function (bridge) {
          bridge.init(function (message, responseCallback) {
            responseCallback(data)
          })
        })
      }
    },
    registerHandler: function (name, fun) { // app通知h5方法
      var _this = this
      _this.init(function (bridge) {
        bridge.registerHandler(name, fun)
      })
    },
    callHandler: function (name, data, fun) { // h5通知app方法
      var _this = this
      _this.init(function (bridge) {
        bridge.callHandler(name, data, fun)
      })
    }
  },
  returnDoubleNum (num) { // 转化双位数
    return num > 9 ? num : ('0' + num)
  },
  checkPhoneNum (num) { // 校验手机号
    var myreg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/
    return !!myreg.test(num)
  },
  checkEmail (num) { // 校验邮箱
    var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/
    return !!myreg.test(num)
  }
}

export default toolClass
