sale.js 9.76 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
/**
 * 频道页面 model
 * @author: wsl<shuiling.wang@yoho.cn>
 * @date: 2016/05/17
 */
'use strict';
const utils = '../../../utils';
const contentCodeConfig = require('../../../config/content-code');

const logger = global.yoho.logger;
const resourcesProcess = require(`${utils}/resources-process`);
const productProcess = require(`${utils}/product-process`);
const processTime = require(`${utils}/time-process`);
const _ = require('lodash');
const api = global.yoho.API;
const serviceAPI = global.yoho.ServiceAPI;
const helpers = global.yoho.helpers;

/**
 * 排序转换
 */
const typeCont = {
    price: ['s_p_desc', 's_p_asc'],
    discount: ['p_d_desc', 'p_d_asc'],
    sale: ['s_n_desc', 's_n_asc'],

    // newest: ['s_t_desc,s_s_desc', 's_t_asc,s_s_asc'],
    newest: ['s_t_desc', 's_t_asc'],
    stock: ['s_s_desc', 's_s_asc'],
    all: ['d_s_desc', 'd_s_asc'],
    category: ['s_t_desc', 's_t_asc']
};

const channelType = {
    boys: '1',
    girls: '2',
    kids: '3',
    lifestyle: '4',
    all: '1,2,3,4'
};

/**
 * 频道转换
 * 产品要求,SALE的导航显示 Boy,Girl 单数形式
 */
const channelHash = {
    boys: 'Boy',
    girls: 'Girl',
    kids: 'Kid',
    lifestyle: 'Lifestyle'
};

const saleNav = (channel) => {
    return {
        channel: channelHash[channel],
        list: [
            {
                title: 'Boy',
                url: helpers.urlFormat('/product/sale', {channel: 'boys'})
            }, {
                title: 'Girl',
                url: helpers.urlFormat('/product/sale', {channel: 'girls'})
            }, {
                title: 'Kid',
                url: helpers.urlFormat('/product/sale', {channel: 'kids'})
            }, {
                title: 'Lifestyle',
                url: helpers.urlFormat('/product/sale', {channel: 'lifestyle'})
            }
        ]
    };
};

/**
 * 资源位code码
 */
const contentCode = contentCodeConfig.sale;

/**
 * 折扣专场列表页及详情页公共方法
 * @param  {[array]} list
 * @return {[array]}
 */
const _processDiscount = (list) => {
    const formatData = [];
    let flag = true;

    list = list || [];

    // list为1条数据是表示详情页,flag 设为false,不需要拼接跳转链接
    if (list.length === 1) {
        flag = false;
    }

    _.forEach(list, (data) => {
        if (flag === true) {
            Object.assign(data, processTime(data.left_time));
        }

        formatData.push(data);
    });

    return formatData;
};

/**
 * 折扣专场接口调用
 * @param  {[object]} params
 * @return {[array]}
 */
const _discount = (params) => {
    params = params || {};

    return api.get('', Object.assign({
        method: 'app.activity.get',
        sort: 2,
        plateform: 3
    }, params), {
        cache: true
    });
};

/**
 * 断码区分类数据处理
 * @param  {[array]} list
 * @return {[array]}
 */
const _processBreakingSort = (list) => {
    const formatData = {};
    const sort = [];
    const sub = [];

    list = list || [];

    _.forEach(list, (data, index) => {
        const allSub = [];

        data.sub.key = index;
        sub.push(data.sub);

        _.forEach(data.sub, (dataSub) => {
            allSub.push(dataSub.size_id);
        });

        sort.push({
            sort_name: data.sort_name,
            sort_id: data.sort_id,
            allSub: allSub
        });
    });

    formatData.sortData = sort;
    formatData.sub = sub;

    return formatData;
};

/**
 * 商品搜索接口请求
 * @param  {[object]} params
 * @return {[array]}
 */
const _searchSales = (params) => {

    let method = 'app.search.sales';

    if (params.type === 'default') {
        // 筛选 默认 不传order
        delete params.order;
    }

    // 排除基本筛选项默认值为0的对象
    for (let str in params) {
        if (str !== 'order' && params[str] === '0' || params[str] === null) {
            delete params[str];
        }
    }

    params.yh_channel = channelType[params.yh_channel];

    if (params.outlets) {
        method = 'app.search.category';
    }

    params = Object.assign({
        limit: '60'
    }, params);

    // 5.0.0 sale 按后台导入顺序排列
    if (params.type === 'all') {
        params.type = '';
    }

    if (typeCont[params.type]) {
        params.order = typeCont[params.type][params.order];
    }

    return api.get('', Object.assign({
        method: method
    }, params), {
        cache: true
    });
};

/**
 * 获取用户数据信息
 * @param  {[string]} uid
 * @return {[array]}
 */
