mobile-check.js 656 Bytes
/**
 * 手机访问检测
 */
'use strict';
const querystring = require('querystring');

module.exports = () => {
    return (req, res, next) => {
        if (req.xhr) {
            return next();
        }

        if (req.noRefer) {
            res.locals.mobileRefer = '';
        } else if (req.isMobile) {
            const redirect = `//m.yohobuy.com${req.url}?${querystring.stringify(req.query || {})}`;

            if (req.shopRedirect) {
                return res.redirect(redirect);
            }
            return res.redirect(req.mobileUrl || res.locals.mobileRefer ||
                redirect);
        }

        return next();
    };
};