product-process.js 15.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
'use strict';
const _ = require('lodash');

const camelCase = global.yoho.camelCase;
const helpers = global.yoho.helpers;

// const images = require(`${global.utils}/images`);

const itemFromBase = {
    search: {domain: 'search', module: 's'}, // 搜索页search.yohobuy.com
    list: {domain: 'list', module: 'c'}, // list.yohobuy.com
    listSale: {domain: 'list', module: 's'}, // list.yohobuy.com/sale
    listNew: {domain: 'list', module: 'n'}, // list.yohobuy.com/new
    brand: {domain: 'brand', module: 'b'}, // xxx.yohobuy.com[品牌域名]
    saleSpecial: {domain: 'sale', module: 's'}// sale.yohobuy.com
};

/**
 * 根据性别来决定  默认图片获取字段   如果是 2、3
 *
 * 则优先从cover2 --》 cover1 -- 》 images_url
 * 否则优先从cover1 --》 cover2 -- 》 images_url
 *
 */
const procProductImg = (product, gender, newSort) => {
    if (gender === '2,3') {
        return product.cover_2 || product.images_url || '';
    }

    if (newSort && gender === '1,2,3') {
        return product.images_url || '';
    }

    return product.cover_1 || product.images_url || '';
};

/**
 * 处理列表大图周边小图数据,拼接添加 href
 * @param origin goods 原始数据
 * @returns {{}} goods 输出数据,结构不变
 */
const handleGoodsListData = (origin) => {
    _.forEach(origin, (value, key) => {
        if (!_.isEmpty(value.goods_list)) {
            _.forEach(value.goods_list, (subValue, subKey) => {
                origin[key].goods_list[subKey].url =
                    helpers.getUrlBySkc(value.product_id, subValue.goods_id, value.cn_alphabet);
            });
        }
    });
    return origin;
};

/**
 * 处理搜索返回同一skc分男女封分别返回问题
 * @param array $images
 * @return string 商品图片
 */
const procGoodsList = (goodsList) => { // eslint-disable-line
    let list = {};

    if (!_.isEmpty(goodsList)) {
        let goods = _.cloneDeep(goodsList);

        _.forEach(goods, value => {
            if (!list[value.productSkc]) {
                list[value.productSkc] = value;
            } else {
                if (value.cover1) {
                    list[value.productSkc].cover1 = value.cover1;
                }
                if (value.cover2) {
                    list[value.productSkc].cover2 = value.cover2;
                }
            }
        });
    }

    return _.toArray(list);
};

/**
 * 处理
 *
 * 2、3: cover2 --> images_url  -> cover1
 * 1,3 :cover1 --> images_url  -> cover2
 * 其他: cover1 --> cover2 --> images_url
 *
 * @param array $images
 * @return string 商品图片
 */
const procProductImgs = (item, gender) => {
    let imgUrl = item.images_url ? item.images_url : '',
        cover1 = item.cover_1 ? item.cover_1 : '',
        cover2 = item.cover_2 ? item.cover_2 : '';

    if (gender === '2,3') {
        return cover2 ? cover2 : (imgUrl ? imgUrl : cover1);// eslint-disable-line
    } else if (gender === '1,3') {
        return cover1 ? cover1 : (imgUrl ? imgUrl : cover2);// eslint-disable-line
    } else {
        return cover1 ? cover1 : (cover2 ? cover2 : imgUrl);// eslint-disable-line
    }
};

const procCnAlphabetName = (nameStr) => {
    return nameStr.match(/\w/ig).join('');
};

/**
 * 商品搜索商品数据处理
 */
