Blame view

apps/channel/models/brand.js 12.7 KB
lijing authored
1 2 3 4 5 6 7 8
/**
 * 品牌页
 * @param params
 * @returns {function()}
 */

'use strict';
郭成尧 authored
9
const _ = require('lodash');
lijing authored
10
const helpers = global.yoho.helpers;
lijing authored
11
lijing authored
12 13
// const utils = '../../../utils';
// const floorProcess = require(`${utils}/floor-process`);
lijing authored
14
lijing authored
15 16 17 18 19 20 21 22 23 24 25 26
/* 男生频道取品牌广告及热门品牌数据的位置码 */
let CODE_TOPPOS_BOYS = 'ce6ac059493ec26241a8cbe0bfa1b17a';

/* 女生频道取品牌广告及热门品牌数据的位置码 */
let CODE_TOPPOS_GIRLS = 'dac99cdedc1f948e84c145aca561f7d2';

/* 潮童频道取品牌广告及热门品牌数据的位置码 */
let CODE_TOPPOS_KIDS = '713784f93f52bb1a7b93916b2bb15510';

/* 创意生活频道取品牌广告及热门品牌数据的位置码 */
let CODE_TOPPOS_LIFESTYLE = 'f1aa914fd23cbcda71a87de6f5416c75';
zhangxiaoru authored
27 28 29 30
module.exports = class extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);
    }
lijing authored
31
zhangxiaoru authored
32 33 34 35
    /**
     * 获取频道接口
     */
    _packageAd(params) {
zhangxiaoru authored
36 37 38 39 40 41 42 43
        return this.get({
            url: '/operations/api/v5/resource/get',
            data: {
                content_code: params.contentCode,
                gender: 1,
                limit: 1000,
                page: 1
            },
zhangxiaoru authored
44 45 46 47
            api: global.yoho.ServiceAPI,
            param: {
                cache: true
            }
zhangxiaoru authored
48 49
        });
    }
lijing authored
50
zhangxiaoru authored
51
    _packageList(channel) {
zhangxiaoru authored
52 53
        return this.get({
            data: {method: 'app.brand.allBrandList', yh_channel: channel},
zhangxiaoru authored
54 55 56 57
            param: {
                cache: true,
                code: 200
            }
zhangxiaoru authored
58 59
        });
    }
lijing authored
60
zhangxiaoru authored
61 62
    getBrandByChannel(channel) {
        let params = {};
lijing authored
63
zhangxiaoru authored
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
        /* 根据频道调用接口 */
        switch (channel) {
            case '1': // 男生
                params = {
                    contentCode: CODE_TOPPOS_BOYS
                };
                break;
            case '2': // 女生
                params = {
                    contentCode: CODE_TOPPOS_GIRLS
                };
                break;
            case '3': // 潮童
                params = {
                    contentCode: CODE_TOPPOS_KIDS
                };
                break;
            case '4': // 创意生活
                params = {
                    contentCode: CODE_TOPPOS_LIFESTYLE
                };
                break;
            default:
                channel = 1;
                break;
        }

        return this._packageAd(params).then(result => {
            let resu = {
                channel: {
                    channels: [{
                        name: 'Boy',
                        channelName: 'boys'
                    }, {
                        name: 'Girl',
                        channelName: 'girls'
                    }, {
                        name: 'Kid',
                        channelName: 'kids'
                    }, {
                        name: 'Lifestyle',
                        channelName: 'lifestyle'
                    }],
                    topData: {
                        bannerTop: {},
                        hotBrandsScroll: {}
                    },
                    searchUrl: helpers.urlFormat('/brands/search', null),
                    brandPage: true,
                    brandType: '全部品牌',
                    secondBrandType: '新入驻品牌',
                    thirdBrandType: '热门品牌'
                }
lijing authored
117 118
            };
zhangxiaoru authored
119 120 121 122 123 124 125
            resu.channel.channels[channel - 1].active = true;
            if (result && result.data) {
                if (result.data[0] && result.data[0].data) {
                    // 轮播图资源位
                    let adList = result.data[0].data;
                    let build = [];
郭成尧 authored
126
                    _.forEach(adList, ad => {
zhangxiaoru authored
127 128 129 130
                        build.push({
                            img: helpers.image(ad.src, 640, 240),
                            url: ad.url
                        });
lijing authored
131
                    });
zhangxiaoru authored
132 133
                    resu.channel.topData.bannerTop.list = build;
                }
lijing authored
134
zhangxiaoru authored
135 136 137 138
                if (result.data[1] && result.data[1].data) {
                    // 推荐品牌资源位
                    let brandList = result.data[1].data.list || [];
                    let brand = [];
lijing authored
139
zhangxiaoru authored
140 141 142 143 144 145
                    brandList.forEach(ad => {
                        brand.push({
                            img: helpers.image(ad.src, 140, 140),
                            url: ad.url,
                            textCn: ad.title
                        });
lijing authored
146
                    });
lijing authored
147
zhangxiaoru authored
148 149 150
                    resu.channel.topData.hotBrandsScroll.name = result.data[1].data.title.title;
                    resu.channel.topData.hotBrandsScroll.list = brand;
                }
lijing authored
151
            }
zhangxiaoru authored
152 153 154
            return resu;
        });
    }
