
var _setCache = (str)=> {// 1支持多用户登录同端，2处理个别浏览器缓存未清理干净
  let _managerId = 0
  _managerId = window.localStorage.getItem('unique_login')// 唯一登录标识
  return `${str}_${_managerId}`
}
export default {
  setUserCache (data) {// 设置登录用户信息缓存
    // eslint-disable-next-line camelcase
    let _unique_login = data.Manager.ID + data.Manager.Logincode
    // eslint-disable-next-line camelcase
    let _cacheName = `etorcurrentuser_${_unique_login}`
    window.localStorage.setItem(_cacheName, JSON.stringify(data))
    window.localStorage.setItem('unique_login', _unique_login)
    window.localStorage.setItem('etorcurrentuser',JSON.stringify(data))// 兼容未使用公共方法的页面
  },
  getUserByCache: function () {// 获取用户信息缓存
    let _cacheName = _setCache('etorcurrentuser')
    var str = window.localStorage.getItem(_cacheName)
    return JSON.parse(str)
  },
  setMenus (data) {
    let _cacheName = 'pmenus'
    window.localStorage.setItem(_cacheName, JSON.stringify(data))
  },
  getMenus () {
    let _cacheName = 'pmenus'
    var str = window.localStorage.getItem(_cacheName)
    return JSON.parse(str)
  },
  isRoot: function () {// 是否超级管理员  
    var currentUser = this.getUserByCache()
    var isRoot = currentUser.Manager.IsRootUser
    return isRoot
  },
  freshToken (type) {
    // 刷新token
    // 0刷新过期时间，1刷新管理小区
    let etorcurrentuser = this.getUserByCache()
    newapi
      .AgainGetToken({ Token: etorcurrentuser.Token, Type: type })
      .then(res => {
        if (res.Code == 10000) {
          console.log(`刷新token成功`)
          etorcurrentuser.Token = res.Data
          this.setUserCache(etorcurrentuser)
        }
      })
      .catch(res => { })
  },
  arr: [],
  dbclick: function (tag) {
    var img_obj = document.getElementsByClassName(tag)
    var body = document.getElementsByTagName('body')[0]
    var obj_arr = new Array()
    var img_arr = new Array()
    var nowindex = 0
    for (var i = 0; i < img_obj.length; i++) {
      obj_arr.push(img_obj[i])
      img_arr.push(img_obj[i].getAttribute('src'))
      img_obj[i].ondblclick = function () {
        var nowurl = this.getAttribute('src')
        // 获取到当前点击是第几个元素
        for (var t = 0; t < img_arr.length; t++) {
          if (img_arr[t] == nowurl) nowindex = t
        }
        var cover_div = document.createElement('div')
        var center_div = document.createElement('div')
        var img_box = document.createElement('div')
        var leftspan = document.createElement('span')
        var rightspan = document.createElement('span')

        cover_div.className = 'cover-div'
        center_div.className = 'center-div'
        img_box.className = 'readimg_box'
        leftspan.className = 'leftspan'
        rightspan.className = 'rightspan'

        center_div.appendChild(img_box)
        body.appendChild(cover_div)
        body.appendChild(center_div)
        body.appendChild(leftspan)
        body.appendChild(rightspan)
        // 外层div点击的时候，删除
        cover_div.onclick = function () {
          body.removeChild(cover_div)
          body.removeChild(leftspan)
          body.removeChild(rightspan)
          body.removeChild(center_div)
          center_div.removeChild(img_box)
        };
        // 添加图片

        for (var n = 0; n < img_arr.length; n++) {
          var oimg = document.createElement('img')
          oimg.className = 'img_cover'
          oimg.src = img_arr[n]
          img_box.appendChild(oimg)
        }
        img_box.style.marginLeft = -nowindex * 500 + 'px'
        rightspan.onclick = function () {
          nowindex++
          if (nowindex >= img_arr.length - 1) {
            nowindex = img_arr.length - 1
          }
          img_box.style.marginLeft = -nowindex * 500 + 'px'
        };
        leftspan.onclick = function () {
          // console.log(img_arr.length - 1);
          // console.log(nowindex);
          nowindex--
          if (nowindex <= 0) {
            nowindex = 0
          }

          img_box.style.marginLeft = -nowindex * 500 + 'px'
        };
      }
    }
  },
  uploadimg: function () {
    var input = document.createElement('input')
    input.setAttribute('type', 'file')
    var event = document.createEvent('MouseEvents')
    event.initMouseEvent(
      'click',
      false,
      true,
      window,
      0,
      0,
      0,
      0,
      0,
      false,
      false,
      true,
      false,
      0,
      null
    )
    input.dispatchEvent(event)
    // this.makebase64()
    var that = this
    // console.log(that.makebase64)
    input.onchange = function (e) {
      var f = input.files[0]
      console.log('e')
      console.log(e)
      console.log('input.files')
      console.log(f.name)
      console.log(f.size / 1024)
      // if(f.name)
      if (f.size / 1024 > 2048) {
        // alert('图片大小不能超过2m')
        // Message({
        //   type: 'error',
        //   message: '图片大小不能超过2m'
        // })
        return
      }
      var reader = new FileReader()
      console.log('reader1')
      console.log(reader)
      reader.readAsDataURL(f)
      console.log('reader')
      console.log(reader)
      reader.onloadend = function (e) {
        e = window.event || e

        var imgbase = e.target.result
      };
    }
  }
}
