coupon-new.js 2.84 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) => {
    _.map(couponList, coupon => {
        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 coupons = _.get(couponApi, 'data.couponList', []);

            if (coupons && coupons.length) {
                coupons = _couponProcess(coupons);
            }

            res.json(coupons);
        })().catch(next);
    }
}

module.exports = new CouponNew();