Blame view

apps/product/models/students-handler.js 4.94 KB
周少峰 authored
1 2 3 4 5 6 7
/**
 * 处理学生营销相关数据结构
 * @date: 2016-08-06 18:20:52
 * @author: sefon<809775195@qq.com>
 */
'use strict';
8
const utils = '../../../utils';
周少峰 authored
9
const _ = require('lodash');
10 11
const helpers = global.yoho.helpers;
const productProcess = require(`${utils}/product-process`);
周少峰 authored
12
const crypto = global.yoho.crypto;
周少峰 authored
13
周少峰 authored
14 15 16 17 18 19 20 21 22
/**
 * 优惠券
 * @params data array
 * @return Object
 */
const getCoupon = (data) => {
    let dest = [];

    _.forEach(data, (value) => {
23 24 25
        dest.push({
            imgUrl: helpers.image(value.image.src, 400, 400),
            url: value.image.url,
周少峰 authored
26
            id: crypto.encryption('yoho9646abcdefgh', value.couponID)
27
        });
周少峰 authored
28 29 30 31 32
    });

    return dest;
};
周少峰 authored
33 34 35 36 37
/**
 * 补零
 * @params data array
 * @return Object
 */
htoooth authored
38
const pad = (num, n) => {
周少峰 authored
39 40 41 42 43 44 45 46
    let len = num.toString().length;

    while (len < n) {
        num = '0' + num;
        len++;
    }
    return num;
};
47
周少峰 authored
48
/**
周少峰 authored
49 50 51 52 53
 * 认证学生总数
 * @params data array
 * @return Object
 */
exports.studentsNum = (data) => {
周少峰 authored
54
    let dest = [];
周少峰 authored
55
周少峰 authored
56 57 58 59 60
    data = pad(data, 7);
    for (let i = 0; i < data.length; i++) {
        dest.push(data.charAt(i));
    }
    return dest;
周少峰 authored
61
};
周少峰 authored
62
周少峰 authored
63
/**
周少峰 authored
64 65 66 67 68
 * 处理学生营销资源位数据
 * @params data array 资源位接口返回数据
 * @return object
 */
exports.studentResource = (data) => {
hongweigao authored
69
    let dest = {couponObj: {}};
70
    let rightsTextObj = [{
yyq authored
71 72 73 74 75 76 77 78
        rightName: '认证立赠100有货币',
        rightContent: '认证成功,立即赠送100有货币。'
    }, {
        rightName: '新品立享9折',
        rightContent: '学生购买指定原价新品时,可立即享受9折优惠,此折扣与VIP折扣不可同时享受。'
    }, {
        rightName: '每1元返1个有货币',
        rightContent: '学生购买指定商品时,每1元返1个有货币(以商品的实际成交金额计算);<br>' +
79 80 81
                    '有货币返还时间:确认收货7日后,系统自动将对应数量的有货币返还至购买账户;<br>' +
                    '有货币有效期:获得当日至次年12月31日,逾期自动作废;<br>' +
                    '查看有货币:登录后,点击“个人中心”在“我的有货币”中可以查看有货币余额及明细。'
yyq authored
82 83 84 85
    }, {
        rightName: '分期付款',
        rightContent: '开通有货分期后,可享受部分商品30天延后付款,最长6个月的分期付款,开启任性购物模式。'
    }];
86
周少峰 authored
87 88 89
    _.forEach(data, (value) => {
        switch (value.template_name) {
            case 'focus':
hongweigao authored
90
                Object.assign(dest, {mainBanner: {list: value.data, floorId: value.template_id}});
周少峰 authored
91 92
                break;
            case 'getCoupon':
hongweigao authored
93
                Object.assign(dest.couponObj, {couponItem: getCoupon(value.data), floorId: value.template_id});
周少峰 authored
94
                break;
周少峰 authored
95
            case 'recommend_content_five':
96 97 98 99
                Object.assign(value.data.list[0], rightsTextObj[0]);
                Object.assign(value.data.list[1], rightsTextObj[1]);
                Object.assign(value.data.list[2], rightsTextObj[2]);
                Object.assign(value.data.list[3], rightsTextObj[3]);
hongweigao authored
100
                Object.assign(dest, {rightsObj: {rightsItem: value.data.list || {}, floorId: value.template_id}});
101 102
                break;
            case 'image_list':
hongweigao authored
103
                Object.assign(dest, {activityObj: {activityItem: value.data.list || {}, floorId: value.template_id} });
周少峰 authored
104
                break;
105 106 107 108
            case 'link':
                Object.assign(dest, {moreProducts: value.data[0].url || ''});
                break;
            case 'single_image':
hongweigao authored
109 110 111 112 113
                Object.assign(dest, {identityObj: {
                    adImg: value.data[0].src || '', url: value.data[0].url,
                    identityItem: {sendCoin: value.data[0].title || 100},
                    floorId: value.template_id}
                });
114
                break;
115
            case 'titleFloor':
hongweigao authored
116
                Object.assign(dest.couponObj, {couponMore: value.data.title || {}});
117
                break;
周少峰 authored
118 119 120 121
            default:
                break;
        }
    });
122
    _.isEmpty(dest.couponObj) ? delete dest.couponObj : '';
周少峰 authored
123 124
    return dest;
};
125 126 127 128 129 130 131

/**
 * 处理学生商品
 * @params data array 资源位接口返回数据
 * @return object
 */
exports.stuProducts = (data) => {
周少峰 authored
132
    let products = productProcess.processProductList(data, {showTags: false});
133 134

    _.forEach(data, (value, key) => {
周少峰 authored
135
        delete data[key].discount;
沈志敏 authored
136 137 138
        data[key].market_price = data[key].sales_price;
        delete data[key].sales_price;
        if (value.student_price) {
hongweigao authored
139
            Object.assign(data[key], {for_stu: true});
140 141 142 143
        }
    });
    return products;
};
wenjiekong authored
144
wenjiekong authored
145 146
exports.stuVerify = (result, params) => {
    let data = {resultParams: params};
htoooth authored
147
wenjiekong authored
148
    if (result.code === 200) {
wenjiekong authored
149
        data.isError = false;
150
        data.isStudent = result.data.isStudent === 1 ? true : false;
151
        data.prompt = result.data.prompt;
wenjiekong authored
152
    } else {
wenjiekong authored
153 154
        data.isError = true;
        data.errorMsg = result.message;
wenjiekong authored
155 156
    }
wenjiekong authored
157
    return data;
wenjiekong authored
158
};