Blame view

apps/product/models/search-api.js 13.9 KB
htoooth authored
1 2 3 4
/**
 * Created by TaoHuang on 2016/6/14.
 */
'use strict';
htoooth authored
5
const _ = require('lodash');
htoooth authored
6
const Fn = require('lodash/fp');
htoooth authored
7
const md5 = require('md5');
yoho authored
8
htoooth authored
9
const config = global.yoho.config;
htoooth authored
10
刘传洋 authored
11
const helpers = global.yoho.helpers;
刘传洋 authored
12 13
const cache = global.yoho.cache;
const logger = global.yoho.logger;
周少峰 authored
14
周少峰 authored
15
// 判断用户是否收藏品牌
yyq authored
16
const isFavoriteBrandUrl = '/shops/service/v1/favorite/getUidBrandFav';
周少峰 authored
17
周少峰 authored
18 19 20
// 根据品牌查询相关文章
const relateArticleUrl = 'guang/service/v2/article/getArticleByBrand';
yyq authored
21 22
const GLOBAL_BASE_URI = '/product/global/list';
htoooth authored
23 24
// 缓存生效时间
const CACHE_TIME_S = 60;
刘传洋 authored
25
htoooth authored
26
function getSearchCacheKey(params) {
htoooth authored
27 28 29
    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('_'));
刘传洋 authored
30
htoooth authored
31
    return 'search_custom_' + md5(genKey(params));
htoooth authored
32
}
33
htoooth authored
34
function _saveCache(key, kv, cacheTime) {
35
    cache.set(key, kv, cacheTime)
htoooth authored
36 37 38 39 40 41 42 43 44 45 46 47
        .catch(err => logger.debug(`product query save cache data fail:${err.toString()}`));
}

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

function getKeyActivityAsync(query) {
    return this.get({
        data: {
            method: 'app.search.word',
            query: query
htoooth authored
48
        }, param: {
htoooth authored
49 50 51
            catch: true,
            code: 200
        }
yyq authored
52
    });
htoooth authored
53
}
yyq authored
54
周少峰 authored
55 56 57 58
/**
 * 获取商品列表
 * @return
 */
htoooth authored
59
function getProductList(params, from) {
周少峰 authored
60
    let finalParams = {
yyq authored
61
        method: 'web.search.search',
yyq authored
62 63 64
        sales: 'Y',
        outlets: 2,
        stocknumber: 1,
刘传洋 authored
65
        need_filter: 'yes',
yyq authored
66
        limit: 60
周少峰 authored
67 68
    };
刘传洋 authored
69
    Object.assign(finalParams, params);
刘传洋 authored
70
yyq authored
71 72 73 74 75
    // 店铺默认排序s_w_desc
    if (params.shopId || params.shop_id || params.shop) {
        finalParams.order = params.order || 's_w_desc';
    }
yyq authored
76 77 78 79
    if (from) {
        finalParams.from = from;
    }
刘传洋 authored
80
    if (!config.useCache) {
htoooth authored
81
        return this.getProductListOrig(finalParams);
刘传洋 authored
82
    } else {
htoooth authored
83
        let cKey = this.getSearchCacheKey(finalParams);
刘传洋 authored
84
85
        return cache.get(cKey)
htoooth authored
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
            .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);
113
                        }
htoooth authored
114
                    }
115
htoooth authored
116 117 118
                    return result;
                });
            });
刘传洋 authored
119
    }
htoooth authored
120
}
刘传洋 authored
121
周少峰 authored
122 123 124 125
/**
 * 获取seo商品列表
 * @return
 */
htoooth authored
126
function getSeoProductList(params, from) {
周少峰 authored
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
    let finalParams = {
        method: 'web.search.forseo',
        sales: 'Y',
        outlets: 2,
        stocknumber: 1,
        need_filter: 'no',
        limit: 60
    };

    Object.assign(finalParams, params);

    if (from) {
        finalParams.from = from;
    }
htoooth authored
142
    return this.getProductListOrig(finalParams);
htoooth authored
143
}
周少峰 authored
144
htoooth authored
145
function getSortListOrig(finalParams) {
htoooth authored
146
    return this.get({data: finalParams, param: config.apiCache});
htoooth authored
147
}
周少峰 authored
148 149 150 151 152

/**
 * 获取分类列表
 * @return
 */
