Authored by hongweigao

品牌不发ajax一次处理

... ... @@ -38,15 +38,6 @@ exports.japanKorean = (req, res, next) => {
}).catch(next);
};
exports.getbrandFloorDataAjax = (req, res, next) => {
const channelType = req.query.channelType || 'boys',
contentCode = req.query.contentCode || '';
channelModel.getbrandFloorDataAjax(channelType, contentCode).then(data => {
res.json(data);
}).catch(next);
};
exports.getNewArrival = (req, res, next) => {
let reqBody = req.body,
pageIndex = reqBody.pageIndex,
... ...
/**
* 频道页 品牌logo helper
* @author: 高红威<hongwei.gao@yoho.cn>
* @date: 2016/12/9
*/
exports.brandLogoList = (brandLogos, showNum) => {
var out = '<ul>',
items = brandLogos.logoBrand,
moreBrand = brandLogos.moreBrand,
i = 0,
item = null,
fillNum = 0;
// 不是每页显示个数的倍数,填充数据
if (items.length % showNum !== 0) {
fillNum = showNum - (items.length % showNum);
for (i = 0; i < fillNum; i++) {
items.push({
href: 'javascript:;',
img: ''
});
}
}
for (i = 0; i < items.length; i++) {
// item = options.fn(items[i]);
item = items[i];
if (i % showNum === 5 || i === 5) {
// 插入切换按钮的位置
out = out + '<li class="logo-brand-switch" data-page="' + Math.floor(i / showNum) + '">' +
'<a class="prev iconfont" href="javascript:;">&#xe609;</a>' +
'<a class="next iconfont" href="javascript:;">&#xe608;</a></li>' +
'<li data-page="' + Math.floor(i / showNum) + '">' +
'<a href="' + item.href + '" target="_blank"><img class="lazy" data-original="' +
item.img + '" alt=""></a>' + '</li>';
} else if (i !== 0 && i % showNum === 0) {
// 插入more的位置,more占的是下一页第一个brand的位置,所以page是i/17
out = out + '<li class="brand-more" data-page="' + Math.floor(i / (showNum + 1)) + '">' +
'<a href="' + moreBrand + '" target="_blank">MORE ></a></li>' +
'<li data-page="' + Math.floor(i / showNum) + '">' +
'<a href="' + item.href + '" target="_blank"><img class="lazy" data-original="' +
item.img + '" alt=""></a>' + '</li>';
} else {
out = out + '<li data-page="' + Math.floor(i / showNum) + '">' +
'<a href="' + item.href + '" target="_blank"><img class="lazy" data-original="' +
item.img + '" alt=""></a>' + '</li>';
}
}
// 加上最后一个more, more占的是下一页第一个brand的位置,这里已经循环不到brand,所以加在末尾
return out + '<li class="brand-more" data-page="' + Math.floor(i / (showNum + 1)) + '">' +
'<a href="' + moreBrand + '" target="_blank">MORE ></a></li></ul>';
};
... ...
... ... @@ -24,7 +24,7 @@ app.use(global.yoho.hbs({
layoutsDir: doraemon,
partialsDir: [path.join(__dirname, 'views/partial')],
views: path.join(__dirname, 'views/action'),
helpers: global.yoho.helpers
helpers: Object.assign(require('./helper'), global.yoho.helpers)
}));
// router
... ...
... ... @@ -159,12 +159,13 @@ const _getSlideData = srcData => {
* @param {Object} srcData 原始数据
* @return {Object} 转换后的数据
*/
const _getNewReportFloorData = (args, type, code) => {
const _getNewReportFloorData = (args) => {
const title = args[0].data.text;
let item = args[1].data;
let secondItem = args[2].data;
let thirdItem = args[3].data;
let forthItem = args.length === 5 ? args[4] : {};
let forthItem = args[4] || {};
let brandLogos = {logoBrand: [], moreBrand: ''};
let list = [];
... ... @@ -199,8 +200,20 @@ const _getNewReportFloorData = (args, type, code) => {
// logo列表
if (forthItem.template_name && forthItem.template_name === 'floor') {
data.newReport.brandUrl = '/getbrandFloorDataAjax?channelType=' + type +
'&contentCode=' + code;
// 品牌数据
_.forEach(forthItem.data, (floorData) => {
let o = {};
o.href = floorData.url;
o.img = helpers.image(floorData.src, 185, 86, 2);
brandLogos.logoBrand.push(o);
});
if (args[5] && args[5].data[0] && args[5].data[0].url) {
brandLogos.moreBrand = args[5].data[0].url || '';
}
data.newReport.brandLogos = brandLogos;
}
return floorDatas;
};
... ... @@ -224,16 +237,32 @@ const _setChannelType = (obj, type) => {
*/
const _getPreBrandTopData = (args, type) => {
const title = args[0].data.text;
let item = args[1].data;
let item = args[1].data,
logoItems = args[2] || {},
brandLogos = {logoBrand: [], moreBrand: ''};
const data = {
preferenceBrands: {
name: title,
imgBrand: [],
brandUrl: '/getbrandFloorDataAjax?channelType=' + type
imgBrand: []
}
};
// logo列表
if (logoItems.template_name && logoItems.template_name === 'floor') {
// 品牌数据
_.forEach(args[2].data, (floorData) => {
let o = {};
o.href = floorData.url;
o.img = helpers.image(floorData.src, 185, 86, 2);
brandLogos.logoBrand.push(o);
});
if (args[3] && args[3].data[0] && args[3].data[0].url) {
brandLogos.moreBrand = args[3].data[0].url;
}
data.preferenceBrands.brandLogos = brandLogos;
}
data.preferenceBrands.imgBrand = item;
if (type === 'kids') {
data.preferenceBrands.sliderColor = '#7AD3F8';
... ... @@ -798,7 +827,7 @@ const _processJKFloorData = (rawData, type, code) => {
// 最新速报
if (text.indexOf('最新速报') >= 0) {
floorData = _getNewReportFloorData(rawData.slice(index, index + 5), type, code);
floorData = _getNewReportFloorData(rawData.slice(index, index + 6));
// 单个图作为最新速报模块用过的,标识一下,不作为广告位了
_.filter(rawData.slice(index, index + 5), function(o) {
... ... @@ -1170,49 +1199,6 @@ const getJKContent = (req) => {
});
};
// 品牌楼层floorData-ajax
const getbrandFloorDataAjax = (type, code) => {
let params = {new_device: 'Y'};
return _requestContent(type, params, code).then(res => {
let contentData = res.data ? res.data.list : [];
let data = {
logoBrand: [],
moreBrand: ''
};
_.forEach(contentData, (d, index) => {
let logoIndex = index;
// 频道页,有两处logo列表,一处优选品牌后面;一处最新速报后面,此处判断
if (d.data && d.data.text && d.data.text.indexOf('优选品牌') >= 0) {
logoIndex = index + 2;
} else if (d.data && d.data.text && d.data.text.indexOf('最新速报') >= 0) {
logoIndex = index + 4;
}
if (logoIndex !== index) {
_.forEach(contentData[logoIndex].data, (floorData) => {
let o = {};
o.href = floorData.url;
o.img = helpers.image(floorData.src, 185, 86, 2);
data.logoBrand.push(o);
});
data.moreBrand = contentData[logoIndex + 1].data[0].url;
return false;
}
});
return data;
});
};
const getResourceData = (formatData) => {
let promiseArr = [];
... ... @@ -1273,7 +1259,6 @@ module.exports = {
getNewArrival: getNewArrival,
getContent: getContent,
getJKContent: getJKContent,
getbrandFloorDataAjax: getbrandFloorDataAjax,
getIndexGuideData: getIndexGuideData,
formatIndexGuideData: formatIndexGuideData,
getResourceData: getResourceData,
... ...
... ... @@ -21,7 +21,6 @@ router.get('/japanKorean', channelController.japanKorean);
router.get('/channel/isNewUserAjax', channelController.hasNewUserFloor);
// ajax
router.get('/getbrandFloorDataAjax', channelController.getbrandFloorDataAjax);
router.post('/common/getNewArrival', channelController.getNewArrival);
router.get('/guide', channelController.getIndexGuide);
... ...
... ... @@ -18,5 +18,9 @@
</a>
</div>
</div>
<div class="logo-brand imgopacity" data-url="{{brandUrl}}"></div>
{{#if brandLogos}}
<div class="logo-brand imgopacity" data-showNum="16">
{{{brandLogoList brandLogos 16}}}
</div>
{{/if}}
</div>
... ...
... ... @@ -16,6 +16,9 @@
</ul>
</div>
</div>
<div class="logo-brand " data-url="{{brandUrl}}">
{{#if brandLogos}}
<div class="logo-brand" data-showNum="10">
{{{brandLogoList brandLogos 10}}}
</div>
{{/if}}
</div>
... ...
... ... @@ -27,7 +27,9 @@
</div>
{{/if}}
{{/ list}}
{{#if brandUrl}}
<div class="logo-brand" data-url="{{brandUrl}}"></div>
{{#if brandLogos}}
<div class="logo-brand" data-showNum="10">
{{{brandLogoList brandLogos 10}}}
</div>
{{/if}}
</div>
... ...
... ... @@ -6,9 +6,9 @@
var $ = require('yoho-jquery'),
lazyLoad = require('yoho-jquery-lazyload');
console.log($('.logo-brand'));
var homePage = $('.home-page').data('page'),
brandUrl = $('.logo-brand').data('url'),
shownum = $('.logo-brand').data('shownum'),
mChannel = $('.home-page').data('mchannel') || '';
var newArrParams = {};
... ... @@ -65,14 +65,13 @@ if (homePage === 'boys') {
if (homePage === 'boys') {
$('.logo-brand').logoBrand({
url: brandUrl
showNum: shownum
});
$('.img-brand').slider2({autoplay: true});
} else {
$('.logo-brand').logoBrand({
showNum: 10,
url: brandUrl
showNum: shownum
});
$('.img-slider-wrapper').slider2({autoplay: true});
}
... ...
... ... @@ -5,11 +5,11 @@
*/
var $ = require('yoho-jquery'),
Handlebars = require('yoho-handlebars'),
lazyLoad = require('yoho-jquery-lazyload');
(function() {
var LogoBrand = function(element, options) {
console.log(options);
this.$element = $(element);
this.options = $.extend({}, $.fn.logoBrand.defaults, options);
... ... @@ -19,81 +19,8 @@ var $ = require('yoho-jquery'),
LogoBrand.prototype = {
init: function() {
this.$element.addClass('logos-' + this.options.showNum);
this._sendRequest();
},
_createHelper: function() {
var showNum = this.options.showNum;
Handlebars.registerHelper('brandList', function(items, options) {
var out = '<ul>',
i = 0,
item = null,
fillNum = 0;
// 不是每页显示个数的倍数,填充数据
if (items.length % showNum !== 0) {
fillNum = showNum - (items.length % showNum);
for (i = 0; i < fillNum; i++) {
items.push({
href: 'javascript:;',
img: ''
});
}
}
for (i = 0; i < items.length; i++) {
item = options.fn(items[i]);
if (i % showNum === 5 || i === 5) {
// 插入切换按钮的位置
out = out + '<li class="logo-brand-switch" data-page="' + Math.floor(i / showNum) + '">' +
'<a class="prev iconfont" href="javascript:;">&#xe609;</a>' +
'<a class="next iconfont" href="javascript:;">&#xe608;</a></li>' +
'<li data-page="' + Math.floor(i / showNum) + '">' + item + '</li>';
} else if (i !== 0 && i % showNum === 0) {
// 插入more的位置,more占的是下一页第一个brand的位置,所以page是i/17
out = out + '<li class="brand-more" data-page="' + Math.floor(i / (showNum + 1)) + '">' +
'<a href="javascript:;" target="_blank">MORE ></a></li>' +
'<li data-page="' + Math.floor(i / showNum) + '">' + item + '</li>';
} else {
out = out + '<li data-page="' + Math.floor(i / showNum) + '">' + item + '</li>';
}
}
// 加上最后一个more, more占的是下一页第一个brand的位置,这里已经循环不到brand,所以加在末尾
return out + '<li class="brand-more" data-page="' + Math.floor(i / (showNum + 1)) + '">' +
'<a href="javascript:;" target="_blank">MORE ></a></li></ul>';
});
},
_sendRequest: function() {
var that = this;
$.ajax({
type: 'GET',
url: that.options.url,
dataType: 'json',
success: function(data) {
var brandTpl,
brandHtml;
if (!data || data.length === 0) {
return;
}
that._createHelper();
brandHtml = '\{{#brandList logoBrand}}' +
'<a href="\{{href}}" target="_blank"><img class="lazy" data-original="\{{img}}" alt=""></a>' +
'\{{/brandList}}';
brandTpl = Handlebars.compile(brandHtml);
that.$element.html(brandTpl(data));
that.$element.find('.brand-more a').attr('href', data.moreBrand);
lazyLoad(that.$element.find('img.lazy'));
that._bindEvent();
}
});
lazyLoad(this.$element.find('img.lazy'));
this._bindEvent();
},
_brandShow: function(hidePage, showPage) {
var that = this;
... ...