Authored by biao

update for doraemen header.js

... ... @@ -31,7 +31,7 @@ global.library = path.resolve('./library');
global.middleware = path.resolve('./doraemon/middleware');
global.utils = path.resolve('./utils');
const seo = require(`${global.middleware}/seo/seo`);
const seo = require(`${global.middleware}/seo`);
... ...
... ... @@ -12,6 +12,7 @@ const SearchAPI = require(`${global.library}/api`).SearchAPI;
const sign = require(`${global.library}/sign`);
const helpers = require(`${global.library}/helpers`);
const images = require(`${global.utils}/images`);
const log = require(`${global.library}/logger`);
const serviceApi = new ServiceAPI();
... ... @@ -793,7 +794,14 @@ const requestContent = type => {
limit: 1000
});
return serviceApi.get('/operations/api/v5/resource/home', data);
return serviceApi.get('/operations/api/v5/resource/home', data).then(res => {
if (res.code === 200) {
return res;
} else {
log.error('获取资源位接口返回状态码 不是 200');
return {};
}
});
};
const floorMap = {
... ... @@ -891,33 +899,31 @@ const processFloorData = (rawData, type) => {
* @return {object}
*/
exports.getContent = type => {
return Promise.all([headerModel.requestHeaderData(), requestContent(type)]).then(res => {
return Promise.all([headerModel.requestHeaderData(type), requestContent(type)]).then(res => {
if (res[0].code === 200 && res[1].code === 200) {
let headerData = res[0].data,
contentData = res[1].data.list;
let headerData = res[0].data || res[0],
contentData = res[1].data ? res[1].data.list : res[1];
let data = {};
let data = {};
const processResult = processFloorData(contentData, type);
const processResult = processFloorData(contentData, type);
data = headerModel.setHeaderData(headerData, type);
data.module = 'channel';
data.page = 'channel';
data.pageType = type;
data.footerTop = true;
data.channel = processResult.floors;
data = headerData;
data.module = 'channel';
data.page = 'channel';
data.pageType = type;
data.footerTop = true;
data.channel = processResult.floors;
return {
rawData: contentData,
floorData: data,
searchPromise: processResult.promise,
singlehotFloorIndex: processResult.singlehotFloorIndex,
singlehotFloorTitile: processResult.singlehotFloorTitile,
channelType: type
};
}
return {
rawData: contentData,
floorData: data,
searchPromise: processResult.promise,
singlehotFloorIndex: processResult.singlehotFloorIndex,
singlehotFloorTitile: processResult.singlehotFloorTitile,
channelType: type
};
}).then(result => {
... ... @@ -934,7 +940,7 @@ exports.getContent = type => {
return result.floorData;
});
} else {
return result.floorData;
return result.floorData || result;
}
});
};
... ... @@ -943,30 +949,28 @@ exports.getContent = type => {
// 优选品牌楼层floorData-ajax
exports.getbrandFloorDataAjax = type => {
return requestContent(type).then(res => {
if (res.code === 200) {
let contentData = res.data.list;
let contentData = res.data ? res.data.list : [];
let data = {
logoBrand: [],
moreBrand: ''
};
let data = {
logoBrand: [],
moreBrand: ''
};
_.forEach(contentData, (d, index) => {
if (d.data.text && d.data.text.indexOf('优选品牌') >= 0) {
_.forEach(contentData[index + 2].data, (floorData) => {
let o = {};
_.forEach(contentData, (d, index) => {
if (d.data.text && d.data.text.indexOf('优选品牌') >= 0) {
_.forEach(contentData[index + 2].data, (floorData) => {
let o = {};
o.href = floorData.url;
o.img = helpers.image(floorData.src, 185, 86, 2);
o.href = floorData.url;
o.img = helpers.image(floorData.src, 185, 86, 2);
data.logoBrand.push(o);
});
data.logoBrand.push(o);
});
data.moreBrand = contentData[index + 3].data[0].url;
}
});
data.moreBrand = contentData[index + 3].data[0].url;
}
});
return data;
}
return data;
});
};
... ...
... ... @@ -13,19 +13,7 @@ const sign = require(`${global.library}/sign`);
const serviceApi = new ServiceAPI();
const getChannelIndex = type => {
const channelMap = {
boys: 0,
girls: 1,
kids: 2,
lifestyle: 3
};
const index = channelMap[type];
return _.isNil(index) ? 0 : index;
};
const log = require(`${global.library}/logger`);
/**
* 获取菜单
... ... @@ -64,19 +52,24 @@ const getMenuData = () => (
/**
* 获取导航
* @param undefined
* @param {Object} data 要处理的数据
* @param {String} type 频道类型
* @return {array} 导航数组
*/
const getNavBar = (data) => {
const getNavBar = (data, type) => {
let navBars = [];
_.forEach(data, function(item) {
_.forEach(data, item => {
let obj = {};
obj.link = item.sort_url;
obj.cn = item.sort_name;
obj.en = item.sort_name_en;
if (type === _.camelCase(item.sort_name_en).toLowerCase()) {
obj.active = true;
}
navBars.push(obj);
});
... ... @@ -86,13 +79,13 @@ const getNavBar = (data) => {
/**
* 获取品牌名字
* @param undefined
* @param {Object} data 要处理数据
* @return {array} 品牌数组
*/
const getBrandItems = (data) => {
let brandItems = [];
_.forEach(data, function(item) {
_.forEach(data, item => {
let obj = {
link: '',
hot: false,
... ... @@ -112,13 +105,13 @@ const getBrandItems = (data) => {
/**
* 获取三级菜单
* @param undefined
* @param {Object} data 要处理数据
* @return {array} 三级菜单数组
*/
const getThirdNav = (data) => {
let thirdNav = [];
_.forEach(data, function(item) {
_.forEach(data, item => {
let obj = {
link: '',
title: '',
... ... @@ -143,29 +136,33 @@ const getThirdNav = (data) => {
/**
* 获取子菜单
* @param undefined
* @param {Object} data 要处理数据
* @param {String} type 频道类型
* @return {array} 子菜单数组
*/
const getSubNav = (data, type) => {
let subNav = [];
_.forEach(data[getChannelIndex(type)].sub, function(item) {
let obj = {};
_.forEach(data, it => {
if (type === _.camelCase(it.sort_name_en).toLowerCase()) {
_.forEach(it.sub, item => {
let obj = {};
obj.link = item.sort_url;
obj.name = item.sort_name;
obj.isHot = item.is_hot === 'Y' ? true : false;
obj.isNew = item.is_new === 'Y' ? true : false;
obj.link = item.sort_url;
obj.name = item.sort_name;
obj.isHot = item.is_hot === 'Y' ? true : false;
obj.isNew = item.is_new === 'Y' ? true : false;
if (item.sub) {
obj.thirdNav = getThirdNav(item.sub);
obj.imgCode = item.content_code;
}
if (item.sub) {
obj.thirdNav = getThirdNav(item.sub);
obj.imgCode = item.content_code;
}
subNav.push(obj);
subNav.push(obj);
});
}
});
return subNav;
};
... ... @@ -178,31 +175,28 @@ const getSubNav = (data, type) => {
* @param {String} 指定页面类型为boys,girls,kids,lifestyle
* @return {object} 头部数据
*/
exports.setHeaderData = (resData, type) => {
const setHeaderData = (resData, type) => {
let data = {
headerData: {
header: true,
headtype: type,
yohoGroup: getMenuData(),
navbars: resData ? getNavBar(resData) : [],
navbars: resData ? getNavBar(resData, type) : [],
subNav: resData ? getSubNav(resData, type) : []
}
};
if (data.headerData.navbars.length) {
data.headerData.navbars[getChannelIndex(type)].active = true;
}
return data;
};
/**
* 请求头部数据
* @param undefined
* @param {String} 频道类型
* @return {promise}
*/
exports.requestHeaderData = () => {
exports.requestHeaderData = type => {
let data = sign.apiSign({
/* eslint-disable */
... ... @@ -210,5 +204,12 @@ exports.requestHeaderData = () => {
/* eslint-enable */
});
return serviceApi.get('operations/api/v6/category/getCategory', data, true);
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 {};
}
});
};
... ...
... ... @@ -31,9 +31,9 @@ var logoAngle = 0,
loopTime = 500;
// handlebars模板
centerFn = handlebars.compile($('#simple-account-info-tpl').html());
loginFn = handlebars.compile($('#header-login-info-tpl').html());
cartFn = handlebars.compile($('#mini-cart-tpl').html());
centerFn = handlebars.compile($('#simple-account-info-tpl').html() || '');
loginFn = handlebars.compile($('#header-login-info-tpl').html() || '');
cartFn = handlebars.compile($('#mini-cart-tpl').html() || '');
// handlebars helper
handlebars.registerHelper('notzero', function(v1, options) {
... ...