Blame view

apps/product/models/sale-handler.js 7.81 KB
郭成尧 authored
1
/*
郭成尧 authored
2 3 4
 * @Author: Targaryen
 * @Date:   2016-05-25 18:16:59
 * @Last Modified by:   Targaryen
郭成尧 authored
5
 * @Last Modified time: 2016-06-20 21:31:11
郭成尧 authored
6
 */
郭成尧 authored
7 8

'use strict';
姜枫 authored
9
const helpers = global.yoho.helpers;
郭成尧 authored
10
const _ = require('lodash');
郭成尧 authored
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
const anHour = 3600000;
const aDay = anHour * 24;
const timeFormat = {
    d: '剩{d}天',
    h: '剩{h}小时',
    m: '剩{m}分钟',
    s: '剩{s}秒',
    dh: '剩{d}天{h}小时',
    dhms: '剩{d}天{h}小时{m}分钟{s}秒',
    hms: '剩{h}小时{m}分钟{s}秒',
    ms: '剩{m}分钟{s}秒'
};

/**
 * 折扣专场专题列表过期时间处理
郭成尧 authored
26 27
 * @param time 接口返回的时间
 * @returns {*|string}
郭成尧 authored
28 29 30 31 32 33 34
 */
const processTime = (time) => {
    let data = {};
    let type = '';

    if (time < anHour) {
        data.warnColor = true;
35 36
        type = 'm';
        data.time = helpers.dayHourMinuteFromDiff(timeFormat[type], time, 'ms');
郭成尧 authored
37 38 39 40 41 42 43
    } else {
        if (time > aDay) {
            type = 'dh';
        } else {
            type = 'h';
        }
44
        data.time = helpers.dayHourMinuteFromDiff(timeFormat[type], time, 'ms');
郭成尧 authored
45 46 47 48
    }

    return data.time;
};
郭成尧 authored
49
郭成尧 authored
50
/**
郭成尧 authored
51
 * 折扣文本切割
郭成尧 authored
52
 * @param text 要切割的文本
张丽霞 authored
53
 * @param targetString 目标字符,为null时按字符数字切割
郭成尧 authored
54
 * @returns {{discount: *, discountText: *}}
郭成尧 authored
55
 */
张丽霞 authored
56
const discountSplit = (text, targetString) => {
郭成尧 authored
57 58 59
    let endNum = 0,
        i;
梁志锋 authored
60
    text = text || '';
郭成尧 authored
61
    for (i = 0; i < text.length; i++) {
张丽霞 authored
62 63 64 65 66 67 68 69
        if (targetString !== null) {
            if (text[i] !== targetString) {
                endNum = i + 1;
            }
        } else {
            if (/^([0-9]|\%)*$/.test(text[i])) {
                endNum = i + 1;
            }
郭成尧 authored
70 71 72 73 74 75 76 77 78 79
        }
    }

    return {
        discount: text.slice(0, endNum),
        discountText: text.slice(endNum, text.length)
    };
};

/**
郭成尧 authored
80
 * 处理折扣专区活动数据
郭成尧 authored
81 82 83
 * @param origin 原始数据
 * @param channel 频道 boys girls kids lifestyle
 * @returns {{}}
郭成尧 authored
84
 */
郭成尧 authored
85
exports.handleSaleActivityData = (origin, channel) => {
郭成尧 authored
86
    let dest = {
郭成尧 authored
87 88 89
        big: [],
        normal: []
    };
郭成尧 authored
90
91
    _.forEach(origin, function(value, key) {
郭成尧 authored
92 93
        let activity = {
            link: helpers.urlFormat('/product/sale/discount/detail', {id: value.id, channel: channel}),
94
            img: value.web_cover_url,
郭成尧 authored
95 96 97 98 99
            time: processTime(parseInt(value.left_time, 10) * 1000),
            brand: value.logo_url,
            title: value.title
        };
张丽霞 authored
100
        activity = Object.assign(activity, discountSplit(value.promotion_name), null);
郭成尧 authored
101 102 103

        if (key < 3) {
            dest.big.push(activity);
104
        } else if (key < 27) {
郭成尧 authored
105 106 107 108 109 110 111
            dest.normal.push(activity);
        }
    });
    return dest;
};

/**
郭成尧 authored
112 113 114
 * 处理折扣专区标题折扣倒计时等数据
 * @param origin 要处理的原始数据
 * @returns {{}}
115 116
 */
exports.handleDiscountTitleData = (origin) => {
郭成尧 authored
117
    let dest = {
郭成尧 authored
118 119 120 121
        title: origin.title,
        up: true,
        time: parseInt(origin.left_time, 10) * 1000
    };
122
张丽霞 authored
123
    Object.assign(dest, discountSplit(origin.promotion_name, '起'));
124 125 126 127 128

    return dest;
};

/**
郭成尧 authored
129
 * 处理首页 banner 数据
郭成尧 authored
130 131
 * @param origin 要处理的原始数据
 * @returns {{}}
郭成尧 authored
132 133
 */
exports.handleSaleBannerData = (origin) => {
郭成尧 authored
134
    let dest = {
郭成尧 authored
135 136
        list: []
    };
郭成尧 authored
137
138
    _.forEach(origin, function(value) {
郭成尧 authored
139
        if (value.template_name === 'focus') {
140
            _.forEach(value.data, function(subValue) {
郭成尧 authored
141 142 143 144 145
                let banner = {
                    bannerHeight: 450,
                    href: subValue.url,
                    img: subValue.src
                };
郭成尧 authored
146 147 148 149 150 151 152 153 154 155

                dest.list.push(banner);
            });
        }
    });

    return dest;
};

