Authored by yyq
Committed by 周少峰

js 跳转移动端

@@ -88,6 +88,7 @@ const logger = global.yoho.logger; @@ -88,6 +88,7 @@ const logger = global.yoho.logger;
88 // dispatcher 88 // dispatcher
89 try { 89 try {
90 const subDomain = require('./doraemon/middleware/sub-domain'); 90 const subDomain = require('./doraemon/middleware/sub-domain');
  91 + const mobileRefer = require('./doraemon/middleware/mobile-refer');
91 const itemNameHandler = require('./doraemon/middleware/item-name-handler'); 92 const itemNameHandler = require('./doraemon/middleware/item-name-handler');
92 const mobileCheck = require('./doraemon/middleware/mobile-check'); 93 const mobileCheck = require('./doraemon/middleware/mobile-check');
93 const user = require('./doraemon/middleware/user'); 94 const user = require('./doraemon/middleware/user');
@@ -98,6 +99,7 @@ try { @@ -98,6 +99,7 @@ try {
98 99
99 // YOHO 前置中间件 100 // YOHO 前置中间件
100 app.use(subDomain()); 101 app.use(subDomain());
  102 + app.use(mobileRefer());
101 app.use(itemNameHandler); 103 app.use(itemNameHandler);
102 app.use(mobileCheck()); 104 app.use(mobileCheck());
103 app.use(setYohoData()); 105 app.use(setYohoData());
  1 +/**
  2 + * page cache 前端js跳转url中间件
  3 + */
  4 +'use strict';
  5 +
  6 +const _ = require('lodash');
  7 +const querystring = require('querystring');
  8 +
  9 +const ptm = {
  10 + // 首页
  11 + '/': '',
  12 + '/woman': '/girls',
  13 + '/kids': '/kids',
  14 + '/lifestyle': '/lifestyle',
  15 + // 登录注册
  16 + '/signin.html': '/signin.htm',
  17 + '/reg.html': '/passport/reg/index',
  18 + '/passport/back/index': '/passport/back/mobile',
  19 + // 列表
  20 + '/product/list/new': '/product/new',
  21 + // 品牌列表
  22 + '/brands': '/brands',
  23 + // 领券中心
  24 + '/coupon/index': '/coupon/floor?title=领券中心&code=b78b32ed81b18dde8ac84fd33602b88b'
  25 +
  26 +};
  27 +
  28 +module.exports = () => {
  29 + return (req, res, next) => {
  30 + let domain = 'm.yohobuy.com';
  31 + let proReg = /^\/product\/pro/,
  32 + guangReg = /^\/guang/,
  33 + guangDetailReg = /.html$/,
  34 + qsReg = /\?/;
  35 +
  36 + if (!req.xhr) {
  37 + let url = _.head(_.split(req.url, '?'));
  38 + let data = {};
  39 +
  40 + if (ptm.hasOwnProperty(url)) {
  41 + data.mobileRefer = `//${domain}${ptm[req.url]}`;
  42 + } else if (url === '/product/list/index') {
  43 + data.mobileRefer = `//list.${domain}`;
  44 + } else if (url === '/product/index/brand') {
  45 + data.mobileRefer = `//${req.query.domain}.${domain}`;
  46 + } else if (url === '/product/search/index') {
  47 + data.mobileRefer = `//search.${domain}`;
  48 + } else if (guangReg.test(url)) {
  49 + data.mobileRefer = `//guang.${domain}`;
  50 +
  51 + if (guangDetailReg.test(url)) {
  52 + data.mobileRefer += url.replace(/\/guang\/detail\/([\d]+).html/, '/info/index?id=$1');
  53 + } else if (url === '/guang/Index/editor') {
  54 + data.mobileRefer += `/author/index?id=${req.query.author_id}`;
  55 + }
  56 + } else if (proReg.test(url)) {
  57 + data.mobileRefer = `//${domain}${url}`;
  58 + }
  59 +
  60 + if (!_.isEmpty(req.query) && !qsReg.test(data.mobileRefer)) {
  61 + data.mobileRefer += `?${querystring.stringify(req.query)}`;
  62 + }
  63 +
  64 + Object.assign(res.locals, data);
  65 + }
  66 + next();
  67 + };
  68 +};
@@ -21,6 +21,9 @@ @@ -21,6 +21,9 @@
21 {{/if}} 21 {{/if}}
22 </head> 22 </head>
23 <body> 23 <body>
  24 + {{#if mobileRefer}}
  25 + <input type="hidden" id="m-refer" value="{{mobileRefer}}">
  26 + {{/if}}
24 {{> header}} 27 {{> header}}
25 {{> common/simple-header}} 28 {{> common/simple-header}}
26 {{#if pageErr}} 29 {{#if pageErr}}
@@ -94,6 +94,15 @@ function getShoppingKey() { @@ -94,6 +94,15 @@ function getShoppingKey() {
94 return JSON.parse(c).k; 94 return JSON.parse(c).k;
95 } 95 }
96 96
  97 +// page cache改造-前端移动端检测
  98 +(function() {
  99 + var mrefer = $('#m-refer').val();
  100 +
  101 + if (mrefer && window.navigator.userAgent.match(/(nokia|iphone|android|ipad|motorola|^mot\-|softbank|foma|docomo|kddi|up\.browser|up\.link|htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam\-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte\-|longcos|pantech|gionee|^sie\-|portalmmm|jig\s browser|hiptop|^ucweb|^benq|haier|^lct|opera\s*mobi|opera\*mini|320x320|240x320|176x220)/i)) { // eslint-disable-line
  102 + window.location = mrefer;
  103 + }
  104 +}());
  105 +
97 // YAS统计代码 106 // YAS统计代码
98 (function(w, d, s, j, f) { 107 (function(w, d, s, j, f) {
99 var a = d.createElement(s); 108 var a = d.createElement(s);