Blame view

apps/product/controllers/list.js 17.4 KB
1 2 3 4 5 6 7
/**
 * 品类|品牌|店铺 controller
 * @author: wsl<shuiling.wang@yoho.cn>
 * @date: 2016/7/21
 */

'use strict';
郭成尧 authored
8
const utils = '../../../utils';
9 10 11 12
const mRoot = '../models';
const headerModel = require('../../../doraemon/models/header');
const listModel = require(`${mRoot}/list`);
const _ = require('lodash');
郭成尧 authored
13
const crypto = global.yoho.crypto;
14
const helpers = global.yoho.helpers;
郭成尧 authored
15
const productProcess = require(`${utils}/product-process`);
徐炜 authored
16
const searchModel = require(`${mRoot}/search`);
17 18

/**
郭成尧 authored
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
 * 从 useragent 获取 uid
 * @returns {*}
 * @private
 */
const _getUidFromUserAgent = (req) => {
    let userAgent = _.split(req.headers['user-agent'], ';');
    let uidString = '';

    _.forEach(userAgent, value => {
        if (_.startsWith(value, 'uid')) {
            uidString = value;
            return;
        }
    });

    return _.split(uidString, '=')[1];
};

/**
郭成尧 authored
38
 * 店铺 - 基础模板
39
 */
zzzzzzz authored
40
const _baseShop = (req, res, shopInfo, shopId) => {
41
郭成尧 authored
42 43 44
    listModel.getBaseShopData(Object.assign(req.query, {
        channel: req.yoho.channel
    }), shopInfo).then(result => {
45 46 47 48 49
        if (result && result.baseShopHome && result.baseShopHome.banner) {
            if (result.baseShopHome.banner.indexOf('?') < 0) {
                result.baseShopHome.banner += '?imageMogr2/auto-orient/strip/thumbnail/x150/crop/640x150';
            }
        }
郭成尧 authored
50 51

        res.render('search/goods-list', Object.assign({
52 53 54
            module: 'product',
            page: 'search-list',
            pageHeader: headerModel.setNav({
郭成尧 authored
55
                navTitle: result.shopName
56
            }),
郭成尧 authored
57
            goodList: result,
58
            showDownloadApp: true,
郭成尧 authored
59
            pageFooter: true,
zzzzzzz authored
60
            shopId: shopId,
郭成尧 authored
61
            shopPage: true
郭成尧 authored
62
        }, result.seoResult));
63
    });
郭成尧 authored
64
65 66 67
};

/**
郭成尧 authored
68
 *
zzzzzzz authored
69 70 71
 * 基础模板收藏
 */
const baseShopFav = (req, res) => {
郭成尧 authored
72 73 74 75 76 77 78
    let allowOrigin = _.get(req, 'headers.origin', null) ?
        req.headers.origin : req.protocol + '://' + req.headers.host;

    res.setHeader('Access-Control-Allow-Origin', allowOrigin);
    res.setHeader('Access-Control-Allow-Credentials', 'true');

    let uid = req.user.uid || req.query.uid || req.cookies.appUid || _getUidFromUserAgent(req);
zzzzzzz authored
79 80
    let shopId = req.query.shopId;
郭成尧 authored
81 82 83 84 85
    listModel.getBrandFavStatus({
        uid: uid,
        id: shopId,
        type: 'shop'
    }).then(result => {
zzzzzzz authored
86 87
        res.json({
            code: 200,
郭成尧 authored
88
            collect: result && result.data,
zzzzzzz authored
89 90 91 92 93 94
        });
    });
};


/**
郭成尧 authored
95
 * 店铺首页
96 97 98 99 100
 * @return int
 */
