Blame view

apps/product/models/outlet.js 7.61 KB
1 2
/**
 * 频道页面 model
3
 * @author: 赵彪<bill.zhao@yoho.cn>
4 5 6
 * @date: 2016/05/09
 */
'use strict';
biao authored
7
8
const utils = '../../../utils';
9
const contentCodeConfig = require('../../../config/content-code');
biao authored
10
王洪广 authored
11
const _ = require('lodash');
姜枫 authored
12 13 14
const log = global.yoho.logger;
const serviceApi = global.yoho.ServiceAPI;
const api = global.yoho.API;
15
姜枫 authored
16
const resourcesProcess = require(`${utils}/resources-process`);
17
沈志敏 authored
18
const dateFormate = (str) => {
runner authored
19 20
    var time = new Date(str * 1000);
    var y = time.getFullYear();
runner authored
21
    var m = time.getMonth() + 1;
runner authored
22 23 24 25
    var d = time.getDate();
    var h = time.getHours();

    return y + '年' + m + '月' + d + '日' + h + '时';
runner authored
26
};
biao authored
27
biao authored
28
// 为了活动卡片特殊样式,将折扣信息拆分开来
29
const _transDiscountToArr = (discount) => {
runner authored
30
    return discount.replace(/(?:\d+[.\d]?)([\u4e00-\u9fa5]{1})/g, function(fullMatch, capture) {
runner authored
31
        if (capture) {
runner authored
32 33
            const arr = [];
runner authored
34
            arr.push(fullMatch.replace(capture, ''));
runner authored
35 36 37 38 39 40
            arr.push(capture);
            return arr;
        } else {
            return fullMatch;
        }
    }).split(',');
runner authored
41
};
runner authored
42
biao authored
43 44 45 46 47 48 49

/**
 * 获取资源位
 * @param  {String} channel 频道
 * @param  {String} contentcode 内容码
 * @return {Promise}
 */
50
const _getOutletResource = (channel, contentcode) => {
biao authored
51
    const params = {
52
        content_code: contentcode || contentCodeConfig.outlet,
biao authored
53
        limit: 25,
biao authored
54
        yh_channel: channel || ''
biao authored
55 56
    };
57 58 59
    return serviceApi.get('operations/api/v5/resource/home', params, {
        cache: true
    }).then(result => {
biao authored
60 61 62
        if (result && result.code === 200) {
            return resourcesProcess(result.data.list);
        } else {
63
            log.error('the response code of outlet "operations/api/v5/resource/home" is NOT 200', result);
biao authored
64
            return [];
biao authored
65 66 67 68
        }
    });
};
biao authored
69 70 71 72 73
/**
 * 转换导航数据
 * @param  {[Object]} 原始导航数据
 * @return {Object}  转换后的数据
 */
74
const _convertNavData = (list) => {
王洪广 authored
75 76 77
    const formatData = [];

    list = list || [];
zhangxiaoru authored
78 79

    // list = camelCase(list);
王洪广 authored
80 81 82
    _.forEach(list, (item) => {
        formatData.push({
            id: item.id,
zhangxiaoru authored
83 84
            name: item.sort_name,
            url: encodeURI(item.sort_url)
王洪广 authored
85 86
        });
    });
biao authored
87
沈志敏 authored
88 89 90
    return {
        data: formatData
    };
王洪广 authored
91 92
};
biao authored
93 94 95 96 97
/**
 * 获取导航数据
 * @param  {String} 导航类型id
 * @return {Promise}
 */
98
const _getNavData = (categoryId) => {
王洪广 authored
99
    const params = {
biao authored
100
        parent_id: categoryId
biao authored
101
    };
王洪广 authored
102
103 104 105
    return serviceApi.get('operations/api/v6/category/getCategory', params, {
        cache: true
    }).then(result => {
biao authored
106
        if (result && result.code === 200) {
107
            let data = _convertNavData(result.data);
biao authored
108 109 110

            data.category = categoryId;
            return data;
biao authored
111
        } else {
112
            log.error('the response code of "operations/api/v6/category/getCategory" is NOT 200', result);
biao authored
113
            return [];
114 115
        }
    });
biao authored
116 117
};
biao authored
118 119 120 121 122
/**
 * 转换奥莱活动数据
 * @param  {Object} data 原始数据
 * @return {Object} 转换后的数据
 */
123
const _convertActicityData = (data) => {
biao authored
124 125
    const formatData = [];
runner authored
126 127 128
    let discountArr = [],
        discountNum = 0,
        discountText = 0;
biao authored
129
biao authored
130
    data = data || [];
131
biao authored
132
    _.forEach(data, (item) => {
biao authored
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
        if (item.promotionName) {
            discountArr = item.promotionName.split('~');
            if (discountArr.length === 1) {
                discountNum = _transDiscountToArr(discountArr[0])[0];
                discountText = _transDiscountToArr(discountArr[0])[1];
            } else {
                discountNum = discountArr[0] + '~' + _transDiscountToArr(discountArr[1])[0];
                discountText = _transDiscountToArr(discountArr[1])[1];
            }


            formatData.push({
                activityUrl: '/product/outlet/activity?id=' + item.id,
                coverUrl: item.coverUrl,
                logoUrl: item.logoUrl,
                title: item.title,
                discountNum: discountNum,
                discountText: discountText,
                productPoolId: item.productPoolId || '',
                leftTime: item.startLeftTime > 0 ? dateFormate(item.startTime) : item.endLeftTime,
                hide: false
            });
runner authored
155
        }
biao authored
156 157
    });
liangxs authored
158
    return formatData;
biao authored
159 160
};
biao authored
161 162 163 164 165
/**
 * 获取奥莱活动详情
 * @param  {String} id 活动id
 * @return {Promise} 调用接口的Promise
 */
