brand-shop.js 2.18 KB
'use strict';

var api = global.yoho.API;

const camelCase = global.yoho.camelCase;
const _ = require('lodash');

/**
 * 查询品牌店铺优惠券结果处理
 * @param  {object} sizeInfo [接口原始数据]
 * @return {object}          [description]
 */
const processBrandCouponData = (data) => {
    var dest = {},
        list = data.data || {},
        distCoupons = [];

    list = camelCase(list);
    dest.code = data.code;
    _.forEach(list.coupons, function(value) {

        if (!value.status) {
            return;
        }
        let coupon = value;

        coupon.money = parseInt(value.money);
        if (coupon.status === 1) {
            coupon.cooponStateMessage = '可以领取';
        } else if (coupon.status === 2) {
            coupon.cooponStateMessage = '已抢光';
        } else if (coupon.status === 3) {
            coupon.cooponStateMessage = '已领取';
        }

        distCoupons.push(coupon);

    });

    if (distCoupons.length > 1) {
        dest.mutilCoupon = true;
    }
    dest.coupons = distCoupons;
    dest.message = list.message;
    return dest;
};


/**
 * 领取品牌店铺优惠券
 * @param  {object} sizeInfo [接口原始数据]
 * @return {object}          [description]
 */
const getBrandCouponStatus = (data) => {
    var dest = {};
    
    dest.code = data.code;
    dest.message = data.message;
    dest.logIn = true;
    return dest;
};

/**
 * 获取品牌信息及其优惠券
 */
exports.getBrandIntro = (data) => {
    var defaultParam = {
            method: 'app.brand.getBrandIntro'
        },
        infoData = Object.assign(defaultParam, data); // 处理完成后,发给后端

    return api.get('', infoData).then(result => {
        return processBrandCouponData(result);
    }); // 所有数据返回一个 Promise,方便 Promise.all 调用
};


/**
 * 用户领券
 */
exports.getCoupon = (data) => {
    var defaultParam = {
            method: 'app.promotion.getCoupon'
        },
        infoData = Object.assign(defaultParam, data); // 处理完成后,发给后端

    return api.get('', infoData).then(result => {
        return getBrandCouponStatus(result);
    }); // 所有数据返回一个 Promise,方便 Promise.all 调用
};