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

const UserData = require('./user-data');
const _ = require('lodash');
const setPager = require(`${global.utils}/pager`).setPager;

module.exports = class extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);
    }

    vipIndex(uid) {
        let userDataModel = new UserData(this.ctx);

        return userDataModel.getVIPInfoByUid(uid).then(vipInfo => {
            let vip = {};
            let data = {};
            let scale = 1 / 3; // 分三等份

            if (_.get(vipInfo, 'code', 400) !== 200) {
                return vip;
            }

            data = vipInfo.data || {};
            vip.level = parseInt(data.current_vip_level, 10) || 0;
            vip.current_total_growth = parseInt(data.current_total_growth, 10) || 0;
            vip.grades = [
                {name: '普通会员', val: 0},
                {name: '银卡会员', val: 800},
                {name: '金卡会员', val: 3000},
                {name: '白金会员', val: 7000}
            ];

            // 还须要成长值
            if (vip.grades[vip.level + 1]) {
                vip.need_total_growth = vip.grades[vip.level + 1].val - vip.current_total_growth;
                vip.need_total_growth = vip.need_total_growth > 0 ? vip.need_total_growth : 0;
            }

            // 进度条百分比
            vip.current_total_percent = 1;
            if (vip.current_total_growth <= vip.grades[1].val) {
                vip.current_total_percent = (vip.current_total_growth * scale / vip.grades[1].val);
            } else if (vip.current_total_growth <= vip.grades[2].val) {
                vip.current_total_percent = (vip.current_total_growth * scale / vip.grades[2].val + scale);
            } else if (vip.current_total_growth <= vip.grades[3].val) {
                vip.current_total_percent = (vip.current_total_growth * scale / vip.grades[3].val + scale * 2);
            }

            vip.current_total_percent = (vip.current_total_percent * 100).toFixed(2);

            vip.user_name = decodeURIComponent(this.ctx && this.ctx.req && this.ctx.req.user.name || '');

            return vip;
        });
    }

    getVipRecord(uid, params) {
        let page = parseInt(params.page, 10) || 1;
        let size = parseInt(params.size, 10) || 1;
        let type = parseInt(params.type, 10) || 1;
        let userDataModel = new UserData(this.ctx);

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

        return userDataModel.getVipRecord({
            uid: uid,
            page: page,
            size: size || 10,
            type: type
        }).then(d => {
            let resData = {type: type};

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

            d.data = d.data || {};

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

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

            if (type === 2) {
                let liverData = [];

                if (d.data.sliver_start_time) {
                    liverData.push({time: d.data.sliver_start_time, content: '普通升级银卡'});
                }

                if (d.data.gold_start_time) {
                    liverData.push({time: d.data.gold_start_time, content: '银卡升级金卡'});
                }

                if (d.data.whitegold_start_time) {
                    liverData.push({time: d.data.whitegold_start_time, content: '金卡升级白金'});
                }

                if (liverData.length) {
                    // 最多三条记录,默认显示分页数据
                    resData.pager = Object.assign({
                        count: 1,
                        curPage: 1,
                        totalPages: 1
                    });
                }

                resData.data = liverData;
                return resData;
            }

            resData.data = d.data.data;
            return resData;
        });
    }
};