判断pc或者移动端
src/router/index.js
// 判断PC or mobile
router.beforeEach((to, from, next) => {
let browserRedirect = function() {
var sUserAgent = navigator.userAgent.toLowerCase();
var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
var bIsMidp = sUserAgent.match(/midp/i) == "midp";
var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
var bIsAndroid = sUserAgent.match(/android/i) == "android";
var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
if (
bIsIpad ||
bIsIphoneOs ||
bIsMidp ||
bIsUc7 ||
bIsUc ||
bIsAndroid ||
bIsCE ||
bIsWM
) {
return false;
} else {
return true;
}
};
let path = to.path;
let dest = path;
if (browserRedirect()) {
// 去除pc下/h5开头路径
if (path.startsWith("/h5")) {
dest = path.replace(/^\/h5/i, "");
dest = dest.padStart(1, "/");
}
// 判断是否找到pc页面,否则使用h5页面
console.log(dest)
if (totalRoutes.findIndex(item => item.path === dest) === -1) {
dest = path;
}
} else {
// 添加mobile下/h5开头
if (!path.startsWith("/h5")) {
dest = "/h5" + path;
dest = dest.endsWith("/") ? dest.slice(0, -1) : dest;
// 判断是否找到h5页面,否则使用pc页面
if (totalRoutes.findIndex(item => item.path === dest) === -1) {
dest = path;
}
}
}
dest === path ? next() : next({
path: dest
})
});