me-gift-service.js 5.9 KB
/**
 * 我的礼品卡列表
 * @author xiaoxiao <xiaoxiao.hao@yoho.cn>
 * @date: 2017/9/1
 */
'use strict';
const Promise = require('bluebird');
const _ = require('lodash');
const md5 = require('md5');
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.smsbind(params.area, params.mobile, params.id, params.captcha);
    }

    // 验证手机号
    checkMobile(params, uid) {
        let {fcode, ccode} = params;

        if (fcode && ccode) {
            if (md5(`${ccode}_${uid}`) === fcode) {
                return Promise.resolve({code: 200});
            } else {
                return Promise.resolve({code: 400, message: '信息校验失败,请刷新后重试'});
            }
        } else {
            return this.meGiftAPi.changeMobileCheck(params.area, params.mobile, params.code);
        }

    }

    // 修改绑定的手机号
    changeMobile(params, uid) {

        return this.checkMobile(params, uid).then(res => {
            if (res.code !== 200) {
                return res;
            }

            let data = res.data || {};

            if (data.isRegister === 'Y') {
                let ccode = Math.floor(Math.random() * 1000000000000);

                return {
                    code: 200,
                    data: {
                        ccode: ccode,
                        fcode: md5(`${ccode}_${uid}`),
                        reCheckType: data.isBind === 'N' ? 1 : 2
                    }
                };
            }

            return this.meGiftAPi.changeMobile(params.area, params.mobile, params.code, uid).then(result => {
                if (result.code === 200) {
                    result.data = {};
                    _.set(this.ctx, 'req.session.USER_MOBILE',
                        params.mobile.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2'));
                }
                return result;
            });
        });
    }

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