me-gift-service.js 5.02 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), params));

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

    // 获取礼品卡消费记录
    consumeList(params, uid) {
        let types = ['类型', '消费', '退款'];

        params.page = Number(params.page) || 1;
        params.type = Number(params.type) || 0;

        return this.meGiftAPi.consumeList(params, uid).then(rlist => {
            let giftData = {};

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

            return giftData;
        });
    }

    // 验证手机是否绑定
    verifyBinMobile(uid) {
        let userInfo = {
            isBinMobile: Number(!!_.get(this.ctx, 'req.user.mobile', false))
        };

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

        return this.meGiftAPi.getProfile(uid).then(d => {
            let mobile = _.get(d, 'data.mobile', '');

            if (mobile) {
                _.set(this.ctx, 'req.session.USER_MOBILE', mobile);
            }

            Object.assign(userInfo, {isBinMobile: Number(!!mobile), email: _.get(d, 'data.email', '')});

            return userInfo;
        });
    }

    // 发送邮箱验证码
    sendEmailCode(params, uid) {
        return this.meGiftAPi.sendEmailCode(params.email, uid);
    }

    // 验证邮箱验证码
    verifyEmail(params, uid) {
        return this.meGiftAPi.verifyEmail(params.code, uid);
    }

    // 发验手机证码
    smsBind(params) {
        return this.meGiftAPi.checkIsCanBind(params.area, params.mobile).then(res => {
            if (_.get(res, 'data.isCanBind') === 'N') {
                return {
                    code: 401,
                    message: '<p>绑定失败,该手机号已被绑定</p><p>请更换手机号</p>'
                };
            }
            return this.meGiftAPi.smsbind(params.area, params.mobile);
        });
    }

    // 修改绑定的手机号
    changeMobile(params, uid) {
        return this.meGiftAPi.changeMobile(params.area, params.mobile, params.code, uid).then(res => {
            if (res.code === 200) {
                _.set(this.ctx, 'req.session.USER_MOBILE', params.mobile.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2'));
            }
            return res;
        });
    }

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