feat: 添加单点登录功能-前端

This commit is contained in:
LokerL 2024-11-20 11:37:39 +08:00
parent b47cd4011f
commit 2642714bb4
5 changed files with 76 additions and 4 deletions

View File

@ -57,4 +57,12 @@ export function getCodeImg() {
method: 'get',
timeout: 20000
})
}
}
export function getAccountName(data) {
return request({
url: '/getAccountName',
method: 'get',
params: data
})
}

View File

@ -8,7 +8,7 @@ import { isRelogin } from '@/utils/request'
NProgress.configure({ showSpinner: false })
const whiteList = ['/login', '/register']
const whiteList = ['/login', '/register', '/LoginSso']
router.beforeEach((to, from, next) => {
NProgress.start()

View File

@ -46,6 +46,11 @@ export const constantRoutes = [
component: () => import('@/views/login'),
hidden: true
},
{
path: '/LoginSso',
component: () => import('@/views/login-sso'),
hidden: true
},
{
path: '/register',
component: () => import('@/views/register'),

View File

@ -1,6 +1,6 @@
import { login, logout, getInfo } from '@/api/login'
import { login, logout, getInfo, getAccountName } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
import {encrypt, decrypt} from '@/utils/secret'
import {encrypt, decrypt} from '@/utils/secret'
const user = {
state: {
token: getToken(),
@ -95,6 +95,22 @@ const user = {
removeToken()
resolve()
})
},
LoginSso({ commit }, code) {
return new Promise((resolve, reject) => {
getAccountName({"code": code}).then((res) => {
if (res.code == 200) {
setToken(res.token)
commit('SET_TOKEN', res.token)
resolve()
} else {
reject(res)
}
}).catch(error => {
reject(error)
})
})
}
}
}

View File

@ -0,0 +1,43 @@
<template>
<div></div>
</template>
<script>
export default {
name: "LoginSso",
data() {
return {
loginRules: {},
//
captchaOnOff: true,
//
register: false,
//
redirect: undefined
};
},
created() {
//
this.loginSso();
},
methods: {
loginSso() {
//code
const code = this.$route.query.code;
console.log("code="+code)
//
if(code==''||code==undefined||code==null){
//code
} else {
this.$store.dispatch("LoginSso", code).then(() => {
this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
}).catch((error) => {
console.error(error);
this.$message.error("单点登录失败");
this.$router.push({ path: "/login" }).catch(()=>{});
});
}
},
}
};
</script>