search.js 16 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 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
/**
 * search
 * @Author: sefon 2016-7-12 16:31:56
 */

'use strict';
const utils = '../../../utils';
const api = global.yoho.API;

const Promise = require('bluebird');
const co = Promise.coroutine;
const helpers = global.yoho.helpers;
const redis = global.yoho.redis;

const SaleApiModel = require('./sale-api');
const SearchApiModel = require('./search-api');

const headerModel = require('../../../doraemon/models/header');
const productProcess = require(`${utils}/product-process-simple`);
const searchHandler = require('./search-handler');
const hotBrandsModel = require('../../../doraemon/models/hot-brands');
const _ = require('lodash');
const Fn = require('lodash/fp');
const md5 = require('md5');
const cache = global.yoho.cache;
const config = global.yoho.config;
const logger = global.yoho.logger;

const needParams = ['query', 'msort', 'misort'];

const CACHE_TIME_S = 60;

function _getCacheKey(params, page) {
    let sortByKey = Fn.pipe(Fn.toPairs, Fn.sortBy(0), Fn.flatten);
    let genKey = Fn.pipe(Fn.cloneDeep, sortByKey, Fn.join('_'));

    return `render_cache_${page}_${md5(genKey(params))}`;
}

function getKeyActivity(query) {
    return this.searchApi.getKeyActivityAsync(query).then(result => {
        return _.get(result, 'data.urlobj.pcUrl', '');
    });
}

const _setSearchData = (result, params, channel) => {
    let changeQuery = Object.assign({}, params);
    let finalResult = {
        headerData: Object.assign(result[0].headerData, {
            header: true
        }),
        search: {}
    };

    // 获取左侧类目数据
    if (result[1].code === 200) {
        let dps = {};

        _.forEach(needParams, (value) => {
            if (params[value]) {
                dps[value] = params[value];
            }
        });

        finalResult.search = {
            leftContent: searchHandler.handleSortData(result[1].data, dps, params),
            isSearch: true
        };
    }

    // 获取商品数据和顶部筛选条件
    if (result[2].code === 200) {

        // 搜索推荐词,获取对应的参数值
        let termsSuggestion = _.get(result[2], 'data.suggestion.terms_suggestion', []);

        // 推荐词 获取无商品,首次搜索,通过isChangedQuery做开关
        if (result[2].data.isChangedQuery === 'Y') {
            Object.assign(changeQuery, {query: termsSuggestion[0]});
        }

        let filters = Object.assign(searchHandler.handleFilterDataAll(result[2].data, changeQuery),
            _.get(finalResult, 'search.leftContent.sort', {}));

        filters.checkedConditions.conditions = _.concat(filters.checkedConditions.conditions,
            _.get(finalResult, 'search.leftContent.checked', []));

        Object.assign(finalResult.search,
            searchHandler.handlePathNavData({total: result[2].data.total}, params, 'search', channel),
            {
                filters: filters,
                opts: searchHandler.handleOptsData(changeQuery, result[2].data.total, result[2].data.filter),
                totalCount: result[2].data.total,
                footPager: searchHandler.handlePagerData(result[2].data.total, changeQuery),
                goods: productProcess.processProductList(result[2].data.product_list,
                    Object.assign({showDiscount: false, from: {type: 'search', params: params}}, params)),
                latestWalk: 6,
                hasNextPage: searchHandler.handleNextPage(changeQuery, result[2].data.total),
                shopEntry: result[3],
                name: params.query,
                suggest: (termsSuggestion.length !== 0) ? termsSuggestion.slice(0, 3) : false,
                changedQuery: result[2].data.isChangedQuery === 'Y',
                suggestFirst: finalResult.search.suggestFirst,
                suggestSecond: finalResult.search.suggestSecond
            });

        if (finalResult.search.changedQuery) {

            finalResult.search.suggestFirst = finalResult.search.suggest[0];
            finalResult.search.suggestSecond = _.slice(termsSuggestion, 1, termsSuggestion.length).splice(0, 3);
        }

        finalResult.criteo = {skn: searchHandler.getCriteo(_.get(finalResult.search, 'goods'))};
    }
    return finalResult;
};

/**
 * 转换价格
 *
 * @param float|string $price 价格
 * @return float|string 转换之后的价格
 */
const _transPrice = (price) => {
    return price ? (price * 1).toFixed(2) : '0.00';
};

/**
 * 获取搜索数据
 * @param  {[type]} origin [description]
 * @return {[type]}        [description]
 */
