common.js 4.29 KB
/**
 * 通用方法
 * @author  h1bomb
 */

var _ = require('lodash');
var request = require('request');

var path = require('path');
var fs = require('fs');
var logger = console;
var config = require('../config/common');

/**
 * 模块名处理,根据环境设置模块名
 * @param  {String} module 模块名
 * @return {String}        模块名
 */
exports.moduleProc = function (module) {
    var env = process.env.NODE_ENV || 'development';
    module = _.trim(module);
    module = _.trimLeft(module, '/');
    
    if (module === '') {
        return false;
    }
    if (env === 'development') {
        return 'js/' + module;
    } else {
        return 'yohobuy-portal/js/' + module;
    }
  // return 'js/'+module;
}



/**
 * http调用,简单封装
 * @param  {Object}   options  request的传参
 * @param  {Function} callback 回调
 */
exports.httpCall = function (options, callback) {
    var defaultOptions = {
        method: 'POST'
    }
    options = _.merge(defaultOptions, options);
    logger.info('httpCall Info:',options);
    //调用服务
    request(options, function (err, res, body) {
        if (err) {
            logger.error('httpCall error:',err);
            callback(true);
        } else if (res && res.statusCode === 200) {
            try {
                var ret = JSON.parse(body);
            } catch(err) {
                logger.error(err,body);
                callback(true);
                return;
            }
            
            if (ret.code !== 200) {
                logger.warn('httpCall warn:',ret);
                callback(true, ret);
            } else {
                callback(null, ret);
            }
        } else {
            logger.error('httpCall error:' + res.statusCode);
            callback(true, {
                code: res.statusCode,
                message: 'server error!!'
            });
        }
    });
}

/**
 * 配置日志打印方式
 * @param {[type]} applogger [description]
 */
exports.setLogger = function(applogger) {
    logger = applogger;
}


//读取某个文件夹下面所有的js文件

/**
 * 读取某个文件夹下面所有的js模块文件
 * @param  {Object}   __dir  文件路径
 */
exports.readDir = function (dir, callback) {
    var files = fs.readdirSync(dir);
    files.forEach(function (file) {
        var filePath = path.join(dir, file);
        if (fs.statSync(filePath).isFile() && /.*\.js$/i.test(file)) {
            callback && callback(require(filePath));
        }
    });
}

/**
 * 过滤敏感信息
 * @param  {[type]} msg [description]
 * @return {[type]}     [description]
 */
exports.maskCode = function (msg) {
    return msg.replace(/\D|\d/g,'*');
}


/**
 * 深度递归设置值
 * @param  {[type]}   obj [description]
 * @param  {[type]}   key [description]
 * @param  {Function} cb  [description]
 * @return {[type]}       [description]
 */
exports.deepSet = function (obj,key,cb) {
    if(!obj||!key||!cb) {
        return false;
    }
    _.forIn(obj, function (val, k) {
        if(k === key) {
            obj[k] = cb(val);
        }
        if (_.isArray(val)) {
            val.forEach(function(el) {
                if (_.isObject(el)) {
                    exports.deepSet(el,key,cb);
                }
            });
        }
        if (_.isObject(obj[k])) {
            exports.deepSet(obj[k],key,cb);
        }
    });
}

exports.getInterfaceConfig = function (routers) {
    return function(req,res) {
        if(req.query.key === config.apiKey) {
            var groupList = [];
            var navs = [];
            var group = {};
            _.forEach(routers.interfacesConfig,function(v,k){
                if(v.url)
                {
                    var nav = v.url.split('/')[1];
                    if(group[nav]) {
                        group[nav].push(v);
                    } else {
                        group[nav] = [v];
                    }
                }
            });

            _.forEach(group,function(v,k){
                groupList.push({list:v,name:k,active:false});
                navs.push({name:k,active:false});
            });

            navs[0].active = true;
            groupList[0].active = true;
            res.render('pages/apidoc/doc',{nav:navs,group:groupList,layout:false});
        } else {
            res.status(403).send('Permission denied');
        }
        
    }
}