用戶未登錄訪問鏈接,前端如何跳轉登錄頁


在某些必須要登錄才能操作的前端項目中,有時候我們用戶會遇到這樣的場景,首次訪問收藏了地址,下次通過收藏的地址訪問的時候登錄已過期,請求接口的時候后端會返回重定向的地址,此時狀態碼為200,無法從狀態碼判斷是否重定向:
 這時候可以在響應攔截器中添加判斷 response.request.responseURL.match(response.config.url) 為null則跳轉登錄頁。
(loginRedirect?linkUrl=" + window.location.pathname)是傳給后端的參數,登錄后返回當前頁
service.interceptors.response.use(
  response => {
    console.log('response', response) // for debug
 // 判斷是否
    let matchUrl = response.request.responseURL.match(response.config.url);
    if (matchUrl!=null) {
      return response.data;
    } else {
      // store.dispatch("LogOut").then(() => {
        window.location.href = "/login/loginRedirect?linkUrl=" + window.location.pathname;
      // });
    }
  },
  error => {
    return Promise.reject(error)
  }
)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM