Blame view

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

'use strict';
8
const utils = '../../../utils';
王水玲 authored
9 10 11 12 13
const mRoot = '../models';
const headerModel = require('../../../doraemon/models/header');
const searchModel = require(`${mRoot}/search`);
const _ = require('lodash');
const helpers = global.yoho.helpers;
14
const searchProcess = require(`${utils}/search-process`);
王水玲 authored
15
王水玲 authored
16 17 18
/**
 * 搜索落地页
 */
王水玲 authored
19
const list = (req, res, next) => {
王水玲 authored
20 21 22 23
    let params = Object.assign({
        isSearch: true, // 搜索列表将最新改成默认的标识
        cartUrl: helpers.urlFormat('/cart/index/index')
    }, req.query);
王水玲 authored
24
    let title = '';
25
    let query = (req.query.query || '').toString();
王水玲 authored
26 27
    let isQueryFirstClass = false; // 标识用户搜的是不是一级品类
    let isQuerySecondClass = false; // 标识用户搜的是不是二级品类
王水玲 authored
28
    let domain = null;
29
    let uid = req.user.uid || 0;
王水玲 authored
30
王水玲 authored
31 32 33
    if (params.shop_id) {
        params.shopId = params.shop_id;
    }
zhangxiaoru authored
34
ccbikai(👎🏻🍜) authored
35
    params.isApp = req.yoho.isApp;
36
    params.physical_channel = req.yoho.channel && searchProcess.getChannelType(req.yoho.channel);
ccbikai(👎🏻🍜) authored
37
郭成尧 authored
38 39 40 41 42 43
    return searchModel.searchKeyActivity(params.query || '').then(activityResult => {
        let activity = _.get(activityResult, 'urlobj.appUrl', '');

        if (activity) {
            return res.redirect(activity);
        } else {
44
            // 获取第一页数据做服务端渲染
姜枫 authored
45
            let initialData = _.assign({
46 47 48
                type: 'default',
                order: '0',
                page: 1,
49
                limit: 12
姜枫 authored
50
            }, params);
51
徐炜 authored
52 53 54
            if (query) {
                query = query.toLowerCase();
            }
郭成尧 authored
55
56 57 58 59
            if (uid) {
                initialData.uid = uid;
            }
郭成尧 authored
60 61
            /* 判断是不是品牌, 是品牌跳到品牌列表页(显示搜索框),判断是不是品类, 是品类加导航标题(不显示搜索框) */
            return Promise.all([
62 63 64
                searchModel.getBrandDomain(query),
                searchModel.getClassNames(),
                searchModel.getSearchData(initialData)
郭成尧 authored
65 66
            ]).then(result => {
                if (query) {
67
                    domain = result[0];
郭成尧 authored
68 69 70 71 72 73 74 75 76 77

                    // 跳转到品牌商品列表页
                    if (domain !== null && !params.shop_id) {
                        let url = helpers.urlFormat('', {
                            from: 'search',
                            query: query

                        }, domain);

                        return res.redirect(url);
78 79
                    }
郭成尧 authored
80 81 82
                    // 品类名称为空时跳出
                    if (!result[1]) {
                        return;
83 84
                    }
郭成尧 authored
85 86 87 88 89 90
                    _.forEach(result[1].first, (obj) => {
                        // 精确查一级品类
                        if (obj === query) {
                            isQueryFirstClass = true;
                            return false;
                        }
91
                    });
郭成尧 authored
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

                    _.forEach(result[1].second, (obj) => {
                        // 精确查二级品类
                        if (obj === query) {
                            isQuerySecondClass = true;
                            return false;
                        }
                    });
                } else {
                    params.query = '';
                }

                // 搜索是一级品类
                if (isQueryFirstClass) {
                    title = '全部' + query;
                } else if (isQuerySecondClass) { // 搜索是二级品类
                    title = query;
                } else { // 搜索其它内容
                    if (query || params.form) {
                        params.search = {
                            default: query === '' ? false : query,
                            url: helpers.urlFormat('', null, 'search')
                        };
                    }
                    title = '搜索';
                }

                title = params.title ? params.title : title;

                res.render('search/list', {
122
                    _noLazy: true,
郭成尧 authored
123 124 125 126 127 128 129
                    module: 'product',
                    page: 'search-list',
                    pageHeader: headerModel.setNav({
                        navTitle: title
                    }),
                    title: title,
                    goodList: params,
130
                    firstPageGoodsList: result[2].list || [],
徐炜 authored
131 132
                    pageFooter: true,
                    shopId: params.shop_id || ''
郭成尧 authored
133 134 135 136
                });
            }).catch(next);
        }
    });
王水玲 authored
137 138
};
王水玲 authored
139
/**
zhangxiaoru authored
140 141 142
 * 搜索主页
 */
