//生成随机数
function getRandomNum(Min, Max) {
    var Range = Max - Min;
    var Rand = Math.random();
    return (Min + Math.round(Rand * Range));
}
exports.getRandomNum = getRandomNum;

//获取客户端IP地址
var getClientIP = function (req) {
    var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress || req.socket.remoteAddress || req.connection.socket.remoteAddress;
    var ipAddress;
    var forwardedIpsStr = req.header('x-forwarded-for');
    if (forwardedIpsStr) {
        var forwardedIps = forwardedIpsStr.split(',');
        ipAddress = forwardedIps[0];
    }
    if (!ipAddress) {
        ipAddress = req.connection.remoteAddress;
    }
    return ipAddress.replace("\"", "");
}
exports.getClientIP = getClientIP;

//获取服务器IP地址
var getServerIP = function () {
    var interfaces = require('os').networkInterfaces();
    for (var devName in interfaces) {
        var iface = interfaces[devName];
        for (var i = 0; i < iface.length; i++) {
            var alias = iface[i];
            if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
                return alias.address;
            }
        }
    }
}
exports.getServerIP = getServerIP;

//获取地址栏参数
function GetUrlParams(name, url) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    if (!url) {
        if (window && window.location && window.location.search) {
            url = window.location.search;
        }
        else {
            url = '';
        }
    }
    var r = url.substr(url.indexOf("?") + 1).match(reg);
    if (r != null) return decodeURI(r[2]);
    return "";
}
exports.GetUrlParams = GetUrlParams;

/*
 * 生成随机字符串
 * len 生成的长度 不传则为默认8 只允许int类型
 * chars 生成指定字符串允许出现的字符 不传则默认大小写字母与数字
 * */
var getRandomStr = function (len, chars) {
    if (!len || typeof (len) != "int") {
        len = 8;
    }
    if (!chars || typeof (chars) != "string") {
        chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    }
    var str = "";
    for (var i = 0; i < len; i++) {
        str += chars.charAt(Math.floor(Math.random() * chars.length));
    }
    return str;
}
exports.getRandomStr = getRandomStr;

//获取Guid
var guid = function () {
    return require('node-uuid').v4();
}
exports.guid = guid;

//获取空guid
var guid_empty = function () {
    return "00000000-0000-0000-0000-000000000000";
}
exports.guid_empty = guid_empty;

//获取启动参数
var getStartArguments = function (splitStr) {
    var RemoveArrayAt = function (self, index) {
        if (index != null && index != undefined && parseInt(index) === index && index >= 0 && self.length && self.length > index) {
            var t = new Array();
            var _ = require('underscore');
            _.each(self, function (o, i) {
                if (index != i) {
                    t.push(o);
                }
            });
            return t;
        }
        return self;
    }
    var argument = RemoveArrayAt(RemoveArrayAt(process.argv, 0), 0);
    var params = {};
    var _ = require('underscore');
    if (splitStr) {
        _.each(argument, function (o, i) {
            var k = o.split(splitStr)[0].replace('-', '');
            var v = o.split(splitStr)[1];
            params[k] = v;
        });
    }
    else {
        var k = "";
        _.each(argument, function (o, i) {
            if (o.StartWith('-')) {
                k = o.replace('-', '');
                params[k] = '';
            }
            else {
                params[k] = o;
            }
        });
    }
    return params;
}
exports.getStartArguments = getStartArguments;