const _shop = (req, res, shopId) => {
    let isApp = req.query.app_version || req.query.appVersion || false;
    let pageHeader = {};
郭成尧 authored
101
    let uid = req.user.uid;
102
zzzzzzz authored
103
    // pagecache前端判断
104 105 106 107 108 109 110 111 112 113 114 115 116
    if (!isApp) {
        pageHeader = {
            pageHeader: _.assign({
                shopPage: {
                    text: '分类',
                    url: helpers.urlFormat('/product/index/category', {
                        shop_id: shopId
                    })
                }
            }, headerModel.setNav({
                navTitle: false
            }))
        };
郭成尧 authored
117 118 119 120
    } else {
        uid = req.query.uid;
        req.session.appUid = uid;
        res.cookie('appUid', uid, {
郭成尧 authored
121
            domain: 'yohobuy.com',
郭成尧 authored
122 123
            expires: new Date(Date.now() + 2592000000) // 有效期一年
        });
124 125
    }
郭成尧 authored
126
    listModel.getShopData(req, shopId, uid, isApp).then(result => {
郭成尧 authored
127
        if (result.goBrand) {
郭成尧 authored
128
郭成尧 authored
129
            /* 若店铺使用基础模板跳转基础模板 */
zzzzzzz authored
130
            _baseShop(req, res, result.goBrand, shopId);
131
        } else {
郭成尧 authored
132 133
            listModel.searchProductBySkn(result.hotListproductSkn).then(hotList => {
                result = _.assign(result, pageHeader, {
郭成尧 authored
134
                    hotList: productProcess.processProductList(hotList, {isApp: isApp})
郭成尧 authored
135 136
                });
郭成尧 authored
137 138 139
                _.forEach(result.hotList, (value, key) => {
                    result.hotList[key].tags = {};
                    result.hotList[key].is_soon_sold_out = false;
郭成尧 authored
140
                    result.hotList[key].tags.is_hot = true;
郭成尧 authored
141 142
                });
ccbikai(👎🏻🍜) authored
143 144
                // 有领券功能,不缓存
                res.set('Cache-Control', 'no-cache');
ccbikai(👎🏻🍜) authored
145
郭成尧 authored
146
                res.render('shop/index', Object.assign({
郭成尧 authored
147 148 149 150 151 152 153 154
                    module: 'product',
                    page: 'shop',
                    shopIndex: result,
                    shopHeadHide: true,
                    gender: req.query.gender,
                    channel: req.query.channel,
                    shopId: shopId,
                    shopPage: true
郭成尧 authored
155
                }, result.seoResult));
郭成尧 authored
156
157 158 159 160 161
            });
        }
    });
};
zzzzzzz authored
162 163 164 165
/**
 * app设置cookie
 */
const shopAppCookie = (req, res) => {
郭成尧 authored
166 167 168 169 170 171
    let allowOrigin = _.get(req, 'headers.origin', null) ?
        req.headers.origin : req.protocol + '://' + req.headers.host;

    res.setHeader('Access-Control-Allow-Origin', allowOrigin);
    res.setHeader('Access-Control-Allow-Credentials', 'true');
zzzzzzz authored
172 173 174 175 176 177 178 179 180 181 182 183 184 185
    let uid = req.query.uid;

    req.session.appUid = uid;
    res.cookie('appUid', uid, {
        domain: 'yohobuy.com',
        expires: new Date(Date.now() + 2592000000) // 有效期一年
    });
    res.json({
        code: 200,
        msg: 'app cookie set',
    });
};

/**
郭成尧 authored
186
 *
zzzzzzz authored
187 188 189
 * 前端请求判断shop是否收藏
 */