exports.processProductList = (list, options) => {
    const pruductList = [];
    let itemNum = 0,
        itemFrom;

    options = Object.assign({
        showTags: true,
        showNew: true,
        showSale: true,
        showFew: true,
        showLimit: true,
        showDiscount: true, // 显示折扣
        newCoverSort: false, // 新封面排序
        width: 290,
        height: 388,
        isApp: false,
        showPoint: true,
        gender: '2,3',
        from: {} // 来源
    }, options);

    // 处理item from
    if (!_.isEmpty(options.from) && itemFromBase[options.from.type]) {
        let f = {domain: '', module: '', key: ''};
        let params = options.from.params || {};

        f.page = params.page || 1;

        Object.assign(f, itemFromBase[options.from.type], {
            page: params.page || 1,
            key: params.misort || params.msort || ''
        });

        if (options.from.type === 'search') {
            f.key = params.query || '';
        }

        itemFrom = `from=${f.domain}-${f.module}-${f.key}_${f.page}_`;
    }

    _.forEach(list, (product) => {

        // 商品信息有问题,则不显示
        if (!product || !product.product_skn || !_.get(product, 'goods_list.length', 0)) {
            return;
        }

        // 市场价和售价一样,则不显示市场价, 不显示折扣信息
        if (product.market_price === product.sales_price) {
            delete product.market_price;
        } else if (options.showDiscount) {
            product.discount = (product.sales_price / product.market_price * 10).toFixed(1);
        }

        // 判别默认的商品是否将默认的图片URL赋值到skn
        let flag = false;

        _.remove(product.goods_list, function(n) {
            return !+n.status;
        });

        // skc图片排序
        product.goods_list = _.orderBy(product.goods_list, ['is_default'], ['desc']);

        if (!product.default_images) {
            // 如果设置了默认图片,就取默认的图片
            _.forEach(product.goods_list, (goods) => {
                if (flag) {
                    return;
                }
                if (goods.is_default === 'Y') {
                    product.default_images = procProductImg(goods);
                    flag = true;
                }
            });

            // 如果还未赋值,则取第一个skc产品的默认图片
            if (!flag) {
                product.default_images = procProductImg(product.goods_list[0]);
            }
        }
        product = Object.assign(product, {
            id: product.product_skn,
            thumb: product.default_images
        });

        product.cn_alphabet = procCnAlphabetName(product.cn_alphabet);

        product.is_few = product.is_soon_sold_out === 'Y';
        product.url = helpers.getUrlBySkc(product.product_id, _.get(product, 'goods_list[0].goods_id'), product.cn_alphabet);// eslint-disable-line

        // tar add 1606071146 品牌链接处理
        product.brandUrl = helpers.urlFormat('', '', product.brand_domain);

        // APP访问需要加附加的参数
        // 备注:如果以后APP的接口太多,可以把这边参数提取出来,变成一个公共的方法来生成,便于以后管理维护
        if (options.isApp) {
            product.url += `?openby:yohobuy={"action":"go.productDetail","params":{"product_skn":'${product.product_id}'}}`; // eslint-disable-line
        } else if (itemFrom) {
            // 累加商品数量
            itemNum++;
            product.url += `?${itemFrom}${itemNum}`;
        }
        if (options.showTags) {
            let tags = [],
                isfew = false;

            _.get(product, 'tags', []).forEach((value) => {
                let tag = {};

                switch (value) {
                    case 'is_soon_sold_out': // 即将售磬
                        options.showFew && (tag.is_few = true, isfew = true);
                        break;
                    case 'is_solded': // 已售磬
                        product.is_solded = true;
                        break;
                    case 'is_new': // 新品NEW
                        options.showNew && (tag.is_new = true);
                        break;
                    case 'is_discount': // SALE
                        options.showSale && (tag.is_sale = true);
                        break;
                    case 'is_limited': // 限量
                        options.showLimit && (tag.is_limit = true);
                        break;
                    case 'is_yohood': // YOHOOD
                        tag.is_new_festival = true;
                        break;
                    case 'is_advance': // 再到着
                        tag.is_re_new = true;
                        break;
                    case 'midYear':// 年中热促
                        tag.is_year_mid_promotion = true;
                        break;
                    case 'yearEnd':// 年终大促
                        tag.is_year_end_promotion = true;
                        break;
                    case 'is_presell':// 预售
                        tag.is_presell = true;
                        break;
                    default:
                        break;
                }
                tags.push(tag);
            });

            product.tags = tags;
            isfew ? product.is_few = isfew : delete product.is_few;
        }
        if (options.query && _.isString(product.product_name)) {
            let qreg = new RegExp(options.query, 'ig');

            product.product_name = product.product_name.replace(qreg, '<span style="color:#c00;">$&</span>');
        }
        pruductList.push(product);
    });
    return handleGoodsListData(pruductList);
};


/**
 * 处理筛选数据
 * @param list
 * @param  string | options
 * @return array 处理之后的筛选数据
 */
