Blame view

apps/product/controllers/sale.js 6.57 KB
王水玲 authored
1 2 3 4 5 6 7
/**
 * sale controller
 * @author: wsl<shuiling.wang@yoho.cn>
 * @date: 2016/5/17
 */

'use strict';
王水玲 authored
8
const mRoot = '../models';
Feng authored
9 10

// const cookie = require(`${library}/cookie`);
王水玲 authored
11
const headerModel = require('../../../doraemon/models/header');
12
const saleModel = require(`${mRoot}/sale`);
陈峰 authored
13
const qs = require('querystring');
王水玲 authored
14
王水玲 authored
15 16 17 18 19 20 21 22
// const queryParam = {
//     brand: '0',
//     gender: '1,2,3',
//     sort: '0',
//     size: '0',
//     price: '0',
//     pD: '0.1,0.9'
// };
王水玲 authored
23
王水玲 authored
24 25 26 27 28 29 30
/**
 * 公共数据处理
 * @param  {[object]} req
 * @param  {[string]} title 标题
 * @param  {[string]} page js文件page名称
 * @return {[type]}
 */
31
const _processPublicData = (req, title, page, backUrl) => {
ccbikai authored
32
    return {
33
        channel: req.yoho.channel,
王水玲 authored
34 35 36
        renderData: {
            module: 'product',
            page: page,
郭成尧 authored
37 38

            // title: title,
ccbikai authored
39
            saleNav: true,
ccbikai authored
40 41
            pageHeader: headerModel.setNav({
                navTitle: title,
42 43
                navBtn: false,
                backUrl: backUrl || ''
ccbikai authored
44
            }),
王水玲 authored
45 46 47
            pageFooter: true
        }
    };
王水玲 authored
48 49
};
50
/**
王水玲 authored
51 52 53 54
 * sale 首页 资源位获取
 * @param  {[object]} req
 * @param  {[object]} res
 * @return {[type]}
55
 */
ccbikai authored
56
let index = (req, res, next) => {
ccbikai authored
57
    let refer = req.get('Referer') || `${global.yoho.config.siteUrl}/${req.cookies._Channel || ''}`;
58
    let reqPath = req.baseUrl + req.path;
陈峰 authored
59
    let backUrl = refer.indexOf(reqPath) === -1 && refer.indexOf('-sale') === -1 ? refer : '';
王水玲 authored
60
ccbikai authored
61 62 63 64
    if (['boys', 'girls', 'kids', 'lifestyle'].indexOf(req.yoho.channel) === -1) {
        return next();
    }
ccbikai authored
65 66 67
    backUrl && res.cookie('saleRefer', backUrl, {
        domain: 'm.yohobuy.com'
    });
68 69 70 71
    backUrl = backUrl || req.cookies.saleRefer;

    let params = _processPublicData(req, 'SALE', 'sale', backUrl);
陈峰 authored
72 73
    params.channel = req.query.channel || params.channel;
王水玲 authored
74
    saleModel.getSaleData(params.channel).then((result) => {
ccbikai authored
75 76

        // 扩展头部频道选择数据
ccbikai authored
77
        Object.assign(params.renderData.pageHeader, {
陈峰 authored
78
            saleNav: saleModel.saleNav(params.channel, req.params[0] || !req.query.channel)
ccbikai authored
79
        });
ccbikai authored
80 81

        // 此处 channel 需要读取 cookies 的 channel
ccbikai authored
82 83 84 85
        if (req.cookies._Channel) {
            res.locals.pageChannel = {};
            res.locals.pageChannel[req.cookies._Channel] = true;
        }
ccbikai(👎🏻🍜) authored
86 87 88 89

        if (!result.length) {
            res.set('Cache-Control', 'no-cache');
        }
王水玲 authored
90
        res.render('sale/index', Object.assign(params.renderData, {
王水玲 authored
91
            content: result,
徐炜 authored
92
            localCss: true,
王水玲 authored
93 94 95 96
            floorHeader: {
                title: {
                    title: '最新降价'
                }
ccbikai authored
97
            }
98
        }));
99
    }).catch(next);
王水玲 authored
100
};
101
陈峰 authored
102
// 301到新路由
陈峰 authored
103
let indexRedirect = (req, res) => {
陈峰 authored
104 105 106 107 108
    let param = qs.stringify(req.query);

    if (param) {
        param = '?' + param;
    }
陈峰 authored
109
    res.redirect(301, `/product/${req.yoho.channel}-sale/${param}`);
陈峰 authored
110 111
};
112
/**
ccbikai authored
113
 * 断码区
王水玲 authored
114 115 116
 * @param  {[object]} req
 * @param  {[object]} res
 * @return {[type]}
117
 */
ccbikai authored
118 119
let breakingYards = (req, res, next) => {
    let params = _processPublicData(req, '断码区', 'break-code');
120
王水玲 authored
121
    saleModel.getBreakCodeData({
王水玲 authored
122
        yhChannel: params.channel
王水玲 authored
123
    }).then((result) => {
124
郝肖肖 authored
125 126 127
        res.render('sale/break-code', Object.assign(params.renderData, result, {
            localCss: true
        }));
128
    }).catch(next);
129
};
王水玲 authored
130 131

