1. 程式人生 > 程式設計 >vue中PC端地址跳轉移動端的操作方法

vue中PC端地址跳轉移動端的操作方法

需求:pc端和移動端是兩個獨立的專案,兩個專案專案中的內容基本相同,連結組合的方式都有規律可循,接到的需求便是在移動端訪問pc端的URL連線時,重定向至移動端對應頁面。

這個需求實現的方式比較明瞭,我的大致思路是在路由守衛處監聽每個進來的路由請求,分析該請求是否是由移動端訪問,若不是,則該路由請求直接放行;若是則分析要進入的路由路徑,提取路徑中的必要欄位,組合稱新的移動端連結即可。

裡面涉及到了三個知識點:1、路由守衛,2、客戶端判斷、3、正則提取文字,接下來就分別按照這幾點講解一下,並附上整個開發過程的原始碼,供大家參考學習或批評指正。

1、路由守衛

  • to:要進入的路由
  • from:從哪個路由訪問
  • next:路由控制引數,常用next(true),和next(false)
//所有的路由請求都會經過該路由守衛,
router.beforeEach((to,from,next) => {
	
    //訪問連結如:http://localhost/page/detail/IUKGEQ/108/9933/32279/8
    //訪問路徑為:/page/detail/IUKGEQ/108/9933/32279/8
  	let toUrl = to.path;
                                                                       
    //該路由請求放行
    next();

});

2、判斷客戶端

navigator.userAgent:可獲取瀏覽器用於HTTP請求的使用者代理頭的值

 if (typeof window !== 'undefined' && typeof window.navigator !== 'undefined') {
            if(/|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
            	//處理移動端的業務邏輯
            }else{
				//處理電腦端的業務邏輯
            }
   }

3、正則表示式()

語法

/正則表示式主體/修飾符(可選)

修飾

表示式 描述
i 執行www.cppcns.com對大小寫不敏感的匹配。
g 執行全域性匹配(查詢所有匹配而非在找到第一個匹配後停止)。
m 執行多行匹配。

search()

search() 方法 用於檢索字串中指定的子字串,或檢索與正則表示式相匹配的子字串,並返回子串的起始位置。若無則返回**-1**。

// 不區分大小寫                  
var index = 'Hello World!'.search(/world/i);

replace()

replace() 方法 用於在字串中用一些字元替換另一些字元,或替換一個與正則表示式匹配的子串。

var txt = 'Microsoft'.replace("Microsoft","World");

test()

test() 方法用於檢測一個字串是否匹配某個模式,如果字串中含有匹配的文字,則返回 true,否則返回 false

var flag = /Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent);

exec()

exec() 方法用於檢索字串中的正則表示式的匹配。

該函式返回一個數組,其中存放匹配的結果。如果未找到匹配,則返回值為 null。

var matchParams = /(\d{1,3})\/(\d{4,6})\/(\d{4,6})/.exec('/page/detail/IUKGEQ/108/9933/32279/8')

正則語法參考:https://www.runoob.com/regexp/regexp-syntax.html

4、原始碼:

export default ({ app }) => {
    app.router.beforeEach((to,next) => {
        if (typeof window !== 'undefined' && typeof window.navigator !== 'undefined') {
            if(!/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
                //電腦端訪問,則直接放行
                next();
            }else{

                var sCode = '';
                let toUrl = to.path;
                //標識獲取方式1:從請求連結中獲取
                //如:/page/detail/IUKGEQ/108/9933/32279/8
                //如:/IUKGEQ
                //正則表示式提取連線中的 六位大寫字母的標識
                let matchArr = toUrl.match('\/([A-Z]{6})');
                if((sCode=='' || sCode == null || sCode == undefined) && matchArr != null){
                    sCode = matchArr[1];
                }
    
                //標識獲取方式2:發起請求獲取Code 
   http://www.cppcns.com             //如:/swpu
                let matchArr2 = toUrl.match('\/([a-z]{3,})');
                if((sCode=='' || sCode == null || sCode == undefined) && matchArr2 != null){
                    var param = matchArr2[1];
                    getSInfo2(param)
                    .then(res => {
                      if (res.data.code) {
                        sCode = res.data.code;
                        //路由跳轉
                        mobileRouteCombine(toUrl,sCode);
                      } else {
                        // 查不到code
                        next();//放行
                      }
                    })
                    .catch(err => {
                        next();//放行
                    });
                }
                //上面兩種種方式如果都無法取出code,則直接放行
                if(sCode=='' || sCode == null || sCode == undefined){
                    next();
                    return;
                }else{
                    //路由跳轉
                    mobileRouteCombine(toUrl,sCode);
                }
            }
        }
        next();
    })
}

/**
 * 移動端路由重組
 * @param {訪問的url地址} toUrl 
 * @param [code] sCode 
 */
function mobileRouteCombine(toUrl,sCode){
    var wxHomeUrl = conf.weixin + '/build/index.html?scode=' + sCode + '#/';
                
    // toUrl為 如 /IUKGEQ 形式,則直接跳轉微信首頁
    if(toUrl.length <= 7){
        location.href = wxHomeUrl;
    }

    //文章列表
    if(toUrl.indexOf('/page/list/') != -1){
        let matchParams = toUrl.match('(\\d{1,3})\/(\\d{4,6})'); 
        let catId = matchParamshttp://www.cppcns.com[2];
        let versionId = matchParams[1];//版本id
        var url = wxHomeUrl +'articleList?catId=' + catId;
        location.href = url;     
    }

    //文章詳情
    if(toUrl.indexOf('/page/detail/') != -1){
        let matchParams = toUrl.match('(\\d{1,6})\/(\\d{4,6})'); 
        let infoId = matchParams[3];
        let catId = matchParams[2];
        let versionId = matchParams[1];//版本id
        var url = wxHomeUrl +'articleDetail?infoId=' + infoId + '&catId=' + catId;
        location.href = url;     
    }
}

到此這篇關於中PC端地址跳轉移動端的文章就介紹到這了,更多相關vue地址跳轉移內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!