Authored by 王水玲

店铺,品牌,品类代码分离

/**
* 品类|品牌|店铺 controller
* @author: wsl<shuiling.wang@yoho.cn>
* @date: 2016/7/21
*/
'use strict';
const mRoot = '../models';
const headerModel = require('../../../doraemon/models/header');
const listModel = require(`${mRoot}/list`);
const _ = require('lodash');
const helpers = global.yoho.helpers;
/**
* 组织店铺页面数据
* @param {array} data 接口返回的店铺页所需数据
* @param {int} shopId 店铺id
* @param {int} isApp app版本
* @return array
*/
const _formShopData = (data, shopId, isApp) => {
let formatData = {
shopId: shopId,
appVersion: isApp
},
single = false;
let brandId;
// 组织店铺装修
if (data.decorator) {
_.forEach(data.decorator.list, (floor) => {
let resData = JSON.parse(floor.resourceData);
floor[_.camelCase(floor.resourceName)] = true;
// 店铺banner
if (floor.shopTopBannerApp) {
formatData.branerImg = resData[0].shopSrc;
}
// 资源位小图 接口返回两组,取每组第一张
if (floor.oneRowTwoColImagesApp) {
let spring = [];
_.forEach(resData, (item) => {
if (item.data) {
spring.push({
url: item.data[0].url,
springType: helpers.image(item.data[0].src)
});
}
});
formatData.spring = spring;
}
// 店铺品牌一览
if (floor.brandBrowse) {
let brandNumber = resData.length;
let brand = {
list: []
};
brandId = '';
// 少于2个不展示 单品店:单品店根据品牌id查询
if (brandNumber < 2) {
brandId = formatData.brand = resData[0].id;
single = true;
} else {
_.forEach(resData, (item) => {
if (item.brandDomain) {
brand.list.push({
url: helpers.urlFormat('', '', item.brandDomain) + (isApp ? `?openby:yohobuy={"action":"go.brand","params":{"shop_id":${shopId},"brand_id":${item.id}}}` : ''),
img: helpers.image(item.brandIco, 640, 400),
brandName: item.brandName
});
brandId += item.id + ',';
}
});
formatData.brandList = brand;
// 大于5个返回更多品牌链接
if (brandNumber > 5) {
formatData.brandList.url = helpers.urlFormat('/product/index/allBrand', {
shop_id: shopId
});
}
}
}
// 资源位大图幻灯
if (floor.largeSlideImgApp) {
let bannerTop = {
data: []
};
_.forEach(resData, (item) => {
if (item.data[0]) {
bannerTop.data.push({
url: item.data[0].url,
src: item.data[0].src
});
}
});
formatData.bannerTop = bannerTop;
}
// 热门品类
if (floor.recommendApp) {
let hotCategory = {
data: {
title: {
title: '热门品类'
},
list: []
}
};
_.forEach(resData, (cate) => {
hotCategory.data.list.push({
url: cate.url,
src: helpers.image(cate.src)
});
});
formatData.hotCategory = hotCategory;
}
// 人气单品
if (floor.hotProductsApp) {
let goods = [];
_.forEach(resData, (item) => {
goods.push({
url: helpers.urlFormat(item.productId, item.goodsId, item.cnAlphabet) + (isApp ? `?openby:yohobuy={"action":"go.productDetail","params":{"product_skn":${item.productSkn}}` : ''),
img: helpers.image(item.src, 235, 314),
productName: item.productName,
salesPrice: item.salesPrice,
presentPrice: item.salesPrice
});
});
formatData.hotList = goods;
}
});
}
// 店铺基本信息
if (data.shopInfo) {
let allGoodsParam = {
title: '全部商品'
};
formatData = _.assign({
logoImg: data.shopInfo.shopLogo,
storeName: (data.shopInfo.isShowShopName === 'Y') ? list.shopName : '',
collect: data.shopInfo.isFavorite === 'Y',
url: helpers.urlFormat('', {
shop_id: shopId
}, 'search'), // 搜索链接
more_url: helpers.urlFormat('', {
shop_id: shopId,
order: 's_n_d',
title: '人气单品'
}, 'search') // 人气单品的链接
}, formatData);
if (single) { // 单品点
allGoodsParam.brand = brandId;
} else { // 店铺id
formatData.shopId = shopId;
allGoodsParam.shopId = shopId;
}
if (!isApp) {
formatData = _.assign({
allGoods: helpers.urlFormat('', allGoodsParam, 'search'),
shopIntroHref: helpers.urlFormat('/product/index/intro', {
shop_id: shopId
}) // 店铺简介页地址
}, formatData);
} else {
formatData = _.assign({
allGoods: `${helpers.urlFormat('', allGoodsParam, 'search')}&openby:yohobuy={"action":"go.list","params":{"title":"全部商品", "actiontype":"0","shop_id":"' . self::$shopId . '","page":"1"}}`,
shopIntroHref: helpers.urlFormat('/product/index/intro', {
shop_id: shopId,
app_version: isApp
}),
more_url: formatData.more_url + `?openby:yohobuy{"action":"go.list","params":{"shop_id":${shopId},"title":"人气单品"}}`
}, formatData);
}
}
// 店铺下面的所有分类
if (data.shopCategory) {
console.log(data.shopCategory);
let total = data.shopCategory.length;
let shopCategory = {
list: []
};
if (total > 6) {
shopCategory.url = helpers.urlFormat('/product/index/category', {
shop_id: shopId
});
} else {
_.forEach(data.shopCategory, (item) => {
shopCategory.list.push({
url: helpers.urlFormat('', {
shop_id: shopId,
sort: item.relationParameter.sort
}, 'search'),
categoryId: item.categoryId,
name: item.categoryName
});
});
}
formatData.shopCategory = shopCategory;
}
if (single) {
formatData.favId = shopId;
formatData.shopId = '';
}
return formatData;
};
/**
* 新店铺首页
* @param {object} req
* @param {int} shopId 店铺id
* @param {int} uid 用户id
* @param {string} isApp app版本
*/
const _getShopData = (req, shopId, uid, isApp) => {
let data = {};
let channel = req.yoho.channel;
return Promise.all([searchModel.getShopDecorator(shopId), searchModel.getShopInfo(shopId, uid)]).then((result) => {
data = {
decorator: result[0], // 店铺装修资源数据
shopInfo: result[1] // 店铺信息
};
// 店铺使用基础模板,返回品牌页面
if (data.shopInfo.shopTemplateType && data.shopInfo.shopTemplateType === '1') {
return {
goBrand: data.shopInfo
};
}
// 店铺分类
return searchModel.getShopCategory(shopId, channel).then((shopCategory) => {
data = _.assign({
shopCategory: shopCategory
}, data);
return _formShopData(data, shopId, isApp); // 组织楼层数据
});
});
};
/**
* 店铺基础模板
*/
const _baseShop = (req, res, data) => {
let params = Object.assign({
cartUrl: helpers.urlFormat('/cart/index/index')
}, req.query);
Promise.all([searchModel.getShopDecorator(data.shopsId), searchModel.getShopBrands(data.shopsId)]).then((result) => {
result[0] = result[0] || {};
console.log(result[0], result[1]);
_.forEach(result[0], (item) => {
if (item.resourceName === 'shopTopBanner_base') {
let banner = JSON.parse(item.resourceData);
params = _.assign({
shopBanner: {
banner: banner[0].shopSrc
}
}, params);
}
});
if (data.multBrandShopType === '2') {
// 转义店铺
params.shop_id = data.shopsId;
} else {
params.brand = result[1];
}
res.render('search/goods-list', {
module: 'product',
page: 'search-list',
pageHeader: headerModel.setNav({
navTitle: data.shopName
}),
title: data.shopName,
goodList: params,
showDownloadApp: true,
pageFooter: true
});
});
};
/**
* 店铺首页 || 若店铺使用基础模板跳转品牌页
* @return int
*/
const _shop = (req, res, shopId) => {
let isApp = req.query.app_version || req.query.appVersion || false;
let pageHeader = {};
let uid = 0;
if (!isApp) {
pageHeader = {
pageHeader: _.assign({
shopPage: {
text: '分类',
url: helpers.urlFormat('/product/index/category', {
shop_id: shopId
})
}
}, headerModel.setNav({
navTitle: false
}))
};
uid = req.user.uid;
} else {
uid = req.query.uid;
req.session.appUid = uid;
res.cookie('appUid', uid, {
domain: 'yohobuy.com',
expires: new Date(Date.now() + 2592000000) // 有效期一年
});
}
_getShopData(req, shopId, uid, isApp).then((result) => {
if (result.goBrand) {
// 跳转基础模板
_baseShop(req, res, result.goBrand);
} else {
result = _.assign(result, pageHeader);
res.render('shop/index', {
module: 'product',
page: 'shop',
shopIndex: result,
shopHeadHide: true,
gender: req.query.gender,
channel: req.query.channel
});
}
});
};
// 品类落地页
const category = (req, res) => {
let params = Object.assign({}, req.query);
res.render('search/goods-list', {
module: 'product',
page: 'search-list',
pageHeader: headerModel.setNav({
navTitle: req.query.title
}),
goodList: params,
showDownloadApp: true,
pageFooter: true
});
};
// 品牌|店铺落地页
const brand = (req, res, next) => {
let params = Object.assign({}, req.query);
let domain = 'colormad';
let uid = req.user.uid || 20000032;
let brandId = 0;
let brandLogo = {};
let title = '';
if (!domain) {
res.redirect('/?go=1');
}
searchModel.getBrandLogoByDomain(domain).then((result) => {
brandLogo = result;
title = brandLogo.name;
if (brandLogo && brandLogo.id) {
brandId = brandLogo.id;
}
params = _.assign({
brand: brandId
}, params);
// 无店铺:0--->品牌页 无单品店有多品店:1--->搜索页 有单品店:2--->店铺页面
if (brandLogo.type === '2' && brandLogo.shopId) {
_shop(req, res, brandLogo.shopId);
} else { // 获取品牌店铺信息
searchModel.getBrandShops(brandId).then((brandShop) => {
if (brandId === 0) {
params.query = domain;
}
// 从搜索页过来的,显示搜索框, 和进入品牌引导信息 或者品牌关联多店铺
if (req.query.from === 'search' || brandShop.length > 0) {
params = {
brandWay: brandShop ? brandShop : brandLogo,
search: {
default: req.query.query,
url: helpers.urlFormat('', null, 'search')
}
};
} else if (brandId !== 0) { // 品牌一览过来的展示品牌介绍和LOGO
return Promise.all([searchModel.getBrandIntro(brandId, uid), searchModel.getBrandBanner(brandId)]).then((result) => {
title = result[0].title;
delete result[0].title;
return _.assign({
banner: result[1]
}, result[0]);
});
}
}).then((brandHome) => {
params.brandHome = brandHome;
res.render('search/goods-list', {
module: 'product',
page: 'search-list',
pageHeader: headerModel.setNav({
navTitle: title !== '' ? title : domain
}),
goodList: params,
showDownloadApp: true,
pageFooter: true
});
});
}
}).catch(next);
};
// 店铺简介
const shopIntro = (req, res, next) => {
let shopId = req.query.shop_id;
let appVersion = req.body.appVersion || false;
let params = {};
if (shopId){
searchModel.getShopIntro(shopId).then((result) => {
if (appVersion) {
params = {
title: '店铺简介'
};
} else {
params = {
pageHeader: headerModel.setNav({
navTitle: '店铺简介'
})
};
}
res.render('shop/intro', _.assign(params, result));
}).catch(next);
} else {
res.redirect('/?go=1');
}
};
/**
* 品牌[店铺]收藏/取消收藏
* id 品牌ID
* opt 操作标识("ok":表示收藏,"cancel":表示取消收藏)
*/
const favoriteBrand = (req, res, next) => {
let id = req.body.id;
let uid = req.user.uid || 20000032;
let opt = req.body.opt || 'ok';
let type = req.body.type || 'product';
let appVersion = req.body.appVersion || false;
if (appVersion === 'true') {
uid = req.cookies.appUid;
}
if (_.isNumber(id)) {
res.json({
code: 401,
message: '参数不正确',
data: false
});
return false;
} else if (!uid) {
res.json({
code: 400,
message: '未登录',
data: helpers.urlFormat('/signin.html', {
refer: decodeURI(req.cookies.refer)
})
});
return false;
} else if (opt !== 'ok') { // 取消收藏
searchModel.setFavoriteCancel(id, uid, type).then((data) => {
res.json(data);
return false;
}).catch(next);
}
searchModel.setFavorite(id, uid, type).then((result) => {
if (!result.code) { // 收藏
result = {
code: 401,
message: '参数不正确',
data: false
};
}
res.json(result);
});
};
module.exports = {
category,
brand,
favoriteBrand,
shopIntro
};
... ...
... ... @@ -7,362 +7,10 @@
'use strict';
const mRoot = '../models';
const headerModel = require('../../../doraemon/models/header');
// const resourcesProcess = require(`${utils}/resources-process`);
const searchModel = require(`${mRoot}/search`);
const _ = require('lodash');
const helpers = global.yoho.helpers;
/**
* 组织店铺页面数据
* @param {array} data 接口返回的店铺页所需数据
* @param {int} shopId 店铺id
* @param {int} isApp app版本
* @return array
*/
const _formShopData = (data, shopId, isApp) => {
let formatData = {
shopId: shopId,
appVersion: isApp
},
single = false;
let brandId;
// 组织店铺装修
if (data.decorator) {
_.forEach(data.decorator.list, (floor) => {
let resData = JSON.parse(floor.resourceData);
floor[_.camelCase(floor.resourceName)] = true;
// 店铺banner
if (floor.shopTopBannerApp) {
formatData.branerImg = resData[0].shopSrc;
}
// 资源位小图 接口返回两组,取每组第一张
if (floor.oneRowTwoColImagesApp) {
let spring = [];
_.forEach(resData, (item) => {
if (item.data) {
spring.push({
url: item.data[0].url,
springType: helpers.image(item.data[0].src)
});
}
});
formatData.spring = spring;
}
// 店铺品牌一览
if (floor.brandBrowse) {
let brandNumber = resData.length;
let brand = {
list: []
};
brandId = '';
// 少于2个不展示 单品店:单品店根据品牌id查询
if (brandNumber < 2) {
brandId = formatData.brand = resData[0].id;
single = true;
} else {
_.forEach(resData, (item) => {
if (item.brandDomain) {
brand.list.push({
url: helpers.urlFormat('', '', item.brandDomain) + (isApp ? `?openby:yohobuy={"action":"go.brand","params":{"shop_id":${shopId},"brand_id":${item.id}}}` : ''),
img: helpers.image(item.brandIco, 640, 400),
brandName: item.brandName
});
brandId += item.id + ',';
}
});
formatData.brandList = brand;
// 大于5个返回更多品牌链接
if (brandNumber > 5) {
formatData.brandList.url = helpers.urlFormat('/product/index/allBrand', {
shop_id: shopId
});
}
}
}
// 资源位大图幻灯
if (floor.largeSlideImgApp) {
let bannerTop = {
data: []
};
_.forEach(resData, (item) => {
if (item.data[0]) {
bannerTop.data.push({
url: item.data[0].url,
src: item.data[0].src
});
}
});
formatData.bannerTop = bannerTop;
}
// 热门品类
if (floor.recommendApp) {
let hotCategory = {
data: {
title: {
title: '热门品类'
},
list: []
}
};
_.forEach(resData, (cate) => {
hotCategory.data.list.push({
url: cate.url,
src: helpers.image(cate.src)
});
});
formatData.hotCategory = hotCategory;
}
// 人气单品
if (floor.hotProductsApp) {
let goods = [];
_.forEach(resData, (item) => {
goods.push({
url: helpers.urlFormat(item.productId, item.goodsId, item.cnAlphabet) + (isApp ? `?openby:yohobuy={"action":"go.productDetail","params":{"product_skn":${item.productSkn}}` : ''),
img: helpers.image(item.src, 235, 314),
productName: item.productName,
salesPrice: item.salesPrice,
presentPrice: item.salesPrice
});
});
formatData.hotList = goods;
}
});
}
// 店铺基本信息
if (data.shopInfo) {
let allGoodsParam = {
title: '全部商品'
};
formatData = _.assign({
logoImg: data.shopInfo.shopLogo,
storeName: (data.shopInfo.isShowShopName === 'Y') ? list.shopName : '',
collect: data.shopInfo.isFavorite === 'Y',
url: helpers.urlFormat('', {
shop_id: shopId
}, 'search'), // 搜索链接
more_url: helpers.urlFormat('', {
shop_id: shopId,
order: 's_n_d',
title: '人气单品'
}, 'search') // 人气单品的链接
}, formatData);
if (single) { // 单品点
allGoodsParam.brand = brandId;
} else { // 店铺id
formatData.shopId = shopId;
allGoodsParam.shopId = shopId;
}
if (!isApp) {
formatData = _.assign({
allGoods: helpers.urlFormat('', allGoodsParam, 'search'),
shopIntroHref: helpers.urlFormat('/product/index/intro', {
shop_id: shopId
}) // 店铺简介页地址
}, formatData);
} else {
formatData = _.assign({
allGoods: `${helpers.urlFormat('', allGoodsParam, 'search')}&openby:yohobuy={"action":"go.list","params":{"title":"全部商品", "actiontype":"0","shop_id":"' . self::$shopId . '","page":"1"}}`,
shopIntroHref: helpers.urlFormat('/product/index/intro', {
shop_id: shopId,
app_version: isApp
}),
more_url: formatData.more_url + `?openby:yohobuy{"action":"go.list","params":{"shop_id":${shopId},"title":"人气单品"}}`
}, formatData);
}
}
// 店铺下面的所有分类
if (data.shopCategory) {
console.log(data.shopCategory);
let total = data.shopCategory.length;
let shopCategory = {
list: []
};
if (total > 6) {
shopCategory.url = helpers.urlFormat('/product/index/category', {
shop_id: shopId
});
} else {
_.forEach(data.shopCategory, (item) => {
shopCategory.list.push({
url: helpers.urlFormat('', {
shop_id: shopId,
sort: item.relationParameter.sort
}, 'search'),
categoryId: item.categoryId,
name: item.categoryName
});
});
}
formatData.shopCategory = shopCategory;
}
if (single) {
formatData.favId = shopId;
formatData.shopId = '';
}
return formatData;
};
/**
* 新店铺首页
* @param {object} req
* @param {int} shopId 店铺id
* @param {int} uid 用户id
* @param {string} isApp app版本
*/
const _getShopData = (req, shopId, uid, isApp) => {
let data = {};
let channel = req.yoho.channel;
return Promise.all([searchModel.getShopDecorator(shopId), searchModel.getShopInfo(shopId, uid)]).then((result) => {
data = {
decorator: result[0], // 店铺装修资源数据
shopInfo: result[1] // 店铺信息
};
// 店铺使用基础模板,返回品牌页面
if (data.shopInfo.shopTemplateType && data.shopInfo.shopTemplateType === '1') {
return {
goBrand: data.shopInfo
};
}
// 店铺分类
return searchModel.getShopCategory(shopId, channel).then((shopCategory) => {
data = _.assign({
shopCategory: shopCategory
}, data);
return _formShopData(data, shopId, isApp); // 组织楼层数据
});
});
};
/**
* 店铺基础模板
*/
const _baseShop = (req, res, data) => {
let params = Object.assign({
cartUrl: helpers.urlFormat('/cart/index/index')
}, req.query);
Promise.all([searchModel.getShopDecorator(data.shopsId), searchModel.getShopBrands(data.shopsId)]).then((result) => {
result[0] = result[0] || {};
console.log(result[0], result[1]);
_.forEach(result[0], (item) => {
if (item.resourceName === 'shopTopBanner_base') {
let banner = JSON.parse(item.resourceData);
params = _.assign({
shopBanner: {
banner: banner[0].shopSrc
}
}, params);
}
});
if (data.multBrandShopType === '2') {
// 转义店铺
params.shop_id = data.shopsId;
} else {
params.brand = result[1];
}
res.render('search/goods-list', {
module: 'product',
page: 'search-list',
pageHeader: headerModel.setNav({
navTitle: data.shopName
}),
title: data.shopName,
goodList: params,
showDownloadApp: true,
pageFooter: true
});
});
};
/**
* 店铺首页 || 若店铺使用基础模板跳转品牌页
* @return int
*/
const _shop = (req, res, shopId) => {
let isApp = req.query.app_version || req.query.appVersion || false;
let pageHeader = {};
let uid = 0;
if (!isApp) {
pageHeader = {
pageHeader: _.assign({
shopPage: {
text: '分类',
url: helpers.urlFormat('/product/index/category', {
shop_id: shopId
})
}
}, headerModel.setNav({
navTitle: false
}))
};
uid = req.user.uid;
} else {
uid = req.query.uid;
req.session.appUid = uid;
res.cookie('appUid', uid, {
domain: 'yohobuy.com',
expires: new Date(Date.now() + 2592000000) // 有效期一年
});
}
_getShopData(req, shopId, uid, isApp).then((result) => {
if (result.goBrand) {
// 跳转基础模板
_baseShop(req, res, result.goBrand);
} else {
result = _.assign(result, pageHeader);
res.render('shop/index', {
module: 'product',
page: 'shop',
shopIndex: result,
shopHeadHide: true,
gender: req.query.gender,
channel: req.query.channel
});
}
});
};
// 搜索落地页
const list = (req, res, next) => {
let params = Object.assign({}, req.query);
... ... @@ -458,122 +106,6 @@ const list = (req, res, next) => {
}).catch(next);
};
// 品类落地页
const category = (req, res) => {
let params = Object.assign({}, req.query);
res.render('search/goods-list', {
module: 'product',
page: 'search-list',
pageHeader: headerModel.setNav({
navTitle: req.query.title
}),
goodList: params,
showDownloadApp: true,
pageFooter: true
});
};
// 品牌|店铺落地页
const brand = (req, res, next) => {
let params = Object.assign({}, req.query);
let domain = 'colormad';
let uid = req.user.uid || 20000032;
let brandId = 0;
let brandLogo = {};
let title = '';
if (!domain) {
res.redirect('/?go=1');
}
searchModel.getBrandLogoByDomain(domain).then((result) => {
brandLogo = result;
title = brandLogo.name;
if (brandLogo && brandLogo.id) {
brandId = brandLogo.id;
}
params = _.assign({
brand: brandId
}, params);
// 无店铺:0--->品牌页 无单品店有多品店:1--->搜索页 有单品店:2--->店铺页面
if (brandLogo.type === '2' && brandLogo.shopId) {
_shop(req, res, brandLogo.shopId);
} else { // 获取品牌店铺信息
searchModel.getBrandShops(brandId).then((brandShop) => {
if (brandId === 0) {
params.query = domain;
}
// 从搜索页过来的,显示搜索框, 和进入品牌引导信息 或者品牌关联多店铺
if (req.query.from === 'search' || brandShop.length > 0) {
params = {
brandWay: brandShop ? brandShop : brandLogo,
search: {
default: req.query.query,
url: helpers.urlFormat('', null, 'search')
}
};
} else if (brandId !== 0) { // 品牌一览过来的展示品牌介绍和LOGO
return Promise.all([searchModel.getBrandIntro(brandId, uid), searchModel.getBrandBanner(brandId)]).then((result) => {
title = result[0].title;
delete result[0].title;
return _.assign({
banner: result[1]
}, result[0]);
});
}
}).then((brandHome) => {
params.brandHome = brandHome;
res.render('search/goods-list', {
module: 'product',
page: 'search-list',
pageHeader: headerModel.setNav({
navTitle: title !== '' ? title : domain
}),
goodList: params,
showDownloadApp: true,
pageFooter: true
});
});
}
}).catch(next);
};
// 店铺简介
const shopIntro = (req, res, next) => {
let shopId = req.query.shop_id;
let appVersion = req.body.appVersion || false;
let params = {};
if (shopId){
searchModel.getShopIntro(shopId).then((result) => {
if (appVersion) {
params = {
title: '店铺简介'
};
} else {
params = {
pageHeader: headerModel.setNav({
navTitle: '店铺简介'
})
};
}
res.render('shop/intro', _.assign(params, result));
}).catch(next);
} else {
res.redirect('/?go=1');
}
};
// ajax 商品数据请求
const search = (req, res, next) => {
let params = Object.assign({}, req.query);
... ... @@ -604,64 +136,8 @@ let filter = (req, res, next) => {
}).catch(next);
};
/**
* 品牌[店铺]收藏/取消收藏
* id 品牌ID
* opt 操作标识("ok":表示收藏,"cancel":表示取消收藏)
*/
const favoriteBrand = (req, res, next) => {
let id = req.body.id;
let uid = req.user.uid || 20000032;
let opt = req.body.opt || 'ok';
let type = req.body.type || 'product';
let appVersion = req.body.appVersion || false;
if (appVersion === 'true') {
uid = req.cookies.appUid;
}
if (_.isNumber(id)) {
res.json({
code: 401,
message: '参数不正确',
data: false
});
return false;
} else if (!uid) {
res.json({
code: 400,
message: '未登录',
data: helpers.urlFormat('/signin.html', {
refer: decodeURI(req.cookies.refer)
})
});
return false;
} else if (opt !== 'ok') { // 取消收藏
searchModel.setFavoriteCancel(id, uid, type).then((data) => {
res.json(data);
return false;
}).catch(next);
}
searchModel.setFavorite(id, uid, type).then((result) => {
if (!result.code) { // 收藏
result = {
code: 401,
message: '参数不正确',
data: false
};
}
res.json(result);
});
};
module.exports = {
list,
filter,
search,
category,
brand,
favoriteBrand,
shopIntro
search
};
... ...
/**
* 品类|品牌|店铺 model
* @author: wsl<shuiling.wang@yoho.cn>
* @date: 2016/07/21
*/
'use strict';
const utils = '../../../utils';
const logger = global.yoho.logger;
const camelCase = global.yoho.camelCase;
const _ = require('lodash');
const helpers = global.yoho.helpers;
const api = global.yoho.API;
/**
* 品牌名称处理
* @param {[object]} list
* @return {[object]}
*/
const _processBrandNames = (list) => {
const formatData = [];
list = list || [];
list = camelCase(list);
_.forEach(list, (item) => {
_.forEach(item, (obj) => {
formatData.push({
brandDomain: obj.brandDomain,
brandName: obj.brandName
});
});
});
return formatData;
};
/**
* 品牌名称处理
* @param {[object]} list
* @return {[object]}
*/
const _processClassNames = (list) => {
const formatData = {
first: {},
second: {}
};
list = list || [];
list = camelCase(list);
_.forEach(list, (item) => {
_.forEach(item, (obj) => {
formatData.first[obj.categoryId] = obj.categoryName;
if (obj.sub) {
_.forEach(obj.sub, (sub) => {
formatData.second[sub.categoryId] = sub.categoryName;
});
}
});
});
return formatData;
};
/* 多品牌店铺列表数据信息处理*/
const _processBrandShops = (list) => {
const formatDat = [];
list = list || [];
list = camelCase(list);
_.forEach(list, (item) => {
if (item.shopId) {
formatDat.push({
url: helpers.urlFormat('/product/index/brand/', {
shop_id: item.brandId
}),
thumb: helpers.image(item.brandIco, 75, 40),
name: item.brandName
});
}
});
return formatDat;
};
/**
* 获取品牌信息数据
* @param {int} brandId 品牌ID
* @return array banner数据
*/
const getBrandIntro = (brandId, uid) => {
let param = {};
if (uid) {
param = {
uid: uid
};
}
return api.get('', _.assign({
method: 'app.brand.getBrandIntro',
brand_id: brandId
}, param), {
cache: true
}).then((result) => {
if (result && result.code === 200) {
let list = camelCase(result.data) || {};
return {
id: list.brandId,
intro: list.brandIntro,
collected: (list.isFavorite && list.isFavorite === 'Y') ? true : false,
title: list.brandName ? list.brandName : ''
};
} else {
logger.error('get brand introduction api return code is not 200');
return {};
}
});
};
/**
* 获取品牌banner数据
* @param {int} brandId 品牌ID
* @return array banner数据
*/
const getBrandBanner = (brandId) => {
return api.get('', {
method: 'app.brand.banner',
brand_id: brandId
}, {
cache: true
}).then((result) => {
if (result && result.code === 200) {
if (result.data.banner) {
return helpers.image(result.data.banner, 640, 150);
} else {
return '';
}
} else {
logger.error('get brand banner api return code is not 200');
return {};
}
});
};
/**
* 获取所有的品牌名称
**/
const getAllBrandNames = () => {
return api.get('', {
method: 'app.brand.brandlist'
}, {
cache: true
}).then((result) => {
if (result && result.code === 200) {
return _processBrandNames(result.data.brands);
} else {
logger.error('get brand all name data api return code is not 200');
return {};
}
});
};
/**
* 获取所有的品类名称
**/
const getClassNames = () => {
return api.get('', {
method: 'app.sort.get'
}, {
cache: true
}).then((result) => {
if (result && result.code === 200) {
return _processClassNames(result.data);
} else {
logger.error('get category name api return code is not 200');
return {};
}
});
};
/**
* 根据品牌域名获取品牌LOGO
* @param {string} domain 品牌域名
* @return array | false
*/
const getBrandLogoByDomain = (domain) => {
return api.get('', {
method: 'web.brand.byDomain',
domain: domain
}, {
cache: true
}).then((result) => {
if (result && result.code === 200) {
if (result.data) {
let formatData = camelCase(result.data);
return {
id: formatData.id,
url: helpers.urlFormat('', null, formatData.brandDomain),
thumb: helpers.image(formatData.brandIco, 75, 40),
name: formatData.brandName,
shopId: formatData.shopId ? formatData.shopId : 0, // 店铺id
type: formatData.type ? formatData.type : 0
};
} else {
return false;
}
} else {
logger.error('get brand logo by domain api return code is not 200');
return {};
}
});
};
/**
* 根据brandId 获取相关店铺列表
* @param brandId
* @return array
*/
const getBrandShops = (brandId) => {
return api.get('', {
method: 'app.shop.queryShopsByBrandId',
brand_id: brandId
}, {
cache: true
}).then((result) => {
if (result && result.code === 200) {
return _processBrandShops(result.data);
} else {
logger.error('get shop list by brandId api return code is not 200');
return {};
}
});
};
/**
* 收藏
* @param {int} id ID
* @param {int} uid 用户ID
* @param type
* @return array
*/
const setFavorite = (id, uid, type) => {
return api.post('', {
method: 'app.favorite.add',
fav_id: id,
id: id,
uid: uid,
type: type
});
};
/**
* 取消收藏
*
* @param {int} id 品牌ID
* @param {int} uid 用户ID
* @param type
* @return array
*/
const setFavoriteCancel = (id, uid, type) => {
return api.post('', {
method: 'app.favorite.cancel',
fav_id: id,
id: id,
uid: uid,
type: type
});
};
/**
* 获取店铺装修的所有资源接口
* @param {int} shopId 店铺id
* @return array
*/
const getShopDecorator = (shopId) => {
return api.get('', {
method: 'app.shopsdecorator.getList',
shop_id: shopId
}, {
cache: true
}).then((result) => {
if (result && result.code === 200) {
return camelCase(result.data);
} else {
logger.error('get shop all resources api return code is not 200');
return '';
}
});
};
/**
* 获取店铺信息
* @param {int} shopId 店铺id
* @param {int} uid 用户id 判断用户是否收藏店铺
* @return array
*/
const getShopInfo = (shopId, uid) => {
return api.get('', {
method: 'app.shops.getIntro',
shop_id: shopId,
uid: uid || 0
}).then((result) => {
if (result && result.code === 200) {
return camelCase(result.data);
} else {
logger.error('get shop info api return code is not 200');
return {};
}
});
};
/**
* 获取店铺下面的所有分类
* @param {int} shopId 店铺id
* @param {string} channel 频道
* @return array
*/
const getShopCategory = (shopId, channel) => {
return api.get('', {
method: 'app.shop.getSortInfo',
yh_channel: channel,
shop_id: shopId
}).then((result) => {
if (result && result.code === 200) {
return camelCase(result.data);
} else {
logger.error('get shop all classify api return code is not 200');
return {};
}
});
};
/**
* 调取店铺简介数据
* @param {int} shopId
*/
const getShopIntro = (shopId) => {
return api.get('', {
method: 'app.shops.getIntro',
shop_id: shopId
}).then((result) => {
if (result && result.code === 200) {
return camelCase(result.data);
} else {
logger.error('get shop intro api return code is not 200');
return {};
}
});
};
/**
* 获取店铺下面的所有品牌
* @param {int} shopId 店铺id
*/
const getShopBrands = (shopId) => {
return api.get('', {
method: 'app.shops.getShopsBrands',
shop_id: shopId
}).then((result) => {
if (result && result.code === 200) {
result = camelCase(result.data);
return result[0].brandId;
} else {
logger.error('gGet all the brands under the shop return code is not 200');
return {};
}
});
};
module.exports = {
getAllBrandNames,
getClassNames,
getBrandLogoByDomain,
getBrandIntro,
getBrandShops,
getBrandBanner,
setFavorite,
setFavoriteCancel,
getShopDecorator,
getShopInfo,
getShopCategory,
getShopIntro,
getShopBrands
};
... ...
... ... @@ -6,11 +6,7 @@
'use strict';
const utils = '../../../utils';
const logger = global.yoho.logger;
const camelCase = global.yoho.camelCase;
const contentCodeConfig = require('../../../config/content-code');
const productProcess = require(`${utils}/product-process`);
const _ = require('lodash');
const helpers = global.yoho.helpers;
const api = global.yoho.API;
/**
... ... @@ -27,11 +23,6 @@ const typeCont = {
};
/**
* 资源位code码
*/
const contentCode = contentCodeConfig.sale;
/**
* 商品搜索接口请求
* @param {[object]} params
* @return {[array]}
... ... @@ -68,142 +59,6 @@ const _searchGoods = (params) => {
};
/**
* 品牌名称处理
* @param {[object]} list
* @return {[object]}
*/
const _processBrandNames = (list) => {
const formatData = [];
list = list || [];
list = camelCase(list);
_.forEach(list, (item) => {
_.forEach(item, (obj) => {
formatData.push({
brandDomain: obj.brandDomain,
brandName: obj.brandName
});
});
});
return formatData;
};
/**
* 品牌名称处理
* @param {[object]} list
* @return {[object]}
*/
const _processClassNames = (list) => {
const formatData = {
first: {},
second: {}
};
list = list || [];
list = camelCase(list);
_.forEach(list, (item) => {
_.forEach(item, (obj) => {
formatData.first[obj.categoryId] = obj.categoryName;
if (obj.sub) {
_.forEach(obj.sub, (sub) => {
formatData.second[sub.categoryId] = sub.categoryName;
});
}
});
});
return formatData;
};
/* 多品牌店铺列表数据信息处理*/
const _processBrandShops = (list) => {
const formatDat = [];
list = list || [];
list = camelCase(list);
_.forEach(list, (item) => {
if (item.shopId) {
formatDat.push({
url: helpers.urlFormat('/product/index/brand/', {
shop_id: item.brandId
}),
thumb: helpers.image(item.brandIco, 75, 40),
name: item.brandName
});
}
});
return formatDat;
};
/**
* 获取品牌信息数据
* @param {int} brandId 品牌ID
* @return array banner数据
*/
const getBrandIntro = (brandId, uid) => {
let param = {};
if (uid) {
param = {
uid: uid
};
}
return api.get('', _.assign({
method: 'app.brand.getBrandIntro',
brand_id: brandId
}, param), {
cache: true
}).then((result) => {
if (result && result.code === 200) {
let list = camelCase(result.data) || {};
return {
id: list.brandId,
intro: list.brandIntro,
collected: (list.isFavorite && list.isFavorite === 'Y') ? true : false,
title: list.brandName ? list.brandName : ''
};
} else {
logger.error('get brand introduction api return code is not 200');
return {};
}
});
};
/**
* 获取品牌banner数据
* @param {int} brandId 品牌ID
* @return array banner数据
*/
const getBrandBanner = (brandId) => {
return api.get('', {
method: 'app.brand.banner',
brand_id: brandId
}, {
cache: true
}).then((result) => {
if (result && result.code === 200) {
if (result.data.banner) {
return helpers.image(result.data.banner, 640, 150);
} else {
return '';
}
} else {
logger.error('get brand banner api return code is not 200');
return {};
}
});
};
/**
* 获取商品数据
*/
const getSearchData = (params) => {
... ... @@ -233,248 +88,7 @@ const getFilterData = (params) => {
});
};
/**
* 获取所有的品牌名称
**/
const getAllBrandNames = () => {
return api.get('', {
method: 'app.brand.brandlist'
}, {
cache: true
}).then((result) => {
if (result && result.code === 200) {
return _processBrandNames(result.data.brands);
} else {
logger.error('get brand all name data api return code is not 200');
return {};
}
});
};
/**
* 获取所有的品类名称
**/
const getClassNames = () => {
return api.get('', {
method: 'app.sort.get'
}, {
cache: true
}).then((result) => {
if (result && result.code === 200) {
return _processClassNames(result.data);
} else {
logger.error('get category name api return code is not 200');
return {};
}
});
};
/**
* 根据品牌域名获取品牌LOGO
* @param {string} domain 品牌域名
* @return array | false
*/
const getBrandLogoByDomain = (domain) => {
return api.get('', {
method: 'web.brand.byDomain',
domain: domain
}, {
cache: true
}).then((result) => {
if (result && result.code === 200) {
if (result.data) {
let formatData = camelCase(result.data);
return {
id: formatData.id,
url: helpers.urlFormat('', null, formatData.brandDomain),
thumb: helpers.image(formatData.brandIco, 75, 40),
name: formatData.brandName,
shopId: formatData.shopId ? formatData.shopId : 0, // 店铺id
type: formatData.type ? formatData.type : 0
};
} else {
return false;
}
} else {
logger.error('get brand logo by domain api return code is not 200');
return {};
}
});
};
/**
* 根据brandId 获取相关店铺列表
* @param brandId
* @return array
*/
const getBrandShops = (brandId) => {
return api.get('', {
method: 'app.shop.queryShopsByBrandId',
brand_id: brandId
}, {
cache: true
}).then((result) => {
if (result && result.code === 200) {
return _processBrandShops(result.data);
} else {
logger.error('get shop list by brandId api return code is not 200');
return {};
}
});
};
/**
* 收藏
* @param {int} id ID
* @param {int} uid 用户ID
* @param type
* @return array
*/
const setFavorite = (id, uid, type) => {
return api.post('', {
method: 'app.favorite.add',
fav_id: id,
id: id,
uid: uid,
type: type
});
};
/**
* 取消收藏
*
* @param {int} id 品牌ID
* @param {int} uid 用户ID
* @param type
* @return array
*/
const setFavoriteCancel = (id, uid, type) => {
return api.post('', {
method: 'app.favorite.cancel',
fav_id: id,
id: id,
uid: uid,
type: type
});
};
/**
* 获取店铺装修的所有资源接口
* @param {int} shopId 店铺id
* @return array
*/
const getShopDecorator = (shopId) => {
return api.get('', {
method: 'app.shopsdecorator.getList',
shop_id: shopId
}, {
cache: true
}).then((result) => {
if (result && result.code === 200) {
return camelCase(result.data);
} else {
logger.error('get shop all resources api return code is not 200');
return '';
}
});
};
/**
* 获取店铺信息
* @param {int} shopId 店铺id
* @param {int} uid 用户id 判断用户是否收藏店铺
* @return array
*/
const getShopInfo = (shopId, uid) => {
return api.get('', {
method: 'app.shops.getIntro',
shop_id: shopId,
uid: uid || 0
}).then((result) => {
if (result && result.code === 200) {
return camelCase(result.data);
} else {
logger.error('get shop info api return code is not 200');
return {};
}
});
};
/**
* 获取店铺下面的所有分类
* @param {int} shopId 店铺id
* @param {string} channel 频道
* @return array
*/
const getShopCategory = (shopId, channel) => {
return api.get('', {
method: 'app.shop.getSortInfo',
yh_channel: channel,
shop_id: shopId
}).then((result) => {
if (result && result.code === 200) {
return camelCase(result.data);
} else {
logger.error('get shop all classify api return code is not 200');
return {};
}
});
};
/**
* 调取店铺简介数据
* @param {int} shopId
*/
const getShopIntro = (shopId) => {
return api.get('', {
method: 'app.shops.getIntro',
shop_id: shopId
}).then((result) => {
if (result && result.code === 200) {
return camelCase(result.data);
} else {
logger.error('get shop intro api return code is not 200');
return {};
}
});
};
/**
* 获取店铺下面的所有品牌
* @param {int} shopId 店铺id
*/
const getShopBrands = (shopId) => {
return api.get('', {
method: 'app.shops.getShopsBrands',
shop_id: shopId
}).then((result) => {
if (result && result.code === 200) {
result = camelCase(result.data);
return result[0].brandId;
} else {
logger.error('gGet all the brands under the shop return code is not 200');
return {};
}
});
};
module.exports = {
getSearchData,
getFilterData,
getAllBrandNames,
getClassNames,
getBrandLogoByDomain,
getBrandIntro,
getBrandShops,
getBrandBanner,
setFavorite,
setFavoriteCancel,
getShopDecorator,
getShopInfo,
getShopCategory,
getShopIntro,
getShopBrands
getFilterData
};
... ...
... ... @@ -27,6 +27,9 @@ const recommendForYou = require(`${cRoot}/recommend-for-you`);
// search controller
const search = require(`${cRoot}/search`);
// 品类|品牌|店铺 controller
const list = require(`${cRoot}/list`);
// 新品到着 controller
const news = require(`${cRoot}/new`);
... ... @@ -68,17 +71,17 @@ router.get('/search/filter', search.filter);
router.get('/search/search', search.search);
// 品类
router.get('/index/index', search.category);
router.get('/index/index', list.category);
// 品牌 | 店铺
router.get('/index/brand', search.brand);
router.get('/index/brand', list.brand);
// 店铺介绍
router.get('/index/intro', search.shopIntro);
router.get('/index/intro', list.shopIntro);
// 品牌|店铺收藏|取消收藏
router.post('/opt/favoriteBrand', search.favoriteBrand);
router.get('/opt/favoriteBrand', search.favoriteBrand);
router.post('/opt/favoriteBrand', list.favoriteBrand);
router.get('/opt/favoriteBrand', list.favoriteBrand);
// 新品到着
router.get('/new', news.newGoods);
... ...