| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 | let utils = {    guid:function() {        function S4() {            return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1)        }        return S4() + S4() + S4() + S4() + S4() + S4() + S4() + S4()    },    getStorage:function(name) {        let value = window.localStorage.getItem(name);        try {            const temp = JSON.parse(value);            let nowDate = new Date();            let expireDate = new Date(temp.expire);            if (temp) {                if (temp.expire) {                    if (nowDate < expireDate) {                        return temp.token                    } else {                        return null;                    }                } else {                    return temp                }            } else {                return null;            }        } catch (e) {            return value;        }    },    setStorage:function(name, value) {        if (value.expire) {            value.expire = new Date().getTime() + value.expire * 1000;        }        window.localStorage.setItem(name, JSON.stringify(value));    },    //格式化日期    formatDate:function(str) {        let date = new Date(str * 1000);        const year = date.getFullYear();        const month = date.getMonth() + 1;        const day = date.getDate();        return [year, month, day].map(formatNumber).join('-');    },    //格式化时间    formatTime:function(str) {        let date = new Date(str * 1000);        const year = date.getFullYear();        const month = date.getMonth() + 1;        const day = date.getDate();        const hour = date.getHours();        const minute = date.getMinutes();        const second = date.getSeconds();        return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':');    },    //加0    formatNumber:function(n) {        n = n.toString();        return n[1] ? n : '0' + n;    },    getBase64Image:function(img) {        var canvas = document.createElement("canvas");        canvas.width = img.width;        canvas.height = img.height;        var ctx = canvas.getContext("2d");        ctx.drawImage(img, 0, 0, img.width, img.height);        var dataURL = canvas.toDataURL("image/png");  // 可选其他值 image/jpeg        return dataURL;    },    main:function(src, cb) {        var image = new Image();        image.src = src + '?v=' + Math.random(); // 处理缓存        image.crossOrigin = "*";  // 支持跨域图片        image.onload = function () {            var base64 = getBase64Image(image);            cb && cb(base64);        }    },    // 处理文件大小    handleSize:function(size) {        if ((size / 1024 / 1024) < 1) {            return (size / 1024).toFixed(2) + 'KB';        } else {            return (size / 1024 / 1024).toFixed(2) + 'MB';        }    }}
 |