/**
 * Created by Administrator on 2020/12/11.
 */

//是否存在指定函数
function isExitsFunction(funcName) {
    try {
        if (typeof(eval(funcName)) == "function") {
            return true;
        }
    } catch (e) {
    }
    return false;
}

//获取url参数
function getQueryValue(key) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == key) {
            return pair[1];
        }
    }
    return (false);
}

//限制微信才能打开
function onlyWeixin() {
    var ua = navigator.userAgent.toLowerCase();
    var isWeixin = ua.indexOf('micromessenger') != -1;
    var isAndroid = ua.indexOf('android') != -1;
    var isIos = (ua.indexOf('iphone') != -1) || (ua.indexOf('ipad') != -1);
    if (!isWeixin) {
        document.head.innerHTML = '<title>抱歉，出错了</title><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0"><link rel="stylesheet" type="text/css" href="https://res.wx.qq.com/open/libs/weui/0.4.1/weui.css">';
        document.body.innerHTML = '<div class="weui_msg"><div class="weui_icon_area"><i class="weui_icon_info weui_icon_msg"></i></div><div class="weui_text_area"><h4 class="weui_msg_title">请在微信客户端打开链接</h4></div></div>';
    }
}

//给网页注入jquery
function addJquery() {
    var head = document.getElementsByTagName('head')[0];
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = 'https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js';
    head.appendChild(script);
}

//表单转json，依赖jQuery
//var post_data = $('#myForm').serializeJson();
if (window.jQuery) {
    // serializeJson 函数
    (function ($) {
        $.fn.serializeJson = function () {
            var serializeObj = {};
            var array = this.serializeArray();
            var str = this.serialize();
            $(array).each(function () {
                if (serializeObj[this.name]) {
                    if ($.isArray(serializeObj[this.name]) && this.value != "") {
                        serializeObj[this.name].push(this.value);
                    } else {
                        if (this.value != "") {
                            serializeObj[this.name] = [serializeObj[this.name], this.value];
                        }
                    }
                } else {
                    if (this.value != "") {
                        serializeObj[this.name] = this.value;
                    }
                }
            });
            return serializeObj;
        };
    })(jQuery);
}

//生成10位时间戳
function getTimestamp() {
    var tmp = Date.parse(new Date()).toString();
    tmp = tmp.substr(0, 10);
    return tmp;
}

//字节大小转换单位
//format_bytes(filesize, '');
function format_bytes(size, delimiter) {
    var units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
    for (var i = 0; size >= 1024 && i < 5; i++) size /= 1024;
    return Math.round(size) + delimiter + units[i];
}

//判断是否数字
function isNumber(str) {
    if (isNaN(Number(str, 10))) {
        return false;
    } else {
        return true;
    }
}

//随机生成密码
function randomPassword(size) {
    var seed = new Array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'm', 'n', 'p', 'Q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
        '2', '3', '4', '5', '6', '7', '8', '9'
    );//数组
    seedlength = seed.length;//数组长度
    var createPassword = '';
    for (i = 0; i < size; i++) {
        j = Math.floor(Math.random() * seedlength);
        createPassword += seed[j];
    }
    return createPassword;
}