exports.processFilter = (list, options) => {
    const filters = {
        classify: []
    };

    const filtersType = {
        brand: {
            name: '所有品牌',
            title: '品牌',
            dataId: 'id',
            subsName: 'brandName',
            firstSub: 0,
            dataType: 'brand'
        },
        color: {
            name: '所有颜色',
            title: '颜色',
            dataId: 'colorId',
            subsName: 'colorName',
            firstSub: 0,
            dataType: 'color'
        },
        discount: {
            name: '所有商品',
            title: '折扣',
            dataId: 'key',
            subsName: 'name',
            firstSub: '0.1,0.9',
            dataType: 'p_d'
        },
        gender: {
            name: '所有性别',
            title: '性别',
            dataId: 'key',
            subsName: 'flag',
            firstSub: '1,2,3',
            dataType: 'gender'
        },
        groupSort: {
            name: '所有品类',
            title: '品类',
            dataId: 'relationParameter',
            subsName: 'categoryName',
            firstSub: 0,
            dataType: 'sort'
        },
        priceRange: {
            name: '所有价格',
            title: '价格',
            dataId: 'key',
            subsName: 'flag',
            firstSub: 0,
            dataType: 'price'
        },
        size: {
            name: '所有尺码',
            title: '尺码',
            dataId: 'sizeId',
            subsName: 'sizeName',
            firstSub: 0,
            dataType: 'size'
        }
    };

    options = Object.assign({
        gender: '1,2,3', // 默认选择的性别,默认1,2,3表示所有
        exclude: null // 需要排除的字段
    }, options);
    list = camelCase(list);

    _.forEach(list, (item, key) => {
        let classify = {
            subs: []
        };

        if (key === 'group_sort') {
            return;
        }

        if ((options.hideSize && key === 'size') || (options.hideSort && key === 'groupSort')) {
            return;
        }

        classify.dataType = filtersType[key].dataType;
        classify.name = filtersType[key].name;
        classify.title = filtersType[key].title;

        classify.subs.push({
            chosed: true,
            dataId: filtersType[key].firstSub,
            name: filtersType[key].name
        });

        _.forEach(item, (sub, index) => {
            let subs = {};

            if (filtersType[key].dataId === 'key') {
                subs.dataId = index;
            } else if (filtersType[key].dataId === 'relationParameter') {
                subs.dataId = sub.relationParameter['sort']; // eslint-disable-line
            } else {
                subs.dataId = sub[filtersType[key].dataId];
            }

            if (filtersType[key].subsName === 'flag') {
                subs.name = sub;
            } else {
                subs.name = sub[filtersType[key].subsName];

                if (key === 'discount') {
                    subs.name = subs.name + '折商品';
                }
            }
            classify.subs.push(subs);
        });

        filters.classify.push(classify);
    });

    return filters;
};

// 处理单个商品
exports.processProduct = (productData, options) => {
    let result = {},
        flag = false;

    options = Object.assign({
        showTags: true,
        showNew: true,
        showSale: true,
        width: 290,
        height: 388,
        showPoint: true
    }, options);

    // 商品信息有问题,则不显示
    if (!productData.product_skn || !productData.goods_list[0]) {
        return false;
    }

    // 市场价和售价一样,则不显示市场价
    if (parseInt(productData.market_price, 0) === parseInt(productData.sales_price, 0)) {
        productData.market_price = false;
    }

    // 设置默认图片
    _.forEach(productData.goods_list, item => {
        if (item.is_default === 'Y') {
            productData.default_images = procProductImgs(item, options.gender);
            flag = true;
        }
    });

    if (!flag) {
        productData.default_images = procProductImgs(productData.goods_list[0], options.gender);
    }

    result.id = productData.product_skn;
    result.product_id = productData.product_id;
    result.thumb = helpers.image(productData.default_images, options.width, options.height);
    result.name = productData.product_name;
    result.price = !productData.market_price ? false : productData.market_price;
    result.salePrice = productData.sales_price;
    if (options.showPoint) {
        result.price += '.00';
        result.salePrice += '.00';
    }

    result.is_soon_sold_out = (productData.is_soon_sold_out === 'Y');
    result.url = helpers.getUrlBySkc(productData.product_id, _.get(productData, "goods_list[0].goods_id"), productData.cn_alphabet);// eslint-disable-line

    if (options.showTags) {
        result.tags = {};
        result.tags.is_new = options.showNew && productData.is_new && productData.is_new === 'Y'; // 新品
        result.tags.is_discount = options.showSale && productData.is_discount && productData.is_discount === 'Y'; // 在售
        result.tags.is_limited = productData.is_limited && productData.is_limited === 'Y'; // 限量
        result.tags.is_yohood = productData.is_yohood && productData.is_yohood === 'Y'; // YOHOOD
        result.tags.midYear = productData['mid-year'] && productData['mid-year'] === 'Y'; // 年中
        result.tags.yearEnd = productData['year-end'] && productData['year-end'] === 'Y'; // 年末
        result.tags.is_advance = productData.is_advance && productData.is_advance === 'Y'; // 再到着

        if (result.is_soon_sold_out && result.tags.is_discount) {
            result.tags.is_new = false;// 打折与即将售完组合显示打折
        } else if (result.tags.is_discount &&
            (result.tags.is_new || result.tags.is_limited || result.tags.is_yohood || result.tags.is_advance)) {
            result.tags.is_discount = false;// 打折与其它组合则隐藏打折
        } else if (result.tags.is_yohood && result.tags.is_new) {
            result.tags.is_new = false;// YOHOOD和新品组合显示YOHOOD
        }
    }
    return result;
};