Utils.js 8.1 KB
'use strict';
import ReactNative from 'react-native';
import * as _ from 'lodash';

function isContains(str, substr) {
    return new RegExp(substr).test(str);
}


function randomString(len) {
  len = len || 32;
  var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';    /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
  var maxPos = $chars.length;
  var pwd = '';
  for (let i = 0; i < len; i++) {
    pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
  }
  return pwd;
}

function parsecategoryFilter(json) {
	let categoryFilterList = json.categoryFilterList;
	let filterCategoryDetailFilterList = json.filterCategoryDetailFilterList;

	for (var i = 0; i < categoryFilterList.length; i++) {
		let key = categoryFilterList[i].key;
		let type = 0;
		if (key == 'sort') {
			type = 0;
		}else if (key == 'brand'){
			type = 1;
		}else if (key == 'color'){
			type = 2;
		}else if (key == 'sizeKey'){
			type = 3;
		}else if (key == 'price'){
			type = 4;
		}else if (key == 'p_d'){
			type = 5;
		}else if (key == 'gender'){
			type = 7;
		}else if (key == 'style'){
			type = 10;
		}
		_.forEach(filterCategoryDetailFilterList, (v, k) => {
			if (k == key) {
				let list = v;
				for (var j = 0; j < list.length; j++) {
					if (list[j].isSelect) {
						categoryFilterList[i].select = list[j];
						categoryFilterList[i].subFilter = {list,key,type};
					}
				}
			}
		});
	}

	return{
		categoryFilterList,
	}
}

