common.js
4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
* 通用方法
* @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');
var staticOptions = require('../staticConfig');
/**
* 模块名处理,根据环境设置模块名
* @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;
}
module=module.replace(/\//g,'');
return 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);
}
});
}
/**
* 获取API DOC配置
* @param {Object} routers 路由
*/
exports.getInterfaceConfig = function (routers) {
var env = process.env.NODE_ENV || 'development';
var cssFile = 'css/all.css';
if(env !== 'development') {
cssFile = staticOptions.staticDir[env]+'/all.css';
}
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',{cssFile:cssFile,nav:navs,group:groupList,layout:false});
} else {
res.status(403).send('Permission denied');
}
}
}