
import * as baseUrls from '@/axios.js'
import store from '@/store/index.js'
import router from '@/router/index.js'

import { JSEncrypt } from "jsencrypt";

let publicKey = "-----BEGIN PUBLIC KEY-----MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC4qkbwIKErstK1sFESPEhOShpRpj4+sOVpJHxl5r/2xLBfA/MrXcAEra5Ro9cXNQSqmLLt8wecoLk/glfa5IdhXV0hRVQplIVs5z3MxcUa9ptKPHUTgh8xMCBvl8sUJKwkmn4vYWeDfHT22EL7Hr1pTMwUhF6WiNlWfQTVoF1rhwIDAQAB-----END PUBLIC KEY-----"

export default {
  getToken() {
    return localStorage.getItem('token')
  },

  getUserAccount() {
    return localStorage.getItem('user_account')
  },

  //图片路径填补
	splitImgHost(url, scale = false, width = 250) {
		if (!url) {
			return ''
		} else if (url.indexOf("http") != -1 || url.indexOf("https") != -1 || url.indexOf("wxfile") != -1) {

		} else {
			url = baseUrls.BASE_IMG_URL + url
		}
		if (scale) {
			url = url + "?x-oss-process=image/resize,w_" + width
		}
		return url;

	},

  isLogin() {
		if (localStorage.getItem('user_account')) {
			return true;
		} else {
			return false
		}
	},

  setUuid(id) {
    sessionStorage.setItem('uuid',id)
  },

  getUuid() {
    return sessionStorage.getItem('uuid')
  },

  exit() {
    
		localStorage.removeItem('user_account')
		localStorage.removeItem('token')
    sessionStorage.removeItem('uuid')
		store.state.userInfo = null

    if(router.currentRoute.path != '/home') {
      router.replace({
        path: '/home'
      })
    }
		
	},

  //当前时间距离目标时间还有多久
	GetRTime(EndTime, isDay = true) {
		var EndTime = EndTime //结束时间
		var NowTime = new Date(); //当前时间
		//后台给我的是10位 精确到秒的 所有下面我就除以了1000，不要小数点后面的
		var t = EndTime - (NowTime.getTime() / 1000).toFixed(0);
		if (t <= 0) {
			return '已结束'
		}
		//如果后台给的是毫秒 上面不用除以1000 下面的计算时间也都要除以1000 这里我去掉1000了
		var d = Math.floor(t / 60 / 60 / 24); //天 var d=Math.floor(t/1000/60/60/24)
		var h = Math.floor(t / 60 / 60 % 24); //时 var h=Math.floor(t/1000/60/60%24)
		var m = Math.floor(t / 60 % 60); //分 var m=Math.floor(t/1000/60%60)
		var s = Math.floor(t % 60); //秒 var s=Math.floor(t/1000%60)
		if (parseInt(d) < 10) {
			d = "0" + d;
		}
		if (parseInt(h) < 10) {
			h = "0" + h;
		}
		if (parseInt(m) < 10) {
			m = "0" + m;
		}
		if (parseInt(s) < 10) {
			s = "0" + s;
		}
		if (isDay) {
			return d;
		}
		return d + '天' + h + '小时' + m + '分' + s + '秒'
	},
	TimeTotimestamp(date) {
		var date = date.replace(/-/g, '/');
		var newDate = new Date(date)
		return (newDate.getTime() / 1000)
	},
	timest() {
		var tmp = Date.parse(new Date()).toString();
		tmp = tmp.substr(0, 10);
		return tmp;
	},

      /**
      * 
     * @param {int} result 
     * @returns {string}
     * @remard 单位S转小时分钟秒
     */
  secondToTime(result,Diszing = true) {
    var h = Math.floor(result / 3600) < 10 ? '0' + Math.floor(result / 3600) : Math.floor(result / 3600);
    var m = Math.floor((result / 60 % 60)) < 10 ? '0' + Math.floor((result / 60 % 60)) : Math.floor((result / 60 % 60));
    var s = Math.floor((result % 60)) < 10 ? '0' + Math.floor((result % 60)) : Math.floor((result % 60));
    if (h == 0 && Diszing) {
      result = m + ":" + s;
    } else {
      result = h + ':' + m + ":" + s
    }
    return result;
  },

  /* 时间戳转换成日期
	 * @param timestamp
	 * @returns {*}
	 */
	timestampToTime(timestamp, isDay = true,hasChinese) {
		var date = new Date(timestamp * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
		var Y = date.getFullYear() + (hasChinese ? '' : '-');
		var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + (hasChinese ? '' : '-');
		var D = date.getDate() < 10 ? '0' + date.getDate() + (hasChinese ? '' : ' ') : date.getDate() + (hasChinese ? '' :' ');
		var h = date.getHours() < 10 ? '0' + date.getHours() + ':' : date.getHours() + ':';
		var m = date.getMinutes() < 10 ? '0' + date.getMinutes() + ':' : date.getMinutes() + ':';
		var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
		if (isDay) {
			
			if(hasChinese) {
				return Y +'年'+ M +'月'+ D + '日';
			} else {
				return Y + M + D;
			}
			
		}
		
		if(hasChinese) {
			return Y +'年'+ M +'月'+ D + '日' + h + m + s;
		} else {
			return Y + M + D + h + m + s;
		}
		
	},

  secondToDate(result){
		var h = Math.floor(result / 3600) < 10 ? '0'+Math.floor(result / 3600) : Math.floor(result / 3600);
		var m = Math.floor((result / 60 % 60)) < 10 ? '0' + Math.floor((result / 60 % 60)) : Math.floor((result / 60 % 60));
		var s = Math.floor((result % 60)) < 10 ? '0' + Math.floor((result % 60)) : Math.floor((result % 60));
			if(h==0){
				result = m + ":" + s;
			}else{
				result = h+':'+m + ":" + s
			}
		return result;
	 },

   getZeroTime () {
		return Number(new Date(new Date().toLocaleDateString()).getTime()/1000)
	},

   /**
    * 
    * @param {*} urlData 
    * @returns 
    * base64转formdata格式文件
    */
   convertBase64UrlToBlob(urlData) { 
    var bytes = window.atob(urlData.split(",")[1]);
    var ab = new ArrayBuffer(bytes.length);
    var ia = new Uint8Array(ab);
    for (var i = 0; i < bytes.length; i++) {
      ia[i] = bytes.charCodeAt(i);
    }
    return new Blob([ab], { type: "image/jpeg" });
  },


  imageToBase64(url,quality = 0.8) {
    return new Promise(resolve => {
      var image = new Image();
      // 解决跨域 Canvas 污染问题,
      image.setAttribute("crossorigin", "anonymous");
      image.onload = function () {
        var canvas = document.createElement("canvas");
        canvas.width = image.width;
        canvas.height = image.height;
        var context = canvas.getContext("2d");
        context.drawImage(image, 0, 0, image.width, image.height);
        var url = canvas.toDataURL("image/jpeg",quality); //将图片格式转为base64

        resolve(url)
      };
      image.src = url + "?time=" + Date.now();
    })
    
  },

  /**
   * 
   * @param {*} data 
   * @returns 
   * 加密
   */
  encryptor(data) {
    let encryptor = new JSEncrypt(); 
    encryptor.setPublicKey(publicKey); 
    let data_encrypted = encryptor.encrypt(data);
    return data_encrypted;
  },

}