Toggle navigation
Toggle navigation
This project
Loading...
Sign in
fe
/
yohobuy-node
·
Commits
Go to a project
GitLab
Go to group
Project
Activity
Files
Commits
Pipelines
0
Builds
0
Graphs
Milestones
Issues
1
Merge Requests
0
Members
Labels
Wiki
Forks
Network
Create a new issue
Download as
Email Patches
Plain Diff
Browse Files
Authored by
毕凯
9 years ago
Commit
72155bb69274708d05ef49b829c4330695076c3a
1 parent
0b659cf2
修改未ES6代码
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
47 additions
and
50 deletions
library/api.js
library/cache.js
library/cookie.js
library/helpers.js
library/logger.js
library/sign.js
library/timer.js
test/library/helpers.test.js
library/api.js
View file @
72155bb
...
...
@@ -5,7 +5,6 @@
*/
'use strict'
;
const
rp
=
require
(
'request-promise'
);
const
qs
=
require
(
'querystring'
);
const
md5
=
require
(
'md5'
);
...
...
@@ -36,13 +35,13 @@ class Http {
* 调用接口
*/
_requestFromAPI
(
options
,
cacheOption
,
reqId
)
{
let
timer
=
new
Timer
();
let
method
=
options
.
method
||
'get'
;
const
timer
=
new
Timer
();
const
method
=
options
.
method
||
'get'
;
log
.
info
(
`
$
{
method
}
api
:
$
{
options
.
url
}?
$
{
qs
.
stringify
(
options
.
qs
)}
`
);
timer
.
put
(
'getApi'
);
// 统计时间开始
return
rp
(
options
).
then
((
result
)
=>
{
le
t
duration
=
timer
.
put
(
'getApi'
);
// 统计时间结束
cons
t
duration
=
timer
.
put
(
'getApi'
);
// 统计时间结束
// 数据校验
if
(
!
result
||
!
result
.
code
)
{
...
...
@@ -55,7 +54,7 @@ class Http {
// 写缓存, 否则返回 Slave 缓存服务器的数据
if
(
config
.
useCache
&&
cacheOption
)
{
le
t
cacheTime
=
_
.
isNumber
(
cacheOption
)
?
cacheOption
:
60
;
cons
t
cacheTime
=
_
.
isNumber
(
cacheOption
)
?
cacheOption
:
60
;
reqId
=
reqId
||
this
.
_getReqId
(
options
);
cache
.
set
(
`
apiCache
:
$
{
reqId
}
`
,
result
,
cacheTime
);
...
...
@@ -65,7 +64,7 @@ class Http {
log
.
info
(
`
get
api
success
:
use
:
$
{
duration
}
ms
`
);
return
result
;
}).
catch
((
err
)
=>
{
le
t
duration
=
timer
.
put
(
'getApi'
);
// 统计时间结束
cons
t
duration
=
timer
.
put
(
'getApi'
);
// 统计时间结束
log
.
error
(
`
$
{
method
}
api
fail
:
use
:
$
{
duration
}
ms
,
code
:
$
{
err
.
statusCode
},
error
:
$
{
err
.
message
}
`
);
log
.
error
(
`
API
:
$
{
options
.
url
}?
$
{
qs
.
stringify
(
options
.
qs
)}
`
);
...
...
@@ -88,8 +87,8 @@ class Http {
* @return {[type]}
*/
_requestFromCache
(
options
,
slave
)
{
let
reqId
=
this
.
_getReqId
(
options
);
let
getCache
=
slave
?
cache
.
getFromSlave
:
cache
.
get
;
const
reqId
=
this
.
_getReqId
(
options
);
const
getCache
=
slave
?
cache
.
getFromSlave
:
cache
.
get
;
log
.
info
(
`
get
cache
:
$
{
reqId
},
url
:
$
{
options
.
url
}?
$
{
qs
.
stringify
(
options
.
qs
)}
`
);
return
getCache
(
`
apiCache
:
$
{
reqId
}
`
).
then
((
result
)
=>
{
...
...
@@ -129,7 +128,7 @@ class Http {
* @return {[type]}
*/
get
(
url
,
data
,
cacheOption
)
{
le
t
options
=
{
cons
t
options
=
{
url
:
`
$
{
this
.
ApiUrl
}
$
{
url
}
`
,
qs
:
data
,
json
:
true
,
...
...
@@ -150,7 +149,7 @@ class Http {
* @param data Obejct
*/
post
(
url
,
data
)
{
le
t
options
=
{
cons
t
options
=
{
url
:
`
$
{
this
.
ApiUrl
}
$
{
url
}
`
,
form
:
data
,
method
:
'post'
,
...
...
library/cache.js
View file @
72155bb
...
...
@@ -4,7 +4,6 @@
* @author bikai kai.bi@yoho.cn
* @date 2016/05/16
*/
'use strict'
;
const
Promise
=
require
(
'bluebird'
);
const
Memcached
=
require
(
'memcached'
);
...
...
library/cookie.js
View file @
72155bb
...
...
@@ -4,9 +4,9 @@
* @return {[string]}
*/
exports
.
getUid
=
(
req
)
=>
{
var
_uid
=
0
,
cookie
=
req
.
cookies
.
_UID
,
cookieList
;
const
cookie
=
req
.
cookies
.
_UID
;
let
_uid
=
0
;
let
cookieList
;
if
(
req
.
isApp
)
{
return
req
.
query
.
uid
||
0
;
...
...
library/helpers.js
View file @
72155bb
...
...
@@ -49,10 +49,10 @@ exports.urlFormat = (uri, qs, module) => {
const
subDomain
=
'.yohobuy.com'
;
const
subName
=
{
default
:
config
.
siteUrl
,
guang
:
'//guang'
+
subDomain
,
list
:
'//list'
+
subDomain
,
search
:
'//search'
+
subDomain
,
huodong
:
'//huodong'
+
subDomain
,
guang
:
`
//guang${subDomain}`,
list
:
`
//list${subDomain}`,
search
:
`
//search${subDomain}`,
huodong
:
`
//huodong${subDomain}`,
activity
:
'//activity.yohobuy.com'
,
index
:
config
.
siteUrl
};
...
...
@@ -62,12 +62,12 @@ exports.urlFormat = (uri, qs, module) => {
if
(
subName
[
module
])
{
url
=
subName
[
module
];
}
else
{
url
=
'//'
+
module
+
subDomain
;
// 规则没匹配到就把模块当作子域名
url
=
`
//${module}${subDomain}`
; // 规则没匹配到就把模块当作子域名
}
url
+=
uri
;
if
(
qs
)
{
url
+=
'?'
+
querystring
.
stringify
(
qs
)
;
url
+=
`
?
$
{
querystring
.
stringify
(
qs
)}
`
;
}
return
url
;
...
...
@@ -106,7 +106,7 @@ exports.dateFormat = (format, date) => {
if
(
date
instanceof
Date
)
{
return
moment
(
date
).
format
(
format
);
}
else
{
le
t
d
=
moment
.
unix
(
date
);
cons
t
d
=
moment
.
unix
(
date
);
return
moment
(
d
).
utc
().
format
(
format
);
}
...
...
@@ -141,7 +141,7 @@ exports.dateDiffFormat = (format, diff, type) => {
return
''
;
}
else
{
type
=
type
||
'ms'
;
le
t
m
=
moment
.
duration
(
diff
,
type
);
cons
t
m
=
moment
.
duration
(
diff
,
type
);
format
.
match
(
/
(\{
.*
?\})
/g
).
forEach
((
s
)
=>
{
format
=
format
.
replace
(
s
,
m
.
get
(
s
.
substring
(
1
,
s
.
length
-
1
)));
...
...
library/logger.js
View file @
72155bb
/**
* 日志工具类
* @author: hbomb<qiqi.zhou@yoho.cn>
* @date: 2016/05/06
*/
'use strict'
;
'use strict'
;
let
winston
=
require
(
'winston'
),
config
=
require
(
'../config/common'
),
FileTransport
=
require
(
'winston-daily-rotate-file'
);
const
winston
=
require
(
'winston'
);
const
config
=
require
(
'../config/common'
);
const
FileTransport
=
require
(
'winston-daily-rotate-file'
);
require
(
'influxdb-winston'
);
require
(
'influxdb-winston'
);
let
logger
=
new
(
winston
.
Logger
)({
transports
:
[
new
(
FileTransport
)(
config
.
loggers
.
infoFile
),
new
(
FileTransport
)(
config
.
loggers
.
errorFile
),
new
(
winston
.
transports
.
UdpTransport
)(
config
.
loggers
.
udp
),
new
(
winston
.
transports
.
Console
)(
config
.
loggers
.
console
)
]
});
const
logger
=
new
(
winston
.
Logger
)({
transports
:
[
new
(
FileTransport
)(
config
.
loggers
.
infoFile
),
new
(
FileTransport
)(
config
.
loggers
.
errorFile
),
new
(
winston
.
transports
.
UdpTransport
)(
config
.
loggers
.
udp
),
new
(
winston
.
transports
.
Console
)(
config
.
loggers
.
console
)
]
});
module
.
exports
=
logger
;
module
.
exports
=
logger
;
...
...
library/sign.js
View file @
72155bb
...
...
@@ -23,9 +23,9 @@ const privateKey = {
* @return {Object} 排序之后的参数对象
*/
const
packageSort
=
argument
=>
{
le
t
newObj
=
{};
cons
t
newObj
=
{};
for
(
le
t
k
of
Object
.
keys
(
argument
).
sort
())
{
for
(
cons
t
k
of
Object
.
keys
(
argument
).
sort
())
{
newObj
[
k
]
=
argument
[
k
];
}
...
...
@@ -38,10 +38,10 @@ const packageSort = argument => {
* @return {string} 生成的签名字符串
*/
const
makeSign
=
argument
=>
{
le
t
qs
=
[];
cons
t
qs
=
[];
_
.
forEach
(
argument
,
function
(
value
,
key
)
{
qs
.
push
(
key
+
'='
+
_
.
trim
(
value
));
_
.
forEach
(
argument
,
(
value
,
key
)
=>
{
qs
.
push
(
`
$
{
key
}
=
$
{
_
.
trim
(
value
)}
`
);
});
return
md5
(
qs
.
join
(
'&'
)).
toLowerCase
();
...
...
@@ -71,8 +71,10 @@ exports.apiSign = (params) => {
// 检查签名,APP 访问 H5 页面的时候需要检查
exports
.
checkSign
=
(
params
)
=>
{
let
clientSecret
=
params
.
client_secret
,
// eslint-disable-line camelcase
sortedParams
;
const
// eslint-disable-line camelcase
clientSecret
=
params
.
client_secret
;
let
sortedParams
;
// 忽略部分参数
delete
params
.
client_secret
;
...
...
library/timer.js
View file @
72155bb
'use strict'
;
/**
* 计时类
* @example
...
...
@@ -10,7 +8,7 @@
* @author: hbomb<qiqi.zhou@yoho.cn>
* @date: 2016/05/07
*/
'use strict'
;
class
Timer
{
constructor
()
{
this
.
timers
=
{};
...
...
@@ -20,10 +18,10 @@ class Timer {
* 打点计时
*/
put
(
label
)
{
le
t
labelTime
=
this
.
timers
[
label
];
cons
t
labelTime
=
this
.
timers
[
label
];
if
(
labelTime
)
{
le
t
duration
=
process
.
hrtime
(
labelTime
);
cons
t
duration
=
process
.
hrtime
(
labelTime
);
return
this
.
_round
(
duration
[
0
],
duration
[
1
]);
}
else
{
...
...
test/library/helpers.test.js
View file @
72155bb
...
...
@@ -20,7 +20,7 @@ test('uri format', t => {
let
uri
=
'/test'
;
let
qs
=
{
name
:
'yoho'
};
let
mod
=
'list'
;
let
expected
=
'//list.
m.
yohobuy.com/test?name=yoho'
;
let
expected
=
'//list.yohobuy.com/test?name=yoho'
;
t
.
is
(
helpers
.
urlFormat
(
uri
,
qs
,
mod
),
expected
);
});
...
...
Please
register
or
login
to post a comment