coupon.js 2.16 KB
'use strict';

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

/**
 * [查询商品详情页优惠券]
 * 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 = [];
        let couPonstatus;

        if (result && result.data) {
            _.forEach(result.data, value => {
                couPonstatus = parseInt(value.status, 10);

                if (couPonstatus === 1 || couPonstatus === 3) {
                    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 === 1
                    });
                }
            });
        }

        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;
    });
};