family.js 3.31 KB
'use strict';

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

class familyModel extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);
    }

    /**
     * 个人详情数据
     */
    _userData(uid) {
        if (uid) {
            return api.get('', {
                method: 'app.passport.profile',
                uid: uid
            }, {
                code: 200
            });
        } else {
            return Promise.resolve({});
        }
    }

    /**
     * 潮流口令
     */
    _getCode(uid) {
        if (uid) {
            return api.get('', {
                method: 'app.invitecode.my',
                uid: uid
            }, {
                code: 200
            });
        } else {
            return Promise.resolve({});
        }
    }

    familyIndex(uid) {
        return Promise.all([
            this._userData(uid),
            this._getCode(uid)
        ]).then((result) => {
            let familyIndexData = {};

            if (result[0] && result[0].data) {
                familyIndexData = _.assign(familyIndexData, {
                    head_ico: result[0].data.head_ico,
                    nickname: result[0].data.nickname,
                    gender: result[0].data.gender,
                    birthday: moment(result[0].data.birthday).format('MM-DD')
                });
            }

            if (result[1] && result[1].data) {
                familyIndexData = _.assign(familyIndexData, result[1].data);
            }

            return familyIndexData;
        });
    }

    formatDay(day) {
        return moment(day).format('YYYY-MM-DD');
    }

    getCoinData(params) {
        let options = {
            data: {
                method: 'app.yohocoin.lists',
                uid: params.uid,
                source: params.source || -1,
                queryType: params.queryType || 0,
                beginTime: params.beginTime ? this.formatDay(parseInt(params.beginTime, 10)) : '',
                endTime: params.endTime ? this.formatDay(parseInt(params.endTime, 10)) : '',
                page: params.page || 1,
                limit: params.limit || 10
            },
            param: {
                code: 200
            }
        };

        return this.get(options).then(result => {
            let resu = {
                coinList: []
            };
            let _this = this;

            if (_.get(result, 'data.coinlist')) {
                _.forEach(result.data.coinlist, function(val) {
                    Object.assign(val, {
                        typeName: _this.checkType(val.iconType),
                        minus: parseInt(val.num, 10) < 0
                    });
                });
                resu.coinList = result.data.coinlist;
            }

            return resu;
        });
    }

    checkType(params) {
        switch (params) {
            case 1:
                return 'yohobuy';
            case 2:
                return 'mars';
            case 3:
                return 'yohonow';
            case 4:
                return 'store';
            case 5:
                return 'green';
            case 6:
                return 'coffee';
            case 7:
                return 'little';
            default:
                return 'yohobuy';
        }
    }
}

module.exports = familyModel;