htoooth authored
153
function getSortList(params, channel) {
周少峰 authored
154
    let finalParams = {
yyq authored
155
        method: 'web.regular.groupsort.sale',
yyq authored
156 157 158
        sales: 'Y', // 在销售商品分类
        status: 1, // 上架商品分类
        stocknumber: 1 // 过滤掉已售罄
周少峰 authored
159 160 161
    };

    Object.assign(finalParams, params);
刘传洋 authored
162
yyq authored
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
    if (channel) {
        switch (channel) {
            case 'boys':
                finalParams.yh_channel = 1;
                break;
            case 'girls':
                finalParams.yh_channel = 2;
                break;
            case 'kids':
                finalParams.yh_channel = 3;
                break;
            case 'lifestyle':
                finalParams.yh_channel = 4;
                break;
            default:
                break;
        }
    }
刘传洋 authored
182
    if (!config.useCache) {
htoooth authored
183
        return this.getSortListOrig(finalParams);
刘传洋 authored
184
    } else {
htoooth authored
185
        let cKey = this.getSearchCacheKey(finalParams);
刘传洋 authored
186
187
        return cache.get(cKey)
htoooth authored
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
            .catch(err => logger.debug(`product query save cache data fail:${err.toString()}`))
            .then(cdata => {
                let cdataObj;

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

                if (cdataObj) {
                    return cdataObj;
                } else {
                    return this.getSortListOrig(finalParams).then(ret => {
                        if (ret && ret.code === 200) {
                            _saveCache(cKey, ret, CACHE_TIME_S);
206
                        }
htoooth authored
207
                        return ret;
208
                    });
htoooth authored
209 210
                }
            });
刘传洋 authored
211
    }
htoooth authored
212
}
周少峰 authored
213 214 215 216 217

/**
 * 获取分类图文介绍
 * @return
 */
htoooth authored
218
function getSortIntro(params) {
周少峰 authored
219 220 221 222 223
    let finalParams = {
        method: 'web.search.banner'
    };

    Object.assign(finalParams, params);
htoooth authored
224
    return this.get({data: finalParams, param: config.apiCache});
htoooth authored
225
}
周少峰 authored
226 227

/**
周少峰 authored
228
 * 获取分类广告
周少峰 authored
229 230
 * @return
 */
htoooth authored
231
function getSortAds(params) {
周少峰 authored
232 233 234 235 236
    let finalParams = {
        method: 'app.ads.list'
    };

    Object.assign(finalParams, params);
htoooth authored
237
    return this.get({data: finalParams, param: config.apiCache});
htoooth authored
238
}
周少峰 authored
239 240

/**
周少峰 authored
241 242 243
 * 获取品牌系列
 * @return
 */
htoooth authored
244
function getBrandShopSeries(params) {
周少峰 authored
245 246 247 248 249
    let finalParams = {
        method: 'web.brand.series',
        status: params.status || 1
    };
yyq authored
250 251 252 253 254 255 256 257 258
    if (params.brandId) {
        finalParams.brand_id = params.brandId;
    } else if (params.shopId) {
        Object.assign(finalParams, {
            method: 'web.shop.series',
            shop_id: params.shopId
        });
    }
htoooth authored
259
    return this.get({data: finalParams, param: config.apiCache});
htoooth authored
260
}
周少峰 authored
261 262

/**
周少峰 authored
263 264 265
 * 获取品牌folder
 * @return
 */
htoooth authored
266
function getBrandShopFolder(params) {
周少峰 authored
267 268 269 270 271
    let finalParams = {
        method: 'web.brand.folder',
        status: params.status || 1
    };
yyq authored
272 273 274 275 276 277 278 279 280
    if (params.brandId) {
        finalParams.brand_id = params.brandId;
    } else if (params.shopId) {
        Object.assign(finalParams, {
            method: 'web.shop.folder',
            shop_id: params.shopId
        });
    }
htoooth authored
281
    return this.get({data: finalParams, param: config.apiCache});
htoooth authored
282
}
周少峰 authored
283
周少峰 authored
284 285 286 287
/**
 * 获取品牌水牌
 * @return
 */
htoooth authored
288
function getNodeContent(params) {
周少峰 authored
289 290 291 292 293 294
    let finalParams = {
        method: 'web.html.content',
        mode: params.mode || 'release',
        node: params.node || ''
    };
htoooth authored
295
    return this.get({data: finalParams, param: config.apiCache});
htoooth authored
296
}
周少峰 authored
297
周少峰 authored
298 299 300 301
/**
 * 一周新品上架
 * @return
 */
