floorParser.js 11 KB
'use strict';

import ReactNative from 'react-native';

const {
    Dimensions,
} = ReactNative;

function calcluateFloorHeight(payload) {
	if (!payload) {
		return {};
	}

	let allFloorHeight = 0;

	if (!payload.list) {
		payload.floorHeight = allFloorHeight;
		return payload;
	}

	let i = 0;
	let count = payload.list.length;
	for(i; i < count; i++) {
		let item = payload.list[i];
		if (!item) {
			continue;
		}

		let templateName = item.template_name;
		let data = item.data;
		if (!templateName || !data) {
			continue;
		}

		const {width, height} = Dimensions.get('window');
		const DEVICE_WIDTH_RATIO = width / 320;
		const DEVICE_WIDTH_RATIO_6 = width / 375;

		let floorHeight = 0;
		let headerHeight = 40;
		switch(templateName) {
			case 'focus': {
				floorHeight = Math.ceil(155 * DEVICE_WIDTH_RATIO);
				break;
			}
			case 'app_icon_list': {
				let count = parseInt(item.number);
				count = count > 0 ? count : 5;
		        let tail = (data.length % count != 0) ? 1 : 0;
		        let lineNumber = data.length/count + tail;

		        let padding5 = (width - 42*5)/12;
				let padding4 = (width - 51*4)/10;
		        let padding = count == 4 ? padding4 : padding5;
				floorHeight = (count == 4 ? 84 : 75) * lineNumber +  padding * (lineNumber - 1) + 12;
				break;
			}
			case 'app_hot_brands': {
				let listLength = data.list.length;
				let lineNumber = parseInt((listLength + 3) / 4);
		        if (listLength % 4 == 0) {
		            lineNumber += 1;
		        }
		        let imageHeight = Math.floor(87 * DEVICE_WIDTH_RATIO);
		        let listHeight = Math.ceil(lineNumber * imageHeight);
		        floorHeight = headerHeight + listHeight;
				break;
            }
            case 'announcement': {
                floorHeight = Math.ceil(35 * DEVICE_WIDTH_RATIO);
				break;
            }
            case 'single_image': {
                floorHeight = Math.ceil(100 * DEVICE_WIDTH_RATIO);
				break;
            }
            case 'single_name_image': {
                floorHeight = headerHeight + Math.ceil(100 * DEVICE_WIDTH_RATIO);
				break;
            }
            case 'recommend_content_one': {
            	let bannerHeight = Math.ceil(200 * DEVICE_WIDTH_RATIO);
				let listHeight = Math.ceil(190 * DEVICE_WIDTH_RATIO);
				floorHeight = headerHeight + bannerHeight + listHeight;
				break;
            }
            case 'recommend_content_two': {
                let bannerHeight = Math.ceil(200 * DEVICE_WIDTH_RATIO);
				let listHeight = Math.ceil(190 * DEVICE_WIDTH_RATIO);
				floorHeight = headerHeight + bannerHeight + listHeight;
				break;
            }
            case 'recommend_content_three': {
            	let imageHeight = Math.floor(87 * DEVICE_WIDTH_RATIO);
            	let lineNumber = parseInt((data.list.length + 3) / 4);
		        let listHeight = Math.floor(lineNumber * imageHeight);
		        let bannerHeight = Math.ceil(100 * DEVICE_WIDTH_RATIO);
		        floorHeight = bannerHeight + listHeight
				break;
            }
            case 'recommend_content_five': {
            	let imageHeight = Math.floor(87 * DEVICE_WIDTH_RATIO);
            	let lineNumber = parseInt((data.list.length + 3) / 4);
	        	let listHeight = Math.floor(lineNumber * imageHeight);
	        	floorHeight = listHeight + headerHeight;
				break;
            }
            case 'trendsetter_collocation': {
            	let topCellWidth = (width - 45) / 2;
            	let cellWidth =  Math.floor(82 * width / 375);
				floorHeight = headerHeight + topCellWidth + 15 * 2 + headerHeight + cellWidth;
				break;
            }
            case 'trendgoods_topic': {
            	let imageHeight = Math.ceil(182 * DEVICE_WIDTH_RATIO);
				let pageHeight = imageHeight + 15 + 16 + 9 + 12 + 30;
				floorHeight = headerHeight + 15 + pageHeight;
				break;
            }
            case 'custom_brands': {
            	let imageHeight = Math.ceil(55 * DEVICE_WIDTH_RATIO);
				let marginTop = 15;
				floorHeight = headerHeight + imageHeight + marginTop * 3;
				break;
            }
            case 'kids_brands':{
            	let itemWidth = width / 4 - 0.5;
				let itemHeight = itemWidth + 20;
				floorHeight = headerHeight + itemHeight * 2 + 0.5;
				break;
            }
            case 'small_pic': {
                floorHeight = Math.ceil(80 * (width - 45) / 2 / 137.5);
				break;
            }
            case 'divideImage': {
                floorHeight = 15;
				break;
            }
            case 'NewProductFloor': {
            	let leftBigImageWidth = width * 315 / 750;
				let leftBigImageHeight = leftBigImageWidth * 420 / 315;
				floorHeight = headerHeight + leftBigImageHeight;
				break;
            }
            case 'popular_single_product': {
            	let bannerHeight = Math.ceil(width * 234 / 750);
				floorHeight = headerHeight + bannerHeight + 192;
				break;
            }
            case 'shopRecommend': {
            	let storeHeight = Math.ceil(239 * DEVICE_WIDTH_RATIO_6 + 10);
				floorHeight = headerHeight + storeHeight;
				break;
            }
            case 'sixLinesFloor': {
            	let imageWidth = Math.floor(width / 2);
				let imageHeight = Math.floor(imageWidth * 180 / 375);
                let lineNumber = Math.floor((data.list.length + 1)/2);
        		floorHeight = headerHeight + lineNumber * imageHeight;
				break;
            }
            case 'livePicture': {
            	let imageWidth = Math.ceil((width - 64) / 3);
				let imageHeight = Math.ceil(imageWidth * 86 / 64);
                floorHeight = headerHeight + imageHeight;
				break;
            }
            case 'sale-1t-1l-4r': {
            	let bannerHeight = Math.floor(width / 3);
            	let leftBigImageWidth = width * 310 / 740;
				let leftBigImageHeight = leftBigImageWidth * 422 / 310;
				let containerHeight = headerHeight + leftBigImageHeight;
		        if(data.big_image.length > 0){
		            containerHeight = containerHeight + bannerHeight;
		        }
                floorHeight = containerHeight;
				break;
            }
            case 'vipUse': {
            	let bannerHeight = Math.ceil((234 / 750) * width);
				let cellHeight = 210;
                floorHeight = headerHeight + bannerHeight + cellHeight;
				break;
            }
            case 'activityProductFloor': {
            	//1 VIP 专享 2 新人专享 3 学生专享 4 新品到着
				const TYPE_VIP = 1;
				const TYPE_NEW_USER = 2;
				const TYPE_STUDENT = 3;
				const TYPE_NEW_PRODUCT = 4;

				let type = item.type;

            	let bannerHeight = Math.ceil((234 / 750) * width);
				let sectionHeight = Math.ceil((419 / 750) * width);
				let cellWidth = 90;
				let cellHeight = 210;
				let cellH = 0;
				if (type == TYPE_VIP || type == TYPE_STUDENT) {
					cellH = cellHeight;
				} else {
					cellH = cellHeight - 20;
				}
        		floorHeight = cellH + headerHeight + bannerHeight;
				break;
            }
            case 'hot_category_individualization': {
            	let cellWidth = width / 4;
				let imageWidth = cellWidth - 14 * 2;
				let imageHeight = imageWidth * 168 / 126;
				let cellHeight = imageHeight + 20;
		        let lineNumber = parseInt((data.list.length + 3) / 4);
		        let listHeight = Math.floor(lineNumber * cellHeight);
		        let containerHeight = listHeight + headerHeight;
                floorHeight = containerHeight;
				break;
            }
            case 'new_user_floor': {
            	let leftBigImageWidth = width * 315 / 750;
				let leftBigImageHeight = leftBigImageWidth * 420 / 315;
				let containerHeight = headerHeight + leftBigImageHeight;
                floorHeight = containerHeight;
				break;
            }
		}

		item.floorHeight = floorHeight;
		allFloorHeight = allFloorHeight + floorHeight;
	}

	payload.floorHeight = allFloorHeight;
	return payload;
}