function parseProductList(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 filter = json && json.filter ? json.filter : {};
	let list = json && json.product_list ? json.product_list : [];
	let filterCategoryDetailFilterList = {};
	let categoryFilterList = [];

	if (filter['gender']) {
		categoryFilterList.push({
			key: 'gender',
			name: '性别',
			isSelect: true,
		});
		let genderList = filter['gender'];
		let newGenderList =[];
		newGenderList.push({
			key:'1,2,3',
			name: '全部',
			isSelect: true,
		});
		for (let gender in genderList) {
			if (genderList.hasOwnProperty(gender)) {
				newGenderList.push({
					key: gender,
					name: genderList[gender],
					isSelect: false,
				});
			}
		}
		filterCategoryDetailFilterList.gender;
		filterCategoryDetailFilterList.gender = newGenderList;
	}

	if (filter['group_sort']) {
		categoryFilterList.push({
			key: 'sort',
			name: '品类',
			isSelect: false,
		});
		let sortList = filter['group_sort'];
		let newSortList = [];
		newSortList.push({
			key: '',
			name: '所有品类',
			isSelect: true,
		});

		sortList.map((item, i) => {
			let subSortList = [];
			subSortList.push({
				key: item.relation_parameter.sort ? item.relation_parameter.sort : '',
				name: '所有'+item.category_name,
				isSelect: true,
			});

			let sub = item['sub'];
			sub.map((items, j) => {
				subSortList.push({
					key: items.relation_parameter.sort ? items.relation_parameter.sort : '',
					name: items.category_name,
					isSelect: false,
				})
			});

			newSortList.push({
				key: item.relation_parameter.sort ? item.relation_parameter.sort : '',
				name: item.category_name,
				isSelect: false,
				sub: subSortList,
			})
		});
		filterCategoryDetailFilterList.sort;
		filterCategoryDetailFilterList.sort = newSortList;
	}

	if (filter['brand']) {
		categoryFilterList.push({
			key: 'brand',
			name: '品牌',
			isSelect: false,
		});
		let brandList = filter['brand'];
		let newBrandList = [];
		newBrandList.push({
			key: '',
			name: '所有品牌',
			isSelect: true,
		});
		brandList.map((item, i) => {
			newBrandList.push({
				key: item.id,
				name: item.brand_name,
				isSelect: false,
				brand_alif : item.brand_alif,
			})
		});
		filterCategoryDetailFilterList.brand;
		filterCategoryDetailFilterList.brand = newBrandList;
	}

	if (filter['color']) {
		categoryFilterList.push({
			key: 'color',
			name: '颜色',
			isSelect: false,
		});
		let colorList = filter['color'];
		let newColorList = [];
		newColorList.push({
			key: '',
			name: '所有颜色',
			isSelect: true,
		});
		colorList.map((item, i) => {
			newColorList.push({
				key: item.color_id,
				name: item.color_name,
				isSelect: false,
			});
		});
		filterCategoryDetailFilterList.color;
		filterCategoryDetailFilterList.color = newColorList;
	}

	if (filter['size']) {
		categoryFilterList.push({
			key: 'sizeKey',
			name: '尺码',
			isSelect: false,
		});
		let sizeList = filter['size'];
		let newSizeList = [];
		newSizeList.push({
			key: '',
			name: '所有尺码',
			isSelect: true,
		});
		sizeList.map((item, i) => {
			newSizeList.push({
				key: item.size_id,
				name: item.size_name,
				isSelect: false,
			});
		});
		filterCategoryDetailFilterList.sizeKey;
		filterCategoryDetailFilterList.sizeKey = newSizeList;
	}

	if (filter['priceRange']) {
		categoryFilterList.push({
			key: 'price',
			name: '价格',
			isSelect: false,
		});
		let priceObject = filter['priceRange'];
		priceObject = sortListByField(priceObject, 'name'); // 折扣,价格区间,需要排序
		let newPriceList = [];
		newPriceList.push({
			key: '',
			name: '所有价格',
			isSelect: true,
		});
		_.forEach(priceObject, (v, k) => {
			newPriceList.push({
				key: v._key,
				name: v._value,
				isSelect: false,
			});
		});
		filterCategoryDetailFilterList.price;
		filterCategoryDetailFilterList.price = newPriceList;
	}

	if (filter['discount']) {
		categoryFilterList.push({
			key: 'p_d',
			name: '折扣',
			isSelect: false,
		});
		let p_dObject = filter['discount'];
		p_dObject = sortListByField(p_dObject, 'name'); // 折扣,价格区间,需要排序
		let newP_dList = [];
		newP_dList.push({
			key: '',
			name: '所有折扣',
			isSelect: true,
		});
		_.forEach(p_dObject, (v, k) => {
			newP_dList.push({
				key: v._key,
				name: v.name,
				isSelect: false,
			});
		});
		filterCategoryDetailFilterList.p_d;
		filterCategoryDetailFilterList.p_d = newP_dList;
	}

	let recId = randomString(40);

    let standard = json && json.standard ? json.standard : [];

    for (var i = 0; i < standard.length; i++) {
        let standardItem =  standard[i]
        standardItem.isSelect = false;
        standardItem.selectFilter = {
            standard_id: '',
            standard_name: '',
        }
    }

    let tabs = json && json.tabs ? json.tabs : {};
    let keys = Object.keys(tabs);
    keys = keys.sort();
    let latestTabs = [];
    for (var i = 0; i < keys.length; i++) {
        let tab = keys[i]
        let item = {
            type: tab,
            name: tabs[tab]
        };
        latestTabs.push(item);
    }
	return {
		list,
		categoryFilterList,
		filterCategoryDetailFilterList,
        standard,
		currentPage,
		pageCount,
		total,
		recId,
        latestTabs,
	};

}

function toArray(obj) {
    if (_.isArray(obj)) {
        return obj;
    }
    let arr = [];

    _.forEach(obj, (v, k) => {
        if (_.isObject(v)) {
            v._key = k;
        } else {
            v = {
                _key: k,
                _value: v
            };
        }
        arr.push(v);
    });

    return arr;
}

/**
 * 按照数组中指定字段排序二维数组
 *
 * @param  array list 需要排序的数组
 * @param  string key 字段名称
 * @param  boolean 有 desc 时候降序排列,默认为false
 */
function sortListByField(list, key, desc) {
    let array = toArray(list);

    array = array.sort((a, b) => {
        let matchNumber = /([\d]+)/g;

        // 有键,使用键的值排序
        if (a[key] && b[key]) {
            let numA = +(_.toArray(a[key].match(matchNumber))[0] || 0); // 取第一个出现的数字排序,如果不存在,取0
            let numB = +(_.toArray(b[key].match(matchNumber))[0] || 0);

            return (desc ? numA > numB : numA < numB) ? -1 : 1;
        }

        // 无键, 使用本身
        let numA = +(_.toArray(a._value.match(matchNumber))[0] || 0);
        let numB = +(_.toArray(b._value.match(matchNumber))[0] || 0);

        return numA < numB ? -1 : 1;
    });

    return array;
}

module.exports = {
	parseProductList,
	parsecategoryFilter,
    isContains,
};