brand-shop.js 2.26 KB
'use strict';
const mRoot = '../models';
const brandShopModel = require(`${mRoot}/brand-shop`);

/**
 * 品牌店铺获取优惠券信息
 * @param  {[type]} req [description]
 * @param  {[type]} res [description]
 * @return {[type]}     [description]
 */
exports.coupon = (req, res, next) => {
// 测试假数据
    // var renderData = {
    //         module: 'product',
    //         page: 'brand-shop'
    //     },
    var brandId = req.query.brand_id || 536,
        uid = req.user.uid || 0;

    // // 测试假数据
    //  res.render('brand-shop/index', {
    //     layout:false,
    //     result: renderData,
    //     module: 'product',
    //     page: 'brand-shop'
    //  }); // 渲染页面

    brandShopModel.getBrandIntro({
        brand_id: brandId,
        uid: uid
    }).then((brandIntroData) => {

        if (brandIntroData.code === 200) {
            // 获取信息成功
        }
        if (brandIntroData.coupons.length) {
            res.render('brand-shop/index', {
                layout: false,
                result: brandIntroData,
                module: 'product',
                page: 'brand-shop'
            });
        }
    }).catch(next);
};

/**
 * 品牌店铺获取优惠券领取
 * @param  {[type]} req [description]
 * @param  {[type]} res [description]
 * @return {[type]}     [description]
 */
exports.getCoupon = (req, res, next) => {

    var uid = req.user.uid || 0,
        couponId = req.query.couponId;

    // 没有登录情况跳登录页面
    if (!uid) {
        res.json({
            result: {logIn: false}
        });
        return;
    }


    brandShopModel.getCoupon({
        uid: uid,
        couponId: couponId
    }).then((couponInfo) => {
        if (couponInfo.code === 200) {
            // 操作成功
        } else if (couponInfo.code === 402) {
            // 优惠券不存在
        } else if (couponInfo.code === 306) {
            // 很抱歉!您的用户身份类型不可使用该券
        } else if (couponInfo.code === 401) {
            // 优惠券已经领取
        } else if (couponInfo.code === 315) {
            // 很抱歉!该优惠券未到使用时间或者已过期
        }
        res.json({
            result: couponInfo // 记得更新优惠券状态
        });
    }).catch(next);
};