lijing authored
155
zhangxiaoru authored
156
    _urlJump(key, row) {
zhangxiaoru authored
157
zhangxiaoru authored
158 159
        return {
            1: helpers.urlFormat('', {from: 'search', query: row.brand_name}, 'search'),
郭成尧 authored
160
            2: `/shop/${row.brand_domain}-${row.shop_id}.html`,
zhangxiaoru authored
161 162 163
            3: helpers.urlFormat('/product/global/list/', {brand: row.global_brand_id})
        }[key];
    }
zhangxiaoru authored
164
zhangxiaoru authored
165 166 167 168 169 170 171
    getBrandListByChannel(channel) {
        return this._packageList(channel).then(result => {
            let resu = {
                brandList: [],
                newBrandWall: [],
                recommandBrandWall: []
            };
lijing authored
172
zhangxiaoru authored
173 174 175
            if (!result || !result.data) {
                return resu;
            }
lijing authored
176
zhangxiaoru authored
177 178
            /* 按字母'A-Z'分组的品牌列表 */
            let allList = result.data.all_list;
ccbikai(👎🏻🍜) authored
179
zhangxiaoru authored
180 181 182 183 184 185
            for (let key in allList) {
                if (key) {
                    let obj = {
                        title: key,
                        list: []
                    };
陈峰 authored
186
zhangxiaoru authored
187
                    allList[key].forEach(row => {
188 189 190 191 192
                        if (row) {
                            obj.list.push({
                                name: _.get(row, 'brand_name', ''),
                                isHot: _.get(row, 'is_hot') === 'Y' ? true : false,
                                isNew: _.get(row, 'is_show_new') === 'Y' ? true : false,
郭成尧 authored
193
                                url: this._urlJump(parseInt(_.get(row, 'type'), 10), row)
194 195
                            });
                        }
陈峰 authored
196
                    });
zhangxiaoru authored
197 198
                    resu.brandList.push(obj);
                }
陈峰 authored
199
            }
lijing authored
200
zhangxiaoru authored
201 202
            // 新入驻品牌
            let newList = result.data.new_list;
ccbikai(👎🏻🍜) authored
203
zhangxiaoru authored
204 205 206 207 208
            for (let key in newList) {
                if (key) {
                    let obj = {
                        list: []
                    };
lijing authored
209
zhangxiaoru authored
210
                    obj.list.push({
211 212 213
                        brandName: _.get(newList, `${key}.brand_name`, ''),
                        url: this._urlJump(parseInt(_.get(newList, `${key}.type`), 10), _.get(newList, `${key}`)),
                        img: helpers.image(_.get(newList, `${key}.brand_ico`), 186, 115),
zhangxiaoru authored
214 215 216
                    });
                    resu.newBrandWall.push(obj);
                }
陈峰 authored
217
            }
lijing authored
218
zhangxiaoru authored
219 220
            // 推荐品牌
            let hotList = result.data.hot_list;
ccbikai(👎🏻🍜) authored
221
zhangxiaoru authored
222 223 224 225 226
            for (let key in hotList) {
                if (key) {
                    let obj = {
                        list: []
                    };
lijing authored
227
zhangxiaoru authored
228 229 230 231 232 233 234
                    obj.list.push({
                        brandName: hotList[key].brand_name,
                        url: this._urlJump(parseInt(hotList[key].type, 10), hotList[key]),
                        img: helpers.image(hotList[key].brand_ico, 186, 115),
                    });
                    resu.recommandBrandWall.push(obj);
                }
陈峰 authored
235
            }
lijing authored
236
zhangxiaoru authored
237 238 239
            return resu;
        });
    }