htoooth authored
302
function getWeekNew(params) {
周少峰 authored
303
周少峰 authored
304 305 306 307 308
    let finalParams = {
        method: 'web.regular.recent'
    };

    Object.assign(finalParams, params);
htoooth authored
309
    return this.get({data: finalParams, param: {cache: config.apiCache}});
htoooth authored
310
}
周少峰 authored
311
htoooth authored
312
function getBrandCouponAsync(brandId, uid) {
yyq authored
313 314 315 316 317 318
    let extra = {code: 200};

    if (!uid) {
        extra.cache = true;
    }
htoooth authored
319 320 321 322 323
    return this.get({
        data: {
            method: 'app.brand.getBrandIntro',
            brand_id: brandId,
            uid: uid
htoooth authored
324
        }, param: extra
htoooth authored
325 326
    });
}
yyq authored
327
328
/**
htoooth authored
329
 * 根据关键词搜索品牌店铺信息
330 331
 * @return
 */
htoooth authored
332
function getBrandShop(query) {
333
    let finalParams = {
yyq authored
334
        method: 'web.search.shopListInfo'
335 336
    };
htoooth authored
337
    return this.get({data: Object.assign(finalParams, {keyword: query}), param: config.apiCache});
htoooth authored
338
}
刘传洋 authored
339 340

/**
周少峰 authored
341
 * 根据搜索得到店铺/品牌
刘传洋 authored
342
 */
htoooth authored
343
function getShopList(params) {
刘传洋 authored
344 345 346 347
    if (!params || !params.query) {
        return;
    }
htoooth authored
348
    return this.getBrandShop(params.query).then(shops => {
yyq authored
349 350 351 352 353 354 355
        let shopEntry = [];

        _.forEach(_.get(shops, 'data.shopList', []), value => {
            let shopInfo = {
                home: helpers.urlFormat('', null, value.shop_domain || value.brand_domain),
                logo: value.shop_logo || value.brand_ico,
                shopName: value.shop_name || value.brand_name,
yyq authored
356
                shopType: '',
yyq authored
357 358 359 360
                sort: []
            };

            if (value.is_global === 'Y') {
361
                shopInfo.home = helpers.urlFormat(GLOBAL_BASE_URI, {brand: value.global_brand_id});
yyq authored
362
                shopInfo.shopType = 'global-brand';
yyq authored
363 364 365
            }

            // 店铺/品牌的小分类
yyq authored
366
            _.forEach(_.get(value, 'sortInfo.sort', []), sortInfo => {
yyq authored
367 368 369
                _.forEach(_.get(sortInfo, 'sub', []), subSort => {
                    let sortHref;
yyq authored
370
                    if (value.is_global !== 'Y') {
yyq authored
371 372 373 374 375
                        sortHref = helpers.urlFormat('', {misort: subSort.sort_id},
                            value.shop_domain || value.brand_domain);
                    } else {
                        sortHref = helpers.urlFormat(GLOBAL_BASE_URI, {
                            misort: subSort.sort_id,
376
                            brand: value.global_brand_id
yyq authored
377 378 379 380 381 382
                        });
                    }
                    shopInfo.sort.push({
                        href: sortHref,
                        name: subSort.sort_name
                    });
刘传洋 authored
383 384 385
                });
            });
yyq authored
386
            shopEntry.push(shopInfo);
周少峰 authored
387 388 389
        });

        return shopEntry;
刘传洋 authored
390
    });
htoooth authored
391
}
周少峰 authored
392 393 394 395 396

/**
 * 搜索提示
 * @return
 */
htoooth authored
397
function getSuggest(params) {
OF1706 authored
398
周少峰 authored
399 400 401 402 403
    let finalParams = {
        method: 'app.search.fuzzy',
        keyword: params.keyword || ''
    };
htoooth authored
404
    return this.get({data: finalParams, param: config.apiCache});
htoooth authored
405
}
406 407

周少峰 authored
408 409 410 411
/**
 * 根据品牌域名获取品牌信息
 * @return
 */
htoooth authored
412
function getBrandData(params) {
周少峰 authored
413 414 415 416 417
    let finalParams = {
        method: 'web.brand.byDomain',
        domain: params.domain || ''
    };
htoooth authored
418
    return this.get({data: finalParams, param: config.apiCache});
htoooth authored
419
}
周少峰 authored
420
周少峰 authored
421 422 423 424
/**
 * 根据uid和品牌id判断品牌是否收藏
 * @return
 */
htoooth authored
425 426 427 428 429 430 431 432
function isFavoriteBrand(uid, brandId) {
    return this.get({
        url: isFavoriteBrandUrl,
        data: {
            uid: uid,
            brandId: brandId
        },
        api: global.yoho.serviceApi
yyq authored
433
    });
htoooth authored
434
}
周少峰 authored
435
周少峰 authored
436 437 438 439
/**
 * 根据shopId获取店铺基本信息
 * @return
 */
