gift-service.js 1.92 KB
/**
 * 个人中心---兑换礼品卡
 * @author gaohongwei <hongwei.gao@yoho.cn>
 * @date: 2016/9/7
 */
'use strict';
const Promise = require('bluebird');
const co = Promise.coroutine;
const _ = require('lodash');
const UserApi = require('./user-api');
const CurrencyApi = require('./currency-api');

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

    /**
     * 礼品卡页面
     */
    index(params, uid) {
        let that = this;

        return co(function*() {
            let respData = {},
                type = params.type || '',
                yohoCoinResult;
            let currencyDataModel = new CurrencyApi(that.ctx);

            if (type !== '') {
                yohoCoinResult = yield currencyDataModel.yohoCoinTotal(uid);
                let yohoCoinInfo = yohoCoinResult.data;

                if (yohoCoinInfo) {
                    respData.gift = {
                        resultInfo: {
                            success: (Number(type) === 1) ? true : false,
                            yohoCoin: yohoCoinInfo.yohocoin_num ? yohoCoinInfo.yohocoin_num : 0
                        }
                    };
                }
            } else {
                respData.gift = {
                    resultInfo: false
                };
            }

            return respData;
        })();
    }

    /**
     * 个人中心-兑换礼品卡提交返回信息
     */
    exchange(req, params, uid) {
        let that = this;

        return co(function*() {
            let data = {};
            let userDataModel = new UserApi(that.ctx);

            data.giftCardCode1 = _.trim(params.giftCardCode1 || '');
            data.giftCardCode2 = _.trim(params.giftCardCode2 || '');
            data.giftCardCode3 = _.trim(params.giftCardCode3 || '');

            let respData = yield userDataModel.exchangeGift(data, uid);

            return respData;
        })();
    }

};