vip-service.js 3.1 KB
'use strict';

const Promise = require('bluebird');
const co = Promise.coroutine;
const userData = require('./user-data');
const moment = require('moment');
const helpers = global.yoho.helpers;
const _ = require('lodash');
const setPager = require(`${global.utils}/pager`).setPager;

const vipIndex = (uid)=>{

    return co(function*() {
        let vipInfo = yield userData.getVIPInfoByUid(uid);

        let data = vipInfo.data, proportion = '0%';

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

        let remainDays = Math.ceil(((data.vip_end_time) * 1000 - Date.now()) / 86400000);

        let preferences = [];

        if (data.enjoy_preferential) {
            preferences = data.enjoy_preferential.map(function(item) {
                return {
                    id: item.id,
                    favTxt: item.title,
                    imgType: helpers.https(item.pic),
                    description: item.description
                };
            });
        }
        let isVip = data.current_vip_level > 0 ? true : false;

        let vip = {
            title: data.current_vip_title,
            nextTitle: data.next_vip_title,
            nextLevel: data.next_vip_level,
            yearCost: Number(data.current_year_cost).toFixed(2),
            totalCost: (+data.current_total_cost).toFixed(2),
            list: preferences,
            level: data.current_vip_level,
            platinum: data.upgrade_need_cost > 0 ? false : true,
            nextCost: Number(data.next_need_cost).toFixed(2),
            balan: Number(data.upgrade_need_cost).toFixed(2),
            proportion: proportion,
            vipLevel: data.next_vip_title,
            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,
            morePreferences: helpers.urlFormat('/help/detail', {id: 7})
        };

        return vip;

    })();
};

const getVipRecord = (uid, params) => {
    let page = parseInt(params.page, 10) || 1;
    let size = parseInt(params.size, 10) || 1;
    let type = parseInt(params.type, 10) || 1;

    type = type > 0 && type < 3 ? type : 1;//1 or 2

    return userData.getVipRecordDemo({
        uid: uid,
        page: page,
        size: size || 10
    }).then(d => {
        var resData = {type: type};

        if (d.code !== 200) {
            return resData;
        }

        let pagerList = setPager(_.get(d.data, 'page_total', 1), Object.assign(params, {page: page}));

        resData.data = d.data.list;
        resData.pager = Object.assign({
            count: _.get(d.data, 'total', 0),
            curPage: page,
            totalPages: _.get(d.data, 'page_total', 0)
        }, pagerList);

        return resData;
    });
};

module.exports = {
    vipIndex,
    getVipRecord
};