vip.js 2.77 KB
/**
 * [个人中心]我的VIP
 * @author: wsl(shuiling.wang@yoho.cn)
 * @date: 2017/03/13
 */

'use strict';

const api = global.yoho.API;
const moment = require('moment');
const _ = require('lodash');

const iconData = [
    {
        id: 1,
        icon: ''
    },
    {
        id: 2,
        icon: ''
    },
    {
        id: 3,
        icon: ''
    },
    {
        id: 4,
        icon: ''
    },
    {
        id: 5,
        icon: ''
    },
    {
        id: 6,
        icon: ''
    },
    {
        id: 9,
        icon: ''
    },
    {
        id: 10,
        icon: ''
    }
];

const vipIndex = (uid)=>{
    return api.get('', {
        method: 'app.passport.vip',
        uid: uid,
        private_key: '0ed29744ed318fd28d2c07985d3ba633'
    }).then(vipInfo => {
        let data = vipInfo.data, proportion = '0%', hasConsume = false;
        let remainDays = Math.ceil(((data.vip_end_time) * 1000 - Date.now()) / 86400000);
        let preferences = [];
        let isVip = data.current_vip_level > 0 ? true : false;

        if (+data.next_need_cost !== 0) {
            proportion = data.current_year_cost * 100 / data.next_need_cost;
            proportion = proportion > 100 ? 100 : proportion;
            hasConsume = proportion > 0;
            proportion = proportion + '%';
        }

        if (data.enjoy_preferential) {
            _.forEach(data.enjoy_preferential, item => {
                if (!_.find(iconData, {id: item.id})) {
                    return;
                }
                preferences.push({
                    id: item.id,
                    icon: _.find(iconData, {id: item.id}).icon,
                    favTxt: item.title,
                    description: item.description
                });
            });
        }


        return {
            curTitle: data.current_vip_title,
            nextTitle: data.next_vip_title,
            nextLevel: data.next_vip_level,
            curLevel: data.current_vip_level,
            yearCost: Number(data.current_year_cost).toFixed(2),
            totalCost: (+data.current_total_cost).toFixed(2),
            list: preferences,
            platinum: data.upgrade_need_cost > 0 ? false : true,
            nextCost: Number(data.next_need_cost).toFixed(2),
            balance: Number(data.upgrade_need_cost).toFixed(2),
            proportion: proportion,
            hasConsume: hasConsume,
            reach: moment(data.vip_reach_time * 1000).format('YYYY.MM.DD'),
            valid: moment(data.vip_start_time * 1000).format('YYYY.MM.DD'),
            end: moment(data.vip_end_time * 1000).format('YYYY.MM.DD'),
            remainDays: remainDays,
            doubtLevel: isVip,
            commonVip: !isVip
        };
    });
};

module.exports = {
    vipIndex
};