// 设置本地存储
export let setStorage = (key, value) => {
    sessionStorage.setItem(key, JSON.stringify(value));
};

// 获取本地存储
export let getStorage = key => {
    return JSON.parse(sessionStorage.getItem(key));
};

// 判断是否是wifi环境
export let isWifi = function() {
    try {
        let wifi = true;
        let ua = window.navigator.userAgent;
        let con = window.navigator.connection;
        // 如果是微信
        if (/MicroMessenger/.test(ua)) {
            if (ua.indexOf("WIFI") >= 0) {
                return true;
            } else {
                wifi = false;
            }
            // 如果支持navigator.connection
        } else if (con) {
            let network = con.type;
            if (network !== "wifi" && network !== "2" && network !== "unknown") {
                wifi = false;
            }
        }
        return wifi;
    } catch (e) {
        return false;
    }
};

/**
 * 复制方法
 * @param value {string} value 需要复制的文本
 * @return isCopy {boolean}  是否能被复制
 */
export let copy = value => {
    if(!value) return false;
    const input = document.createElement("input");
    input.setAttribute("readonly", "readonly");
    input.setAttribute("value", value);
    document.body.appendChild(input);
    input.setSelectionRange(0, 9999);
    let isCopy = document.execCommand("copy");
    if (isCopy) {
        input.select(); // 选中文本
        document.execCommand("copy");
        //console.log("复制成功");
    }
    document.body.removeChild(input);
    return isCopy;
};

