function addAero(num) {
    return num * 1 < 10 ? '0' + num : num;
}
export default class utils {
    //获取地址栏参数拿到uuid
    static GetQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    }

    static setHTMLBackground(setImage, bgColor) {
        let rootHtml = document.getElementById('rootHtml');
        if (bgColor) {
            rootHtml.style.background = setImage;
        } else {
            rootHtml.style.background = `url('${setImage}') 0% 0% / 100% 100%  no-repeat`;
            rootHtml.style.backgroundSize = 'cover';
        }
        // if (!delId) return;
        // document.getElementById(delId).style.background = 'rgba(0,0,0,0)';
        // document.getElementById(delId).style.backgroundColor = 'transparent';
    }
    static writeLocalStorage(name, str) {
        if (window.localStorage) {
            localStorage.setItem(name, str)
        } else {
            alert("浏览器支持localStorage");
        }
    }
    static readLocalstorage(name) {
        if (window.localStorage) {
            return localStorage.getItem(name)
        } else {
            alert("浏览器支持localStorage");
        }
    }
    // static locationName(name) {
    //     return new URLSearchParams(location.search).get(name)
    // }
    //时间戳转为时间
    static formatDate(time, flag) {
        const t = new Date(time * 1);
        const tf = function (i) {
            return (i < 10 ? '0' : '') + i
        };
        const year = t.getFullYear();
        const month = tf(t.getMonth() + 1);
        const day = tf(t.getDate());
        const hour = tf(t.getHours());
        const minute = tf(t.getMinutes());
        //console.log( month + '月' + day + '日' + hour + ':' + minute);
        if (flag) {
            return year + '年' + month + '月' + day + '日 ' + hour + ':' + minute;
        } else {
            return year + '年' + month + '月' + day + '日';
        }
    }
    // 获得当前时间
    static getDate() {
        return new Date()
    }
    // 生成星星数组
    static createStar(allStar, starNum) {
        /**
         * allStar星星展示首先确定有多少颗星
         * starNum 多少颗星星是亮的
         * starArr 星星数组，1是亮的，0是不亮的
         */
        let starArr = [];
        for (let i = 0; i < allStar; i++) {
            starArr.push(1)
        }
        for (let i = 0; i < allStar; i++) {
            starArr.push(0)
        }
        const length = starArr.length;
        const newStarArr = starArr.slice(allStar - starNum, length - starNum)
        return newStarArr
    }
    // 生成星星dom元素把星星数组传进来
    static renderStar(starArr, fileName) {
        let starStr = ''
        const file = fileName || 'img'
        starArr.forEach(item => {
            const starimg = item === 1 ? `assets/${file}/star1_1.png` : `assets/${file}/star1_2.png`;
            starStr += `
          <div class="star-item">
              <img src="${ starimg }" alt="">
          </div>
          `
        })
        return starStr
    }
    // 生成日历年
    static createdYear(start, end) {
        const yearNum = end - start;
        let str = ''
        for (var i = 0; i <= yearNum; i++) {
            str += `
                <li class="option-item" data-type="year"  fe-role="Widget">${start + i}</li>
            `
        }
        return str
    }
    // 生成月
    static createdMonth() {
        let str = ''
        for (var i = 1; i <= 12; i++) {
            str += `
                <li class="option-item" data-type="month"  fe-role="Widget">${i < 10 ? '0' + i : i}</li>
            `
        }
        return str
    }
    // 生成日
    static createdDay(dayNum) {
        let str = ''
        for (var i = 1; i <= dayNum; i++) {
            str += `
                <li class="option-item" data-type="day"  fe-role="Widget">${i < 10 ? '0' + i : i}</li>
            `
        }
        return str
    }
    // 元素选中
    static $(name) {
        return window.document.querySelector.call(window.document, name)
    }
    // 设置背景图
    static setBg(imgOrColor) {
        if (/png|jpg|gif/.test(imgOrColor)) {
            document.body.style.background = `url(${imgOrColor}) 0% 0% / 100% 100% no-repeat`
        } else {
            document.body.style.background = `${imgOrColor}`
        }
    }
    // 全局处理返回按钮
    static setBack(hide, top) {
        top = top || '.72'
        document.querySelector('.back').style.display =  hide ? 'none' : 'block';
        document.querySelector('.back').style.top = top + 'rem'
    }
    // 秒转分和秒
    static s_to_ms(time) {
        const _time = Math.floor(time)
        const s = (_time % 60) < 10 ? '0' + _time % 60 : _time % 60;
        const m = Math.floor(_time / 60) < 10 ? '0' + Math.floor(_time / 60) : Math.floor(_time / 60)
        return m + ':' + s
    }
    // 解密视频
    static DecryptEcb(keyword, secretKey) {
        try {
            const key = CryptoJS.enc.Utf8.parse(secretKey)
            // 转换base64
            const encryptedData = CryptoJS.enc.Base64.parse(keyword)
            const encryptedHexStr = CryptoJS.enc.Hex.parse(encryptedData.toString())
            const encryptedBase64Str = CryptoJS.enc.Base64.stringify(encryptedHexStr)
            const decryptedData = CryptoJS.AES.decrypt(encryptedBase64Str, key, {
                mode: CryptoJS.mode.ECB,
                padding: CryptoJS.pad.Pkcs7
            })

            const decryptedStr = decryptedData.toString(CryptoJS.enc.Utf8)
            console.log("解密后:", decryptedStr) // 解密后:onlystar
            return decryptedStr.toString()
        } catch (error) {
            console.log(error)
            return ''
        }
    }
    static countSecondTime(endtime) {
        var nowtime = new Date(); //获取当前时间
        var lefttime = endtime - nowtime.getTime(), //距离结束时间的毫秒数
            lefth = addAero(Math.floor(lefttime / (1000 * 60 * 60) % 24)), //计算小时数
            leftm = addAero(Math.floor(lefttime / (1000 * 60) % 60)), //计算分钟数
            lefts = addAero(Math.floor(lefttime / 1000 % 60)); //计算秒数
        return [lefth,leftm,lefts] //返回倒计时的字符串
    }
}