const index = (req, res, next) => {
143 144
    let title = '搜索',
        uid = req.user.uid || 0;
145
陈峰 authored
146
    ((render) => {
陈峰 authored
147
        if (_.get(req, 'app.locals.wap.search.removeHotSearch', false)) {
陈峰 authored
148 149
            render([]);
        } else {
150
            searchModel.getSearchIndex(uid).then((result) => {
陈峰 authored
151 152 153
                render(result);
            }).catch(next);
        }
zhangxiaoru authored
154
陈峰 authored
155
    })((result) => {
zhangxiaoru authored
156 157 158 159 160 161
        res.render('search/index', {
            module: 'product',
            page: 'search-index',
            pageHeader: headerModel.setNav({
                navTitle: title
            }),
zhangxiaoru authored
162
            pageFooter: true,
zhangxiaoru authored
163
            width750: true,
zhangxiaoru authored
164
            search: {
165
                defaultTerms: (result && result.hotTerms && result.hotTerms.defaultTerms && result.hotTerms.defaultTerms.length !== 0) ? result.hotTerms.defaultTerms[0].content : '',
zhangxiaoru authored
166
                url: helpers.urlFormat('', null, 'search'),
zhangxiaoru authored
167 168
                hotTerms: result.hotTerms,
                wantTerms: result.guessTerms
zhangxiaoru authored
169
            }
zhangxiaoru authored
170
zhangxiaoru authored
171
        });
zhangxiaoru authored
172
    });
zhangxiaoru authored
173 174 175
};

/**
zhangxiaoru authored
176 177 178 179 180 181 182 183 184 185 186 187
 * 联动搜索
 */
let fuzzyDatas = (req, res, next) => {
    let params = req.query.keyword;

    searchModel.getFuzzyDatas(params).then((result) => {
        res.json(result);
    }).catch(next);
};


/**
王水玲 authored
188 189
 * ajax 商品数据请求
 */
王水玲 authored
190
const search = (req, res, next) => {
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');
196
王水玲 authored
197
    let params = Object.assign({}, req.query);
198 199 200 201 202
    let uid = req.user.uid || 0;

    if (uid) {
        params.uid = uid;
    }
王水玲 authored
203
zzzzzzz authored
204
    params.isApp = req.yoho.isApp;
205
    params.limit = 24;
206
王水玲 authored
207
    searchModel.getSearchData(params).then((result) => {
208
郭成尧 authored
209
        if (result && result.list &&
210
            parseInt(params.page, 10) === 1 && parseInt(params.start, 10) > 0) {
211 212 213 214
            // 首屏渲染时,使用 'start' 参数裁减已渲染数据
            result.list = result.list.slice(params.start || 0);
        }
215
        if (result.list && result.list.length > 0) {
zhangxiaoru authored
216 217 218
            res.render('search/page', {
                layout: false,
                new: result.list,
219 220
                total: result.total,
                _noLazy: params.noLazy || false
zhangxiaoru authored
221 222 223 224
            });
        } else {
            res.json(result);
        }
王水玲 authored
225 226 227 228 229 230 231
    }).catch(next);
};

/**
 * 筛选
 */
let filter = (req, res, next) => {
232
    res.header('Access-Control-Allow-Origin', '*');
233
王水玲 authored
234 235 236 237 238 239 240 241 242 243 244 245 246 247
    let params = Object.assign({}, req.query);

    searchModel.getFilterData(params).then((result) => {
        res.render('search/filter', {
            layout: false,
            params: params,
            filter: result
        });
    }).catch(next);
};

module.exports = {
    list,
    filter,
zhangxiaoru authored
248
    search,
zhangxiaoru authored
249 250
    index,
    fuzzyDatas
王水玲 authored
251
};