gbk2utf.js 807 Bytes
'use strict';

const urlEncode = require('urlencode');
const _ = require('lodash');
const helpers = global.yoho.helpers;
const allowSubDomain = ['list', 'search'];


module.exports = (req, res, next) => {
    let query = req.query.query;
    let subDomain = req.subdomains[0];

    if (query && _.includes(allowSubDomain, subDomain)) {
        try {
            decodeURIComponent(query);
            return next();
        } catch (e) {
            try {
                query = urlEncode.decode(query, 'gb2312');
                req.query.query = query;
                return res.status(301).redirect(helpers.urlFormat('', req.query, subDomain));
            } catch (e1) {
                return res.redirect(helpers.urlFormat('', null, subDomain));
            }
        }
    }

    return next();
};