// 遵守pc与移动的命名规范,移动端前面加 m.,就会自动拼接拦截路径,自动响应式匹配pc/移动 const mobilepath = [ '/user/m_user', ] const pcpath = [ '/', ] export default function ({ isServer, req, redirect, route }) { // 这两个只有不同项目不同端口才用得上 // let pcOrigin = 'http://sincelocal.aupup.com:9003' // let mobileOrigin = 'http://sincelocal.aupup.com:9004' // console.log('navigator',navigator) let isMobile = ua => { return !!ua.match(/AppleWebKit.*Mobile.*/) } let userAgent = req ? req.headers['user-agent'] : navigator.userAgent || '' if(!req) return // http不能直接写死,要取出来,可能是https const request=req?.headers?.referer?.split('://')[0]||'http'; // console.log(request,'当前的请求头'); // 如果是移动端,但是确访问了pc的路径 if (isMobile(userAgent) && pcpath.includes(route.path.toLowerCase())) { console.log('重定向到移动端', req.headers.host, route.fullPath) return redirect( `${request}://${req.headers.host}` + `/m.${route.fullPath.substring(1, route.fullPath.length)}` ) } else if ( !isMobile(userAgent) && mobilepath.includes(route.path.toLowerCase()) ) { // 如果是pc进了移动 // 重定向到pc端 console.log('重定向到PC端', req.headers.host, route.fullPath) return redirect( `${request}://${req.headers.host}` + `/${route.fullPath.substring(1, route.fullPath.length)}` ) } // return isMobile(userAgent) ? '' : redirect(pcOrigin + route.fullPath) // 使用redirect 重定向到外链需要加上前缀:http / https }