Blame view

apps/channel/models/search-api.js 3.05 KB
htoooth authored
1 2 3
/**
 * Created by TaoHuang on 2016/6/14.
 */
htoooth authored
4
htoooth authored
5 6 7 8 9 10 11 12 13 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;
htoooth authored
15
const getSearchCacheKey = params => {
htoooth authored
16 17 18 19 20
    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));
htoooth authored
21
};
htoooth authored
22
htoooth authored
23
const saveCache = (key, kv, cacheTime) => {
htoooth authored
24
    cache.set(key, kv, cacheTime)
htoooth authored
25 26 27
         .catch(err => logger.debug(`product query save cache data fail:${err.toString()}`));
};
htoooth authored
28 29 30
module.exports = class extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);
htoooth authored
31 32
    }
htoooth authored
33 34
    getProductListOrig(finalParams) {
        return this.get({data: finalParams});
htoooth authored
35 36
    }
htoooth authored
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
    /**
     * 获取商品列表
     * @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';
        }
htoooth authored
57
htoooth authored
58 59 60
        if (from) {
            finalParams.from = from;
        }
htoooth authored
61
htoooth authored
62 63 64 65
        if (!config.useCache) {
            return this.getProductListOrig(finalParams);
        } else {
            let cKey = getSearchCacheKey(finalParams);
htoooth authored
66
htoooth authored
67 68 69 70
            return cache.get(cKey)
                .catch(err => logger.debug(`product query save cache data fail:${err.toString()}`))
                .then(cdata => {
                    let hasCache = false;
htoooth authored
71
htoooth authored
72
                    if (cdata) {
htoooth authored
73
htoooth authored
74 75 76 77 78
                        try {
                            cdata = JSON.parse(cdata);
                        } catch (e) {
                            logger.debug('getProductList cache data parse fail.');
                        }
htoooth authored
79
htoooth authored
80 81 82
                        if (cdata.filter && cdata.standard) {
                            hasCache = true;
                            finalParams.need_filter = 'no';
htoooth authored
83
                        }
htoooth authored
84
                    }
htoooth authored
85
htoooth authored
86 87 88
                    return this.getProductListOrig(finalParams).then(result => {
                        if (hasCache && result && result.data) {
                            Object.assign(result.data, cdata);
htoooth authored
89
                        } else {
htoooth authored
90 91 92 93 94 95
                            if (result && result.data && result.data.filter) {
                                saveCache(cKey, Object.assign({}, {
                                    filter: result.data.filter,
                                    standard: result.data.standard
                                }), CACHE_TIME_S);
                            }
htoooth authored
96 97
                        }
htoooth authored
98
                        return result;
htoooth authored
99 100
                    });
                });
htoooth authored
101
        }
htoooth authored
102 103 104
    }

};