coupon.js 2.08 KB
'use strict';

const _ = require('lodash');
const crypto = global.yoho.crypto;
const api = global.yoho.API;

const couponStatus = {
    1: '立即领取',
    2: '已抢光',
    3: '已领取'
};

/**
 * [查询商品详情页优惠券]
 * doc: http://git.yoho.cn/yoho-documents/api-interfaces/blob/master/促销/product_detail_page_coupon.md
 */
exports.queryProdPageCoupons = (uid, skn, brandId) => {
    const param = {
        method: 'app.coupons.queryProdPageCoupons',
        uid,
        skn,
        brandId
    };

    return api.get('', param);
};

/**
 *  [用户领券]
 *  doc: http://git.yoho.cn/yoho-documents/api-interfaces/blob/master/促销/promotion.md
 */
exports.getCoupon = (uid, couponId) => {
    const param = {
        method: 'app.promotion.getCoupon',
        uid,
        couponId
    };

    return api.post('', param);
};

/**
 * [获取店铺优惠券列表]
 * @param  {[type]}  params  [参数]
 * @return {[type]}          []
 */
exports.shopCouponsList = (params) => {
    return api.get('', Object.assign({
        method: 'shop.coupons.list'
    }, params), {
        cache: params.uid ? false : 180
    }).then(result => {
        let shopCoupons = [];

        if (result && result.data) {
            _.forEach(result.data, value => {
                shopCoupons.push({
                    validity: value.couponValidity,
                    id: crypto.encryption('', value.coupon_id + ''),
                    name: value.coupon_name,
                    pic: value.coupon_pic,
                    money: parseInt(value.money, 10),
                    status: couponStatus[value.status] || '立即领取'
                });
            });
        }

        return {
            couponsOne: shopCoupons.length <= 1,
            coupons: shopCoupons
        };
    });
};

/**
 * 用戶領券
 * @param uid
 * @param couponId
 * @returns {*|Promise.<TResult>}
 */
exports.receiveCoupon = (uid, couponId) => {
    return api.get('', {
        method: 'app.promotion.getCoupon',
        couponId: couponId,
        uid: uid
    }).then(result => {
        return result;
    });
};