Vue 2.x折腾记 - (12) Nuxt.js写一个校验访问浏览器设备类型及环境的中间件

前言

这个需求很是常见,分享出去的页面的有时候在手机访问,有时候别人是PC打开的;javascript

如果不是共享同一个页面的状况,就须要拦截跳转了;java

固然你要共享同一个页面也能够(放大化到PC也须要添加某些CSS,也需判断设备)android

思路

本质上仍是校验UA, 只是此次是从req拿到,而不是从客户端获取再作处理ios

客户端的处理的姿式web

  • 拿到window.navigator.userAgent
  • 写一个判断的JS,匹配,返回对应的类型
  • 拿到类型以后咱们再考虑是否去跳转,或者作一些行为处理

服务端的处理姿式微信

其实基本和上面的思路同样的,只是咱们能作处理的时间提早了框架

不用等到客户端页面渲染完毕后,再去判断,再作处理网站

用户的体验上会好不少ui

理清了逻辑咱们就能够开始写了url

谈谈Nuxt生命周期

Nuxt.js就是一个Vue的服务端渲染框架,和React的服务端渲染框架Next.js相似,

咱们这里使用的版本是v1.4.2(默认初始化版本是基于Express的),

让咱们看官方给出的Nuxt执行生命周期流程

render(渲染)以前有几个阶段,通用全局配置均在middleware(中间件)阶段

那为何不在nuxtServerInit去作一些处理,由于这里只能触发storeaction

代码实现

这里已经假设你已经大致看完官方文档,对于目录结构什么都了解为前提!

deviceType.js(utils目录)

// 这里的判断类型是本身整理的,覆盖面只涵盖我工做领域的
// 能够按需追加

/** * * @param {*} UA ,就是userAgent * @returns type: 设备类型 * env: 访问环境(微信/微博/qq) * masklayer: 就是给外部拿到判断是否显示遮罩层的,一些特殊环境要引导用户到外部去打开访问 */

function isWechat(UA) {
  return /MicroMessenger/i.test(UA) ? true : false;
}

function isWeibo(UA) {
  return /Weibo/i.test(UA) ? true : false;
}

function isQQ(UA) {
  return /QQ/i.test(UA) ? true : false;
}

function isMoible(UA) {
  return /(Android|webOS|iPhone|iPod|tablet|BlackBerry|Mobile)/i.test(UA)
    ? true
    : false;
}

function isIOS(UA) {
  return /iPhone|iPad|iPod/i.test(UA) ? true : false;
}

function isAndroid(UA) {
  return /Android/i.test(UA) ? true : false;
}

export function deviceType(UA) {
  if (isMoible(UA)) {
    if (isIOS(UA)) {
      if (isWechat(UA)) {
        return {
          type: "ios",
          env: "wechat",
          masklayer: true,
        };
      }
      if (isWeibo(UA)) {
        return {
          type: "ios",
          env: "weibo",
          masklayer: true,
        };
      }
      if (isQQ(UA)) {
        return {
          type: "ios",
          env: "qq",
          masklayer: true,
        };
      }
      return {
        type: "ios",
      };
    }
    if (isAndroid(UA)) {
      if (isWechat(UA)) {
        return {
          type: "android",
          env: "wechat",
          masklayer: true,
        };
      }
      if (isWeibo(UA)) {
        return {
          type: "android",
          env: "weibo",
          masklayer: true,
        };
      }
      if (isQQ(UA)) {
        return {
          type: "android",
          env: "qq",
          masklayer: true,
        };
      }
      return {
        type: "android",
      };
    }

    return {
      type: "mobile",
    };
  } else {
    return {
      type: "pc",
    };
  }
}




复制代码

device.js(middleware目录)

// @ts-nocheck
import { deviceType } from "~/utils/deviceType";
export default function(context) {
  // @ts-ignore
  context.userAgent = process.server
    ? context.req.headers["user-agent"]
    : navigator.userAgent;
  // 给全局上下文添加一个属性来保存咱们返回的匹配信息
  context.deviceType = deviceType(context.userAgent);
  // 这里注入到store,是由于我部分页面须要判断机型请求不一样的数据,
  // 大家没有用到的话能够移除
  context.store.commit("SetDeviceType", context.deviceType);

  // 如果判断UA非移动端的,就在这里作处理了..
  // context.redirect(status,url) 这个能够重定向到外部网站
  // 如果内部访问能够直接用router对象push
  if (context.deviceType.type === "pc") {
    // context.redirect(301,'https://wwww.baidu.com')
  }
}

复制代码

nuxt.config.js

这种功能是面向全站的,因此要注入到全局,因此页面都默认执行

往router注入中间件便可全局生效

module.exports = {
  router: {
    middleware: ["device"],
  },
};

复制代码

总结

至此,常规的开发已经基本知足

有不对之处亦或者更好的实现请留言,会及时修正,谢谢阅读。

相关文章
相关标签/搜索