Blame view

doraemon/middleware/rewrite.js 5.25 KB
1 2 3 4 5 6 7 8
/**
 * 路由重写
 * @author: chenfeng<feng.chen@yoho.cn>
 * @date: 2017/2/20
 */
'use strict';

const typeLib = require('../../config/type-lib');
陈峰 authored
9
const _ = require('lodash');
陈峰 authored
10 11
const utils = require('../../utils');
const helpers = global.yoho.helpers;
郭成尧 authored
12
const listParamsProcess = require('../../utils/list-params-process');
yyq authored
13 14

// const stringProcess = require('../../utils/string-process');
陈峰 authored
15 16 17 18

/**
 * 解析url规则中的参数
 */
陈峰 authored
19
const resolve = (req, res, next) => {
20
    let path,
陈峰 authored
21 22 23
        params = {
            channel: req.yoho.channel
        };
24
25 26 27 28
    path = _.join(_.map(req.params, v => {
        return v;
    }), '-');
陈峰 authored
29
    if (!path) {
30 31
        return next();
    }
32
    let conditions = path.split('-').filter(cond => cond);
33
陈峰 authored
34 35 36 37 38
    _.each(conditions, condition => {
        if (typeLib.channels[condition]) {
            params.channel = condition;
        } else if (condition.indexOf('_') >= 0) {
            let item = condition.split('_');
39
陈峰 authored
40 41 42
            if (item.length === 2) {
                params[item[0]] = item[1];
            }
43
        } else if (condition >= 0 && !params.id) {
陈峰 authored
44 45 46 47 48 49 50 51 52
            params.id = _.parseInt(condition);
        }
    });
    req.yoho.channel = params.channel;
    res.locals.channel = params.channel;
    res.locals.pageChannel = { [params.channel]: true };
    res.locals.setChannel = true;
    Object.assign(req.query, params);
    next();
53 54
};
陈峰 authored
55 56 57
/**
 * 简介channel参数
 */
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
const channel = (req, res, next) => {
    let channelName;

    if (req.query.channel) {
        if (req.query.channel >= 0) {
            channelName = typeLib.channelNames[req.query.channel];
        } else if (typeLib.channels[req.query.channel]) {
            channelName = req.query.channel;
        }
    } else if (req.query.gender) {
        let gender = req.query.gender;

        switch (gender) {
            case typeLib.gender.boys:
                gender = 1;
                break;
            case typeLib.gender.girls:
                gender = 2;
                break;
            case typeLib.gender.kids:
                gender = 3;
                break;
            case typeLib.gender.lifestyle:
                gender = 4;
                break;
陈峰 authored
83 84
            default:
                break;
85 86 87 88
        }
        channelName = typeLib.channelNames[gender];
    }
    channelName = channelName || req.yoho.channel;
陈峰 authored
89 90 91
    req.yoho.channel = channelName;
    delete req.query.channel;
    delete req.query.gender;
92 93 94
    next();
};
陈峰 authored
95 96 97 98 99 100 101 102 103 104 105 106 107 108
/**
 * 参数排序
 */
const sortParams = (req, res, next) => {
    let sorts = utils.mapSort(req.query);
    let queryKeys = _.keys(req.query);
    let index = 0;
    let matched = _.map(sorts, (val, key) => {
        return key === queryKeys[index++];
    });

    if (_.every(matched, match => match)) {
        return next();
    } else {
毕凯 authored
109 110 111 112 113 114 115
        let subdomain = 'list';

        if (req.hostname.indexOf('search') === 0) {
            subdomain = 'search';
        }

        return res.redirect(helpers.urlFormat('/', sorts, subdomain));
陈峰 authored
116 117 118
    }
};
郭成尧 authored
119 120 121 122 123
/**
 * 解析 Path 类型泛商品列表 URL 的参数
 */
const resolvePathParams = (req, res, next) => {
    let queryParams = req.query;
郭成尧 authored
124
    let pathParams = req.params.pathParams;
郭成尧 authored
125
郭成尧 authored
126 127
    pathParams = _.replace(pathParams, '.html', '');
郭成尧 authored
128
    // 1. 取 path 的参数
郭成尧 authored
129
    req.query = listParamsProcess.getParams(pathParams);
郭成尧 authored
130 131 132 133

    // 2. 取查询字符串参数
    _.assign(req.query, queryParams);
郭成尧 authored
134 135 136 137 138 139 140 141
    // 3. 取 params 参数
    if (req.params) {
        if (req.params.pathParams) {
            delete req.params.pathParams;
        }
        _.assign(req.query, req.params);
    }
yyq authored
142 143 144 145 146
    // if (req.query) {
    //     _.forEach(req.query, (perParam, index) => {
    //         req.query[index] = stringProcess.paramsFilter(perParam);
    //     });
    // }
郭成尧 authored
147 148 149
    return next();
};
150 151 152 153
/**
 * 解析 Path 类型泛商品列表异步请求中 URL 的参数
 */
const resolvePathParamsAjax = (req, res, next) => {
郭成尧 authored
154
    let pathParams = _.last(_.split(req.query.currentUrl, '/'));
郭成尧 authored
155 156 157

    pathParams = _.replace(pathParams, '.html', '');
郭成尧 authored
158
    let currentUrlParams = listParamsProcess.getParams(pathParams);
郭成尧 authored
159
    let queryParams = {};
160 161 162

    delete req.query.currentUrl;
郭成尧 authored
163 164 165 166
    if (currentUrlParams.order && req.query.type === 'default') {
        req.query.order = currentUrlParams.order;
    }
郭成尧 authored
167 168 169 170
    if (currentUrlParams.brand && req.query.brand === '0') {
        req.query.brand = currentUrlParams.brand;
    }
郭成尧 authored
171
    req.query = _.assign(queryParams, currentUrlParams, req.query);
172 173 174 175

    return next();
};
郭成尧 authored
176 177 178 179
/**
 * 解析 Path 类型店铺商品列表异步请求中 URL 的参数
 */
const resolveShopPathParamsAjax = (req, res, next) => {
郭成尧 authored
180 181 182 183 184 185 186 187 188 189 190 191 192 193
    let shopId;
    let currentUrlParams;
    let pathSplitArr = _.compact(_.split(req.query.currentUrl, '/'));
    let pathParams = _.last(pathSplitArr);

    if (pathSplitArr.length === 2) {
        shopId = _.last(_.split(pathParams, '-'));
    } else {
        currentUrlParams = listParamsProcess.getParams(pathParams);

        let shopInfoPath = _.replace(req.query.currentUrl, `/${pathParams}`, '');

        shopId = _.last(_.split(shopInfoPath, '-'));
    }
郭成尧 authored
194 195 196 197 198 199 200 201

    delete req.query.currentUrl;

    _.assign(req.query, currentUrlParams, {shop_id: shopId});

    return next();
};
202
module.exports = {
203
    resolve,
陈峰 authored
204
    channel,
郭成尧 authored
205
    sortParams,
206
    resolvePathParams,
郭成尧 authored
207 208
    resolvePathParamsAjax,
    resolveShopPathParamsAjax
209
};