const shopFav = (req, res) => {
郭成尧 authored
190 191 192 193 194 195
    let allowOrigin = _.get(req, 'headers.origin', null) ?
        req.headers.origin : req.protocol + '://' + req.headers.host;

    res.setHeader('Access-Control-Allow-Origin', allowOrigin);
    res.setHeader('Access-Control-Allow-Credentials', 'true');
郭成尧 authored
196
    let uid = req.user.uid || req.query.uid || req.cookies.appUid || _getUidFromUserAgent(req);
zzzzzzz authored
197 198
    let shopId = req.query.shopId;
郭成尧 authored
199 200 201 202 203
    listModel.getBrandFavStatus({
        uid: uid,
        id: shopId,
        type: 'shop',
    }).then(result => {
zzzzzzz authored
204 205
        res.json({
            code: 200,
郭成尧 authored
206
            collect: result && result.data
zzzzzzz authored
207 208 209 210
        });
    });
};
211
// 品类落地页
徐炜 authored
212
const category = (req, res, next) => {
213 214
    let params = Object.assign({}, req.query);
郭成尧 authored
215 216 217 218 219 220 221 222 223
    /* 勿修改,唤起 APP 使用 */
    let appParams = Object.assign({}, req.query, {
        title: req.query.sort_name || ''
    });

    let appPath = 'yohobuy://yohobuy.com/goapp?openby:yohobuy={"action":"go.list","params":' +
        JSON.stringify(appParams) +
        '}';
224 225
    let uid = req.user.uid || 0;
226
    // 获取第一页数据做服务端渲染
徐炜 authored
227
    let initialData = _.assign({
228
        gender: params.gender,
229
        sort: parseInt(params.sort, 10) || '',
230 231 232
        type: 'default',
        order: '0',
        page: 1,
233
        limit: 12,
徐炜 authored
234
    }, params);
235
236 237 238 239
    if (uid) {
        initialData.uid = uid;
    }
240
    searchModel.getSearchData(initialData).then((firstScreenGoodsList) => {
徐炜 authored
241 242 243 244 245 246 247 248
        res.render('search/goods-list', {
            _noLazy: true, // 首屏不使用lazyload
            module: 'product',
            page: 'search-list',
            pageHeader: headerModel.setNav({
                navTitle: req.query.title || req.query.sort_name
            }),
            goodList: params,
249
            firstPageGoodsList: firstScreenGoodsList.list,
徐炜 authored
250
            showDownloadApp: true,
251
            pageFooter: true,
姜枫 authored
252
            category: true,
郭成尧 authored
253
            localCss: true,
陈峰 authored
254 255
            appPath: appPath,
            introText: req.query.intro_text
徐炜 authored
256 257
        });
    }).catch(next);
258 259
};
郭成尧 authored
260 261 262 263 264 265
/**
 * 品牌店铺的入口
 * @param req
 * @param res
 * @param next
 */