/**
ccbikai authored
132
 * 折扣专场列表
王水玲 authored
133 134 135
 * @param  {[object]} req
 * @param  {[object]} res
 * @return {[type]}
王水玲 authored
136
 */
ccbikai authored
137 138
let discount = (req, res, next) => {
    let params = _processPublicData(req, '折扣专场', 'discount');
王水玲 authored
139
王水玲 authored
140
    saleModel.getDiscountData(params.channel).then((result) => {
141
        res.render('sale/discount', Object.assign(params.renderData, {
142 143
            data: result,
            localCss: true
144
        }));
145
    }).catch(next);
王水玲 authored
146
};
王水玲 authored
147
ccbikai authored
148
王水玲 authored
149
/**
ccbikai authored
150
 * 折扣专场专题详情
王水玲 authored
151 152 153
 * @param  {[object]} req
 * @param  {[object]} res
 * @return {[type]}
王水玲 authored
154
 */
ccbikai authored
155
let discountDetail = (req, res, next) => {
王水玲 authored
156
    let id = req.query.id;
ccbikai authored
157
    let params = _processPublicData(req, '', 'discount-detail');
王水玲 authored
158
159
    saleModel.getDiscountDetailData(id, req.yoho.channel).then((result) => {
王水玲 authored
160
        params.renderData.pageHeader.navTitle = result.title;
161
162 163 164
        res.render('sale/discount-detail', Object.assign(params.renderData, result, {
            localCss: true
        }));
165
    }).catch(next);
王水玲 authored
166
};
王水玲 authored
167
168 169 170 171 172 173 174 175
let getTime = (req, res, next) => {
    let id = req.query.id;

    saleModel.getDiscountTime(id, req.yoho.channel).then((time) => {
        res.json(time);
    }).catch(next);
};
王水玲 authored
176
/**
王水玲 authored
177 178 179 180
 * vip 会员专享
 * @param  {[object]} req
 * @param  {[object]} res
 * @return {[type]}
王水玲 authored
181
 */
ccbikai authored
182 183
let vip = (req, res, next) => {
    let params = _processPublicData(req, '会员专享', 'vip');
王水玲 authored
184
王水玲 authored
185 186
    saleModel.getVipData(params.channel).then((result) => {
        res.render('sale/vip', Object.assign(params.renderData, {
郝肖肖 authored
187 188
            content: result,
            localCss: true
王水玲 authored
189
        }));
190
    }).catch(next);
王水玲 authored
191
};
王水玲 authored
192
王水玲 authored
193 194 195 196 197 198
/**
 * 筛选
 * @param  {[object]} req
 * @param  {[object]} res
 * @return {[type]}
 */
ccbikai authored
199
let filter = (req, res, next) => {
ccbikai authored
200 201 202
    let params = Object.assign({}, req.query);

    saleModel.getFilterData(params).then((result) => {
王水玲 authored
203
        res.render('sale/filter', {
ccbikai authored
204 205 206 207
            layout: false,
            params: params,
            filter: result
        });
208
    }).catch(next);
ccbikai authored
209 210
};
王水玲 authored
211 212 213 214 215 216 217

/**
 * 商品搜索
 * @param  {[object]} req
 * @param  {[object]} res
 * @return {[type]}
 */
ccbikai authored
218
let search = (req, res, next) => {
郭成尧 authored
219
    let params = req.query; // tar modified 161121 safe
王水玲 authored
220
    let uid = req.user.uid || 0;
ccbikai authored
221
ccbikai authored
222 223 224 225 226 227 228 229
    // 断码区域必须传尺码
    if (params.saleType === '1' && (!params.breakSort || !params.breakSize)) {
        return res.render('sale/product', {
            layout: false,
            params: params
        });
    }
230 231 232 233
    if (uid) {
        params.uid = uid;
    }
ccbikai authored
234
    saleModel.getSearchData(params, uid).then((result) => {
235
        let vipLevel = result[1].cur_level;
王水玲 authored
236 237 238
        let vipObj = {};

        if (req.query.saleType === '2') {
王水玲 authored
239
            vipObj = {
王水玲 authored
240
                saleVip: (req.query.saleType === '2' && (!uid || vipLevel === '0')),
王水玲 authored
241
                vipLevel: vipLevel,
王水玲 authored
242 243 244 245
                saleViplogin: vipLevel >= 1 ? true : false,
                vipPrice1: vipLevel === '1',
                vipPrice2: vipLevel === '2',
                vipPrice3: vipLevel === '3'
王水玲 authored
246
            };
王水玲 authored
247 248
        }
王水玲 authored
249
        res.render('sale/product', Object.assign({
王水玲 authored
250
            layout: false,
ccbikai authored
251
            params: params,
252 253
            goods: result[0],
            _noLazy: params._noLazy || false
王水玲 authored
254
        }, vipObj));
255
    }).catch(next);
王水玲 authored
256
};
ccbikai authored
257 258 259 260 261 262 263 264

module.exports = {
    index,
    breakingYards,
    discount,
    discountDetail,
    vip,
    filter,
265
    search,
陈峰 authored
266 267
    getTime,
    indexRedirect
ccbikai authored
268
};