function randomWord (randomFlag, min, max) {
  let str = ''
  let range = min
  let arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
  // 随机产生
  if (randomFlag) {
    range = Math.round(Math.random() * (max - min)) + min
  }
  for (var i = 0; i < range; i++) {
    let pos = Math.round(Math.random() * (arr.length - 1))
    str += arr[pos]
  }
  return str
}
module.exports = {
  getPosition: function (dom, parent) {
    var ret = {
      x: 0,
      y: 0
    }
    while (dom && dom !== parent && dom !== document.documentElement) {
      ret.x += dom.offsetLeft
      ret.y += dom.offsetTop
      dom = dom.offsetParent
    }
    return ret
  },
  reg: {
    idCard: /^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|[xX])$/,
    phone: /^1(?:3\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\d|9\d)\d{8}$/,
    email: /^[A-Za-z\d]+([_.-][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/,
    guhua: /^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}$/
  },
  isEmptyObject: function (obj) {
    for (var key in obj) {
      return false
    };
    return true
  },
  isWide: function () {
    return Math.round(devicePixelRatio * 100) === 100 ? window.innerWidth > 1600 : true
  },
  isWorkPhone: function (phone) {
    let phones = [
      /^1(?:3\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\d|9\d)\d{8}$/
    ]
    for (let i = 0; i < phones.length; i++) {
      if (phones[i].test(phone)) {
        return true
      }
    }
    return false
  },
  formatDate: function (time) {
    var date = new Date(time)
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()
    // var hour = date.getHours()
    // var minute = date.getMinutes()
    // var second = date.getSeconds()
    //  return year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second;
    return year + '-' + (String(month).length > 1 ? month : '0' + month) + '-' +
      (String(day).length > 1 ? day : '0' + day)
      // + ' ' + (String(hour).length > 1 ? hour : '0' + hour) + ':' + (String(minute).length > 1 ? minute : '0' + minute) +
      //  ':' + (String(second).length > 1 ? second : '0' + second)
  },

  encodeStr: function (str) {
    const NUM = 2
    const front = randomWord(false, 8)
    const middle = randomWord(false, 8)
    const end = randomWord(false, 8)

    let str1 = str.substring(0, NUM)
    let str2 = str.substring(NUM)

    return front + str2 + middle + str1 + end
  }
}