陈峰 authored
240
zhangxiaoru authored
241 242 243 244 245 246 247
    /**
     * 获取搜索的品牌列表
     *
     * @param int $channel 1表示男生频道, 2表示女生频道, 3表示潮童频道, 4表示创意生活频道
     * @return array
     */
    getBrandForSearch(channel) {
zhangxiaoru authored
248 249 250 251 252
        return this.get({
            data: {
                method: 'app.brand.allBrandList',
                yh_channel: channel || 1
            },
zhangxiaoru authored
253 254 255 256
            param: {
                cache: true,
                code: 200
            }
zhangxiaoru authored
257 258 259 260 261 262
        }).then(result => {

            return result;

        });
    }
陈峰 authored
263
zhangxiaoru authored
264 265 266 267 268 269 270
    /**
     * 品牌搜索历史
     * @param int $uid
     * @author sefon 2016-5-17 12:12:28
     * @return array
     */
    branchSearchHistory(params) {
zhangxiaoru authored
271 272 273 274 275 276
        return this.get({
            data: {
                method: 'app.search.hotBrandRecords',
                uid: params.uid,
                records: params.records || ''
            },
zhangxiaoru authored
277
            param: {code: 200}
zhangxiaoru authored
278 279 280 281 282 283
        }).then(result => {

            return result;

        });
    }
陈峰 authored
284
zhangxiaoru authored
285
    addSearchHistory(param) {
陈峰 authored
286
zhangxiaoru authored
287 288 289 290 291 292
        return this.get({
            data: {
                method: 'app.search.hotBrandRecords',
                uid: param.uid,
                records: param.records
            },
zhangxiaoru authored
293
            param: {code: 200}
zhangxiaoru authored
294 295
        });
    }
陈峰 authored
296
zhangxiaoru authored
297 298 299 300 301 302
    /**
     * 热搜品牌
     * @author sefon 2016-5-17 12:20:26
     * @return array
     */
    branchSearchHot() {
zhangxiaoru authored
303 304
        return this.get({
            data: {method: 'app.search.hotBrands'},
zhangxiaoru authored
305 306 307 308
            param: {
                cache: true,
                code: 200
            }
zhangxiaoru authored
309
        }).then(result => {
陈峰 authored
310
zhangxiaoru authored
311
            return result;
陈峰 authored
312
zhangxiaoru authored
313 314
        });
    }