166
const _getActivityDetail = (id) => {
runner authored
167 168
    var params = {
        method: 'app.outlets.activityGet',
沈志敏 authored
169
        sort: 1, // 接口规定传1
biao authored
170
        platform: 3, // h5平台代号
runner authored
171
        id: id,
沈志敏 authored
172
        type: 0 // 接口规定传0
runner authored
173 174
    };
biao authored
175
176 177 178
    return api.get('', params, {
        cache: true
    }).then(res => {
runner authored
179
        if (res.code === 200) {
180
            return _convertActicityData(res.data);
runner authored
181
        } else {
zhangxiaoru authored
182
            log.error('the response code of "app.outlets.activityGet" is NOT 200', res);
runner authored
183 184 185 186 187
            return {};
        }
    });
};
biao authored
188 189 190 191 192
/**
 * 获取奥莱资频道页活动列表
 * @param  {Object} data 请求接口所需的参数
 * @return {Promise} 调用接口的Promise
 */
193
const _getHomeActivity = (data) => {
biao authored
194 195
    var params = {
        method: 'app.outlets.activityGet',
biao authored
196
        platform: 3 // h5平台代号
biao authored
197 198
    };
199 200 201
    return api.get('', _.assign(params, data), {
        cache: true
    }).then(res => {
202
        return _convertActicityData(res.data);
biao authored
203 204
    });
};
biao authored
205
biao authored
206 207 208 209 210 211 212
/**
 * 获取奥莱资首页内容
 * @param  {String} categoryId 父级菜单id,用于标明当前页面是奥莱页面
 * @param  {Strting} channel 奥莱频道
 * @param  {Strting} code 内容码
 * @return {Promise} 调用接口的Promise
 */
213
const getContent = (categoryId, channel, code) => {
biao authored
214
    let params = {
biao authored
215
        type: 0, // 获取全部奥莱活动列表, 不区分是否将开始或结束
biao authored
216 217
        yh_channel: channel
    };
biao authored
218
219
    const p = [_getNavData(categoryId), _getOutletResource(channel, code), _getHomeActivity(params)];
biao authored
220
biao authored
221
    return Promise.all(p).then(data => {
biao authored
222 223 224 225 226
        return {
            nav: data[0] || [],
            content: data[1] || [],
            activity: data[2]
        };
biao authored
227 228
    });
};
biao authored
229
biao authored
230 231 232 233 234
/**
 * 获取奥莱活动详情
 * @param  {String} id 活动id
 * @return {Promise} 调用接口的Promise
 */
235 236
const getActivity = (id) => {
    return _getActivityDetail(id).then(res => {
biao authored
237
        return {
runner authored
238
            activity: res,
biao authored
239
            productPool: res[0] && res[0].productPoolId || '',
沈志敏 authored
240
            activityTitle: res[0] && res[0].title || '奥莱',
biao authored
241
            saleType: 4 // 促销类型, 奥莱为4
biao authored
242
        };
biao authored
243
    });
biao authored
244
};
biao authored
245
biao authored
246 247
/**
 * 获取即将开始或即将结束的活动列表
biao authored
248
 * @param  {Number} type 标明是上线预告还是即将结束
biao authored
249 250 251
 * @param  {String} categoryId 父级菜单id,用于标明当前页面是奥莱页面
 * @return {Object} 活动列表数据
 */
252
const getRecentActivity = (type, categoryId) => {
liangxs authored
253 254 255
    var params = {
        type: type
    };
biao authored
256
257
    return Promise.all([_getNavData(categoryId), _getHomeActivity(params)]).then(res => {
biao authored
258 259

        return {
biao authored
260
            nav: res[0] || [],
biao authored
261
            activity: res[1]
262
        };
biao authored
263 264
    });
};
265
266 267 268 269 270
// pageCache 单独获取活动时间
const getActivityTime = (params) => {
    return api.get('', _.assign({
        method: 'app.outlets.activityGet',
        platform: 3 // h5平台代号
271 272 273
    }, params), {
        cache: true
    }).then(res => {
274 275 276 277 278 279 280 281 282 283 284 285 286 287
        var times = [];

        if (res && res.code === 200) {
            _.forEach(res.data, item => {
                times.push(item.startLeftTime > 0 ? dateFormate(item.startTime) : item.endLeftTime);
            });
            return times;
        } else {
            log.error('the response code of "app.outlets.activityGet" is NOT 200', res);
            return {};
        }
    });
};
288
module.exports = {
289 290 291 292
    getContent,
    getActivity,
    getRecentActivity,
    getActivityTime
沈志敏 authored
293
};