grade-new.js 7.17 KB
'use strict';

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

// 格式年月日
const _formatDay = (day) => {
    return moment(day).format('YYYY-MM-DD');
};

/**
 * 会员等级
 * @param params
 */

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

    getGradeGrade(uid, channel) {

        let options = {
            data: {
                method: 'app.passport.vip',
                uid: uid,
                channel: channel || 1
            },
            param: {
                code: 200
            }
        };

        return this.get(options).then(result => {
            return result;
        });
    }

    getGradeUser(uid, channel) {
        let options = {
            data: {
                method: 'app.passport.profile',
                uid: uid,
                channel: channel || 1
            },
            param: {
                code: 200
            }
        };

        return this.get(options).then(result => {
            return result;
        });
    }

    getGrowthVip(uid) {
        let options = {
            data: {
                method: 'app.passport.growthvip',
                uid: uid
            },
            param: {
                code: 200
            }
        };

        return this.get(options).then(result => {
            return result;
        });
    }

    getHis(params) {
        let options = {
            data: {
                method: 'app.passport.growthhistory',
                uid: params.uid,
                page: params.page
            },
            param: {
                code: 200
            }
        };

        return this.get(options).then(result => {
            return result;
        });
    }

    index(param) {
        if (param.uid) {
            return api.all([
                this.getGradeGrade(param.uid, param.channel),
                this.getGradeUser(param.uid, param.channel),
                this.getGrowthVip(param.uid)
            ]).then((result) => {

                let resu = {
                    vipGrade: []
                };

                let enp = {};

                let obj = {
                    privilege: []
                };

                if (result[0] && result[0].data) {

                    _.forEach(result[0].data.enjoy_preferential, function(val) {

                        enp = {
                            description: val.description,
                            pic: val.pic,
                            title: val.title,
                            href: val.id === 8
                        };

                        obj.privilege.push(enp);

                    });

                    switch (result[0].data.current_vip_level) {
                        case '0': // 普通会员
                            obj = _.assign(obj, {
                                vip0: true
                            });
                            break;
                        case '1': // 银卡会员
                            obj = _.assign(obj, {
                                vip1: true
                            });
                            break;
                        case '2': // 金卡会员
                            obj = _.assign(obj, {
                                vip2: true
                            });
                            break;
                        case '3': // 白金会员
                            obj = _.assign(obj, {
                                vip3: true
                            });
                            break;
                        default:

                    }

                    obj = _.assign(obj, {
                        allUrl: helpers.urlFormat('/home/privilege')
                    });
                }

                if (result[1] && result[1].data) {
                    obj = _.assign(obj, {
                        name: result[1].data.nickname,
                        headIco: result[1].data.head_ico
                    });
                }

                if (result[2] && result[2].data) {
                    let nowGrowth = parseInt(result[2].data.current_total_growth, 10) || 0;
                    let nextGrowth = parseInt(result[2].data.upgrade_need_growth, 10) || 0;

                    obj = _.assign(obj, {
                        nowGrowth: nowGrowth,
                        nextGrowth: nextGrowth,
                        percent: nowGrowth < 7000 ? nowGrowth / 70 : 100
                    });
                }
                resu.vipGrade.push(obj);
                return resu;
            });
        } else {
            return Promise.resolve({
                noUid: true
            });
        }
    }

    grow(param) {
        return api.all([
            this.getGrowthVip(param.uid),
            this.getHis(param)
        ]).then((result) => {
            let resu = {
                levelHis: [],
                detailHis: []
            };

            if (result) {
                if (result[0] && result[0].data) {
                    let build = [];

                    if (result[0].data.sliver_start_time) {
                        build.push({
                            tip: '普通升级为银卡',
                            time: _formatDay(result[0].data.sliver_start_time * 1000)
                        });
                    }

                    if (result[0].data.gold_start_time) {
                        build.push({
                            tip: '银卡升级为金卡',
                            time: _formatDay(result[0].data.gold_start_time * 1000)
                        });
                    }

                    if (result[0].data.whitegold_start_time) {
                        build.push({
                            tip: '金卡升级为白金',
                            time: _formatDay(result[0].data.whitegold_start_time * 1000)
                        });
                    }

                    resu.levelHis = build;
                }

                if (result[1] && result[1].data && result[1].data.data) {
                    let build = [];

                    _.forEach(result[1].data.data, function(val) {
                        build.push({
                            title: val.typeDesc,
                            time: _formatDay(val.createTime * 1000),
                            value: val.growthValue
                        });
                    });

                    resu.detailHis = build;
                }
            }

            return resu;
        });
    }

    getHisAjax(param) {
        return api.all([
            this.getHis(param)
        ]).then((result) => {
            let resu = {
                detailHis: []
            };

            if (result && result[0] && result[0].data) {
                let build = [];

                _.forEach(result[0].data.data, function(val) {
                    build.push({
                        title: val.typeDesc,
                        time: _formatDay(val.createTime * 1000),
                        value: val.growthValue
                    });
                });

                resu.detailHis = build;
            }

            return resu;
        });
    }
};