me-gift-service.js 3.41 KB
/**
 * 我的礼品卡列表
 * @author xiaoxiao <xiaoxiao.hao@yoho.cn>
 * @date: 2017/9/1
 */
'use strict';
const Promise = require('bluebird');
const _ = require('lodash');
const helpers = global.yoho.helpers;
const MeGiftAPi = require('./me-gift-api');
const setPager = require(`${global.utils}/pager`).setPager;
const crypto = global.yoho.crypto;

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

    headTab(params) {
        let type = params.type;
        let tabs = [{
            name: '使用中',
            url: helpers.urlFormat('/home/megift'),
            active: false
        }, {
            name: '已过期',
            url: helpers.urlFormat('/home/megift', {type: 1}),
            active: false
        }, {
            name: '已用完',
            url: helpers.urlFormat('/home/megift', {type: 2}),
            active: false
        }, {
            name: '已冻结',
            url: helpers.urlFormat('/home/megift', {type: 3}),
            active: false
        }, {
            name: '添加礼品卡',
            class: 'add-gift',
            active: false
        }];

        tabs[type].active = true;
        return tabs;
    }

    // 礼品卡列表-status=[1-可使用,2-已冻结,3-已过期,4-已用完]
    getList(params, uid) {
        let status = 1;

        params.type = Number(params.type > -1 && params.type < 4 ? params.type : 0);
        params.page = Number(params.page) || 1;

        switch (params.type) {
            case 1: status = 3; break;
            case 2: status = 4; break;
            case 3: status = 2; break;
            default: status = 1;
        }

        return Promise.all([
            this.meGiftAPi.getList(uid, status, params.page),
            this.verifyBinMobile(uid)
        ]).then(rlist => {
            let giftData = {};

            giftData.data = _.get(rlist[0], 'data.giftCardInfoBOList', []);
            giftData.pager = Object.assign({
                count: _.get(rlist[0], 'data.total', 0),
                curPage: params.page,
                totalPages: _.get(rlist[0], 'data.pageSize', 0)
            }, setPager(_.get(rlist[0], 'data.pageSize', 0), {
                page: params.page
            }));

            return {
                giftData: giftData,
                userInfo: rlist[1],
                tabs: this.headTab(params)
            };
        });
    }

    // 礼品卡列表
    detailList(params) {
        return Promise.resolve({tabs: this.headTab(params)});
    }

    // 礼品卡列表
    verifyBinMobile(uid) {
        let userInfo = {
            isBinMobile: _.get(this.ctx, 'req.session.isBinMobile', false)
        };

        if (userInfo.isBinMobile) {
            return Promise.resolve(userInfo);
        }

        return this.meGiftAPi.getProfile(uid).then(lres => {
            lres = _.get(lres, 'data', {});

            let isBinMobile = Number(!!lres.verify_mobile);

            _.set(this.ctx, 'req.session.isBinMobile', isBinMobile);

            return Object.assign({}, userInfo, {
                isBinMobile: isBinMobile,
                email: lres.verify_email,
                mobile: lres.verify_mobile
            });
        });
    }

    // 激活礼品卡
    activateGift(params, uid) {
        return this.meGiftAPi.activateGift(uid, params.cardCode, crypto.encryption('yoho9646yoho9646', params.cardPwd));
    }

};