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
ea6db8edfbada350c0d6775b84384c87a1307fac
1 parent
3dcbeb5d
add cache
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
121 additions
and
9 deletions
apps/product/models/search-api.js
apps/product/models/search-api.js
View file @
ea6db8e
...
...
@@ -13,6 +13,8 @@ const serviceApi = global.yoho.ServiceAPI;
const
_
=
require
(
'lodash'
);
const
helpers
=
global
.
yoho
.
helpers
;
const
images
=
require
(
'../../../utils/images.js'
);
const
cache
=
global
.
yoho
.
cache
;
const
logger
=
global
.
yoho
.
logger
;
const
getSortByConditionAsync
=
(
condition
)
=>
{
return
api
.
get
(
'sortgroup.json'
,
condition
);
...
...
@@ -27,6 +29,18 @@ const isFavoriteBrandUrl = '/shops/service/v1/favorite/';
// 根据品牌查询相关文章
const
relateArticleUrl
=
'guang/service/v2/article/getArticleByBrand'
;
const
getSearchCackeKey
=
params
=>
{
let
ks
=
[];
_
.
forEach
(
params
,
(
val
,
key
)
=>
{
if
(
params
.
hasOwnProperty
(
key
)
&&
!
_
.
includes
([
'page'
,
'limit'
,
'need_filter'
,
'order'
],
key
))
{
ks
.
push
(
val
);
}
});
return
ks
.
join
(
'_'
);
}
/**
* 获取商品列表
* @return
...
...
@@ -37,11 +51,46 @@ const getProductList = (params) => {
// method: 'app.search.li',
order
:
's_n_desc'
,
need_filter
:
'yes'
,
limit
:
60
};
Object
.
assign
(
finalParams
,
params
);
return
yohoApi
.
get
(
''
,
finalParams
);
let
cKey
=
getSearchCackeKey
(
finalParams
);
return
cache
.
get
(
cKey
).
catch
().
then
(
cdata
=>
{
let
hasCache
=
false
;
if
(
cdata
)
{
try
{
cdata
=
JSON
.
parse
(
cdata
);
}
catch
(
e
)
{
logger
.
debug
(
'getProductList cache data parse fail.'
);
}
if
(
cdata
.
filter
&&
cdata
.
standard
)
{
hasCache
=
true
;
finalParams
.
need_filter
=
'no'
;
}
}
return
yohoApi
.
get
(
''
,
finalParams
).
then
(
result
=>
{
if
(
hasCache
&&
result
&&
result
.
data
)
{
Object
.
assign
(
result
.
data
,
cdata
);
}
else
{
if
(
result
&&
result
.
data
&&
result
.
data
.
filter
)
{
cache
.
set
(
cKey
,
Object
.
assign
({},
{
filter
:
result
.
data
.
filter
,
standard
:
result
.
data
.
standard
}));
}
}
return
result
;
});
});
};
/**
...
...
@@ -57,7 +106,30 @@ const getSortList = (params) => {
};
Object
.
assign
(
finalParams
,
params
);
return
yohoApi
.
get
(
''
,
finalParams
);
let
cKey
=
getSearchCackeKey
(
finalParams
);
return
cache
.
get
(
cKey
).
catch
().
then
(
cdata
=>
{
let
cdataObj
;
if
(
cdata
)
{
try
{
cdataObj
=
JSON
.
parse
(
cdata
);
}
catch
(
e
)
{
logger
.
debug
(
'getSortList cache data parse fail.'
);
}
}
if
(
cdataObj
)
{
return
cdataObj
;
}
else
{
return
yohoApi
.
get
(
''
,
finalParams
).
then
(
ret
=>
{
cache
.
set
(
cKey
,
ret
);
return
ret
;
});
}
});
};
/**
...
...
@@ -150,9 +222,28 @@ const getBrandShop = (query) => {
};
Object
.
assign
(
finalParams
,
{
keyword
:
query
});
return
yohoApi
.
get
(
''
,
finalParams
).
then
(
ret
=>
{
if
(
ret
&&
ret
.
code
===
200
)
{
return
ret
.
data
;
let
cKey
=
getSearchCackeKey
(
finalParams
);
return
cache
.
get
(
cKey
).
catch
().
then
(
cdata
=>
{
let
retObj
;
try
{
if
(
cdata
)
retObj
=
JSON
.
parse
(
cdata
);
}
catch
(
e
)
{
logger
.
debug
(
'getBrandShop cache data parse fail.'
);
}
if
(
retObj
)
{
return
retObj
;
}
else
{
return
yohoApi
.
get
(
''
,
finalParams
).
then
(
ret
=>
{
if
(
ret
&&
ret
.
code
===
200
)
{
cache
.
set
(
cKey
,
ret
.
data
);
return
ret
.
data
;
}
});
return
}
});
};
...
...
@@ -163,12 +254,33 @@ const getBrandShop = (query) => {
*/
const
getShopsByBrandId
=
bid
=>
{
return
yohoApi
.
get
(
''
,
{
let
finalParams
=
{
method
:
'app.shop.queryShopsByBrandId'
,
brand_id
:
bid
}).
then
(
ret
=>
{
if
(
ret
&&
ret
.
code
===
200
)
{
return
ret
.
data
;
}
let
cKey
=
getSearchCackeKey
(
finalParams
);
return
cache
.
get
(
cKey
).
catch
().
then
(
cdata
=>
{
let
cdataObj
;
if
(
cdata
)
{
try
{
cdataObj
=
JSON
.
parse
(
cdata
);
}
catch
(
e
)
{
logger
.
debug
(
'getShopsByBrandId cache data parse fail.'
);
}
}
if
(
cdataObj
)
{
return
cdataObj
;
}
else
{
return
yohoApi
.
get
(
''
,
finalParams
).
then
(
ret
=>
{
if
(
ret
&&
ret
.
code
===
200
)
{
cache
.
set
(
cKey
,
ret
.
data
);
return
ret
.
data
;
}
});
}
});
};
...
...
Please
register
or
login
to post a comment