266 267
const brand = (req, res, next) => {
    let params = Object.assign({}, req.query);
郭成尧 authored
268
    let domain = req.query.domain;
王水玲 authored
269
    let uid = req.user.uid;
270 271 272 273
    let brandId = 0;
    let brandLogo = {};
    let title = '';
郭成尧 authored
274 275 276 277 278 279
    /* tar add 160909 如果有 shop_id 直接跳转 */
    if (req.query.shop_id) {
        _shop(req, res, req.query.shop_id);
        return;
    }
280 281
    if (!domain) {
        res.redirect('/?go=1');
郭成尧 authored
282
        return;
283 284
    }
郭成尧 authored
285
    listModel.getBrandLogoByDomain(domain).then(result => {
郭成尧 authored
286 287 288 289
        if (!result) {
            return next();
        }
290 291 292 293 294 295 296 297 298 299 300 301 302
        brandLogo = result;

        title = brandLogo.name;

        if (brandLogo && brandLogo.id) {
            brandId = brandLogo.id;
        }

        params = _.assign({
            brand: brandId
        }, params);

        // 无店铺:0--->品牌页 无单品店有多品店:1--->搜索页 有单品店:2--->店铺页面
郭成尧 authored
303
        if (req.query.from !== 'search' && brandLogo.type === '2' && brandLogo.shopId) {
304 305
            _shop(req, res, brandLogo.shopId);
        } else { // 获取品牌店铺信息
郭成尧 authored
306
            listModel.getBrandShops(brandId, req).then(brandShop => {
307 308 309 310
                if (brandId === 0) {
                    params.query = domain;
                }
郭成尧 authored
311 312
                params.seoResult = brandShop.seoResult;
313 314
                // 从搜索页过来的,显示搜索框, 和进入品牌引导信息 或者品牌关联多店铺
                if (req.query.from === 'search' || brandShop.length > 0) {
郭成尧 authored
315 316
                    params = _.assign({
                        brandWay: _.isEmpty(brandShop) ? brandLogo : brandShop,
317 318 319 320
                        search: {
                            default: req.query.query,
                            url: helpers.urlFormat('', null, 'search')
                        }
郭成尧 authored
321
                    }, params);
郭成尧 authored
322
                } else if (brandId && brandId !== 0 && brandId !== '' && brandId !== 'undefined') { // 品牌一览过来的展示品牌介绍和LOGO
323 324 325
                    return Promise.all([listModel.getBrandIntro(brandId, uid), listModel.getBrandBanner(brandId)]).then((resData) => { //eslint-disable-line
                        title = resData[0].title;
                        delete resData[0].title;
326 327

                        return _.assign({
328 329
                            banner: resData[1]
                        }, resData[0]);
330 331
                    });
                }
郭成尧 authored
332
            }).then(brandHome => {
333 334
                params.brandHome = brandHome;
335
                if (!brandHome || !brandHome.id) {
ccbikai(👎🏻🍜) authored
336 337 338
                    res.set('Cache-Control', 'no-cache');
                }
郭成尧 authored
339
                res.render('search/goods-list', Object.assign({
340 341 342 343 344 345 346
                    module: 'product',
                    page: 'search-list',
                    pageHeader: headerModel.setNav({
                        navTitle: title !== '' ? title : domain
                    }),
                    goodList: params,
                    showDownloadApp: true,
郭成尧 authored
347
                    pageFooter: true,
zzzzzzz authored
348
                    domain: req.query.domain,
郭成尧 authored
349
                    shopPage: true
郭成尧 authored
350
                }, params.seoResult));
351 352 353 354 355
            });
        }
    }).catch(next);
};
郭成尧 authored
356
/**
郭成尧 authored
357
 *
郭成尧 authored
358 359 360
 * @param req
 * @param res
 */
zzzzzzz authored
361 362 363 364 365 366 367
const brandFav = (req, res) => {
    let domain = req.query.domain;
    let uid = req.user.uid;
    let brandId = 0;

    listModel.getBrandLogoByDomain(domain).then((result) => {
        brandId = result.id;
郭成尧 authored
368 369 370 371 372
        listModel.getBrandFavStatus({
            uid: uid,
            id: brandId,
            type: 'brand',
        }).then(result2 => {
zzzzzzz authored
373 374 375 376 377
            res.json(result2);
        });
    });
};
郭成尧 authored
378 379 380 381 382 383
/**
 * 店铺简介
 * @param req
 * @param res
 * @param next
 */
384 385 386 387 388
const shopIntro = (req, res, next) => {
    let shopId = req.query.shop_id;
    let appVersion = req.body.appVersion || false;
    let params = {};
389
    if (parseInt(shopId, 10)) {
郭成尧 authored
390
        listModel.getShopIntro(shopId).then(result => {
391 392 393 394 395 396 397 398 399 400 401 402
            if (appVersion) {
                params = {
                    title: '店铺简介'
                };
            } else {
                params = {
                    pageHeader: headerModel.setNav({
                        navTitle: '店铺简介'
                    })
                };
            }
郭成尧 authored
403 404 405 406
            res.render('shop/intro', _.assign(params, result, {
                module: 'product',
                page: 'intro'
            }));
407 408 409 410 411 412 413 414 415 416 417 418
        }).catch(next);
    } else {
        res.redirect('/?go=1');
    }
};

/**
 * 品牌[店铺]收藏/取消收藏
 * id 品牌ID
 * opt 操作标识("ok":表示收藏,"cancel":表示取消收藏)
 */
