coupon-new.js 3.63 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.usedOrOvertime = 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 })
            ]);
            let pageData = _.get(couponsApi, 'data', {});
            let couponNum = _.get(couponNumApi, 'data', {});
            let couponNumStr = {};
            let couponList = _.get(pageData, 'couponList', []);

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

            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', []);
            let showUsedTip = false;

            if (couponsList && couponsList.length) {
                if (type === 'use') {
                    showUsedTip = '以上是近期已使用的优惠券';
                }

                if (type === 'overtime') {
                    showUsedTip = '以上是近期已失效的优惠券';
                }
            }

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

module.exports = new CouponNew();