function getSearchData(params, channel) {
    let searchParams = searchHandler.getSearchParams(params);

    switch (channel) {
        case 'boys':
            searchParams.physical_channel = 1;
            break;
        case 'girls':
            searchParams.physical_channel = 2;
            break;
        case 'kids':
            searchParams.physical_channel = 3;
            break;
        case 'lifestyle':
            searchParams.physical_channel = 4;
            break;
        default:
            break;
    }

    // 调用接口
    let apiMethod = [
        headerModel.requestHeaderData(channel),
        this.searchApi.getSortList(Object.assign({}, searchParams, {msort: '', misort: '', sort: ''})),
        this.searchApi.getProductList(searchParams, 'fuzzySearch'),
        this.searchApi.getShopList(params)
    ];

    return api.all(apiMethod).then(result => {
        if (_.get(result, '[2].data.isChangedQuery', '') === 'Y') {
            return this.searchApi.getSortList(Object.assign({}, searchParams, {
                msort: '',
                query: _.get(result, '[2]data.suggestion.terms_suggestion[0]', ''),
                misort: '', sort: ''
            })).then(subRes => {
                result[1] = subRes;

                return _setSearchData(result, params, channel);
            });
        }

        return _setSearchData(result, params, channel);
    });
}

function getSearchDataPre(params, channel) {
    let cKey = _getCacheKey(Object.assign({channel: channel}, params), 'search');
    let getOriginData = () => {
        return this.getSearchData(params, channel).then(result => {

            // 查询结果为空则不cache
            if (config.useCache && !_.isEmpty(_.get(result, 'search.goods', []))) {
                cache.set(cKey, result, CACHE_TIME_S)
                    .catch(err => logger.debug(`search render cache data save fail:${err.toString()}`));
            }

            return result;
        });
    };

    if (config.useCache) {
        return cache.get(cKey).catch(err => {
            logger.debug(`get search render cache data fail:${err.toString()}`);

            return getOriginData();
        }).then(cdata => {
            let hasCache = false;

            if (cdata) {
                try {
                    cdata = JSON.parse(cdata);
                    hasCache = true;
                } catch (e) {
                    logger.debug('search render cache data parse fail.');
                }
            }

            if (hasCache) {
                return cdata;
            } else {
                return getOriginData();
            }
        });
    }

    return getOriginData();
}

/**
 * 搜索提示
 */
function getSuggest(params) {
    return this.searchApi.getSuggest(params).then(result => {
        let dest = [];

        if (result.code === 200) {
            dest = searchHandler.handleSuggest(result.data);
        }

        return dest;
    });
}

function getListBrandsFilter(params, nparams) {
    return this.searchApi.getBrands4Filter(Object.assign({}, params, nparams)).then(result => {
        let dest = [];

        if (result.code === 200) {
            dest = searchHandler.handleFilterBrands(result.data, params);
        }

        return dest;
    });
}

function getBrands4Filter(params) {

    let nparams = {};

    if (params && params.price) {
        let mp = params.price.split(',');
        let nmp = [];

        nmp[0] = (mp && mp[0]) || 0;
        nmp[1] = (mp && mp[1]) || 99999;
        nparams.price = nmp.join(',');
    }

    if (params && params.from) {
        nparams.from = params.from;
        delete params.from;
    }

    // 生成路径包含callback 导致多次调用多个 callback问题
    if (params && params.callback) {
        delete params.callback;
    }

    const specialId = parseInt(`0${params.specialsale_id || ''}`, 10);

    if (specialId) {
        return this.saleApi.getSaleSpecialAsync(specialId).then(special => {
            if (special && special.data) {
                let data = special.data;

                // 传品牌ID参数
                if (data.brand_id) {
                    nparams.brand = data.brand_id;
                }

                // 传促销id,促销id为空时传专区id
                if (data.ispromotion) {
                    nparams.promotion = data.ispromotion;
                }
            }

            return this.getListBrandsFilter(params, nparams);
        });
    }

    // 商品详情页促销跳转
    if (params && params.psp_id) {
        Object.assign(nparams, {
            promotion_id: params.psp_id,
            method: 'app.promotion.aggBrand'
        });
    }

    // 优惠券商品列表
    if (params && params.cpc_id && params.coupon_code) {
        Object.assign(nparams, {
            coupon_id: params.cpc_id,
            coupon_code: params.coupon_code,
            method: 'app.coupon.aggBrand'
        });
    }

    return this.getListBrandsFilter(params, nparams);
}


/**
 * 搜索少或无 可能喜欢
 * @param int $page 分页页码

 * @return array

 * */
function getSearchLessProduct(channelNum, uid, udid, page) {
    return co(function* () {

        let result = {

            code: 200,
            message: '',
            data: {

                header: '可能喜欢的商品',
                hasPrev: false,
                hasNext: false,
                item: null

            }
        };

        if (!_.isNumber(page)) {
            return result;
        }

        let together = yield this.searchApi.lessRecommend(channelNum, uid, udid, '100003', 20);

        if (page > 5) {
            page = 1;
        }

        if (!_.get(together, 'data.product_list')) {
            return result;
        }

        result.data.hasPrev = true;
        result.data.hasNext = true;

        result.data.item = _.map(together.data.product_list, it => {

            let item = {
                id: it.product_id,
                skn: it.product_skn,
                title: it.product_name,
                img: it.default_images ? helpers.image(it.default_images, 180, 240) : '',
                alt: it.product_name,
                price: '¥' + _transPrice(it.sales_price),
                marketPrice: _transPrice(it.market_price) <= _transPrice(it.sales_price) ?
                    false : ('¥' + _transPrice(it.market_price))
            };

            item.href = helpers.getUrlBySkc(it.product_skn);

            return item;

        });


        // 当数据量不足6个时,判定为没有下一页

        if (page === 1) {
            result.data.hasPrev = false;
        }

        if (!result.data.item || result.data.item.length < 6) {
            result.data.hasNext = false;
        }

        return result;

    }).bind(this)();

}