const favoriteBrand = (req, res, next) => {
郭成尧 authored
419 420 421 422
    let allowOrigin = _.get(req, 'headers.origin', null) ?
        req.headers.origin : req.protocol + '://' + req.headers.host;

    res.setHeader('Access-Control-Allow-Origin', allowOrigin);
郭成尧 authored
423 424
    res.setHeader('Access-Control-Allow-Credentials', 'true');
425
    let id = req.query.id;
王水玲 authored
426
    let uid = req.user.uid;
427 428 429
    let opt = req.query.opt || 'ok';
    let type = req.query.type || 'product';
    let appVersion = req.query.appVersion || false;
zhangxiaoru authored
430
    let refer = encodeURIComponent(req.get('referer'));
郭成尧 authored
431
郭成尧 authored
432
    let url = helpers.urlFormat('/signin.html') + '?refer=' + refer;
433
zhangxiaoru authored
434
郭成尧 authored
435
    if (appVersion && appVersion !== 'false') {
姜枫 authored
436 437 438 439 440 441 442 443 444
        if (req.query.uid) {
            try {
                uid = crypto.decrypt('', req.query.uid);
            } catch (err) {
                uid = req.query.uid;
            }
        } else {
            uid = req.cookies.appUid;
        }
zhangxiaoru authored
445
        uid = parseInt(uid, 10);
郭成尧 authored
446 447 448 449 450

        if (!uid || uid === 'undefined') {
            uid = _getUidFromUserAgent(req);
        }
郭成尧 authored
451
        url = `${refer}&openby:yohobuy={"action":"go.weblogin","params":{"jumpurl":{"url":"${refer}","param":{}},"requesturl":{"param":{"method":"app.favorite.add","id":"${id}","fav_id":"${id}","type":"shop"},"url":"${_.get(global, 'yoho.API.ApiUrl', '')}"},"priority":"Y"}}`; // eslint-disable-line
郭成尧 authored
452
453 454
    }
郭成尧 authored
455 456
    if (!id) {
        return res.json({
457 458 459 460
            code: 401,
            message: '参数不正确',
            data: false
        });
461
    }
王水玲 authored
462
463
    if (!uid) {
郭成尧 authored
464
        return res.json({
465 466
            code: 400,
            message: '未登录',
王水玲 authored
467
            data: url
468 469 470
        });
    }
471 472 473 474 475 476 477 478 479
    if (opt !== 'ok') { // 取消收藏
        listModel.setFavoriteCancel(id, uid, type).then(result => {
            res.json(result);
        }).catch(next);
    } else {
        listModel.setFavorite(id, uid, type).then(result => {
            res.json(result);
        }).catch(next);
    }
480 481
};
郭成尧 authored
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
/**
 * 店铺品类页面
 * @param req
 * @param res
 * @param next
 */
const shopCategory = (req, res, next) => {

    listModel.getShopCategory({
        shopId: req.query.shop_id,
        channel: req.yoho.channel,
        gender: req.query.gender || '1,3'
    }).then(result => {
        res.render('shop/category', {
            content: result,
郭成尧 authored
497
            allProduct: result.allProduct,
郭成尧 authored
498 499 500 501 502 503 504 505 506
            module: 'product',
            page: 'category',
            pageHeader: headerModel.setNav({
                navTitle: '品类'
            }),
        });
    }).catch(next);
};
郭成尧 authored
507 508 509 510 511 512 513 514 515 516 517 518 519
/**
 * 用戶領券
 * @param req
 * @param res
 * @param next
 */
