Parcourir la source

修改单点登录

修改单点登录
dangyunlong il y a 2 semaines
Parent
commit
34dcf7ef84
2 fichiers modifiés avec 46 ajouts et 22 suppressions
  1. 31 21
      src/permission.js
  2. 15 1
      src/utils/auth.js

+ 31 - 21
src/permission.js

@@ -11,7 +11,7 @@ NProgress.configure({ showSpinner: false })
 
 //2.引入单点登录需要的方法 start ---------------------------------------->
 import { getToken, setUserUrl, getUseType, getUserUrl,setUseType,setWebSiteId,
-hashParams,removeToken } from '@/utils/auth' //设置cookie的方法
+hashParams,removeToken,getLoginType,getBackUrlValue } from '@/utils/auth' //设置cookie的方法
 import URL from '@/utils/baseUrl'; //获得请求路径
 import { getInfo } from '@/api/user' //获得用户信息
 //2.引入单点登录需要的方法 end ---------------------------------------->
@@ -29,29 +29,39 @@ router.beforeEach(async (to, from, next) => {
     if (hasToken) {
         //3.4.1 如果有token,而且是在登录页面
         if (to.path === '/login') {
-            // Retrieve userurl
-            const userurl = hashParams();
-            if (userurl) {
-                setUserUrl(userurl, 86400)
-            }
-            const userInfo = await getInfo();
-            console.log("User Info:", userInfo);
-            if (userInfo.code === 200) {
-                console.log("####")
-                //每次返回login的时候都获取一下用户类型和网站id
-                setUseType(userInfo.data.type_id, 86400)
-                setWebSiteId(userInfo.data.website_id, 86400)
-                next({ path: '/' })
-                NProgress.done() 
-            } else {
-                if(userInfo.code == -1){
-                    next({ path: '/' })
-                    NProgress.done()
-                }else{
+            //获取登录参数,是backurl还是userurl
+            let loginType = getLoginType(window.location.href)
+            if(loginType == 'userurl'){
+                //如果是userurl
+                const userurl = hashParams();
+                if (userurl) {
+                    setUserUrl(userurl, 86400)
+                }
+                const userInfo = await getInfo();
+                console.log("User Info:", userInfo);
+                if (userInfo.code === 200) {
+                    console.log("####")
+                    //每次返回login的时候都获取一下用户类型和网站id
+                    setUseType(userInfo.data.type_id, 86400)
+                    setWebSiteId(userInfo.data.website_id, 86400)
                     next({ path: '/' })
-                    NProgress.done()
+                    NProgress.done() 
+                } else {
+                    if(userInfo.code == -1){
+                        next({ path: '/' })
+                        NProgress.done()
+                    }else{
+                        next({ path: '/' })
+                        NProgress.done()
+                    }
                 }
             }
+            if(loginType == 'backurl'){
+                //如果是backurl
+                const backurl = getBackUrlValue(window.location.href)
+                //带着token返回原网站
+                window.location.href = backurl + '?backurl=' + backurl + '&admintoken=' + getToken()
+            }
         } else {
             //3.4.2 如果有token,但是没在登录页面
             const hasRoles = store.getters.roles && store.getters.roles.length > 0

+ 15 - 1
src/utils/auth.js

@@ -103,6 +103,19 @@ export function removeLoginStatus() {
   return Cookies.remove(loginStatus)
 }
 
+//6.3 获取登录类型
+export function getLoginType(url) {
+    const match = url.match(/[?&](backurl|userurl)=/);
+    return match ? match[1] : null;
+}
+
+//6.4 获取backurl的值
+export function getBackUrlValue(url) {
+    const regex = /[?&](?:backurl|userurl)=([^&#]+)/;
+    const match = url.match(regex);
+    return match ? decodeURIComponent(match[1]) : null;
+}
+
 // Function to parse hash parameters from the URL
 export function hashParams() {
   const urlString = window.location.href;
@@ -120,10 +133,11 @@ export function hashParams() {
   if (backurl) {
     // Create a URL object to extract the domain
     const backUrlObject = new URL(backurl);
-    return backUrlObject.hostname; // Return only the domain  
+    return backUrlObject.hostname; // Return only the domain     
   }
 
   return null; // Return null if userurl is not present
 }
 
 
+