brand.js 13.7 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 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
/**
 * 品牌页
 * @param params
 * @returns {function()}
 */

'use strict';

var api = global.yoho.API;
const service = global.yoho.ServiceAPI;
const helpers = global.yoho.helpers;

// const utils = '../../../utils';
// const floorProcess = require(`${utils}/floor-process`);

/* 男生频道取品牌广告及热门品牌数据的位置码 */
let CODE_TOPPOS_BOYS = 'ce6ac059493ec26241a8cbe0bfa1b17a';

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

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

/* 创意生活频道取品牌广告及热门品牌数据的位置码 */
let CODE_TOPPOS_LIFESTYLE = 'f1aa914fd23cbcda71a87de6f5416c75';

/**
 * 获取频道接口
 */

const _packageAd = (params) => {
    return service.get('/operations/api/v5/resource/get', {
        content_code: params.contentCode,
        gender: 1,
        limit: 1000,
        page: 1
    }, {
        cache: true
    });
};

const _packageList = (channel) => {
    return api.get('', {
        method: 'app.brand.allBrandList',
        yh_channel: channel
    }, {
        cache: true,
        code: 200
    });
};

const getBrandByChannel = (channel) => {
    let params = {};

    /* 根据频道调用接口 */
    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 _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: '热门品牌'
            }
        };

        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 = [];

                adList.forEach(ad => {
                    build.push({
                        img: helpers.image(ad.src, 640, 240),
                        url: ad.url
                    });
                });
                resu.channel.topData.bannerTop.list = build;
            }

            if (result.data[1] && result.data[1].data) {
                // 推荐品牌资源位
                let brandList = result.data[1].data.list || [];
                let brand = [];

                brandList.forEach(ad => {
                    brand.push({
                        img: helpers.image(ad.src, 140, 140),
                        url: ad.url,
                        textCn: ad.title
                    });
                });

                resu.channel.topData.hotBrandsScroll.name = result.data[1].data.title.title;
                resu.channel.topData.hotBrandsScroll.list = brand;
            }
        }
        return resu;
    });
};

const getBrandListByChannel = (channel) => {
    return _packageList(channel).then(result => {
        let resu = {
            brandList: [],
            newBrandWall: [],
            recommandBrandWall: []
        };

        if (!result || !result.data) {
            return resu;
        }

        /* 按字母'A-Z'分组的品牌列表 */
        let allList = result.data.all_list;

        for (let key in allList) {
            if (key) {
                let obj = {
                    title: key,
                    list: []
                };

                allList[key].forEach(row => {
                    obj.list.push({
                        name: row.brand_name,
                        isHot: row.is_hot === 'Y' ? true : false,
                        isNew: row.is_show_new === 'Y' ? true : false,
                        url: parseInt(row.type, 10) !== 3 ? helpers.urlFormat('/product/index/brand', {
                            domain: row.brand_domain,
                            shopType: row.type
                        }) : helpers.urlFormat('/product/global/list/', {
                            brand: row.global_brand_id
                        })
                    });
                });
                resu.brandList.push(obj);
            }
        }

        // 新入驻品牌
        let newList = result.data.new_list;

        for (let key in newList) {
            if (key) {
                let obj = {
                    list: []
                };

                obj.list.push({
                    brandName: newList[key].brand_name,
                    url: helpers.urlFormat('/product/index/brand', {
                        domain: newList[key].brand_domain,
                        shopType: newList[key].type
                    }),
                    img: helpers.image(newList[key].brand_ico, 186, 115),
                });
                resu.newBrandWall.push(obj);
            }
        }

        // 推荐品牌
        let hotList = result.data.hot_list;

        for (let key in hotList) {
            if (key) {
                let obj = {
                    list: []
                };

                obj.list.push({
                    brandName: hotList[key].brand_name,
                    url: helpers.urlFormat('/product/index/brand', {
                        domain: hotList[key].brand_domain,
                        shopType: hotList[key].type
                    }),
                    img: helpers.image(hotList[key].brand_ico, 186, 115),
                });
                resu.recommandBrandWall.push(obj);
            }
        }

        return resu;
    });
};

/**
 * 获取搜索的品牌列表
 *
 * @param int $channel 1表示男生频道, 2表示女生频道, 3表示潮童频道, 4表示创意生活频道
 * @return array
 */

const getBrandForSearch = (channel) => {
    return api.get('', {
        method: 'app.brand.brandlist',
        yh_channel: channel || 1

    }, {
        cache: true,
        code: 200
    }).then(result => {

        return result;

    });
};

/**
 * 品牌搜索历史
 * @param int $uid
 * @author sefon 2016-5-17 12:12:28
 * @return array
 */

const branchSearchHistory = (params) => {
    return api.get('', {
        method: 'app.search.hotBrandRecords',
        uid: params.uid,
        records: params.records || ''

    }, {
        code: 200
    }).then(result => {

        return result;

    });
};

