nuxtjs2.x项目PC移动互相跳转

1、在plugins目录下新建terminalToggle.js,写入以下代码

(function () {
  let sUserAgent = navigator.userAgent.toLowerCase();
  let isIpad = sUserAgent.match(/ipad/i) == "ipad";
  let isIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
  let isMidp = sUserAgent.match(/midp/i) == "midp";
  let isUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
  let isUc = sUserAgent.match(/ucweb/i) == "ucweb";
  let isAndroid = sUserAgent.match(/android/i) == "android";
  let isCE = sUserAgent.match(/windows ce/i) == "windows ce";
  let isWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
  let isMobile =isIpad || isIphoneOs || isMidp || isUc7 || isUc || isAndroid || isCE || isWM;

  if (isMobile) { //【如果是PC端项目,使用这段代码】
    let PC_URL = process.env.VUE_APP_WEBSITE_PC_URL;
    let H5_URL = process.env.VUE_APP_WEBSITE_H5_URL;
    if (PC_URL && H5_URL) {
      let nowUrl = window.location.href;
      if (nowUrl.includes(PC_URL)) { // 跳转至移动端地址
        window.location.href = nowUrl.replace(PC_URL, H5_URL);
      }
    }
  }

 if (!isMobile) { //【如果是H5端项目,使用这段代码】
    let PC_URL = process.env.VUE_APP_WEBSITE_PC_URL;
    let H5_URL = process.env.VUE_APP_WEBSITE_H5_URL;
    if (PC_URL && H5_URL) {
      let nowUrl = window.location.href;
      if (nowUrl.includes(H5_URL)) { // 跳转至PC端地址
        window.location.href = nowUrl.replace(H5_URL, PC_URL);
      }
    }
  }
})();

2、在nuxt.config.js的plugins配置项里引入

posted @ 2024-07-15 09:57  huihuihero  阅读(29)  评论(0编辑  收藏  举报