Blame view

apps/home/models/vip-service.js 4.12 KB
1 2
'use strict';
郝肖肖 authored
3
const UserData = require('./user-data');
4 5
const _ = require('lodash');
const setPager = require(`${global.utils}/pager`).setPager;
6
郝肖肖 authored
7 8 9 10 11 12 13 14
module.exports = class extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);
    }

    vipIndex(uid) {
        let userDataModel = new UserData(this.ctx);
15 16 17
        return userDataModel.getVIPInfoByUid(uid).then(vipInfo => {
            let vip = {};
            let data = {};
18
            let scale = 1 / 3; // 分三等份
郝肖肖 authored
19
郝肖肖 authored
20
            if (_.get(vipInfo, 'code', 400) !== 200) {
21
                return vip;
郝肖肖 authored
22 23
            }
24 25 26 27
            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 = [
郝肖肖 authored
28 29 30 31
                {name: '普通会员', val: 0},
                {name: '银卡会员', val: 800},
                {name: '金卡会员', val: 3000},
                {name: '白金会员', val: 7000}
32 33
            ];
34
            // 还须要成长值
35 36 37
            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;
郝肖肖 authored
38 39
            }
40
            // 进度条百分比
郝肖肖 authored
41
            vip.current_total_percent = 1;
42 43 44 45 46
            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) {
郝肖肖 authored
47
                vip.current_total_percent = (vip.current_total_growth * scale / vip.grades[3].val + scale * 2);
48 49
            }
郝肖肖 authored
50
            vip.current_total_percent = (vip.current_total_percent * 100).toFixed(2);
51
郝肖肖 authored
52
            vip.user_name = decodeURIComponent(this.ctx && this.ctx.req && this.ctx.req.user.name || '');
郝肖肖 authored
53
郝肖肖 authored
54
            return vip;
55
        });
郝肖肖 authored
56 57 58 59 60 61 62 63 64 65
    }

    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
66
        return userDataModel.getVipRecord({
郝肖肖 authored
67 68
            uid: uid,
            page: page,
69 70
            size: size || 10,
            type: type
郝肖肖 authored
71
        }).then(d => {
72
            let resData = {type: type};
郝肖肖 authored
73 74 75 76 77

            if (d.code !== 200) {
                return resData;
            }
郝肖肖 authored
78 79 80 81 82 83 84 85 86 87
            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);
郝肖肖 authored
88
郝肖肖 authored
89 90
            if (type === 2) {
                let liverData = [];
91 92

                if (d.data.sliver_start_time) {
郝肖肖 authored
93
                    liverData.push({time: d.data.sliver_start_time, content: '普通升级银卡'});
94 95 96
                }

                if (d.data.gold_start_time) {
郝肖肖 authored
97
                    liverData.push({time: d.data.gold_start_time, content: '银卡升级金卡'});
98 99 100
                }

                if (d.data.whitegold_start_time) {
郝肖肖 authored
101
                    liverData.push({time: d.data.whitegold_start_time, content: '金卡升级白金'});
102 103
                }
郝肖肖 authored
104 105 106 107 108 109 110 111 112 113
                if (liverData.length) {
                    // 最多三条记录,默认显示分页数据
                    resData.pager = Object.assign({
                        count: 1,
                        curPage: 1,
                        totalPages: 1
                    });
                }

                resData.data = liverData;
114 115 116 117
                return resData;
            }

            resData.data = d.data.data;
118
            return resData;
郝肖肖 authored
119 120
        });
    }
121
};