const _setSearchKeywordData = (result, params, channel) => {
    let changeQuery = Object.assign({}, params);
    let finalResult = {
        headerData: Object.assign(result[0].headerData, {
            header: true
        }),
        search: {}
    };

    _.unset(changeQuery, 'query');

    // 获取左侧类目数据
    if (result[1].code === 200) {

        finalResult.search = {
            leftContent: searchHandler.handleSuggestData(_.get(result, '[1].data', []), params.query),
            isSearch: true
        };
    }

    // 获取商品数据和顶部筛选条件
    if (result[2].code === 200) {
        Object.assign(finalResult.search,
            searchHandler.handlePathNavData({total: result[2].data.total}, params, 'search', channel),
            {
                opts: searchHandler.handleOptsData(changeQuery, result[2].data.total),
                totalCount: result[2].data.total,
                footPager: searchHandler.handlePagerData(result[2].data.total, changeQuery),
                goods: productProcess.processProductList(result[2].data.product_list,
                    Object.assign({showDiscount: false, from: {type: 'search', params: params}}, params)),
                latestWalk: 6,
                hasNextPage: searchHandler.handleNextPage(changeQuery, result[2].data.total),
                name: params.query
            });

        finalResult.criteo = {skn: searchHandler.getCriteo(_.get(finalResult.search, 'goods'))};
    }
    return finalResult;
};

/**
 * 获取搜索建议数据
 * @param  {[type]} origin [description]
 * @return {[type]}        [description]
 */
function getSearchKeywordData(params, channel) {
    let searchParams = searchHandler.getSearchParams(params);

    switch (channel) {
        case 'boys':
            searchParams.physical_channel = 1;
            break;
        case 'girls':
            searchParams.physical_channel = 2;
            break;
        case 'kids':
            searchParams.physical_channel = 3;
            break;
        case 'lifestyle':
            searchParams.physical_channel = 4;
            break;
        default:
            break;
    }

    searchParams.need_filter = 'no';

    // 调用接口
    let apiMethod = [
        headerModel.requestHeaderData(channel),
        this.searchApi.getSuggest({keyword: searchParams.query.substring(0, 2)}),
        this.searchApi.getSeoProductList(searchParams, 'fuzzySearch')
    ];

    return api.all(apiMethod).then(result => {

        return _setSearchKeywordData(result, params, channel);
    });
}

/**
 * 获取搜索建议数据
 * @id  {[number]} origin [description]
 * @param  {[object]} origin [description]
 * @channel  {[string]} origin [description]
 * @return {[object]}        [description]
 */
function getSearchKeywordDataById(id, params, channel) {
    return co(function* () {
        let redisData = yield redis.all([
            ['get', `global:yoho:seo:keywords:id:${id}`]
        ]);

        if (!redisData[0]) {
            return false;
        }

        redisData = JSON.parse(redisData[0]);

        params.query = redisData.name;

        let resData = yield this.getSearchKeywordData(params, channel);

        resData.queryKey = params.query;

        if (!_.isEmpty(redisData.data)) {
            _.forEach(redisData.data, value => {
                if (!value) {
                    return;
                }

                let href = helpers.urlFormat(`/chanpin/${value.id}.html`, null, 'www');

                Object.assign(value, {
                    name: value.keyword,
                    href: href,
                    url: href
                });
            });

            let rarr = _.chunk(redisData.data, 12);

            if (rarr.length) {
                _.set(resData, 'search.leftContent.allSuggest.list', rarr.shift());
                _.set(resData, 'recommendKeywordsInfo', {
                    recommendKeywordsTitle: '更多推荐',
                    recommendKeywords: _.concat(...rarr)
                });
            }
        }

        // 前10的热销品牌
        Object.assign(resData.search.leftContent, searchHandler.hotBrands(hotBrandsModel.hotBrands().slice(0, 10)));

        return resData;
    }).bind(this)();
}


module.exports = class extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);

        this.searchApi = new SearchApiModel(ctx);
        this.saleApi = new SaleApiModel(ctx);

        this.getKeyActivity = getKeyActivity.bind(this);
        this.getSearchData = getSearchData.bind(this);
        this.getSearchDataPre = getSearchDataPre.bind(this);
        this.getSuggest = getSuggest.bind(this);
        this.getListBrandsFilter = getListBrandsFilter.bind(this);
        this.getBrands4Filter = getBrands4Filter.bind(this);
        this.getSearchLessProduct = getSearchLessProduct.bind(this);
        this.getSearchKeywordData = getSearchKeywordData.bind(this);
        this.getSearchKeywordDataById = getSearchKeywordDataById.bind(this);
    }
};