filter-qs.js 881 Bytes
/**
 * query string 过滤
 * @author: yyq<yanqing.yang@yoho.cn>
 * @date: 2017/1/17
 */
'use strict';

const _ = require('lodash');

module.exports = (req, res, next) => {
    let query = req.query;
    let int = ['color', 'limit', 'specialsale_id', 'page', 'ageLevel'];

    if (!_.isEmpty(query)) {
        for (let i in query) {
            if (query.hasOwnProperty(i)) {
                if (!query[i]) {
                    return;
                }

                if (int.indexOf(i) !== -1) { // 查询参数类型转换
                    query[i] = parseInt(`0${query[i]}`, 10);
                } else { // 过滤第三方推广参数错误拼接
                    let spt = query[i].split('?');

                    if (spt.length > 1) {
                        query[i] = spt[0];
                    }
                }
            }
        }
    }

    next();
};