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
yyq
9 years ago
Commit
44f4b54b0f7040c9e263007cb165d280d7ac2ad9
1 parent
4a5bef3d
code 公共处理方法
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
57 additions
and
33 deletions
doraemon/models/header.js
library/api.js
doraemon/models/header.js
View file @
44f4b54
...
...
@@ -13,8 +13,6 @@ const sign = require(`${global.library}/sign`);
const
serviceApi
=
new
ServiceAPI
();
const
log
=
require
(
`
$
{
global
.
library
}
/logger`
)
;
/**
* 获取菜单
* @param undefined
...
...
@@ -179,13 +177,11 @@ const getSubNav = (data, type) => {
*/
const
setHeaderData
=
(
resData
,
type
)
=>
(
{
headerData
:
{
header
:
true
,
headType
:
type
,
yohoGroup
:
getMenuData
(),
navbars
:
resData
?
getNavBar
(
resData
,
type
)
:
[],
subNav
:
resData
?
getSubNav
(
resData
,
type
)
:
[]
}
header
:
true
,
headType
:
type
,
yohoGroup
:
getMenuData
(),
navbars
:
resData
?
getNavBar
(
resData
,
type
)
:
[],
subNav
:
resData
?
getSubNav
(
resData
,
type
)
:
[]
}
);
...
...
@@ -204,12 +200,12 @@ exports.requestHeaderData = (type, parentId) => {
type
=
type
||
'boys'
;
return
serviceApi
.
get
(
'operations/api/v6/category/getCategory'
,
data
,
true
).
then
(
res
=>
{
if
(
res
&&
res
.
code
===
200
)
{
return
setHeaderData
(
res
.
data
,
type
);
}
else
{
log
.
error
(
'获取头部信息的接口返回状态码 不是 200'
);
return
{};
}
return
serviceApi
.
get
(
'operations/api/v6/category/getCategory'
,
data
,
{
cache
:
true
,
code
:
200
}).
then
(
res
=>
{
return
{
headerData
:
res
?
setHeaderData
(
res
.
data
,
type
)
:
{}
};
});
};
...
...
library/api.js
View file @
44f4b54
...
...
@@ -55,9 +55,28 @@ class Http {
}
/**
* 处理接口返回状态码
*/
_handleDataCode
(
data
,
code
,
url
)
{
let
result
=
{};
code
=
code
||
200
;
if
(
_
.
toNumber
(
data
.
code
)
===
_
.
toNumber
(
code
))
{
_
.
unset
(
data
,
'code'
);
_
.
forEach
(
data
,
(
value
,
key
)
=>
{
_
.
set
(
result
,
key
,
value
);
});
}
else
{
log
.
error
(
`
API
:
$
{
url
}
return
code
not
$
{
code
}
`
);
}
return
result
;
}
/**
* 调用接口
*/
_requestFromAPI
(
options
,
cacheOption
,
reqId
)
{
_requestFromAPI
(
options
,
param
,
reqId
)
{
const
timer
=
new
Timer
();
const
method
=
options
.
method
||
'get'
;
...
...
@@ -71,9 +90,15 @@ class Http {
return
Promise
.
reject
(
API_BAD_RETSULT
);
}
// 处理返回数据状态码
if
(
param
&&
param
.
code
)
{
result
=
this
.
_handleDataCode
(
result
,
param
.
code
,
options
.
url
);
}
// 写缓存, 否则返回 Slave 缓存服务器的数据
if
(
config
.
useCache
&&
cacheOption
)
{
const
cacheTime
=
_
.
isNumber
(
cacheOption
)
?
cacheOption
:
60
;
if
(
options
.
method
===
'get'
&&
config
.
useCache
&&
param
&&
param
.
cache
)
{
const
cacheTime
=
_
.
isNumber
(
param
.
cache
)
?
param
.
cache
:
60
;
const
catchErr
=
(
err
)
=>
{
log
.
error
(
`
cache
:
$
{
err
.
toString
()}
`
);
};
...
...
@@ -92,7 +117,7 @@ class Http {
log
.
error
(
`
API
:
$
{
options
.
url
}?
$
{
qs
.
stringify
(
options
.
qs
)}
`
);
// 使用缓存的时候,读取二级缓存
if
(
config
.
useCache
&&
cacheOption
)
{
if
(
config
.
useCache
&&
param
&&
param
.
cache
)
{
return
this
.
_requestFromCache
(
options
,
true
);
}
return
Promise
.
resolve
(
API_CALL_FAIL
);
...
...
@@ -103,9 +128,10 @@ class Http {
* 读取缓存
* @param {[object]} options
* @param {[boolean]} slave true: 读取二级缓存
* @param {[object]} param 请求API处理参数
* @return {[type]}
*/
_requestFromCache
(
options
,
slave
)
{
_requestFromCache
(
options
,
slave
,
param
)
{
const
reqId
=
this
.
_getReqId
(
options
);
const
getCache
=
slave
?
cache
.
getFromSlave
:
cache
.
get
;
...
...
@@ -122,14 +148,14 @@ class Http {
// 读取缓存失败,并且不是二级缓存的时候,调用 API
if
(
!
slave
)
{
return
this
.
_requestFromAPI
(
options
,
true
,
reqId
);
return
this
.
_requestFromAPI
(
options
,
param
,
reqId
);
}
}).
catch
(()
=>
{
log
.
error
(
slave
?
SLAVE_CACHE_FAIL
:
MASTER_CACHE_FAIL
);
// 读取缓存失败,并且不是二级缓存的时候,调用 API
if
(
!
slave
)
{
return
this
.
_requestFromAPI
(
options
,
true
,
reqId
);
return
this
.
_requestFromAPI
(
options
,
param
,
reqId
);
}
return
Promise
.
resolve
(
API_CALL_FAIL
);
...
...
@@ -140,10 +166,10 @@ class Http {
* 使用 get 请求获取接口
* @param {[string]} url
* @param {[object]} data
* @param {[
bool or number]} cacheOption 使用数字时,数字表示缓存时间
* @param {[
object]} param 数据处理参数
* @return {[type]}
*/
get
(
url
,
data
,
cacheOption
)
{
get
(
url
,
data
,
param
)
{
const
options
=
{
url
:
`
$
{
this
.
ApiUrl
}
$
{
url
}
`
,
qs
:
data
.
client_secret
?
data
:
sign
.
apiSign
(
data
),
...
...
@@ -153,19 +179,21 @@ class Http {
};
// 从缓存获取数据
if
(
config
.
useCache
&&
cacheOption
)
{
return
this
.
_requestFromCache
(
options
);
if
(
config
.
useCache
&&
param
&&
param
.
catch
)
{
return
this
.
_requestFromCache
(
options
,
false
,
param
);
}
return
this
.
_requestFromAPI
(
options
,
cacheOption
);
return
this
.
_requestFromAPI
(
options
,
param
);
}
/**
* post
* @param url String
* @param data Obejct
* 使用 post 请求获取接口
* @param {[string]} url
* @param {[object]} data
* @param {[object]} param 数据处理参数
* @return {[type]}
*/
post
(
url
,
data
)
{
post
(
url
,
data
,
param
)
{
const
options
=
{
url
:
`
$
{
this
.
ApiUrl
}
$
{
url
}
`
,
form
:
data
.
client_secret
?
data
:
sign
.
apiSign
(
data
),
...
...
@@ -174,7 +202,7 @@ class Http {
timeout
:
3000
};
return
this
.
_requestFromAPI
(
options
);
return
this
.
_requestFromAPI
(
options
,
param
);
}
all
(
list
)
{
...
...
Please
register
or
login
to post a comment