coupon.js 2.52 KB
'use strict';

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

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

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

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

        return this.get({
            data: param
        });
    }

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

        return this.post({
            data: param
        });
    }

    /**
     * [获取店铺优惠券列表]
     * @param  {[type]}  params  [参数]
     * @return {[type]}          []
     */
    shopCouponsList(params) {
        return this.get({
            data: Object.assign({
                method: 'shop.coupons.list'
            }, params),
            param: {
                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>}
     */
    receiveCoupon(uid, couponId) {
        return this.get({
            data: {
                method: 'app.promotion.getCoupon',
                couponId: couponId,
                uid: uid
            }
        }).then(result => {
            return result;
        });
    }
};