const userCoupon = (req, res, next) => {
    let allowOrigin = _.get(req, 'headers.origin', null) ?
        req.headers.origin : req.protocol + '://' + req.headers.host;

    res.setHeader('Access-Control-Allow-Origin', allowOrigin);
    res.setHeader('Access-Control-Allow-Credentials', 'true');
520
    if (!req.body.couponID) {
郭成尧 authored
521 522 523
        return;
    }
524 525
    let cryptCouponId = crypto.decrypt('', req.body.couponID);
    let uid = req.body.uid || req.user.uid;
郝肖肖 authored
526 527
    let isApp = req.body.app_version || req.body.appVersion || false;
    let data = {};
郭成尧 authored
528
郭成尧 authored
529 530 531
    cryptCouponId = parseInt(cryptCouponId, 10);
    uid = parseInt(uid, 10);
郭成尧 authored
532
    if (uid) {
郭成尧 authored
533
        listModel.receiveCoupon(
郭成尧 authored
534
            uid,
郭成尧 authored
535 536 537
            cryptCouponId
        ).then(result => {
            res.json(result);
郭成尧 authored
538
            return;
郭成尧 authored
539
        }).catch(next);
郭成尧 authored
540
    } else {
郝肖肖 authored
541
        data.code = 4401;// 401错误已经被接口占用
郭成尧 authored
542
        let refer = req.get('referer');
郝肖肖 authored
543
        let toUrl = helpers.urlFormat('/signin.html', {refer: refer});
郭成尧 authored
544
郝肖肖 authored
545 546
        if (isApp) {
            toUrl += '&openby:yohobuy={"action":"go.weblogin","params":{"jumpurl":{"url":"' + refer +
郭成尧 authored
547
                '","param":{}},"requesturl":{"param":{"method":"app.promotion.getCoupon","couponId":"' +
郝肖肖 authored
548
                cryptCouponId + '"},"url":"' + _.get(global, 'yoho.API.ApiUrl', '') + '"},"priority":"Y"}}';
郭成尧 authored
549
        }
郭成尧 authored
550
郝肖肖 authored
551 552
        data.url = toUrl;
        res.json(data);
郭成尧 authored
553 554 555
    }
};
556 557 558 559 560 561 562
/**
 * 获取店铺优惠券列表
 * @param req
 * @param res
 * @param next
 */
const getShopCouponsList = (req, res, next) => {
563 564 565 566 567 568
    let allowOrigin = _.get(req, 'headers.origin', null) ?
        req.headers.origin : req.protocol + '://' + req.headers.host;

    res.setHeader('Access-Control-Allow-Origin', allowOrigin);
    res.setHeader('Access-Control-Allow-Credentials', 'true');
569
    let uid = req.query.uid || req.user.uid;
570
    let shopId = parseInt(req.query.shopId, 10);
571 572
    let param = {};
573 574 575 576
    if (shopId) {
        if (uid) {
            param.uid = uid;
        }
577
578
        param.shop_id = shopId;
579
580 581 582 583 584 585
        listModel.shopCouponsList(param).then(result => {
            res.json(result);
        }).catch(next);
    } else {
        res.json([]);
    }
586 587
};
郝肖肖 authored
588 589 590 591 592 593 594
/**
 * 获取品牌优惠券列表
 * @param req
 * @param res
 * @param next
 */
const getBrandCouponsList = (req, res, next) => {
595 596 597 598 599 600
    let allowOrigin = _.get(req, 'headers.origin', null) ?
        req.headers.origin : req.protocol + '://' + req.headers.host;

    res.setHeader('Access-Control-Allow-Origin', allowOrigin);
    res.setHeader('Access-Control-Allow-Credentials', 'true');
郝肖肖 authored
601
    let uid = req.query.uid || req.user.uid;
602
    let brandId = parseInt(req.query.brandId, 10);
郝肖肖 authored
603 604
    let param = {};
605 606 607 608
    if (brandId) {
        if (uid) {
            param.uid = uid;
        }
郝肖肖 authored
609
610
        param.brand_id = brandId;
郝肖肖 authored
611
612 613 614 615 616 617
        listModel.brandCouponsList(param).then(result => {
            res.json(result);
        }).catch(next);
    } else {
        res.json([]);
    }
郝肖肖 authored
618 619
};
620 621 622 623
module.exports = {
    category,
    brand,
    favoriteBrand,
zzzzzzz authored
624 625 626 627 628
    shopIntro,
    brandFav,
    shopAppCookie,
    shopFav,
    baseShopFav,
郭成尧 authored
629
    shopCategory,
630
    userCoupon,
郝肖肖 authored
631 632
    getShopCouponsList,
    getBrandCouponsList
633
};