gift-service.js 2 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 headerModel = require('../../../doraemon/models/header');
 const userApi = require('./user-api');
 const currencyApi = require('./CurrencyData');

 /**
 * 礼品卡页面
 */
exports.index = (params, uid, channel) => {
    return co(function*() {
        let respData = {},
            type = params.type || '',
            yohoCoinResult;

        respData.headerData = yield headerModel.requestHeaderData(channel);
        if (type !== '') {
            yohoCoinResult = yield currencyApi.yohoCoinTotal(uid);
            let yohoCoinInfo = yohoCoinResult.data;
            console.info(yohoCoinInfo);
            if (yohoCoinInfo) {
                respData.gift = {
                    resultInfo: {
                        success: (type === 1) ? true : false,
                        yohoCoin: yohoCoinInfo.yohocoin_num ? yohoCoinInfo.yohocoin_num : 0
                    }
                }
            }
        }else {
            respData.gift = {
                resultInfo: false
            }
        }

        return respData;
    })();
};

/**
 * 个人中心-兑换礼品卡提交返回信息
 */
exports.exchange = (params, uid) => {
    return co(function*() {
        let data = {};

        data.giftCardCode1 = _.trim(params.giftCardCode1 || ''),
        data.giftCardCode2 = _.trim(params.giftCardCode2 || ''),
        data.giftCardCode3 = _.trim(params.giftCardCode3 || ''),
        data.captchaCode = _.trim(params.verifyCode || '').toLowerCase();

         // if (captchaCode && captchaCode !== req.session.captcha) {
         if (data.captchaCode === '') {
             return {
                 code: 400,
                 message: '图形验证码不正确'
             };

         }
         let respData = yield userApi.exchangeGift(data, uid);
        
         return respData;
    })();
};