const _getUserProfile = (uid) => {
    if (!uid) {
        return Promise.resolve({
            code: 200,
            data: {}
        });
    }
    return api.get('', {
        method: 'app.passport.profile',
        uid: uid
    }, {
        cache: true
    });
};

/**
 * 获取资源位数据
 * @param  {[string]} page
 * @return {[array]}
 */
const _getResources = (page, channel) => {
    return serviceAPI.get('operations/api/v5/resource/get', {
        content_code: contentCode[channel][page]
    }, {
        cache: true
    }).then((result) => {
        if (result && result.code === 200) {
            return resourcesProcess(result.data);
        } else {
            logger.error('SALE content resource code no 200');
            return [];
        }
    });
};

/**
 * 获取断码区分类数据
 * @param  {[string]} yhChannel
 * @return {[object]}
 */
const _getBreakingSort = (yhChannel) => {
    return api.get('', {
        method: 'app.sale.getBreakingSort',
        yh_channel: channelType[yhChannel] || '1'
    }, {
        cache: true
    }).then((result) => {
        if (result && result.code === 200) {

            return _processBreakingSort(result.data);
        } else {
            logger.error('api app.sale.getBreakingSort code no 200');
            return {};
        }
    });
};

/**
 * 获取商品数据
 */
const getSearchData = (params, uid) => {
    return Promise.all([
        _searchSales(params).then((result) => {
            if (result && result.code === 200) {
                return productProcess.processProductList(result.data.product_list || [], {
                    yh_channel: params.yh_channel,
                    showSale: false
                });
            } else {
                logger.error('api SALE product search code no 200');
                return [];
            }
        }),
        _getUserProfile(uid).then((result) => {

            if (result && result.code === 200) {
                return result.data.vip_info ? result.data.vip_info : {};
            } else {
                logger.error('api get user info code no 200');
                return {};
            }
        })
    ]);
};

/**
 * 获取筛选数据
 * @param  {[object]} params
 * @return {[array]}
 */
const getFilterData = (params) => {
    return _searchSales(params).then((result) => {
        if (result && result.code === 200) {
            return productProcess.processFilter(result.data.filter || [], {
                hideSize: params.saleType === '1',
                hideSort: params.saleType === '1'
            });
        } else {
            logger.error('SALE search product code no 200');
            return [];
        }
    });
};

/**
 * 获取sale首页数据
 * @return {[array]}
 */
const getSaleData = (channel) => {
    return _getResources('sale', channel);
};


/**
 * 获取会员享数据
 * @return {[array]}
 */
const getVipData = (channel) => {
    return _getResources('vip', channel);
};


/**
 * 获取断码区数据
 * @param  {[object]} params
 * @return {[object]}
 */
const getBreakCodeData = (params) => {
    params = params || {};
    return Promise.all([_getResources('breakCode', params.yhChannel), _getBreakingSort(params.yhChannel)])
        .then((result) => {
            return {
                content: result[0],
                nav: result[1]
            };
        });
};

/**
 * 获取折扣专场专题列表数据
 * @param  {[object]} params
 * @return {[object]}
 */
const getDiscountData = (yhChannel) => {
    return _discount({
        yh_channel: channelType[yhChannel] || '1'
    }).then((result) => {
        if (result && result.code === 200) {
            return {
                list: _processDiscount(result.data)
            };
        } else {
            logger.error('api discount list code no 200');
            return {};
        }
    });
};

/**
 * 获取折扣专场专题详情数据
 * @param  {[string]} id
 * @return {[object]}
 */
const getDiscountDetailData = (id, yhChannel) => {
    let res = {};
    let param = {
        id: id,
        yh_channel: channelType[yhChannel] || '1'
    };

    return _discount(param).then((result) => {
        if (result && result.code === 200) {
            res = _processDiscount(result.data);
            if (res[0] && res[0].cover_url) {
                res[0].cover_url = res[0].cover_url.replace('/extent/{width}x{height}', '');
            }
            return {
                title: res[0] && res[0].title,
                product_pool: res[0] && res[0].product_pool,
                activity: {
                    cover_url: res[0] && res[0].cover_url

                    // left_time: res[0] && res[0].left_time
                }
            };
        } else {
            logger.error('discount detail code no 200');
            return {};
        }
    });
};

const getDiscountTime = (id, yhChannel) => {
    let param = {
        id: id,
        yh_channel: channelType[yhChannel] || '1'
    };

    return _discount(param).then((result) => {
        if (result && result.code === 200) {
            return result.data[0].left_time;
        } else {
            logger.error('get discount time code no 200');
            return {};
        }
    });
};

module.exports = {
    getSaleData,
    getBreakCodeData,
    getDiscountData,
    getDiscountDetailData,
    getVipData,
    getFilterData,
    getSearchData,
    saleNav,
    getDiscountTime
};