nuxt.js middleware 中间件实现判断手机端跳转

vue里面写法如下:

export default function ({ isServer, req, redirect, route }) {
    let pcOrigin = 'https://www.xxxxx.com'
    let mobileOrigin = 'https://m.xxxxx.com'
    let isMobile = (ua) => {
      return !!ua.match(/AppleWebKit.*Mobile.*/)
    }
    let userAgent = req ? req.headers['user-agent'] : navigator.userAgent || ''
    if(route.params.novel_id || route.params.comic_id) {
        return isMobile(userAgent) ? redirect(mobileOrigin + route.fullPath) : ''
    } else {
        if(/ipad/i.test(userAgent)){  // 不包含ipad
            return
          } else {
            return isMobile(userAgent) ? redirect(mobileOrigin + '/app') : ''
          } 
    }
    // return isMobile(userAgent) ? redirect(mobileOrigin + route.fullPath) : ''
    // 使用redirect 重定向到外链需要加上前缀:http / https
  }
  

  

posted @ 2021-10-27 09:55  落灯花  阅读(684)  评论(0编辑  收藏  举报