陈峰 authored
315
zhangxiaoru authored
316 317 318 319 320 321 322
    /**
     * 获取搜索的品牌列表 pagecache重构
     *
     * @param int $channel 1表示男生频道, 2表示女生频道, 3表示潮童频道, 4表示创意生活频道
     * @return array
     */
    branchSearch(params) {
zhangxiaoru authored
323
        return Promise.all([
lijing authored
324
zhangxiaoru authored
325
            this.getBrandForSearch(params.channel),
326
zhangxiaoru authored
327 328
            // branchSearchHistory(params),
            this.branchSearchHot(),
lijing authored
329
zhangxiaoru authored
330
        ]).then(result => {
lijing authored
331
zhangxiaoru authored
332 333 334 335 336 337 338
            let resu = {
                brandPage: true,
                pageFooter: true,
                hot: [],
                history: [],
                brandList: []
            };
lijing authored
339
zhangxiaoru authored
340
            if (result && result[0] && result[0].data) {
lijing authored
341
zhangxiaoru authored
342
                let brandList = result[0].data.all_list;
lijing authored
343
zhangxiaoru authored
344
                let obj = {};
lijing authored
345
zhangxiaoru authored
346 347 348
                for (let key in brandList) {
                    if (key) {
                        let build = [];
陈峰 authored
349
zhangxiaoru authored
350 351 352 353 354 355 356 357 358 359 360
                        brandList[key].forEach(row => {
                            build.push({
                                name: row.brand_name,
                                isHot: row.is_hot === 'Y' ? true : false,
                                isNew: row.is_show_new === 'Y' ? true : false,
                                url: this._urlJump(parseInt(row.type, 10), row),
                                brandId: row.id,
                                brandDomain: row.brand_domain,
                                searchName: row.brand_name_en + row.brand_name_cn
                            });
                            obj[key] = build;
陈峰 authored
361
                        });
zhangxiaoru authored
362
                    }
陈峰 authored
363
                }
zhangxiaoru authored
364 365

                resu.brandList = JSON.stringify(obj);
lijing authored
366 367 368
            }

zhangxiaoru authored
369
            if (result && result[1] && result[1].data) {
lijing authored
370
zhangxiaoru authored
371
                let hotList = result[1].data;
lijing authored
372
zhangxiaoru authored
373 374 375
                for (let key in hotList) {
                    if (key) {
                        let obj = {};
lijing authored
376
zhangxiaoru authored
377 378 379 380 381
                        obj = Object.assign(obj, {
                            name: hotList[key].brandName
                        });
                        resu.hot.push(obj);
                    }
陈峰 authored
382
                }
lijing authored
383
            }
lijing authored
384
zhangxiaoru authored
385
            // if (result && result[1] && result[1].data) {
lijing authored
386
zhangxiaoru authored
387
            //     let hisList = result[1].data;
lijing authored
388
zhangxiaoru authored
389
            //     let obj = [];
lijing authored
390
zhangxiaoru authored
391
            //     let build = {
lijing authored
392
zhangxiaoru authored
393
            //         list: []
lijing authored
394
zhangxiaoru authored
395
            //     };
lijing authored
396
zhangxiaoru authored
397
            //     for (let key in hisList) {
lijing authored
398
zhangxiaoru authored
399 400 401 402
            //         obj.push({
            //             name: hisList[key].searchTerms,
            //             searchTime: hisList[key].searchTime
            //         });
lijing authored
403
zhangxiaoru authored
404
            //     }
lijing authored
405
zhangxiaoru authored
406
            //     build.list = obj;
lijing authored
407
zhangxiaoru authored
408
            //     resu.history = build;
lijing authored
409
zhangxiaoru authored
410
            // }
lijing authored
411
zhangxiaoru authored
412
            return resu;
lijing authored
413
zhangxiaoru authored
414 415
        });
    }
lijing authored
416
zhangxiaoru authored
417
    delBrandSearchHistory(param) {
lijing authored
418
zhangxiaoru authored
419 420 421 422 423
        return this.get({
            data: {
                method: 'app.search.clearHotBrandRecords',
                uid: param.uid
            },
zhangxiaoru authored
424
            param: {code: 200}
zhangxiaoru authored
425
        }).then(result => {
lijing authored
426
zhangxiaoru authored
427
            return result;
lijing authored
428
zhangxiaoru authored
429 430
        });
    }
431
zhangxiaoru authored
432 433 434 435 436 437 438 439
    /**
     * [异步获取用户最近搜索词]
     */
    branchSearchHistoryAsync(uid) {
        return this.branchSearchHistory({
            uid
        }).then((result) => {
            let hisList = result.data;
陈峰 authored
440
zhangxiaoru authored
441
            let obj = [];
陈峰 authored
442
zhangxiaoru authored
443
            let build = {
陈峰 authored
444
zhangxiaoru authored
445
                list: []
陈峰 authored
446
zhangxiaoru authored
447
            };
陈峰 authored
448
zhangxiaoru authored
449 450 451 452 453 454 455
            for (let key in hisList) {
                if (key) {
                    obj.push({
                        name: hisList[key].searchTerms,
                        searchTime: hisList[key].searchTime
                    });
                }
陈峰 authored
456
            }
陈峰 authored
457
zhangxiaoru authored
458
            build.list = obj;
lijing authored
459
zhangxiaoru authored
460 461 462
            return build;
        });
    }
lijing authored
463
};