/**
156
 * 处理首页 banner 小图 threePicture楼层和small_pic楼层都去一遍,产品确认只会有一个接口有数据
郭成尧 authored
157 158
 * @param origin 要处理的原始数据
 * @returns {Array}
郭成尧 authored
159 160
 */
exports.handleSaleBannerSmallData = (origin) => {
郭成尧 authored
161
    let dest = [];
郭成尧 authored
162
    let count = 0;
163
    let hasThreePic = false;
郭成尧 authored
164
165
    _.forEach(origin, function(value) {
166
        if (value.template_name === 'threePicture') {
167
            hasThreePic = true;
郭成尧 authored
168 169
        }
    });
170
    _.forEach(origin, function(value) {
171
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
        if (hasThreePic) {
            if (value.template_name === 'threePicture') {
                _.forEach(value.data, function(picList) {
                    if (count++ < 3) {
                        let smallPic = {
                            link: picList.url,
                            icon: picList.src,
                            title: picList.title,
                            desc: picList.alt
                        };

                        dest.push(smallPic);
                    }
                });
            }
        } else {
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
            if (value.template_name === 'small_pic') {
                _.forEach(value.data, function(picList) {
                    if (count++ < 4) {
                        let smallPic = {
                            link: picList.url,
                            icon: picList.src,
                            title: picList.title,
                            desc: picList.alt
                        };

                        dest.push(smallPic);
                    }
                });
            }
        }
郭成尧 authored
204 205 206
    });

    return dest;
郭成尧 authored
207
};
208 209

/**
郭成尧 authored
210
 * 处理首页楼层分类数据
郭成尧 authored
211
 * 修改为取大类前 4 个
郭成尧 authored
212 213 214
 * @param origin 要处理的原始数据
 * @param saleType 获取数据分属哪个类目:断码区,会员专享,折扣专场等
 * @param channel 频道
郭成尧 authored
215
 * @param breakingSizeSort 为断码去额外拼接参数,接口比较垃圾嘛,没办法
郭成尧 authored
216
 * @returns {{}}
217
 */
郭成尧 authored
218
exports.handleSaleCategoryData = (origin, saleType, channel, breakingSizeSort) => {
郭成尧 authored
219
220 221 222 223 224 225 226 227
    let dest = {
        navItem: [
            {
                title: '今日推荐',
                vip: true
            }
        ]
    };
郭成尧 authored
228
郭成尧 authored
229 230
    let urlLocation = {};
郭成尧 authored
231 232
    // title urlLocation 处理
    switch (saleType) {
郭成尧 authored
233
        case '5':
郭成尧 authored
234
            dest.title = '断码区';
235 236 237
            urlLocation = {
                url: '?saleType=' + saleType + '&order=s_t_desc&channel=' + channel +
                '&breakSize=' + breakingSizeSort.breakSize + '&breakSort=' + breakingSizeSort.breakSort,
238
                localUrl: '?saleType=' + saleType + '&order=s_t_desc&channel=' + channel,
239 240
                limit: 14
            };
郭成尧 authored
241 242 243
            break;
        case '2':
            dest.title = 'VIP会员专享';
244 245 246 247
            urlLocation = {
                url: '?saleType=' + saleType + '&order=s_t_desc&channel=' + channel,
                limit: 11
            };
郭成尧 authored
248 249 250
            break;
        case '3':
            dest.title = '最新降价';
251 252 253 254
            urlLocation = {
                url: '?saleType=0&order=s_t_desc&channel=' + channel,
                limit: 14
            };
郭成尧 authored
255 256 257 258
            break;
        default:
            break;
    }
259
    dest.urlLocation = dest.navItem[0].urlLocation = urlLocation.url + `&limit=${urlLocation.limit}`;
郭成尧 authored
260 261 262 263 264 265

    let count = 0;

    _.forEach(origin, (value) => {

        if (count++ < 4) {
266 267 268 269 270 271 272 273
            let forBreakingUrl = '';

            if (saleType === '1') {
                forBreakingUrl = urlLocation.localUrl;
            } else {
                forBreakingUrl = urlLocation.url;
            }
274
            let nav = {
275
                urlLocation: forBreakingUrl + '&sort=' + value.relation_parameter.sort +
276 277 278 279
                `&limit=${(urlLocation.limit + 1)}`,
                title: value.category_name,
                newDiscount: true
            };
280
郭成尧 authored
281 282 283 284 285
            dest.navItem.push(nav);
        }
    });

    return dest;
286
};
郭成尧 authored
287 288

/**
289 290 291 292
 * 处理断码区尺码数据,输出整合后的筛选条件
 * @param origin
 */
exports.handleSaleBreakingSizeData = (origin) => {
郭成尧 authored
293 294 295 296
    let dest = {
        breakSize: '',
        breakSort: ''
    };
297 298 299 300 301 302 303 304

    _.forEach(origin, value => {
        dest.breakSort += value.sort_id;

        _.forEach(value.sub, subValue => {
            dest.breakSize += subValue.size_id + ',';
        });
    });
305
306
    return dest;
307
};