coupon-new.js 3.54 KB
const co = Promise.coroutine;
const _ = require('lodash');
const headerModel = require('../../../doraemon/models/header'); // 头部model
const CouponNewModel = require('../models/coupon-new');
const helpers = global.yoho.helpers;

/**
 * 优惠券数据处理
 */
const _couponProcess = (couponList, type) => {
    _.map(couponList, coupon => {
        if (type === 'use') {
            coupon.is_used = true;
        }

        if (type === 'use' || coupon.is_overtime === 'Y' || coupon.is_invalid === 'Y') {
            coupon.usedOvertimeOrInValid = true;
        }

        coupon.useNowLink = helpers.urlFormat(`/list/cc${coupon.coupon_code}-cd${coupon.coupon_id}?intro_text=以下商品可使用【${coupon.coupon_name}】优惠券&title=优惠活动商品`, '', null); // eslint-disable-line
        return coupon;
    });
    return couponList;
};

class CouponNew {
    page(req, res, next) {
        let uid = req.user.uid;

        co(function* () {
            let [couponsApi, couponNumApi] = yield Promise.all([
                req.ctx(CouponNewModel).couponGet({ uid }),
                req.ctx(CouponNewModel).getCouponNums({ uid })
            ]);

            // console.log('api:', couponNumApi);

            let pageData = _.get(couponsApi, 'data', {});
            let couponNumStr = {
                notuse: 0,
                use: 0,
                overtime: 0
            };
            let couponNum = Object.assign(couponNumStr, _.get(couponNumApi, 'data', null));

            let couponList = _.get(pageData, 'couponList', []);

            if (couponList && couponList.length) {
                pageData.couponList = _couponProcess(couponList);
            }

            if (couponNum) {
                if (couponNum.notuse > 99) {
                    couponNumStr.notuse = '99+';
                } else {
                    couponNumStr.notuse = couponNum.notuse + '';
                }

                if (couponNum.use > 99) {
                    couponNumStr.use = '99+';
                } else {
                    couponNumStr.use = couponNum.use + '';
                }

                if (couponNum.overtime > 99) {
                    couponNumStr.overtime = '99+';
                } else {
                    couponNumStr.overtime = couponNum.overtime + '';
                }
            }

            res.render('coupons/page', {
                module: 'home',
                page: 'coupon-new',
                localCss: true,
                width750: true,
                pageData,
                couponNumStr,
                pageHeader: headerModel.setNav({
                    navTitle: '优惠券',
                    myConponPageNavBtn: true,
                    navBtn: false
                })
            });

        })().catch(next);
    }

    /**
     * 优惠券数据
     */
    coupons(req, res, next) {
        let uid = req.user.uid;
        let type = req.body.type;
        let filter = req.body.filter;
        let page = req.body.page;

        co(function* () {
            let couponApi = yield req.ctx(CouponNewModel).couponGet({
                uid, type, filter, page
            });
            let couponsList = _.get(couponApi, 'data.couponList', []);

            if (couponsList && couponsList.length) {
                couponsList = _couponProcess(couponsList, type);
                res.render('coupons/coupons', {
                    couponsList,
                    layout: false
                });
            } else {
                res.json([]);
            }
        })().catch(next);
    }
}

module.exports = new CouponNew();