const addSearchHistory = (param) => {

    return api.get('', {

        method: 'app.search.hotBrandRecords',
        uid: param.uid,
        records: param.records

    }, {
        code: 200
    });
};

/**
 * 热搜品牌
 * @author sefon 2016-5-17 12:20:26
 * @return array
 */
const branchSearchHot = () => {
    return api.get('', {
        method: 'app.search.hotBrands'

    }, {
        cache: true,
        code: 200
    }).then(result => {

        return result;

    });
};

// /**
//  * 获取搜索的品牌列表
//  *
//  * @param int $channel 1表示男生频道, 2表示女生频道, 3表示潮童频道, 4表示创意生活频道
//  * @return array
//  */
// const branchSearch = (params) => {
//     return api.all([

//         getBrandForSearch(params.channel),
//         branchSearchHistory(params),
//         branchSearchHot(),

//     ]).then(result => {

//         let resu = {
//             brandPage: true,
//             showDownloadApp: true,
//             pageFooter: true,
//             hot: [],
//             history: [],
//             brandList: []
//         };

//         if (result && result[0] && result[0].data) {

//             let brandList = result[0].data.brands;

//             let obj = {};

//             for (let key in brandList) {

//                 let build = [];

//                 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: helpers.urlFormat('', null, row.brand_domain),
//                         brandId: row.id,
//                         brandDomain: row.brand_domain,
//                         searchName: row.brand_name_en + row.brand_name_cn
//                     });

//                     obj[key] = build;

//                 });

//             }

//             resu.brandList = JSON.stringify(obj);
//         }


//         if (result && result[2] && result[2].data) {

//             let hotList = result[2].data;

//             for (let key in hotList) {

//                 let obj = {};

//                 obj = Object.assign(obj, {name: hotList[key].brandName});

//                 resu.hot.push(obj);

//             }

//         }

//         if (result && result[1] && result[1].data) {

//             let hisList = result[1].data;

//             let obj = [];

//             let build = {

//                 list: []

//             };

//             for (let key in hisList) {

//                 obj.push({
//                     name: hisList[key].searchTerms,
//                     searchTime: hisList[key].searchTime
//                 });

//             }

//             build.list = obj;

//             resu.history = build;

//         }

//         return resu;

//     });
// };
/**
 * 获取搜索的品牌列表 pagecache重构
 *
 * @param int $channel 1表示男生频道, 2表示女生频道, 3表示潮童频道, 4表示创意生活频道
 * @return array
 */
const branchSearch = (params) => {
    return api.all([

        getBrandForSearch(params.channel),

        // branchSearchHistory(params),
        branchSearchHot(),

    ]).then(result => {

        let resu = {
            brandPage: true,
            pageFooter: true,
            hot: [],
            history: [],
            brandList: []
        };

        if (result && result[0] && result[0].data) {

            let brandList = result[0].data.brands;

            let obj = {};

            for (let key in brandList) {
                if (key) {
                    let build = [];

                    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: helpers.urlFormat('/product/index/brand?domain=' + row.brand_domain),
                            brandId: row.id,
                            brandDomain: row.brand_domain,
                            searchName: row.brand_name_en + row.brand_name_cn
                        });
                        obj[key] = build;
                    });
                }
            }

            resu.brandList = JSON.stringify(obj);
        }


        if (result && result[1] && result[1].data) {

            let hotList = result[1].data;

            for (let key in hotList) {
                if (key) {
                    let obj = {};

                    obj = Object.assign(obj, {
                        name: hotList[key].brandName
                    });
                    resu.hot.push(obj);
                }
            }
        }

        // if (result && result[1] && result[1].data) {

        //     let hisList = result[1].data;

        //     let obj = [];

        //     let build = {

        //         list: []

        //     };

        //     for (let key in hisList) {

        //         obj.push({
        //             name: hisList[key].searchTerms,
        //             searchTime: hisList[key].searchTime
        //         });

        //     }

        //     build.list = obj;

        //     resu.history = build;

        // }

        return resu;

    });
};

const delBrandSearchHistory = (param) => {

    return api.get('', {
        method: 'app.search.clearHotBrandRecords',
        uid: param.uid

    }, {
        code: 200
    }).then(result => {

        return result;

    });
};

/**
 * [异步获取用户最近搜索词]
 */
const branchSearchHistoryAsync = (uid) => {
    return branchSearchHistory({
        uid
    }).then((result) => {
        let hisList = result.data;

        let obj = [];

        let build = {

            list: []

        };

        for (let key in hisList) {
            if (key) {
                obj.push({
                    name: hisList[key].searchTerms,
                    searchTime: hisList[key].searchTime
                });
            }
        }

        build.list = obj;

        return build;
    });
};

module.exports = {
    getBrandByChannel,
    getBrandListByChannel,
    branchSearch,
    delBrandSearchHistory,
    addSearchHistory,
    branchSearchHistoryAsync
};