students-handler.js 3.13 KB
/**
 * 处理学生营销相关数据结构
 * @date: 2016-08-06 18:20:52
 * @author: sefon<809775195@qq.com>
 */
'use strict';

const utils = '../../../utils';
const _ = require('lodash');
const helpers = global.yoho.helpers;
const productProcess = require(`${utils}/product-process`);
const crypto = global.yoho.crypto;

/**
 * 优惠券
 * @params data array
 * @return Object
 */
const getCoupon = (data) => {
    let dest = [];

    _.forEach(data, (value) => {
        dest.push({
            imgUrl: helpers.image(value.image.src, 400, 400),
            url: value.image.url,
            id: crypto.encryption('yoho9646abcdefgh', value.couponID)
        });
    });

    return dest;
};

/**
 * 补零
 * @params data array
 * @return Object
 */
const pad = (num, n) =>{
    let len = num.toString().length;

    while (len < n) {
        num = '0' + num;
        len++;
    }
    return num;
};

/**
 * 认证学生总数
 * @params data array
 * @return Object
 */
exports.studentsNum = (data) => {
    let dest = [];

    data = pad(data, 7);
    for (let i = 0; i < data.length; i++) {
        dest.push(data.charAt(i));
    }
    return dest;
};

/**
 * 处理学生营销资源位数据
 * @params data array 资源位接口返回数据
 * @return object
 */
exports.studentResource = (data) => {
    let dest = {};

    _.forEach(data, (value) => {
        switch (value.template_name) {
            case 'focus':
                Object.assign(dest, {mainBanner: {list: value.data}});
                break;
            case 'getCoupon':
                Object.assign(dest, {couponItem: getCoupon(value.data)});
                break;
            case 'recommend_content_five':
                Object.assign(dest, {rightsItem: value.data.list || {}});
                break;
            case 'image_list':
                Object.assign(dest, {activityItem: value.data.list || {}});
                break;
            case 'link':
                Object.assign(dest, {moreProducts: value.data[0].url || ''});
                break;
            case 'single_image':
                Object.assign(dest, {adImg: value.data[0].src || '', url: value.data[0].url});
                Object.assign(dest, {identityItem: {sendCoin: value.data[0].title || 100}});
                break;
            case 'titleFloor':
                Object.assign(dest, {couponMore: value.data.title || {}});
                break;
            default:
                break;
        }
    });

    return dest;
};

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

    _.forEach(data, (value, key) => {
        delete data[key].discount;
        delete data[key].salesPrice;
        if (value.studentPrice) {
            Object.assign(data[key], {forStu: true});

        }
    });
    return products;
};

exports.stuVerify = (result) => {
    let data = {};

    if (result.code === 200) {
        data = result.data;
        data.isStudent = data.isStudent === 1 ? true : false;
    } else {
        data.msg = result.message;
    }

    return data;
};