function homeDataParse(data, channel) {
	let {list, cached, isFetching, isFirstLoad, endReached, favorite, bottomBanner, hotList, newList, selectIndex} = data;
	let floorList = list.size > 0 ? list.toArray() : cached.get('list').toArray();
	let dataSource = {};
	if (channel == '4') {
		let favlist = selectIndex == 0 ? newList : hotList;
		if (endReached) {
			dataSource = {
				floor: floorList,
				lifeStyleFav:favlist.toArray(),
				footer: ['1']
			}
		}else {
			dataSource = {
				floor: floorList,
			}
		}
	} else {
		let favoriteList = favorite.get('list');
		let bottomBannerList = bottomBanner.get('list');
		if (favoriteList.size == 0) {
			dataSource = {
			   floor: floorList,
			}
		}else {
			if (favoriteList.size > 0 && endReached) {
				dataSource = {
				   floor: floorList,
				   favoriteHeader: ['1'],
				   favorite: favoriteList.toArray(),
				   bottomBanner: [bottomBannerList],
				   footer: ['1']
				}
				if (channel == 3) {
					dataSource = {
					   floor: floorList,
					   favoriteHeader: ['1'],
					   favorite: favoriteList.toArray(),
					   footer: ['1']
					}
				}
			}else {
				dataSource = {
				   floor: floorList,
				   favoriteHeader: ['1'],
				   favorite: favoriteList.toArray()
				}
			}
		}
	}

	return dataSource;
}

