search-api.js 3.05 KB
/**
 * Created by TaoHuang on 2016/6/14.
 */

const Fn = require('lodash/fp');
const md5 = require('md5');

const config = global.yoho.config;
const cache = global.yoho.cache;
const logger = global.yoho.logger;

// 缓存生效时间
const CACHE_TIME_S = 60;

const getSearchCacheKey = params => {
    let removeUnusedKey = Fn.omit(['page', 'limit', 'need_filter', 'order']);
    let sortByKey = Fn.pipe(Fn.toPairs, Fn.sortBy(0), Fn.flatten);
    let genKey = Fn.pipe(Fn.cloneDeep, removeUnusedKey, sortByKey, Fn.join('_'));

    return 'search_custom_' + md5(genKey(params));
};

const saveCache = (key, kv, cacheTime) => {
    cache.set(key, kv, cacheTime)
        .catch(err => logger.debug(`product query save cache data fail:${err.toString()}`));
};

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

    getProductListOrig(finalParams) {
        return this.get({data: finalParams});
    }

    /**
     * 获取商品列表
     * @return
     */
    getProductList(params, from) {
        let finalParams = {
            method: 'web.search.search',
            sales: 'Y',
            outlets: 2,
            stocknumber: 1,
            need_filter: 'yes',
            limit: 60
        };

        Object.assign(finalParams, params);

        // 店铺默认排序s_w_desc
        if (params.shopId || params.shop_id || params.shop) {
            finalParams.order = params.order || 's_w_desc';
        }

        if (from) {
            finalParams.from = from;
        }

        if (!config.useCache) {
            return this.getProductListOrig(finalParams);
        } else {
            let cKey = getSearchCacheKey(finalParams);

            return cache.get(cKey)
                .catch(err => logger.debug(`product query save cache data fail:${err.toString()}`))
                .then(cdata => {
                    let hasCache = false;

                    if (cdata) {

                        try {
                            cdata = JSON.parse(cdata);
                        } catch (e) {
                            logger.debug('getProductList cache data parse fail.');
                        }

                        if (cdata.filter && cdata.standard) {
                            hasCache = true;
                            finalParams.need_filter = 'no';
                        }
                    }

                    return this.getProductListOrig(finalParams).then(result => {
                        if (hasCache && result && result.data) {
                            Object.assign(result.data, cdata);
                        } else {
                            if (result && result.data && result.data.filter) {
                                saveCache(cKey, Object.assign({}, {
                                    filter: result.data.filter,
                                    standard: result.data.standard
                                }), CACHE_TIME_S);
                            }
                        }

                        return result;
                    });
                });
        }
    }

};