htoooth authored
440
function getShopInfo(shopId, uid) {
周少峰 authored
441 442 443 444 445 446
    let finalParams = {
        method: 'app.shops.getIntro',
        shop_id: shopId || 0,
        uid: uid || 0
    };
htoooth authored
447
    return this.get({data: finalParams, param: config.apiCache});
周少峰 authored
448
htoooth authored
449
}
周少峰 authored
450
周少峰 authored
451 452 453
/**
 * 查询店铺下面的所有品牌
 */
htoooth authored
454 455 456
function getShopBrands(shopId) {
    return this.get({
        data: {method: 'app.shops.getShopsBrands', shop_id: shopId || 0},
htoooth authored
457
        param: config.apiCache
htoooth authored
458 459
    });
}
周少峰 authored
460 461 462 463

/**
 * 查询店铺装修
 */
htoooth authored
464 465 466
function getShopDecorator(shopId) {
    return this.get({
        data: {method: 'app.shopsdecorator.getList', shop_id: shopId || 0},
htoooth authored
467
        param: config.apiCache
htoooth authored
468 469
    });
}
周少峰 authored
470 471 472 473

/**
 * 通过品牌获取相关文章
 */
htoooth authored
474
function getArticleByBrand(brand, udid, limit) {
周少峰 authored
475 476 477 478 479 480
    let params = {
        brand_id: brand || 0,
        udid: udid,
        limit: limit || 6
    };
htoooth authored
481 482 483 484 485 486
    return this.get({
        url: relateArticleUrl,
        data: params,
        api: global.yoho.ServiceAPI
    });
}
周少峰 authored
487
htoooth authored
488 489 490 491 492 493 494
function getBrands4Filter(params) {
    return this.get({
        data: Object.assign({
            method: 'web.regular.aggBrand'
        }, params)
    });
}
周少峰 authored
495
OF1706 authored
496
/**
OF1706 authored
497 498
 * 有可能喜欢的商品
OF1706 authored
499 500 501 502 503 504 505
 * @param int $channel 频道,1代表男生,2代表女生,3代表潮童,4代表创意生活
 * @param $uid 用户ID
 * @param $udid 设备ID
 * @param $rec_pos 位置码
 * @param $limit 数量限制
 * @return array 接口返回的数据
 * */
OF1706 authored
506
htoooth authored
507
function lessRecommend(channelNum, uid, udid, recPos, limit) {
OF1706 authored
508
    let param = {
OF1706 authored
509
        method: 'app.search.newLast7day',
OF1706 authored
510
        yh_channel: channelNum,
OF1706 authored
511 512 513 514 515 516 517 518 519
        udid: udid,
        rec_pos: recPos,
        limit: limit
    };

    if (uid) {
        param.uid = uid;
    }
htoooth authored
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 548 549 550 551 552
    return this.get({data: param});
}

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

        this.getKeyActivityAsync = getKeyActivityAsync.bind(this);
        this.getProductList = getProductList.bind(this);
        this.getSeoProductList = getSeoProductList.bind(this);
        this.getSortList = getSortList.bind(this);
        this.getSortIntro = getSortIntro.bind(this);
        this.getSortAds = getSortAds.bind(this);
        this.getBrandShopFolder = getBrandShopFolder.bind(this);
        this.getBrandShopSeries = getBrandShopSeries.bind(this);
        this.getWeekNew = getWeekNew.bind(this);
        this.getBrandCouponAsync = getBrandCouponAsync.bind(this);
        this.getBrandShop = getBrandShop.bind(this);
        this.getSuggest = getSuggest.bind(this);
        this.getBrandData = getBrandData.bind(this);
        this.getNodeContent = getNodeContent.bind(this);
        this.isFavoriteBrand = isFavoriteBrand.bind(this);
        this.getShopInfo = getShopInfo.bind(this);
        this.getShopBrands = getShopBrands.bind(this);
        this.getShopDecorator = getShopDecorator.bind(this);
        this.getArticleByBrand = getArticleByBrand.bind(this);
        this.getShopList = getShopList.bind(this);
        this.getBrands4Filter = getBrands4Filter.bind(this);
        this.getProductListOrig = getProductListOrig.bind(this);
        this.getSearchCacheKey = getSearchCacheKey.bind(this);
        this.lessRecommend = lessRecommend.bind(this);
        this.getSortListOrig = getSortListOrig.bind(this);
    }
htoooth authored
553
};