function channelCacheKey(channelKey) {
	return 'YH_RNCacheTypeHomeChannel' + channelKey;
}

function parseHomeFloor(json) {
	let data = json && json.data ? json.data : {};
	let md5 = json && json.md5 ? json.md5 : '';
	let list = data && data.list ? data.list : [];
	let content_code = data && data.content_code ? data.content_code : '';
	return {
		md5,
		list,
		content_code,
	};
}

function parseBoyGirlFavorite(json) {
	let currentPage = json && json.page ? json.page : 1;
	let pageCount = json && json.page_total ? json.page_total : 0;
	let total = json && json.total ? json.total : 0;

	let list = json && json.product_list ? json.product_list : [];

	return {
		currentPage,
		pageCount,
		total,
		list
	}
}

function parseKidsFavorite(json) {
	let currentPage = json && json.page ? json.page : 1;
	let pageCount = json && json.page_total ? json.page_total : 0;
	let total = json && json.total ? json.total : 0;

	let list = json && json.product_list ? json.product_list : [];

	return {
		currentPage,
		pageCount,
		total,
		list
	}
}

function parseLifeStyleFavorite(json) {
	let product_list = json && json.product_list ? json.product_list : {};
	let newList = product_list && product_list.new ? product_list.new : [];
	let hotList = product_list && product_list.top ? product_list.top : [];
	return {
		newList,
		hotList
	}
}

function parseBottomBanner(json) {
	let banner = json && json[0] ? json[0] : {};
	let list =  banner.data;
	return {
		list
	}
}

function parseShopInfo(json) {
	let payload = {};
	for (var i = 0; i < json.length; i++) {
		let shopItem = json[i];
		let shopId = shopItem.id;
		payload[shopId] = shopItem;
	}
	return payload;
}

function parseRecommendPop(json) {
	let product_list = json && json.product_list ? json.product_list : [];
	let rec_id = json && json.rec_id ? json.rec_id : '';
	return {
		list: product_list,
		rec_id,
	}
}

module.exports = {
	calcluateFloorHeight,
	homeDataParse,
	channelCacheKey,
    parseHomeFloor,
   	parseBoyGirlFavorite,
   	parseKidsFavorite,
   	parseLifeStyleFavorite,
   	parseBottomBanner,
   	parseShopInfo,
	parseRecommendPop
};