Commit 598cd7de authored by qiyaxin's avatar qiyaxin

iam系统对接-登录完善

parent 53c55e16
...@@ -6,3 +6,6 @@ VITE_APP_ENV = 'development' ...@@ -6,3 +6,6 @@ VITE_APP_ENV = 'development'
# 奇瑞一致性/开发环境 # 奇瑞一致性/开发环境
VITE_APP_BASE_API = '/dev-api' VITE_APP_BASE_API = '/dev-api'
# 奇瑞一致性/开发环境登录路由
VITE_APP_LOGIN_ROUTE = '/login'
...@@ -9,3 +9,6 @@ VITE_APP_BASE_API = '/prod-api' ...@@ -9,3 +9,6 @@ VITE_APP_BASE_API = '/prod-api'
# 是否在打包时开启压缩,支持 gzip 和 brotli # 是否在打包时开启压缩,支持 gzip 和 brotli
VITE_BUILD_COMPRESS = gzip VITE_BUILD_COMPRESS = gzip
# 奇瑞一致性/生产环境登录路由
VITE_APP_LOGIN_ROUTE = 'https://iamuat.mychery.com:5443/idp/oauth2/authorize?client_id=FACTS&redirect_uri=http://106.3.99.64:20073/login&response_type=code'
...@@ -9,3 +9,6 @@ VITE_APP_BASE_API = 'http://106.3.99.64:20073/chery-api/' ...@@ -9,3 +9,6 @@ VITE_APP_BASE_API = 'http://106.3.99.64:20073/chery-api/'
# 是否在打包时开启压缩,支持 gzip 和 brotli # 是否在打包时开启压缩,支持 gzip 和 brotli
VITE_BUILD_COMPRESS = gzip VITE_BUILD_COMPRESS = gzip
# 奇瑞一致性/生产环境登录路由
VITE_APP_LOGIN_ROUTE = 'https://iamuat.mychery.com:5443/idp/oauth2/authorize?client_id=FACTS&redirect_uri=http://106.3.99.64:20073/login&response_type=code'
...@@ -8,6 +8,7 @@ import { isRelogin } from '@/utils/request' ...@@ -8,6 +8,7 @@ import { isRelogin } from '@/utils/request'
import useUserStore from '@/store/modules/user' import useUserStore from '@/store/modules/user'
import useSettingsStore from '@/store/modules/settings' import useSettingsStore from '@/store/modules/settings'
import usePermissionStore from '@/store/modules/permission' import usePermissionStore from '@/store/modules/permission'
import axios from 'axios'
NProgress.configure({ showSpinner: false }) NProgress.configure({ showSpinner: false })
...@@ -17,6 +18,12 @@ const isWhiteList = (path) => { ...@@ -17,6 +18,12 @@ const isWhiteList = (path) => {
return whiteList.some((pattern) => isPathMatch(pattern, path)) return whiteList.some((pattern) => isPathMatch(pattern, path))
} }
// 后端登录接口
const loginBySSOApi = '/auth/loginBySSO'
const unifiedLoginRoute = '/login'
// 登录路由
const loginRoute = import.meta.env.VITE_APP_LOGIN_ROUTE
router.beforeEach((to, from, next) => { router.beforeEach((to, from, next) => {
NProgress.start() NProgress.start()
if (getToken()) { if (getToken()) {
...@@ -59,16 +66,59 @@ router.beforeEach((to, from, next) => { ...@@ -59,16 +66,59 @@ router.beforeEach((to, from, next) => {
next() next()
} }
} }
} else { }
// //若依系统登录
// else {
// // 没有token
// if (isWhiteList(to.path)) {
// // 在免登录白名单,直接进入
// next()
// } else {
// next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
// NProgress.done()
// }
// }
//iam系统登录
else {
// 没有token // 没有token
if (isWhiteList(to.path)) { if (isWhiteList(to.path)) {
// 在免登录白名单,直接进入 // 在免登录白名单,直接进入
if (to.path === unifiedLoginRoute) {
// 从URL中截取code参数
const urlParams = new URLSearchParams(window.location.search)
const code = urlParams.get('code')
if (code) {
// 请求后端/auth/loginBySSO接口
axios
.post(loginBySSOApi, { ticket: code })
.then((response) => {
if (response.data.success) {
// 登录成功,跳转到首页
next({ path: '/' })
} else {
ElMessage.error('登录失败,请重试')
next()
}
})
.catch((error) => {
ElMessage.error('登录请求出错,请重试')
next() next()
})
} else {
next()
}
} else { } else {
next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页 next()
}
} else {
// 跳转到IAM登录页面
window.location.href = loginRoute
NProgress.done() NProgress.done()
} }
} }
}) })
router.afterEach(() => { router.afterEach(() => {
......
...@@ -86,6 +86,11 @@ import { getCodeImg } from '@/api/login' ...@@ -86,6 +86,11 @@ import { getCodeImg } from '@/api/login'
import Cookies from 'js-cookie' import Cookies from 'js-cookie'
import { encrypt, decrypt } from '@/utils/jsencrypt' import { encrypt, decrypt } from '@/utils/jsencrypt'
import useUserStore from '@/store/modules/user' import useUserStore from '@/store/modules/user'
// import { useRoute, useRouter } from 'vue-router'
// import { getCurrentInstance } from 'vue'
// 引入环境变量
const loginRoute = import.meta.env.VITE_APP_LOGIN_ROUTE
const title = import.meta.env.VITE_APP_TITLE const title = import.meta.env.VITE_APP_TITLE
const userStore = useUserStore() const userStore = useUserStore()
...@@ -124,6 +129,17 @@ watch( ...@@ -124,6 +129,17 @@ watch(
) )
function handleLogin() { function handleLogin() {
// 不是开发环境
if (loginRoute !== "/login"){
// 检查路径中是否有 code 参数,没有就跳转至iam登录页面
const urlParams = new URLSearchParams(window.location.search)
const code = urlParams.get('code')
if (!code) {
window.location.href = loginRoute
return
}
}
proxy.$refs.loginRef.validate((valid) => { proxy.$refs.loginRef.validate((valid) => {
if (valid) { if (valid) {
loading.value = true loading.value = true
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment