Blame view

apps/brands/models/brands-model.js 15.3 KB
hongweigao authored
1 2 3 4 5 6 7 8 9 10 11 12

/**
 * 品牌一览 model
 * @author: ghw<hongwei.gao@yoho.cn>
 * @date: 2016/9/29
 */
'use strict';

const Promise = require('bluebird');
const co = Promise.coroutine;
const _ = require('lodash');
const helpers = global.yoho.helpers;
OF1706 authored
13 14 15
const BrandApi = require('./brands-api');
const path = require('path');
const imgUtils = require(path.join(global.utils, 'images'));
hongweigao authored
16 17

// 品牌一览资源位CODE码
hongweigao authored
18 19 20 21 22
const channelCode = {
    boys_brand: '8b16b7baf9a66fbe553a6caa97d2ce2a',
    girls_brand: 'c95ae9e40f0add10549b819f821ad626',
    kids_brand: 'c575c6bfdfa4125fae7d24bbec7119c8',
    lifestyle_brand: '84b7926282fdef92f1039bdcf77c18ba',
hongweigao authored
23
    brand_list: 'f0f72b1e8f30e6ad086dfc4401f3a856', // 品牌列表资源位CODE码
hongweigao authored
24
    brand_plusstar_banner_boys: 'd0149783b8dd2adaf083fd10556c39a9',
25
    brand_plusstar_banner_girls: 'aad7a43e9a04ac7c70ae7f0c1acf86ef',
hongweigao authored
26 27
    brand_plusstarindex_boys: 'a833aed63d28457156310e97faa7fa37', // plusstarindex男首资源位
    brand_plusstarindex_girls: '6e4f162be3b3ba44f3bfcf1c38bdb745' // plusstarindex女首资源位
hongweigao authored
28
};
29
hongweigao authored
30 31 32 33
const BOYS = 'boys';
const GIRLS = 'girls';
const KIDS = 'kids';
const LIFESTYLE = 'lifestyle';
hongweigao authored
34
yyq authored
35 36
const GLOBAL_BASE_URI = '/product/global/list';
OF1706 authored
37 38 39
module.exports = class extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);
hongweigao authored
40 41
    }
OF1706 authored
42 43 44 45 46 47 48
    /**
     * 获取品牌一览资源位&channelType
     *
     * @param string $channelStr
     * @return array
     */
    switchBrandParams(channel) {
hongweigao authored
49
OF1706 authored
50
        let req = {};
hongweigao authored
51
OF1706 authored
52
        switch (channel) {
hongweigao authored
53
OF1706 authored
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
            case BOYS:
                req = {
                    channelType: 1,
                    brandCode: channelCode.brand_plusstar_banner_boys
                };
                break;
            case GIRLS:
                req = {
                    channelType: 2,
                    brandCode: channelCode.brand_plusstar_banner_girls
                };
                break;
            case KIDS:
                req = {
                    channelType: 3,
                    brandCode: channelCode.kids_brand
                };
                break;
            case LIFESTYLE:
                req = {
                    channelType: 4,
                    brandCode: channelCode.lifestyle_brand
                };
                break;
            default:
                req = {
                    channelType: 1,
                    brandCode: channelCode.boys_brand
                };
                break;
84
        }
OF1706 authored
85 86
        return req;
    }
87
OF1706 authored
88 89 90 91 92 93 94
    /**
     * 获取品牌一览页面,品牌top
     * @param string $channel 频道名称
     */
    getBrandViewTop(channel) {
        let that = this;
        let brandData = new BrandApi(that.ctx);
95
OF1706 authored
96 97
        return co(function*() {
            let switchParams = that.switchBrandParams(channel);
hongweigao authored
98
OF1706 authored
99
            let res = yield brandData.getBrandTopData(switchParams.brandCode);
100
OF1706 authored
101 102 103
            let result = {},
                brandAds = [],
                brandLogos = [];
104
OF1706 authored
105 106 107
            if (!res || res.code !== 200) {
                return result;
            }
hongweigao authored
108
OF1706 authored
109 110
            // 头部10个品牌小图块 url
            if (res.data && res.data instanceof Array && res.data[1].data && res.data[1].data.list) {
OF1706 authored
111
OF1706 authored
112 113 114 115 116 117 118 119 120 121 122 123
                _.forEach(res.data[1].data.list, subValue => {
                    brandAds.push({
                        name: subValue.name,
                        src: helpers.image(imgUtils.getImageUrl(
                            (subValue.src.match(/(\S*)imageView2/)[1] +
                            'imageMogr2/thumbnail/{width}x{height}/extent/{width}x{height}/' +
                            'background/d2hpdGU=/position/center/quality/80'),
                             136, 57), 136, 57),
                        url: subValue.url
                    });
                });
            }
hongweigao authored
124
OF1706 authored
125 126 127 128
            // 头部品牌图块,广告位
            if (res.data && res.data instanceof Array && res.data[0].data) {
                _.forEach(res.data[0].data, (subValue, k) => {
                    let srcUrl;
hongweigao authored
129
OF1706 authored
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
                    // kids lifestyle 第一张图尺寸不同
                    if (switchParams.channelType === 1 || switchParams.channelType === 2) {
                        srcUrl = helpers.image(subValue.src, 222, 180, 3);
                    } else {
                        srcUrl = (k === 0) ? helpers.image(subValue.src, 570, 280, 3) :
                            helpers.image(subValue.src, 280, 280, 3);
                    }
                    let brandPlusstarItem = {
                        name: subValue.title,
                        src: srcUrl,
                        url: subValue.url
                    };

                    if (channel === BOYS || channel === GIRLS) {
                        if (k === 0) {
                            brandPlusstarItem.url = helpers.urlFormat('/brands/plusstar', {channel: channel});
                        } else {
                            brandPlusstarItem.url = helpers.urlFormat('/brands/plusstar', {id: k, channel: channel});
                        }
                    }
hongweigao authored
150
OF1706 authored
151 152 153
                    brandLogos.push(brandPlusstarItem);
                });
            }
hongweigao authored
154
OF1706 authored
155 156 157 158 159 160
            // 整合brandTop数据结构,boys、girls
            if (switchParams.channelType === 1 || switchParams.channelType === 2) {
                result.isTab = true;
            }
            result.tabHeader = brandLogos;
            result.logos = brandAds;
hongweigao authored
161
162
            return result;
OF1706 authored
163 164
        })();
    }
165
OF1706 authored
166 167 168 169 170 171 172 173 174
    /**
     * 获取品牌一览list
     * @param string $channel 频道名称
     * @param int start  开始位置 1 开始
     * @param int length 取数长度 0 取到最后
     */
    getBrandViewList(channel, start, length) {
        let that = this;
        let brandData = new BrandApi(that.ctx);
hongweigao authored
175
OF1706 authored
176 177
        return co(function*() {
            let switchParams = that.switchBrandParams(channel);
hongweigao authored
178
OF1706 authored
179
            let res = yield brandData.getBrandListData(switchParams.channelType);
180
OF1706 authored
181 182
            let result = [],
                numResult = {};
hongweigao authored
183
OF1706 authored
184 185 186
            if (!res || res.code !== 200) {
                return result;
            }
187
OF1706 authored
188 189 190 191 192 193 194 195 196 197 198 199 200
            // 品牌list A-Z 0-9
            if (res.data && res.data.all_list) {

                _.forEach(res.data.all_list, (subValue, key) => {
                    let listTmp = [];

                    _.forEach(subValue, ssubValue => {
                        let extQs = {};
                        let baseUri = '';

                        // 为品牌名称
                        let href;
yyq authored
201 202 203 204 205
                        // if (switchParams.channelType === 1) {
                        //     Object.assign(extQs, {gender: '1,3'});
                        // } else if (switchParams.channelType === 2) {
                        //     Object.assign(extQs, {gender: '2,3'});
                        // }
OF1706 authored
206 207 208 209 210 211 212 213 214 215 216 217 218 219
                        let shopInfo, shopId;

                        switch (ssubValue.type * 1) {
                            case 1:
                                extQs = {
                                    query: ssubValue.brand_name,
                                    brand: ssubValue.id
                                };
                                ssubValue.brand_domain = 'search';
                                break;
                            case 2:
                                shopInfo = _.get(ssubValue, 'shop_info.yoho_shop_list[0]', {});
                                shopId = shopInfo.shop_id || ssubValue.shop_id;
yyq authored
220 221 222
                                baseUri = `/shop/${_.compact([shopInfo.shop_domain || ssubValue.brand_domain ||
                                    'default_domain', shopId]).join('-')}.html`;
                                ssubValue.brand_domain = '';
OF1706 authored
223 224 225 226 227 228 229 230 231 232
                                break;
                            case 3:
                                Object.assign(extQs, {brand: ssubValue.global_brand_id});
                                ssubValue.brand_domain = '';
                                baseUri = GLOBAL_BASE_URI;
                                break;
                            default:
                                break;
                        }
yyq authored
233
                        href = helpers.urlFormat(baseUri, _.isEmpty(extQs) ? null : extQs, ssubValue.brand_domain);
OF1706 authored
234 235 236 237 238 239 240 241 242 243 244

                        let brandItem = {
                            name: ssubValue.brand_name,
                            key: ssubValue.id,
                            href: href
                        };

                        if (ssubValue.is_hot === 'Y') {
                            brandItem.hot = 'hot';
                        }
                        listTmp.push(brandItem);
245
OF1706 authored
246
                    });
hongweigao authored
247
OF1706 authored
248 249 250 251 252 253 254 255 256 257
                    if (key.match(/\d+/g)) {
                        numResult = {// 把0-9提出来
                            key: key,
                            val: _.sortBy(listTmp, 'name')// 对name排序
                        };
                    } else {
                        result.push({
                            key: key,
                            val: _.sortBy(listTmp, 'name')// 对name排序
                        });
hongweigao authored
258
                    }
hongweigao authored
259 260
                });
OF1706 authored
261
            }
262
OF1706 authored
263 264
            // 只取部分数据
            let begin;
hongweigao authored
265
OF1706 authored
266 267 268 269 270
            if (start) {
                begin = (start - 1) ? (start - 1) : 0;
                begin = (begin > 0) ? begin : 0;
                result = length ? result.slice(begin, length + begin) : result.slice(begin);
            }
hongweigao authored
271
OF1706 authored
272 273 274 275 276 277
            result = _.sortBy(result, 'key');// A-Z排序
            result.push(numResult); // 0-9放到最后
            result.navigation = _.map(result, 'key');
            return result;
        })();
    }
hongweigao authored
278
OF1706 authored
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
    /**
     * 获取单个广告浮窗内容
     *
     * @param int $brandId
     * @param int $uid
     * @return array
     */
    getBrandInfo(brandId, uid) {
        let that = this;
        let brandData = new BrandApi(that.ctx);

        return co(function*() {
            let data = {},
                imgs = [];

            // 获取品牌简介
            let res = yield brandData.getBrandIntro(brandId, uid);

            if (!res || res.code !== 200) {
298 299
                return data;
            }
OF1706 authored
300 301 302
            if (res.data) {
                // 获取品牌下的产品信息
                let proInfo = yield brandData.getProductByBrand(brandId, 3);
hongweigao authored
303
OF1706 authored
304 305 306 307 308 309 310 311 312 313
                if (!proInfo || proInfo.code !== 200) {
                    return data;
                }
                let proInfoTmp = proInfo.data.product_list ? proInfo.data.product_list : [];

                if (!_.isEmpty(proInfoTmp)) {
                    _.forEach(proInfoTmp, subValue => {
                        imgs.push({
                            src: helpers.image(subValue.default_images, 80, 100, 3)
                        });
hongweigao authored
314
                    });
OF1706 authored
315 316 317 318 319 320 321 322 323 324 325
                }

                // 整合
                data = {
                    key: res.data.brand_id,
                    icon: helpers.image(res.data.brand_ico, 80, 50, 3),
                    title: res.data.brand_name,
                    content: res.data.brand_intro,
                    subtitle: 'FEATURED ITEMS',
                    imgs: imgs
                };
hongweigao authored
326
            }
OF1706 authored
327 328 329
            return data;
        })();
    }
hongweigao authored
330
OF1706 authored
331 332 333 334 335 336 337 338 339
    /**
     * 多个品牌ID获取品牌信息
     *
     * @param array $brandIds
     * @return array
     */
    getBrandInfoByIds(brandIds) {
        let that = this;
        let brandData = new BrandApi(that.ctx);
hongweigao authored
340
OF1706 authored
341 342
        return co(function*() {
            let res = yield brandData.getBrandInfoByIds(brandIds);
hongweigao authored
343
OF1706 authored
344
            let brandsInfo = {};
hongweigao authored
345
OF1706 authored
346 347 348 349 350 351
            if (!res || res.code !== 200) {
                return brandsInfo;
            }
            if (res.data && res.code === 200) {
                _.forEach(res.data, (subValue, k) => {
                    subValue.desc = _.trim(subValue.brand_intro.replace(/(\t)|(\n)|(\r)|(&nbsp;)/g, '')
hongweigao authored
352
                        .replace(/<.*?>/ig, ''));
OF1706 authored
353 354 355 356 357
                    subValue.url = subValue.brand_domain;
                    delete subValue.brand_intro;
                    brandsInfo[k] = subValue;
                });
            }
hongweigao authored
358
OF1706 authored
359 360 361
            return brandsInfo;
        })();
    }
hongweigao authored
362
OF1706 authored
363 364 365 366 367 368 369 370 371
    /**
     * 获取plusstar品牌列表项目
     *
     * @param string $channel
     * @return array
     */
    getPlusstarBrandListItem(channel) {
        let that = this;
        let brandData = new BrandApi(that.ctx);
hongweigao authored
372
OF1706 authored
373 374 375
        return co(function*() {
            let code = channel === 'girls' ? channelCode.brand_plusstar_banner_girls :
                channelCode.brand_plusstar_banner_boys;
hongweigao authored
376
OF1706 authored
377 378
            // 资源位数据
            let resource = yield brandData.getPlusstarBrandListItem(code);
hongweigao authored
379
OF1706 authored
380
            let items = [];
hongweigao authored
381
OF1706 authored
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
            if (!resource || resource.code !== 200) {
                return items;
            }
            if (resource.data && resource.code === 200) {
                items[0] = {name: '所有品牌', src: '', url: helpers.urlFormat('/brands',
                           {channel: channel}), brandType: ''};
                items[1] = {name: '设计新潮', src: '', url: '', brandType: 4};
                items[2] = {name: '潮流经典', src: '', url: '', brandType: 1};
                items[3] = {name: '明星潮牌', src: '', url: '', brandType: 2};
                items[4] = {name: '原创潮牌', src: '', url: '', brandType: 3};

                let resourceData = resource.data,
                    pos = 0;

                _.forEach(items, (subValue, k) => {
                    if (_.isEmpty(subValue.url)) {
                        subValue.url = helpers.urlFormat('/brands/plusstar', {id: k, channel: channel});
                    }
hongweigao authored
400
OF1706 authored
401 402 403 404 405 406 407
                    if (pos in resourceData[0].data) {
                        subValue.src = helpers.image(resourceData[0].data[pos].src, 222, 180, 1);
                        subValue.name = resourceData[0].data[pos].title;
                    }
                    pos++;
                });
            }
hongweigao authored
408
OF1706 authored
409 410 411
            return items;
        })();
    }
hongweigao authored
412
OF1706 authored
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
    /**
     * 获取Plustar列表
     *
     * @param string $brandType
     * @param string $gender
     * @return array
     */
    getPlustarList(brandType, gender) {
        let that = this;
        let brandData = new BrandApi(that.ctx);

        return co(function*() {
            let list = yield brandData.getPlusstarList(brandType, gender);

            let brandList = {},
                data = {},
                brandsIds = [],
                result = {brandsIds: [], data: {}};

            if (!list || list.code !== 200) {
                return result;
            }
            if (list.data && list.data.data && list.data.data.list) {
                brandList = list.data.data.list[0];
            }
            if (brandList.data) {
                _.forEach(brandList.data, brand => {
                    let src = '';
441
OF1706 authored
442 443 444 445 446 447 448 449 450 451 452 453 454
                    if (brand.data[0]) {
                        src = helpers.image(brand.data[0].src, 320, 160, 1);
                    }
                    data[brand.brand_id] = {
                        brand_id: brand.brand_id,
                        name: brand.brand_name,
                        sort_id: brand.sort_id,
                        src: src,
                        desc: '',
                        url: ''
                    };
                    brandsIds.push(brand.brand_id);
                });
hongweigao authored
455
OF1706 authored
456 457 458
                result.brandsIds = brandsIds;
                result.data = data;
            }
hongweigao authored
459
OF1706 authored
460 461 462
            return result;
        })();
    }
hongweigao authored
463
};